From 7d86f40bdb2135655b5b4fe8cbcc1ac102114f86 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Sat, 1 Apr 2017 12:17:37 +0200 Subject: Empty tag search will look for not tagged links Fixes #784 From now, searching for tags with an empty value will return only not tagged links, with the search bar showing `x results [not tagged]`. Note that using the api, the searchtags request parameter must be set to `false` to get the same result. - [ ] Update API doc --- application/FeedBuilder.php | 5 +++++ application/LinkDB.php | 27 +++++---------------------- application/LinkFilter.php | 30 ++++++++++++++++++++++++++++++ application/Utils.php | 4 ++++ 4 files changed, 44 insertions(+), 22 deletions(-) (limited to 'application') diff --git a/application/FeedBuilder.php b/application/FeedBuilder.php index a1f4da48..7377bcec 100644 --- a/application/FeedBuilder.php +++ b/application/FeedBuilder.php @@ -97,6 +97,11 @@ class FeedBuilder */ public function buildData() { + // Search for untagged links + if (isset($this->userInput['searchtags']) && empty($this->userInput['searchtags'])) { + $this->userInput['searchtags'] = false; + } + // Optionally filter the results: $linksToDisplay = $this->linkDB->filterSearch($this->userInput); diff --git a/application/LinkDB.php b/application/LinkDB.php index 4cee2af9..a03c2c06 100644 --- a/application/LinkDB.php +++ b/application/LinkDB.php @@ -450,29 +450,12 @@ You use the community supported version of the original Shaarli project, by Seba public function filterSearch($filterRequest = array(), $casesensitive = false, $visibility = 'all') { // Filter link database according to parameters. - $searchtags = !empty($filterRequest['searchtags']) ? escape($filterRequest['searchtags']) : ''; - $searchterm = !empty($filterRequest['searchterm']) ? escape($filterRequest['searchterm']) : ''; + $searchtags = isset($filterRequest['searchtags']) ? escape($filterRequest['searchtags']) : ''; + $searchterm = isset($filterRequest['searchterm']) ? escape($filterRequest['searchterm']) : ''; - // Search tags + fullsearch. - if (! empty($searchtags) && ! empty($searchterm)) { - $type = LinkFilter::$FILTER_TAG | LinkFilter::$FILTER_TEXT; - $request = array($searchtags, $searchterm); - } - // Search by tags. - elseif (! empty($searchtags)) { - $type = LinkFilter::$FILTER_TAG; - $request = $searchtags; - } - // Fulltext search. - elseif (! empty($searchterm)) { - $type = LinkFilter::$FILTER_TEXT; - $request = $searchterm; - } - // Otherwise, display without filtering. - else { - $type = ''; - $request = ''; - } + // Search tags + fullsearch - blank string parameter will return all links. + $type = LinkFilter::$FILTER_TAG | LinkFilter::$FILTER_TEXT; + $request = [$searchtags, $searchterm]; $linkFilter = new LinkFilter($this); return $linkFilter->filter($type, $request, $casesensitive, $visibility); diff --git a/application/LinkFilter.php b/application/LinkFilter.php index 81832a4b..0e887d38 100644 --- a/application/LinkFilter.php +++ b/application/LinkFilter.php @@ -253,6 +253,9 @@ class LinkFilter { // Implode if array for clean up. $tags = is_array($tags) ? trim(implode(' ', $tags)) : $tags; + if ($tags === false) { + return $this->filterUntagged($visibility); + } if (empty($tags)) { return $this->noFilter($visibility); } @@ -295,6 +298,33 @@ class LinkFilter return $filtered; } + /** + * Return only links without any tag. + * + * @param string $visibility return only all/private/public links. + * + * @return array filtered links. + */ + public function filterUntagged($visibility) + { + $filtered = []; + foreach ($this->links as $key => $link) { + if ($visibility !== 'all') { + if (! $link['private'] && $visibility === 'private') { + continue; + } else if ($link['private'] && $visibility === 'public') { + continue; + } + } + + if (empty(trim($link['tags']))) { + $filtered[$key] = $link; + } + } + + return $filtered; + } + /** * Returns the list of articles for a given day, chronologically sorted * diff --git a/application/Utils.php b/application/Utils.php index 5c077450..87e5cc8f 100644 --- a/application/Utils.php +++ b/application/Utils.php @@ -91,6 +91,10 @@ function endsWith($haystack, $needle, $case = true) */ function escape($input) { + if (is_bool($input)) { + return $input; + } + if (is_array($input)) { $out = array(); foreach($input as $key => $value) { -- cgit v1.2.3