]>
git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/LinkFilter.php
6 * Perform search and filter operation on link data list.
11 * @var string permalinks.
13 public static $FILTER_HASH = 'permalink';
16 * @var string text search.
18 public static $FILTER_TEXT = 'fulltext';
21 * @var string tag filter.
23 public static $FILTER_TAG = 'tags';
26 * @var string filter by day.
28 public static $FILTER_DAY = 'FILTER_DAY';
31 * @var string Allowed characters for hashtags (regex syntax).
33 public static $HASHTAG_CHARS = '\p{Pc}\p{N}\p{L}\p{Mn}';
36 * @var array all available links.
41 * @param array $links initialization.
43 public function __construct($links)
45 $this->links
= $links;
49 * Filter links according to parameters.
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.
56 * @return array filtered link list.
58 public function filter($type, $request, $casesensitive = false, $privateonly = false)
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);
69 if (isset($request[1])) {
70 $lf = new LinkFilter($filtered);
71 $filtered = $lf->filterFulltext($request[1], $privateonly);
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);
83 return $this->noFilter($privateonly);
88 * Unknown filter, but handle private only.
90 * @param bool $privateonly returns private link only if true.
92 * @return array filtered links.
94 private function noFilter($privateonly = false)
102 foreach ($this->links
as $value) {
103 if ($value['private']) {
104 $out[$value['linkdate']] = $value;
113 * Returns the shaare corresponding to a smallHash.
115 * @param string $smallHash permalink hash.
117 * @return array $filtered array containing permalink data.
119 * @throws LinkNotFoundException if the smallhash doesn't match any link.
121 private function filterSmallHash($smallHash)
124 foreach ($this->links
as $l) {
125 if ($smallHash == smallHash($l['linkdate'])) {
126 // Yes, this is ugly and slow
127 $filtered[$l['linkdate']] = $l;
132 if (empty($filtered)) {
133 throw new LinkNotFoundException();
140 * Returns the list of links corresponding to a full-text search
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).
149 * print_r($mydb->filterFulltext('hollandais'));
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
155 * @param string $searchterms search query.
156 * @param bool $privateonly return only private links if true.
158 * @return array search results.
160 private function filterFulltext($searchterms, $privateonly = false)
162 if (empty($searchterms)) {
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]));
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));
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);
184 $andSearch[] = $needle;
188 $keys = array('title', 'description', 'url', 'tags');
190 // Iterate over every stored link.
191 foreach ($this->links
as $link) {
193 // ignore non private links when 'privatonly' is on.
194 if (! $link['private'] && $privateonly === true) {
198 // Concatenate link fields to search across fields.
199 // Adds a '\' separator for exact search terms.
201 foreach ($keys as $key) {
202 $content .= mb_convert_case($link[$key], MB_CASE_LOWER
, 'UTF-8') . '\\';
208 // First, we look for exact term search
209 for ($i = 0; $i < count($exactSearch) && $found; $i++
) {
210 $found = strpos($content, $exactSearch[$i]) !== false;
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;
220 for ($i = 0; $i < count($excludeSearch) && $found; $i++
) {
221 $found = strpos($content, $excludeSearch[$i]) === false;
225 $filtered[$link['linkdate']] = $link;
234 * Returns the list of links associated with a given list of tags
236 * You can specify one or more tags, separated by space or a comma, e.g.
237 * print_r($mydb->filterTags('linux programming'));
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.
243 * @return array filtered links.
245 public function filterTags($tags, $casesensitive = false, $privateonly = false)
247 // Implode if array for clean up.
248 $tags = is_array($tags) ? trim(implode(' ', $tags)) : $tags;
253 $searchtags = self
::tagsStrToArray($tags, $casesensitive);
255 if (empty($searchtags)) {
259 foreach ($this->links
as $link) {
260 // ignore non private links when 'privatonly' is on.
261 if (! $link['private'] && $privateonly === true) {
265 $linktags = self
::tagsStrToArray($link['tags'], $casesensitive);
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'])
281 $filtered[$link['linkdate']] = $link;
289 * Returns the list of articles for a given day, chronologically sorted
291 * Day must be in the form 'YYYYMMDD' (e.g. '20120125'), e.g.
292 * print_r($mydb->filterDay('20120125'));
294 * @param string $day day to filter.
296 * @return array all link matching given day.
298 * @throws Exception if date format is invalid.
300 public function filterDay($day)
302 if (! checkDateFormat('Ymd', $day)) {
303 throw new Exception('Invalid date format');
307 foreach ($this->links
as $l) {
308 if (startsWith($l['linkdate'], $day)) {
309 $filtered[$l['linkdate']] = $l;
317 * Check if a tag is found in the taglist, or as an hashtag in the link description.
319 * @param string $tag Tag to search.
320 * @param array $taglist List of tags for the current link.
321 * @param string $description Link description.
323 * @return bool True if found, false otherwise.
325 protected function searchTagAndHashTag($tag, $taglist, $description)
327 if (in_array($tag, $taglist)) {
331 if (preg_match('/(^| )#'. $tag .'([^'. self
::$HASHTAG_CHARS .']|$)/mui', $description) > 0) {
339 * Convert a list of tags (str) to an array. Also
340 * - handle case sensitivity.
341 * - accepts spaces commas as separator.
343 * @param string $tags string containing a list of tags.
344 * @param bool $casesensitive will convert everything to lowercase if false.
346 * @return array filtered tags string.
348 public static function tagsStrToArray($tags, $casesensitive)
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);
354 return array_values(array_filter(explode(' ', trim($tagsOut)), 'strlen'));
358 class LinkNotFoundException
extends Exception
360 protected $message = 'The link you are trying to reach does not exist or has been deleted.';