]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/LinkFilter.php
d4fe28df6d4968af4ac673110022ab35d2be9488
[github/shaarli/Shaarli.git] / application / LinkFilter.php
1 <?php
2
3 /**
4 * Class LinkFilter.
5 *
6 * Perform search and filter operation on link data list.
7 */
8 class LinkFilter
9 {
10 /**
11 * @var string permalinks.
12 */
13 public static $FILTER_HASH = 'permalink';
14
15 /**
16 * @var string text search.
17 */
18 public static $FILTER_TEXT = 'fulltext';
19
20 /**
21 * @var string tag filter.
22 */
23 public static $FILTER_TAG = 'tags';
24
25 /**
26 * @var string filter by day.
27 */
28 public static $FILTER_DAY = 'FILTER_DAY';
29
30 /**
31 * @var string Allowed characters for hashtags (regex syntax).
32 */
33 public static $HASHTAG_CHARS = '\p{Pc}\p{N}\p{L}\p{Mn}';
34
35 /**
36 * @var array all available links.
37 */
38 private $links;
39
40 /**
41 * @param array $links initialization.
42 */
43 public function __construct($links)
44 {
45 $this->links = $links;
46 }
47
48 /**
49 * Filter links according to parameters.
50 *
51 * @param string $type Type of filter (eg. tags, permalink, etc.).
52 * @param mixed $request Filter content.
53 * @param bool $casesensitive Optional: Perform case sensitive filter if true.
54 * @param bool $privateonly Optional: Only returns private links if true.
55 *
56 * @return array filtered link list.
57 */
58 public function filter($type, $request, $casesensitive = false, $privateonly = false)
59 {
60 switch($type) {
61 case self::$FILTER_HASH:
62 return $this->filterSmallHash($request);
63 case self::$FILTER_TAG | self::$FILTER_TEXT:
64 if (!empty($request)) {
65 $filtered = $this->links;
66 if (isset($request[0])) {
67 $filtered = $this->filterTags($request[0], $casesensitive, $privateonly);
68 }
69 if (isset($request[1])) {
70 $lf = new LinkFilter($filtered);
71 $filtered = $lf->filterFulltext($request[1], $privateonly);
72 }
73 return $filtered;
74 }
75 return $this->noFilter($privateonly);
76 case self::$FILTER_TEXT:
77 return $this->filterFulltext($request, $privateonly);
78 case self::$FILTER_TAG:
79 return $this->filterTags($request, $casesensitive, $privateonly);
80 case self::$FILTER_DAY:
81 return $this->filterDay($request);
82 default:
83 return $this->noFilter($privateonly);
84 }
85 }
86
87 /**
88 * Unknown filter, but handle private only.
89 *
90 * @param bool $privateonly returns private link only if true.
91 *
92 * @return array filtered links.
93 */
94 private function noFilter($privateonly = false)
95 {
96 if (! $privateonly) {
97 krsort($this->links);
98 return $this->links;
99 }
100
101 $out = array();
102 foreach ($this->links as $value) {
103 if ($value['private']) {
104 $out[$value['linkdate']] = $value;
105 }
106 }
107
108 krsort($out);
109 return $out;
110 }
111
112 /**
113 * Returns the shaare corresponding to a smallHash.
114 *
115 * @param string $smallHash permalink hash.
116 *
117 * @return array $filtered array containing permalink data.
118 *
119 * @throws LinkNotFoundException if the smallhash doesn't match any link.
120 */
121 private function filterSmallHash($smallHash)
122 {
123 $filtered = array();
124 foreach ($this->links as $l) {
125 if ($smallHash == smallHash($l['linkdate'])) {
126 // Yes, this is ugly and slow
127 $filtered[$l['linkdate']] = $l;
128 return $filtered;
129 }
130 }
131
132 if (empty($filtered)) {
133 throw new LinkNotFoundException();
134 }
135
136 return $filtered;
137 }
138
139 /**
140 * Returns the list of links corresponding to a full-text search
141 *
142 * Searches:
143 * - in the URLs, title and description;
144 * - are case-insensitive;
145 * - terms surrounded by quotes " are exact terms search.
146 * - terms starting with a dash - are excluded (except exact terms).
147 *
148 * Example:
149 * print_r($mydb->filterFulltext('hollandais'));
150 *
151 * mb_convert_case($val, MB_CASE_LOWER, 'UTF-8')
152 * - allows to perform searches on Unicode text
153 * - see https://github.com/shaarli/Shaarli/issues/75 for examples
154 *
155 * @param string $searchterms search query.
156 * @param bool $privateonly return only private links if true.
157 *
158 * @return array search results.
159 */
160 private function filterFulltext($searchterms, $privateonly = false)
161 {
162 if (empty($searchterms)) {
163 return $this->links;
164 }
165
166 $filtered = array();
167 $search = mb_convert_case(html_entity_decode($searchterms), MB_CASE_LOWER, 'UTF-8');
168 $exactRegex = '/"([^"]+)"/';
169 // Retrieve exact search terms.
170 preg_match_all($exactRegex, $search, $exactSearch);
171 $exactSearch = array_values(array_filter($exactSearch[1]));
172
173 // Remove exact search terms to get AND terms search.
174 $explodedSearchAnd = explode(' ', trim(preg_replace($exactRegex, '', $search)));
175 $explodedSearchAnd = array_values(array_filter($explodedSearchAnd));
176
177 // Filter excluding terms and update andSearch.
178 $excludeSearch = array();
179 $andSearch = array();
180 foreach ($explodedSearchAnd as $needle) {
181 if ($needle[0] == '-' && strlen($needle) > 1) {
182 $excludeSearch[] = substr($needle, 1);
183 } else {
184 $andSearch[] = $needle;
185 }
186 }
187
188 $keys = array('title', 'description', 'url', 'tags');
189
190 // Iterate over every stored link.
191 foreach ($this->links as $link) {
192
193 // ignore non private links when 'privatonly' is on.
194 if (! $link['private'] && $privateonly === true) {
195 continue;
196 }
197
198 // Concatenate link fields to search across fields.
199 // Adds a '\' separator for exact search terms.
200 $content = '';
201 foreach ($keys as $key) {
202 $content .= mb_convert_case($link[$key], MB_CASE_LOWER, 'UTF-8') . '\\';
203 }
204
205 // Be optimistic
206 $found = true;
207
208 // First, we look for exact term search
209 for ($i = 0; $i < count($exactSearch) && $found; $i++) {
210 $found = strpos($content, $exactSearch[$i]) !== false;
211 }
212
213 // Iterate over keywords, if keyword is not found,
214 // no need to check for the others. We want all or nothing.
215 for ($i = 0; $i < count($andSearch) && $found; $i++) {
216 $found = strpos($content, $andSearch[$i]) !== false;
217 }
218
219 // Exclude terms.
220 for ($i = 0; $i < count($excludeSearch) && $found; $i++) {
221 $found = strpos($content, $excludeSearch[$i]) === false;
222 }
223
224 if ($found) {
225 $filtered[$link['linkdate']] = $link;
226 }
227 }
228
229 krsort($filtered);
230 return $filtered;
231 }
232
233 /**
234 * Returns the list of links associated with a given list of tags
235 *
236 * You can specify one or more tags, separated by space or a comma, e.g.
237 * print_r($mydb->filterTags('linux programming'));
238 *
239 * @param string $tags list of tags separated by commas or blank spaces.
240 * @param bool $casesensitive ignore case if false.
241 * @param bool $privateonly returns private links only.
242 *
243 * @return array filtered links.
244 */
245 public function filterTags($tags, $casesensitive = false, $privateonly = false)
246 {
247 // Implode if array for clean up.
248 $tags = is_array($tags) ? trim(implode(' ', $tags)) : $tags;
249 if (empty($tags)) {
250 return $this->links;
251 }
252
253 $searchtags = self::tagsStrToArray($tags, $casesensitive);
254 $filtered = array();
255 if (empty($searchtags)) {
256 return $filtered;
257 }
258
259 foreach ($this->links as $link) {
260 // ignore non private links when 'privatonly' is on.
261 if (! $link['private'] && $privateonly === true) {
262 continue;
263 }
264
265 $linktags = self::tagsStrToArray($link['tags'], $casesensitive);
266
267 $found = true;
268 for ($i = 0 ; $i < count($searchtags) && $found; $i++) {
269 // Exclusive search, quit if tag found.
270 // Or, tag not found in the link, quit.
271 if (($searchtags[$i][0] == '-'
272 && $this->searchTagAndHashTag(substr($searchtags[$i], 1), $linktags, $link['description']))
273 || ($searchtags[$i][0] != '-')
274 && ! $this->searchTagAndHashTag($searchtags[$i], $linktags, $link['description'])
275 ) {
276 $found = false;
277 }
278 }
279
280 if ($found) {
281 $filtered[$link['linkdate']] = $link;
282 }
283 }
284 krsort($filtered);
285 return $filtered;
286 }
287
288 /**
289 * Returns the list of articles for a given day, chronologically sorted
290 *
291 * Day must be in the form 'YYYYMMDD' (e.g. '20120125'), e.g.
292 * print_r($mydb->filterDay('20120125'));
293 *
294 * @param string $day day to filter.
295 *
296 * @return array all link matching given day.
297 *
298 * @throws Exception if date format is invalid.
299 */
300 public function filterDay($day)
301 {
302 if (! checkDateFormat('Ymd', $day)) {
303 throw new Exception('Invalid date format');
304 }
305
306 $filtered = array();
307 foreach ($this->links as $l) {
308 if (startsWith($l['linkdate'], $day)) {
309 $filtered[$l['linkdate']] = $l;
310 }
311 }
312 ksort($filtered);
313 return $filtered;
314 }
315
316 /**
317 * Check if a tag is found in the taglist, or as an hashtag in the link description.
318 *
319 * @param string $tag Tag to search.
320 * @param array $taglist List of tags for the current link.
321 * @param string $description Link description.
322 *
323 * @return bool True if found, false otherwise.
324 */
325 protected function searchTagAndHashTag($tag, $taglist, $description)
326 {
327 if (in_array($tag, $taglist)) {
328 return true;
329 }
330
331 if (preg_match('/(^| )#'. $tag .'([^'. self::$HASHTAG_CHARS .']|$)/mui', $description) > 0) {
332 return true;
333 }
334
335 return false;
336 }
337
338 /**
339 * Convert a list of tags (str) to an array. Also
340 * - handle case sensitivity.
341 * - accepts spaces commas as separator.
342 *
343 * @param string $tags string containing a list of tags.
344 * @param bool $casesensitive will convert everything to lowercase if false.
345 *
346 * @return array filtered tags string.
347 */
348 public static function tagsStrToArray($tags, $casesensitive)
349 {
350 // We use UTF-8 conversion to handle various graphemes (i.e. cyrillic, or greek)
351 $tagsOut = $casesensitive ? $tags : mb_convert_case($tags, MB_CASE_LOWER, 'UTF-8');
352 $tagsOut = str_replace(',', ' ', $tagsOut);
353
354 return array_values(array_filter(explode(' ', trim($tagsOut)), 'strlen'));
355 }
356 }
357
358 class LinkNotFoundException extends Exception
359 {
360 protected $message = 'The link you are trying to reach does not exist or has been deleted.';
361 }