diff options
Diffstat (limited to 'application/LinkDB.php')
-rw-r--r-- | application/LinkDB.php | 118 |
1 files changed, 13 insertions, 105 deletions
diff --git a/application/LinkDB.php b/application/LinkDB.php index f771ac8b..19ca6435 100644 --- a/application/LinkDB.php +++ b/application/LinkDB.php | |||
@@ -17,8 +17,10 @@ | |||
17 | * - private: Is this link private? 0=no, other value=yes | 17 | * - private: Is this link private? 0=no, other value=yes |
18 | * - tags: tags attached to this entry (separated by spaces) | 18 | * - tags: tags attached to this entry (separated by spaces) |
19 | * - title Title of the link | 19 | * - title Title of the link |
20 | * - url URL of the link. Can be absolute or relative. | 20 | * - url URL of the link. Used for displayable links (no redirector, relative, etc.). |
21 | * Can be absolute or relative. | ||
21 | * Relative URLs are permalinks (e.g.'?m-ukcw') | 22 | * Relative URLs are permalinks (e.g.'?m-ukcw') |
23 | * - real_url Absolute processed URL. | ||
22 | * | 24 | * |
23 | * Implements 3 interfaces: | 25 | * Implements 3 interfaces: |
24 | * - ArrayAccess: behaves like an associative array; | 26 | * - ArrayAccess: behaves like an associative array; |
@@ -332,114 +334,20 @@ You use the community supported version of the original Shaarli project, by Seba | |||
332 | } | 334 | } |
333 | 335 | ||
334 | /** | 336 | /** |
335 | * Returns the list of links corresponding to a full-text search | 337 | * Filter links. |
336 | * | 338 | * |
337 | * Searches: | 339 | * @param string $type Type of filter. |
338 | * - in the URLs, title and description; | 340 | * @param mixed $request Search request, string or array. |
339 | * - are case-insensitive. | 341 | * @param bool $casesensitive Optional: Perform case sensitive filter |
342 | * @param bool $privateonly Optional: Returns private links only if true. | ||
340 | * | 343 | * |
341 | * Example: | 344 | * @return array filtered links |
342 | * print_r($mydb->filterFulltext('hollandais')); | ||
343 | * | ||
344 | * mb_convert_case($val, MB_CASE_LOWER, 'UTF-8') | ||
345 | * - allows to perform searches on Unicode text | ||
346 | * - see https://github.com/shaarli/Shaarli/issues/75 for examples | ||
347 | */ | ||
348 | public function filterFulltext($searchterms) | ||
349 | { | ||
350 | // FIXME: explode(' ',$searchterms) and perform a AND search. | ||
351 | // FIXME: accept double-quotes to search for a string "as is"? | ||
352 | $filtered = array(); | ||
353 | $search = mb_convert_case($searchterms, MB_CASE_LOWER, 'UTF-8'); | ||
354 | $keys = array('title', 'description', 'url', 'tags'); | ||
355 | |||
356 | foreach ($this->_links as $link) { | ||
357 | $found = false; | ||
358 | |||
359 | foreach ($keys as $key) { | ||
360 | if (strpos(mb_convert_case($link[$key], MB_CASE_LOWER, 'UTF-8'), | ||
361 | $search) !== false) { | ||
362 | $found = true; | ||
363 | } | ||
364 | } | ||
365 | |||
366 | if ($found) { | ||
367 | $filtered[$link['linkdate']] = $link; | ||
368 | } | ||
369 | } | ||
370 | krsort($filtered); | ||
371 | return $filtered; | ||
372 | } | ||
373 | |||
374 | /** | ||
375 | * Returns the list of links associated with a given list of tags | ||
376 | * | ||
377 | * You can specify one or more tags, separated by space or a comma, e.g. | ||
378 | * print_r($mydb->filterTags('linux programming')); | ||
379 | */ | 345 | */ |
380 | public function filterTags($tags, $casesensitive=false) | 346 | public function filter($type, $request, $casesensitive = false, $privateonly = false) |
381 | { | 347 | { |
382 | // Same as above, we use UTF-8 conversion to handle various graphemes (i.e. cyrillic, or greek) | 348 | $linkFilter = new LinkFilter($this->_links); |
383 | // FIXME: is $casesensitive ever true? | 349 | $requestFilter = is_array($request) ? implode(' ', $request) : $request; |
384 | $t = str_replace( | 350 | return $linkFilter->filter($type, trim($requestFilter), $casesensitive, $privateonly); |
385 | ',', ' ', | ||
386 | ($casesensitive ? $tags : mb_convert_case($tags, MB_CASE_LOWER, 'UTF-8')) | ||
387 | ); | ||
388 | |||
389 | $searchtags = explode(' ', $t); | ||
390 | $filtered = array(); | ||
391 | |||
392 | foreach ($this->_links as $l) { | ||
393 | $linktags = explode( | ||
394 | ' ', | ||
395 | ($casesensitive ? $l['tags']:mb_convert_case($l['tags'], MB_CASE_LOWER, 'UTF-8')) | ||
396 | ); | ||
397 | |||
398 | if (count(array_intersect($linktags, $searchtags)) == count($searchtags)) { | ||
399 | $filtered[$l['linkdate']] = $l; | ||
400 | } | ||
401 | } | ||
402 | krsort($filtered); | ||
403 | return $filtered; | ||
404 | } | ||
405 | |||
406 | |||
407 | /** | ||
408 | * Returns the list of articles for a given day, chronologically sorted | ||
409 | * | ||
410 | * Day must be in the form 'YYYYMMDD' (e.g. '20120125'), e.g. | ||
411 | * print_r($mydb->filterDay('20120125')); | ||
412 | */ | ||
413 | public function filterDay($day) | ||
414 | { | ||
415 | if (! checkDateFormat('Ymd', $day)) { | ||
416 | throw new Exception('Invalid date format'); | ||
417 | } | ||
418 | |||
419 | $filtered = array(); | ||
420 | foreach ($this->_links as $l) { | ||
421 | if (startsWith($l['linkdate'], $day)) { | ||
422 | $filtered[$l['linkdate']] = $l; | ||
423 | } | ||
424 | } | ||
425 | ksort($filtered); | ||
426 | return $filtered; | ||
427 | } | ||
428 | |||
429 | /** | ||
430 | * Returns the article corresponding to a smallHash | ||
431 | */ | ||
432 | public function filterSmallHash($smallHash) | ||
433 | { | ||
434 | $filtered = array(); | ||
435 | foreach ($this->_links as $l) { | ||
436 | if ($smallHash == smallHash($l['linkdate'])) { | ||
437 | // Yes, this is ugly and slow | ||
438 | $filtered[$l['linkdate']] = $l; | ||
439 | return $filtered; | ||
440 | } | ||
441 | } | ||
442 | return $filtered; | ||
443 | } | 351 | } |
444 | 352 | ||
445 | /** | 353 | /** |