diff options
author | Florian Voigt <flvoigt@me.com> | 2016-01-24 06:13:11 +0000 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2016-02-01 19:18:27 +0100 |
commit | ebd8075a896484e43c7c938bbc606b67df102013 (patch) | |
tree | 4e78232aa52c3037281b401a2bae08ed0fca9b7c /application | |
parent | db36b8812d30d2a1dcb1a5a0544893ee863f45dd (diff) | |
download | Shaarli-ebd8075a896484e43c7c938bbc606b67df102013.tar.gz Shaarli-ebd8075a896484e43c7c938bbc606b67df102013.tar.zst Shaarli-ebd8075a896484e43c7c938bbc606b67df102013.zip |
Implemented searching for a phrase in double-quotes or all words in no particular order.
+ unit tests
Diffstat (limited to 'application')
-rw-r--r-- | application/LinkFilter.php | 45 |
1 files changed, 32 insertions, 13 deletions
diff --git a/application/LinkFilter.php b/application/LinkFilter.php index cf647371..b2e6530f 100644 --- a/application/LinkFilter.php +++ b/application/LinkFilter.php | |||
@@ -136,16 +136,21 @@ class LinkFilter | |||
136 | */ | 136 | */ |
137 | private function filterFulltext($searchterms, $privateonly = false) | 137 | private function filterFulltext($searchterms, $privateonly = false) |
138 | { | 138 | { |
139 | // FIXME: explode(' ',$searchterms) and perform a AND search. | 139 | $search = mb_convert_case(html_entity_decode($searchterms), MB_CASE_LOWER, 'UTF-8'); |
140 | // FIXME: accept double-quotes to search for a string "as is"? | ||
141 | $filtered = array(); | ||
142 | $search = mb_convert_case($searchterms, MB_CASE_LOWER, 'UTF-8'); | ||
143 | $explodedSearch = explode(' ', trim($search)); | 140 | $explodedSearch = explode(' ', trim($search)); |
144 | $keys = array('title', 'description', 'url', 'tags'); | 141 | $keys = array('title', 'description', 'url', 'tags'); |
142 | $found = true; | ||
143 | $searchExactPhrase = false; | ||
145 | 144 | ||
145 | // Check if we're using double-quotes to search for the exact string | ||
146 | if ($search[0] == '"' && $search[strlen($search) - 1] == '"') { | ||
147 | $searchExactPhrase = true; | ||
148 | |||
149 | // Remove the double-quotes as they are not what we search for | ||
150 | $search = substr($search, 1, -1); | ||
151 | } | ||
146 | // Iterate over every stored link. | 152 | // Iterate over every stored link. |
147 | foreach ($this->links as $link) { | 153 | foreach ($this->links as $link) { |
148 | $found = false; | ||
149 | 154 | ||
150 | // ignore non private links when 'privatonly' is on. | 155 | // ignore non private links when 'privatonly' is on. |
151 | if (! $link['private'] && $privateonly === true) { | 156 | if (! $link['private'] && $privateonly === true) { |
@@ -154,19 +159,33 @@ class LinkFilter | |||
154 | 159 | ||
155 | // Iterate over searchable link fields. | 160 | // Iterate over searchable link fields. |
156 | foreach ($keys as $key) { | 161 | foreach ($keys as $key) { |
157 | // Search full expression. | 162 | // Be optimistic |
158 | if (strpos( | 163 | $found = true; |
159 | mb_convert_case($link[$key], MB_CASE_LOWER, 'UTF-8'), | 164 | |
160 | $search | 165 | // FIXME: Find a better word for where you're searching in |
161 | ) !== false) { | 166 | $haystack = mb_convert_case($link[$key], MB_CASE_LOWER, 'UTF-8'); |
162 | $found = true; | ||
163 | } | ||
164 | 167 | ||
168 | // When searching for the phrase, check if it's in the haystack... | ||
169 | if ( $searchExactPhrase && strpos($haystack, $search) !== false) { | ||
170 | break; | ||
171 | } | ||
172 | else { | ||
173 | // Iterate over keywords, if keyword is not found, | ||
174 | // no need to check for the others. We want all or nothing. | ||
175 | foreach($explodedSearch as $keyword) { | ||
176 | if(strpos($haystack, $keyword) === false) { | ||
177 | $found = false; | ||
178 | break; | ||
179 | } | ||
180 | } | ||
181 | } | ||
182 | |||
183 | // One of the fields of the link matches, no need to check the other. | ||
165 | if ($found) { | 184 | if ($found) { |
166 | break; | 185 | break; |
167 | } | 186 | } |
168 | } | 187 | } |
169 | 188 | ||
170 | if ($found) { | 189 | if ($found) { |
171 | $filtered[$link['linkdate']] = $link; | 190 | $filtered[$link['linkdate']] = $link; |
172 | } | 191 | } |