LinkFilter.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. /**
  3. * Class LinkFilter.
  4. *
  5. * Perform search and filter operation on link data list.
  6. */
  7. class LinkFilter
  8. {
  9. /**
  10. * @var string permalinks.
  11. */
  12. public static $FILTER_HASH = 'permalink';
  13. /**
  14. * @var string text search.
  15. */
  16. public static $FILTER_TEXT = 'fulltext';
  17. /**
  18. * @var string tag filter.
  19. */
  20. public static $FILTER_TAG = 'tags';
  21. /**
  22. * @var string filter by day.
  23. */
  24. public static $FILTER_DAY = 'FILTER_DAY';
  25. /**
  26. * @var array all available links.
  27. */
  28. private $links;
  29. /**
  30. * @param array $links initialization.
  31. */
  32. public function __construct($links)
  33. {
  34. $this->links = $links;
  35. }
  36. /**
  37. * Filter links according to parameters.
  38. *
  39. * @param string $type Type of filter (eg. tags, permalink, etc.).
  40. * @param string $request Filter content.
  41. * @param bool $casesensitive Optional: Perform case sensitive filter if true.
  42. * @param bool $privateonly Optional: Only returns private links if true.
  43. *
  44. * @return array filtered link list.
  45. */
  46. public function filter($type, $request, $casesensitive = false, $privateonly = false)
  47. {
  48. switch($type) {
  49. case self::$FILTER_HASH:
  50. return $this->filterSmallHash($request);
  51. break;
  52. case self::$FILTER_TEXT:
  53. return $this->filterFulltext($request, $privateonly);
  54. break;
  55. case self::$FILTER_TAG:
  56. return $this->filterTags($request, $casesensitive, $privateonly);
  57. break;
  58. case self::$FILTER_DAY:
  59. return $this->filterDay($request);
  60. break;
  61. default:
  62. return $this->noFilter($privateonly);
  63. }
  64. }
  65. /**
  66. * Unknown filter, but handle private only.
  67. *
  68. * @param bool $privateonly returns private link only if true.
  69. *
  70. * @return array filtered links.
  71. */
  72. private function noFilter($privateonly = false)
  73. {
  74. if (! $privateonly) {
  75. krsort($this->links);
  76. return $this->links;
  77. }
  78. $out = array();
  79. foreach ($this->links as $value) {
  80. if ($value['private']) {
  81. $out[$value['linkdate']] = $value;
  82. }
  83. }
  84. krsort($out);
  85. return $out;
  86. }
  87. /**
  88. * Returns the shaare corresponding to a smallHash.
  89. *
  90. * @param string $smallHash permalink hash.
  91. *
  92. * @return array $filtered array containing permalink data.
  93. */
  94. private function filterSmallHash($smallHash)
  95. {
  96. $filtered = array();
  97. foreach ($this->links as $l) {
  98. if ($smallHash == smallHash($l['linkdate'])) {
  99. // Yes, this is ugly and slow
  100. $filtered[$l['linkdate']] = $l;
  101. return $filtered;
  102. }
  103. }
  104. return $filtered;
  105. }
  106. /**
  107. * Returns the list of links corresponding to a full-text search
  108. *
  109. * Searches:
  110. * - in the URLs, title and description;
  111. * - are case-insensitive.
  112. *
  113. * Example:
  114. * print_r($mydb->filterFulltext('hollandais'));
  115. *
  116. * mb_convert_case($val, MB_CASE_LOWER, 'UTF-8')
  117. * - allows to perform searches on Unicode text
  118. * - see https://github.com/shaarli/Shaarli/issues/75 for examples
  119. *
  120. * @param string $searchterms search query.
  121. * @param bool $privateonly return only private links if true.
  122. *
  123. * @return array search results.
  124. */
  125. private function filterFulltext($searchterms, $privateonly = false)
  126. {
  127. // FIXME: explode(' ',$searchterms) and perform a AND search.
  128. // FIXME: accept double-quotes to search for a string "as is"?
  129. $filtered = array();
  130. $search = mb_convert_case($searchterms, MB_CASE_LOWER, 'UTF-8');
  131. $explodedSearch = explode(' ', trim($search));
  132. $keys = array('title', 'description', 'url', 'tags');
  133. // Iterate over every stored link.
  134. foreach ($this->links as $link) {
  135. $found = false;
  136. // ignore non private links when 'privatonly' is on.
  137. if (! $link['private'] && $privateonly === true) {
  138. continue;
  139. }
  140. // Iterate over searchable link fields.
  141. foreach ($keys as $key) {
  142. // Search full expression.
  143. if (strpos(
  144. mb_convert_case($link[$key], MB_CASE_LOWER, 'UTF-8'),
  145. $search
  146. ) !== false) {
  147. $found = true;
  148. }
  149. if ($found) {
  150. break;
  151. }
  152. }
  153. if ($found) {
  154. $filtered[$link['linkdate']] = $link;
  155. }
  156. }
  157. krsort($filtered);
  158. return $filtered;
  159. }
  160. /**
  161. * Returns the list of links associated with a given list of tags
  162. *
  163. * You can specify one or more tags, separated by space or a comma, e.g.
  164. * print_r($mydb->filterTags('linux programming'));
  165. *
  166. * @param string $tags list of tags separated by commas or blank spaces.
  167. * @param bool $casesensitive ignore case if false.
  168. * @param bool $privateonly returns private links only.
  169. *
  170. * @return array filtered links.
  171. */
  172. public function filterTags($tags, $casesensitive = false, $privateonly = false)
  173. {
  174. $searchtags = $this->tagsStrToArray($tags, $casesensitive);
  175. $filtered = array();
  176. foreach ($this->links as $l) {
  177. // ignore non private links when 'privatonly' is on.
  178. if (! $l['private'] && $privateonly === true) {
  179. continue;
  180. }
  181. $linktags = $this->tagsStrToArray($l['tags'], $casesensitive);
  182. if (count(array_intersect($linktags, $searchtags)) == count($searchtags)) {
  183. $filtered[$l['linkdate']] = $l;
  184. }
  185. }
  186. krsort($filtered);
  187. return $filtered;
  188. }
  189. /**
  190. * Returns the list of articles for a given day, chronologically sorted
  191. *
  192. * Day must be in the form 'YYYYMMDD' (e.g. '20120125'), e.g.
  193. * print_r($mydb->filterDay('20120125'));
  194. *
  195. * @param string $day day to filter.
  196. *
  197. * @return array all link matching given day.
  198. *
  199. * @throws Exception if date format is invalid.
  200. */
  201. public function filterDay($day)
  202. {
  203. if (! checkDateFormat('Ymd', $day)) {
  204. throw new Exception('Invalid date format');
  205. }
  206. $filtered = array();
  207. foreach ($this->links as $l) {
  208. if (startsWith($l['linkdate'], $day)) {
  209. $filtered[$l['linkdate']] = $l;
  210. }
  211. }
  212. ksort($filtered);
  213. return $filtered;
  214. }
  215. /**
  216. * Convert a list of tags (str) to an array. Also
  217. * - handle case sensitivity.
  218. * - accepts spaces commas as separator.
  219. * - remove private tags for loggedout users.
  220. *
  221. * @param string $tags string containing a list of tags.
  222. * @param bool $casesensitive will convert everything to lowercase if false.
  223. *
  224. * @return array filtered tags string.
  225. */
  226. public function tagsStrToArray($tags, $casesensitive)
  227. {
  228. // We use UTF-8 conversion to handle various graphemes (i.e. cyrillic, or greek)
  229. $tagsOut = $casesensitive ? $tags : mb_convert_case($tags, MB_CASE_LOWER, 'UTF-8');
  230. $tagsOut = str_replace(',', ' ', $tagsOut);
  231. return explode(' ', trim($tagsOut));
  232. }
  233. }