From c51fae92dc7d3080def81a2797e0d683b3e6d82a Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Tue, 23 Feb 2016 19:21:14 +0100 Subject: Allow crossed search between terms and tags * Partial fix of #449 * Current use case: search term + click on tag. * LinkFilter now returns all links if no filter is given. * Unit tests. --- application/LinkDB.php | 3 +-- application/LinkFilter.php | 27 +++++++++++++++++++++++---- 2 files changed, 24 insertions(+), 6 deletions(-) (limited to 'application') diff --git a/application/LinkDB.php b/application/LinkDB.php index 9488ac45..1b505620 100644 --- a/application/LinkDB.php +++ b/application/LinkDB.php @@ -353,8 +353,7 @@ You use the community supported version of the original Shaarli project, by Seba public function filter($type = '', $request = '', $casesensitive = false, $privateonly = false) { $linkFilter = new LinkFilter($this->_links); - $requestFilter = is_array($request) ? implode(' ', $request) : $request; - return $linkFilter->filter($type, trim($requestFilter), $casesensitive, $privateonly); + return $linkFilter->filter($type, $request, $casesensitive, $privateonly); } /** diff --git a/application/LinkFilter.php b/application/LinkFilter.php index 17594e8f..3fd803cb 100644 --- a/application/LinkFilter.php +++ b/application/LinkFilter.php @@ -55,16 +55,25 @@ class LinkFilter switch($type) { case self::$FILTER_HASH: return $this->filterSmallHash($request); - break; + case self::$FILTER_TAG | self::$FILTER_TEXT: + if (!empty($request)) { + $filtered = $this->links; + if (isset($request[0])) { + $filtered = $this->filterTags($request[0], $casesensitive, $privateonly); + } + if (isset($request[1])) { + $lf = new LinkFilter($filtered); + $filtered = $lf->filterFulltext($request[1], $privateonly); + } + return $filtered; + } + return $this->noFilter($privateonly); case self::$FILTER_TEXT: return $this->filterFulltext($request, $privateonly); - break; case self::$FILTER_TAG: return $this->filterTags($request, $casesensitive, $privateonly); - break; case self::$FILTER_DAY: return $this->filterDay($request); - break; default: return $this->noFilter($privateonly); } @@ -138,6 +147,10 @@ class LinkFilter */ private function filterFulltext($searchterms, $privateonly = false) { + if (empty($searchterms)) { + return $this->links; + } + $filtered = array(); $search = mb_convert_case(html_entity_decode($searchterms), MB_CASE_LOWER, 'UTF-8'); $exactRegex = '/"([^"]+)"/'; @@ -219,6 +232,12 @@ class LinkFilter */ public function filterTags($tags, $casesensitive = false, $privateonly = false) { + // Implode if array for clean up. + $tags = is_array($tags) ? trim(implode(' ', $tags)) : $tags; + if (empty($tags)) { + return $this->links; + } + $searchtags = self::tagsStrToArray($tags, $casesensitive); $filtered = array(); if (empty($searchtags)) { -- cgit v1.2.3