]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/LinkFilter.php
8f147974e9d5cf232f3cf3f509b3401fcf5e6b84
[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 LinkDB all available links.
37 */
38 private $links;
39
40 /**
41 * @param LinkDB $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 string $visibility Optional: return only all/private/public links
55 * @param string $untaggedonly Optional: return only untagged links. Applies only if $type includes FILTER_TAG
56 *
57 * @return array filtered link list.
58 */
59 public function filter($type, $request, $casesensitive = false, $visibility = 'all', $untaggedonly = false)
60 {
61 if (! in_array($visibility, ['all', 'public', 'private'])) {
62 $visibility = 'all';
63 }
64
65 switch ($type) {
66 case self::$FILTER_HASH:
67 return $this->filterSmallHash($request);
68 case self::$FILTER_TAG | self::$FILTER_TEXT: // == "vuotext"
69 $noRequest = empty($request) || (empty($request[0]) && empty($request[1]));
70 if ($noRequest) {
71 if ($untaggedonly) {
72 return $this->filterUntagged($visibility);
73 }
74 return $this->noFilter($visibility);
75 }
76 if ($untaggedonly) {
77 $filtered = $this->filterUntagged($visibility);
78 } else {
79 $filtered = $this->links;
80 }
81 if (!empty($request[0])) {
82 $filtered = (new LinkFilter($filtered))->filterTags($request[0], $casesensitive, $visibility);
83 }
84 if (!empty($request[1])) {
85 $filtered = (new LinkFilter($filtered))->filterFulltext($request[1], $visibility);
86 }
87 return $filtered;
88 case self::$FILTER_TEXT:
89 return $this->filterFulltext($request, $visibility);
90 case self::$FILTER_TAG:
91 if ($untaggedonly) {
92 return $this->filterUntagged($visibility);
93 } else {
94 return $this->filterTags($request, $casesensitive, $visibility);
95 }
96 case self::$FILTER_DAY:
97 return $this->filterDay($request);
98 default:
99 return $this->noFilter($visibility);
100 }
101 }
102
103 /**
104 * Unknown filter, but handle private only.
105 *
106 * @param string $visibility Optional: return only all/private/public links
107 *
108 * @return array filtered links.
109 */
110 private function noFilter($visibility = 'all')
111 {
112 if ($visibility === 'all') {
113 return $this->links;
114 }
115
116 $out = array();
117 foreach ($this->links as $key => $value) {
118 if ($value['private'] && $visibility === 'private') {
119 $out[$key] = $value;
120 } elseif (! $value['private'] && $visibility === 'public') {
121 $out[$key] = $value;
122 }
123 }
124
125 return $out;
126 }
127
128 /**
129 * Returns the shaare corresponding to a smallHash.
130 *
131 * @param string $smallHash permalink hash.
132 *
133 * @return array $filtered array containing permalink data.
134 *
135 * @throws LinkNotFoundException if the smallhash doesn't match any link.
136 */
137 private function filterSmallHash($smallHash)
138 {
139 $filtered = array();
140 foreach ($this->links as $key => $l) {
141 if ($smallHash == $l['shorturl']) {
142 // Yes, this is ugly and slow
143 $filtered[$key] = $l;
144 return $filtered;
145 }
146 }
147
148 if (empty($filtered)) {
149 throw new LinkNotFoundException();
150 }
151
152 return $filtered;
153 }
154
155 /**
156 * Returns the list of links corresponding to a full-text search
157 *
158 * Searches:
159 * - in the URLs, title and description;
160 * - are case-insensitive;
161 * - terms surrounded by quotes " are exact terms search.
162 * - terms starting with a dash - are excluded (except exact terms).
163 *
164 * Example:
165 * print_r($mydb->filterFulltext('hollandais'));
166 *
167 * mb_convert_case($val, MB_CASE_LOWER, 'UTF-8')
168 * - allows to perform searches on Unicode text
169 * - see https://github.com/shaarli/Shaarli/issues/75 for examples
170 *
171 * @param string $searchterms search query.
172 * @param string $visibility Optional: return only all/private/public links.
173 *
174 * @return array search results.
175 */
176 private function filterFulltext($searchterms, $visibility = 'all')
177 {
178 if (empty($searchterms)) {
179 return $this->noFilter($visibility);
180 }
181
182 $filtered = array();
183 $search = mb_convert_case(html_entity_decode($searchterms), MB_CASE_LOWER, 'UTF-8');
184 $exactRegex = '/"([^"]+)"/';
185 // Retrieve exact search terms.
186 preg_match_all($exactRegex, $search, $exactSearch);
187 $exactSearch = array_values(array_filter($exactSearch[1]));
188
189 // Remove exact search terms to get AND terms search.
190 $explodedSearchAnd = explode(' ', trim(preg_replace($exactRegex, '', $search)));
191 $explodedSearchAnd = array_values(array_filter($explodedSearchAnd));
192
193 // Filter excluding terms and update andSearch.
194 $excludeSearch = array();
195 $andSearch = array();
196 foreach ($explodedSearchAnd as $needle) {
197 if ($needle[0] == '-' && strlen($needle) > 1) {
198 $excludeSearch[] = substr($needle, 1);
199 } else {
200 $andSearch[] = $needle;
201 }
202 }
203
204 $keys = array('title', 'description', 'url', 'tags');
205
206 // Iterate over every stored link.
207 foreach ($this->links as $id => $link) {
208 // ignore non private links when 'privatonly' is on.
209 if ($visibility !== 'all') {
210 if (! $link['private'] && $visibility === 'private') {
211 continue;
212 } elseif ($link['private'] && $visibility === 'public') {
213 continue;
214 }
215 }
216
217 // Concatenate link fields to search across fields.
218 // Adds a '\' separator for exact search terms.
219 $content = '';
220 foreach ($keys as $key) {
221 $content .= mb_convert_case($link[$key], MB_CASE_LOWER, 'UTF-8') . '\\';
222 }
223
224 // Be optimistic
225 $found = true;
226
227 // First, we look for exact term search
228 for ($i = 0; $i < count($exactSearch) && $found; $i++) {
229 $found = strpos($content, $exactSearch[$i]) !== false;
230 }
231
232 // Iterate over keywords, if keyword is not found,
233 // no need to check for the others. We want all or nothing.
234 for ($i = 0; $i < count($andSearch) && $found; $i++) {
235 $found = strpos($content, $andSearch[$i]) !== false;
236 }
237
238 // Exclude terms.
239 for ($i = 0; $i < count($excludeSearch) && $found; $i++) {
240 $found = strpos($content, $excludeSearch[$i]) === false;
241 }
242
243 if ($found) {
244 $filtered[$id] = $link;
245 }
246 }
247
248 return $filtered;
249 }
250
251 /**
252 * generate a regex fragment out of a tag
253 * @param string $tag to to generate regexs from. may start with '-' to negate, contain '*' as wildcard
254 * @return string generated regex fragment
255 */
256 private static function tag2regex($tag)
257 {
258 $len = strlen($tag);
259 if (!$len || $tag === "-" || $tag === "*") {
260 // nothing to search, return empty regex
261 return '';
262 }
263 if ($tag[0] === "-") {
264 // query is negated
265 $i = 1; // use offset to start after '-' character
266 $regex = '(?!'; // create negative lookahead
267 } else {
268 $i = 0; // start at first character
269 $regex = '(?='; // use positive lookahead
270 }
271 $regex .= '.*(?:^| )'; // before tag may only be a space or the beginning
272 // iterate over string, separating it into placeholder and content
273 for (; $i < $len; $i++) {
274 if ($tag[$i] === '*') {
275 // placeholder found
276 $regex .= '[^ ]*?';
277 } else {
278 // regular characters
279 $offset = strpos($tag, '*', $i);
280 if ($offset === false) {
281 // no placeholder found, set offset to end of string
282 $offset = $len;
283 }
284 // subtract one, as we want to get before the placeholder or end of string
285 $offset -= 1;
286 // we got a tag name that we want to search for. escape any regex characters to prevent conflicts.
287 $regex .= preg_quote(substr($tag, $i, $offset - $i + 1), '/');
288 // move $i on
289 $i = $offset;
290 }
291 }
292 $regex .= '(?:$| ))'; // after the tag may only be a space or the end
293 return $regex;
294 }
295
296 /**
297 * Returns the list of links associated with a given list of tags
298 *
299 * You can specify one or more tags, separated by space or a comma, e.g.
300 * print_r($mydb->filterTags('linux programming'));
301 *
302 * @param string $tags list of tags separated by commas or blank spaces.
303 * @param bool $casesensitive ignore case if false.
304 * @param string $visibility Optional: return only all/private/public links.
305 *
306 * @return array filtered links.
307 */
308 public function filterTags($tags, $casesensitive = false, $visibility = 'all')
309 {
310 // get single tags (we may get passed an array, even though the docs say different)
311 $inputTags = $tags;
312 if (!is_array($tags)) {
313 // we got an input string, split tags
314 $inputTags = preg_split('/(?:\s+)|,/', $inputTags, -1, PREG_SPLIT_NO_EMPTY);
315 }
316
317 if (!count($inputTags)) {
318 // no input tags
319 return $this->noFilter($visibility);
320 }
321
322 // build regex from all tags
323 $re = '/^' . implode(array_map("self::tag2regex", $inputTags)) . '.*$/';
324 if (!$casesensitive) {
325 // make regex case insensitive
326 $re .= 'i';
327 }
328
329 // create resulting array
330 $filtered = array();
331
332 // iterate over each link
333 foreach ($this->links as $key => $link) {
334 // check level of visibility
335 // ignore non private links when 'privateonly' is on.
336 if ($visibility !== 'all') {
337 if (! $link['private'] && $visibility === 'private') {
338 continue;
339 } elseif ($link['private'] && $visibility === 'public') {
340 continue;
341 }
342 }
343 $search = $link['tags']; // build search string, start with tags of current link
344 if (strlen(trim($link['description'])) && strpos($link['description'], '#') !== false) {
345 // description given and at least one possible tag found
346 $descTags = array();
347 // find all tags in the form of #tag in the description
348 preg_match_all(
349 '/(?<![' . self::$HASHTAG_CHARS . '])#([' . self::$HASHTAG_CHARS . ']+?)\b/sm',
350 $link['description'],
351 $descTags
352 );
353 if (count($descTags[1])) {
354 // there were some tags in the description, add them to the search string
355 $search .= ' ' . implode(' ', $descTags[1]);
356 }
357 };
358 // match regular expression with search string
359 if (!preg_match($re, $search)) {
360 // this entry does _not_ match our regex
361 continue;
362 }
363 $filtered[$key] = $link;
364 }
365 return $filtered;
366 }
367
368 /**
369 * Return only links without any tag.
370 *
371 * @param string $visibility return only all/private/public links.
372 *
373 * @return array filtered links.
374 */
375 public function filterUntagged($visibility)
376 {
377 $filtered = [];
378 foreach ($this->links as $key => $link) {
379 if ($visibility !== 'all') {
380 if (! $link['private'] && $visibility === 'private') {
381 continue;
382 } elseif ($link['private'] && $visibility === 'public') {
383 continue;
384 }
385 }
386
387 if (empty(trim($link['tags']))) {
388 $filtered[$key] = $link;
389 }
390 }
391
392 return $filtered;
393 }
394
395 /**
396 * Returns the list of articles for a given day, chronologically sorted
397 *
398 * Day must be in the form 'YYYYMMDD' (e.g. '20120125'), e.g.
399 * print_r($mydb->filterDay('20120125'));
400 *
401 * @param string $day day to filter.
402 *
403 * @return array all link matching given day.
404 *
405 * @throws Exception if date format is invalid.
406 */
407 public function filterDay($day)
408 {
409 if (! checkDateFormat('Ymd', $day)) {
410 throw new Exception('Invalid date format');
411 }
412
413 $filtered = array();
414 foreach ($this->links as $key => $l) {
415 if ($l['created']->format('Ymd') == $day) {
416 $filtered[$key] = $l;
417 }
418 }
419
420 // sort by date ASC
421 return array_reverse($filtered, true);
422 }
423
424 /**
425 * Convert a list of tags (str) to an array. Also
426 * - handle case sensitivity.
427 * - accepts spaces commas as separator.
428 *
429 * @param string $tags string containing a list of tags.
430 * @param bool $casesensitive will convert everything to lowercase if false.
431 *
432 * @return array filtered tags string.
433 */
434 public static function tagsStrToArray($tags, $casesensitive)
435 {
436 // We use UTF-8 conversion to handle various graphemes (i.e. cyrillic, or greek)
437 $tagsOut = $casesensitive ? $tags : mb_convert_case($tags, MB_CASE_LOWER, 'UTF-8');
438 $tagsOut = str_replace(',', ' ', $tagsOut);
439
440 return preg_split('/\s+/', $tagsOut, -1, PREG_SPLIT_NO_EMPTY);
441 }
442 }
443
444 class LinkNotFoundException extends Exception
445 {
446 /**
447 * LinkNotFoundException constructor.
448 */
449 public function __construct()
450 {
451 $this->message = t('The link you are trying to reach does not exist or has been deleted.');
452 }
453 }