]>
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 LinkDB all available links.
41 * @param LinkDB $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 string $visibility Optional: return only all/private/public links
56 * @return array filtered link list.
58 public function filter($type, $request, $casesensitive = false, $visibility = 'all')
60 if (! in_array($visibility, ['all', 'public', 'private'])) {
65 case self
::$FILTER_HASH:
66 return $this->filterSmallHash($request);
67 case self
::$FILTER_TAG | self
::$FILTER_TEXT:
68 if (!empty($request)) {
69 $filtered = $this->links
;
70 if (isset($request[0])) {
71 $filtered = $this->filterTags($request[0], $casesensitive, $visibility);
73 if (isset($request[1])) {
74 $lf = new LinkFilter($filtered);
75 $filtered = $lf->filterFulltext($request[1], $visibility);
79 return $this->noFilter($visibility);
80 case self
::$FILTER_TEXT:
81 return $this->filterFulltext($request, $visibility);
82 case self
::$FILTER_TAG:
83 return $this->filterTags($request, $casesensitive, $visibility);
84 case self
::$FILTER_DAY:
85 return $this->filterDay($request);
87 return $this->noFilter($visibility);
92 * Unknown filter, but handle private only.
94 * @param string $visibility Optional: return only all/private/public links
96 * @return array filtered links.
98 private function noFilter($visibility = 'all')
100 if ($visibility === 'all') {
105 foreach ($this->links
as $key => $value) {
106 if ($value['private'] && $visibility === 'private') {
108 } else if (! $value['private'] && $visibility === 'public') {
117 * Returns the shaare corresponding to a smallHash.
119 * @param string $smallHash permalink hash.
121 * @return array $filtered array containing permalink data.
123 * @throws LinkNotFoundException if the smallhash doesn't match any link.
125 private function filterSmallHash($smallHash)
128 foreach ($this->links
as $key => $l) {
129 if ($smallHash == $l['shorturl']) {
130 // Yes, this is ugly and slow
131 $filtered[$key] = $l;
136 if (empty($filtered)) {
137 throw new LinkNotFoundException();
144 * Returns the list of links corresponding to a full-text search
147 * - in the URLs, title and description;
148 * - are case-insensitive;
149 * - terms surrounded by quotes " are exact terms search.
150 * - terms starting with a dash - are excluded (except exact terms).
153 * print_r($mydb->filterFulltext('hollandais'));
155 * mb_convert_case($val, MB_CASE_LOWER, 'UTF-8')
156 * - allows to perform searches on Unicode text
157 * - see https://github.com/shaarli/Shaarli/issues/75 for examples
159 * @param string $searchterms search query.
160 * @param string $visibility Optional: return only all/private/public links.
162 * @return array search results.
164 private function filterFulltext($searchterms, $visibility = 'all')
166 if (empty($searchterms)) {
167 return $this->noFilter($visibility);
171 $search = mb_convert_case(html_entity_decode($searchterms), MB_CASE_LOWER
, 'UTF-8');
172 $exactRegex = '/"([^"]+)"/';
173 // Retrieve exact search terms.
174 preg_match_all($exactRegex, $search, $exactSearch);
175 $exactSearch = array_values(array_filter($exactSearch[1]));
177 // Remove exact search terms to get AND terms search.
178 $explodedSearchAnd = explode(' ', trim(preg_replace($exactRegex, '', $search)));
179 $explodedSearchAnd = array_values(array_filter($explodedSearchAnd));
181 // Filter excluding terms and update andSearch.
182 $excludeSearch = array();
183 $andSearch = array();
184 foreach ($explodedSearchAnd as $needle) {
185 if ($needle[0] == '-' && strlen($needle) > 1) {
186 $excludeSearch[] = substr($needle, 1);
188 $andSearch[] = $needle;
192 $keys = array('title', 'description', 'url', 'tags');
194 // Iterate over every stored link.
195 foreach ($this->links
as $id => $link) {
197 // ignore non private links when 'privatonly' is on.
198 if ($visibility !== 'all') {
199 if (! $link['private'] && $visibility === 'private') {
201 } else if ($link['private'] && $visibility === 'public') {
206 // Concatenate link fields to search across fields.
207 // Adds a '\' separator for exact search terms.
209 foreach ($keys as $key) {
210 $content .= mb_convert_case($link[$key], MB_CASE_LOWER
, 'UTF-8') . '\\';
216 // First, we look for exact term search
217 for ($i = 0; $i < count($exactSearch) && $found; $i++
) {
218 $found = strpos($content, $exactSearch[$i]) !== false;
221 // Iterate over keywords, if keyword is not found,
222 // no need to check for the others. We want all or nothing.
223 for ($i = 0; $i < count($andSearch) && $found; $i++
) {
224 $found = strpos($content, $andSearch[$i]) !== false;
228 for ($i = 0; $i < count($excludeSearch) && $found; $i++
) {
229 $found = strpos($content, $excludeSearch[$i]) === false;
233 $filtered[$id] = $link;
241 * Returns the list of links associated with a given list of tags
243 * You can specify one or more tags, separated by space or a comma, e.g.
244 * print_r($mydb->filterTags('linux programming'));
246 * @param string $tags list of tags separated by commas or blank spaces.
247 * @param bool $casesensitive ignore case if false.
248 * @param string $visibility Optional: return only all/private/public links.
250 * @return array filtered links.
252 public function filterTags($tags, $casesensitive = false, $visibility = 'all')
254 // Implode if array for clean up.
255 $tags = is_array($tags) ? trim(implode(' ', $tags)) : $tags;
256 if ($tags === false) {
257 return $this->filterUntagged($visibility);
260 return $this->noFilter($visibility);
263 $searchtags = self
::tagsStrToArray($tags, $casesensitive);
265 if (empty($searchtags)) {
269 foreach ($this->links
as $key => $link) {
270 // ignore non private links when 'privatonly' is on.
271 if ($visibility !== 'all') {
272 if (! $link['private'] && $visibility === 'private') {
274 } else if ($link['private'] && $visibility === 'public') {
279 $linktags = self
::tagsStrToArray($link['tags'], $casesensitive);
282 for ($i = 0 ; $i < count($searchtags) && $found; $i++
) {
283 // Exclusive search, quit if tag found.
284 // Or, tag not found in the link, quit.
285 if (($searchtags[$i][0] == '-'
286 && $this->searchTagAndHashTag(substr($searchtags[$i], 1), $linktags, $link['description']))
287 || ($searchtags[$i][0] != '-')
288 && ! $this->searchTagAndHashTag($searchtags[$i], $linktags, $link['description'])
295 $filtered[$key] = $link;
302 * Return only links without any tag.
304 * @param string $visibility return only all/private/public links.
306 * @return array filtered links.
308 public function filterUntagged($visibility)
311 foreach ($this->links
as $key => $link) {
312 if ($visibility !== 'all') {
313 if (! $link['private'] && $visibility === 'private') {
315 } else if ($link['private'] && $visibility === 'public') {
320 if (empty(trim($link['tags']))) {
321 $filtered[$key] = $link;
329 * Returns the list of articles for a given day, chronologically sorted
331 * Day must be in the form 'YYYYMMDD' (e.g. '20120125'), e.g.
332 * print_r($mydb->filterDay('20120125'));
334 * @param string $day day to filter.
336 * @return array all link matching given day.
338 * @throws Exception if date format is invalid.
340 public function filterDay($day)
342 if (! checkDateFormat('Ymd', $day)) {
343 throw new Exception('Invalid date format');
347 foreach ($this->links
as $key => $l) {
348 if ($l['created']->format('Ymd') == $day) {
349 $filtered[$key] = $l;
354 return array_reverse($filtered, true);
358 * Check if a tag is found in the taglist, or as an hashtag in the link description.
360 * @param string $tag Tag to search.
361 * @param array $taglist List of tags for the current link.
362 * @param string $description Link description.
364 * @return bool True if found, false otherwise.
366 protected function searchTagAndHashTag($tag, $taglist, $description)
368 if (in_array($tag, $taglist)) {
372 if (preg_match('/(^| )#'. $tag .'([^'. self
::$HASHTAG_CHARS .']|$)/mui', $description) > 0) {
380 * Convert a list of tags (str) to an array. Also
381 * - handle case sensitivity.
382 * - accepts spaces commas as separator.
384 * @param string $tags string containing a list of tags.
385 * @param bool $casesensitive will convert everything to lowercase if false.
387 * @return array filtered tags string.
389 public static function tagsStrToArray($tags, $casesensitive)
391 // We use UTF-8 conversion to handle various graphemes (i.e. cyrillic, or greek)
392 $tagsOut = $casesensitive ? $tags : mb_convert_case($tags, MB_CASE_LOWER
, 'UTF-8');
393 $tagsOut = str_replace(',', ' ', $tagsOut);
395 return preg_split('/\s+/', $tagsOut, -1, PREG_SPLIT_NO_EMPTY
);
399 class LinkNotFoundException
extends Exception
401 protected $message = 'The link you are trying to reach does not exist or has been deleted.';