3 namespace Shaarli\Bookmark
;
6 use Shaarli\Bookmark\Exception\LinkNotFoundException
;
11 * Perform search and filter operation on link data list.
16 * @var string permalinks.
18 public static $FILTER_HASH = 'permalink';
21 * @var string text search.
23 public static $FILTER_TEXT = 'fulltext';
26 * @var string tag filter.
28 public static $FILTER_TAG = 'tags';
31 * @var string filter by day.
33 public static $FILTER_DAY = 'FILTER_DAY';
36 * @var string Allowed characters for hashtags (regex syntax).
38 public static $HASHTAG_CHARS = '\p{Pc}\p{N}\p{L}\p{Mn}';
41 * @var LinkDB all available links.
46 * @param LinkDB $links initialization.
48 public function __construct($links)
50 $this->links
= $links;
54 * Filter links according to parameters.
56 * @param string $type Type of filter (eg. tags, permalink, etc.).
57 * @param mixed $request Filter content.
58 * @param bool $casesensitive Optional: Perform case sensitive filter if true.
59 * @param string $visibility Optional: return only all/private/public links
60 * @param string $untaggedonly Optional: return only untagged links. Applies only if $type includes FILTER_TAG
62 * @return array filtered link list.
64 public function filter($type, $request, $casesensitive = false, $visibility = 'all', $untaggedonly = false)
66 if (!in_array($visibility, ['all', 'public', 'private'])) {
71 case self
::$FILTER_HASH:
72 return $this->filterSmallHash($request);
73 case self
::$FILTER_TAG | self
::$FILTER_TEXT: // == "vuotext"
74 $noRequest = empty($request) || (empty($request[0]) && empty($request[1]));
77 return $this->filterUntagged($visibility);
79 return $this->noFilter($visibility);
82 $filtered = $this->filterUntagged($visibility);
84 $filtered = $this->links
;
86 if (!empty($request[0])) {
87 $filtered = (new LinkFilter($filtered))->filterTags($request[0], $casesensitive, $visibility);
89 if (!empty($request[1])) {
90 $filtered = (new LinkFilter($filtered))->filterFulltext($request[1], $visibility);
93 case self
::$FILTER_TEXT:
94 return $this->filterFulltext($request, $visibility);
95 case self
::$FILTER_TAG:
97 return $this->filterUntagged($visibility);
99 return $this->filterTags($request, $casesensitive, $visibility);
101 case self
::$FILTER_DAY:
102 return $this->filterDay($request);
104 return $this->noFilter($visibility);
109 * Unknown filter, but handle private only.
111 * @param string $visibility Optional: return only all/private/public links
113 * @return array filtered links.
115 private function noFilter($visibility = 'all')
117 if ($visibility === 'all') {
122 foreach ($this->links
as $key => $value) {
123 if ($value['private'] && $visibility === 'private') {
125 } elseif (!$value['private'] && $visibility === 'public') {
134 * Returns the shaare corresponding to a smallHash.
136 * @param string $smallHash permalink hash.
138 * @return array $filtered array containing permalink data.
140 * @throws \Shaarli\Bookmark\Exception\LinkNotFoundException if the smallhash doesn't match any link.
142 private function filterSmallHash($smallHash)
145 foreach ($this->links
as $key => $l) {
146 if ($smallHash == $l['shorturl']) {
147 // Yes, this is ugly and slow
148 $filtered[$key] = $l;
153 if (empty($filtered)) {
154 throw new LinkNotFoundException();
161 * Returns the list of links corresponding to a full-text search
164 * - in the URLs, title and description;
165 * - are case-insensitive;
166 * - terms surrounded by quotes " are exact terms search.
167 * - terms starting with a dash - are excluded (except exact terms).
170 * print_r($mydb->filterFulltext('hollandais'));
172 * mb_convert_case($val, MB_CASE_LOWER, 'UTF-8')
173 * - allows to perform searches on Unicode text
174 * - see https://github.com/shaarli/Shaarli/issues/75 for examples
176 * @param string $searchterms search query.
177 * @param string $visibility Optional: return only all/private/public links.
179 * @return array search results.
181 private function filterFulltext($searchterms, $visibility = 'all')
183 if (empty($searchterms)) {
184 return $this->noFilter($visibility);
188 $search = mb_convert_case(html_entity_decode($searchterms), MB_CASE_LOWER
, 'UTF-8');
189 $exactRegex = '/"([^"]+)"/';
190 // Retrieve exact search terms.
191 preg_match_all($exactRegex, $search, $exactSearch);
192 $exactSearch = array_values(array_filter($exactSearch[1]));
194 // Remove exact search terms to get AND terms search.
195 $explodedSearchAnd = explode(' ', trim(preg_replace($exactRegex, '', $search)));
196 $explodedSearchAnd = array_values(array_filter($explodedSearchAnd));
198 // Filter excluding terms and update andSearch.
199 $excludeSearch = array();
200 $andSearch = array();
201 foreach ($explodedSearchAnd as $needle) {
202 if ($needle[0] == '-' && strlen($needle) > 1) {
203 $excludeSearch[] = substr($needle, 1);
205 $andSearch[] = $needle;
209 $keys = array('title', 'description', 'url', 'tags');
211 // Iterate over every stored link.
212 foreach ($this->links
as $id => $link) {
213 // ignore non private links when 'privatonly' is on.
214 if ($visibility !== 'all') {
215 if (!$link['private'] && $visibility === 'private') {
217 } elseif ($link['private'] && $visibility === 'public') {
222 // Concatenate link fields to search across fields.
223 // Adds a '\' separator for exact search terms.
225 foreach ($keys as $key) {
226 $content .= mb_convert_case($link[$key], MB_CASE_LOWER
, 'UTF-8') . '\\';
232 // First, we look for exact term search
233 for ($i = 0; $i < count($exactSearch) && $found; $i++
) {
234 $found = strpos($content, $exactSearch[$i]) !== false;
237 // Iterate over keywords, if keyword is not found,
238 // no need to check for the others. We want all or nothing.
239 for ($i = 0; $i < count($andSearch) && $found; $i++
) {
240 $found = strpos($content, $andSearch[$i]) !== false;
244 for ($i = 0; $i < count($excludeSearch) && $found; $i++
) {
245 $found = strpos($content, $excludeSearch[$i]) === false;
249 $filtered[$id] = $link;
257 * generate a regex fragment out of a tag
259 * @param string $tag to to generate regexs from. may start with '-' to negate, contain '*' as wildcard
261 * @return string generated regex fragment
263 private static function tag2regex($tag)
266 if (!$len || $tag === "-" || $tag === "*") {
267 // nothing to search, return empty regex
270 if ($tag[0] === "-") {
272 $i = 1; // use offset to start after '-' character
273 $regex = '(?!'; // create negative lookahead
275 $i = 0; // start at first character
276 $regex = '(?='; // use positive lookahead
278 $regex .= '.*(?:^| )'; // before tag may only be a space or the beginning
279 // iterate over string, separating it into placeholder and content
280 for (; $i < $len; $i++
) {
281 if ($tag[$i] === '*') {
285 // regular characters
286 $offset = strpos($tag, '*', $i);
287 if ($offset === false) {
288 // no placeholder found, set offset to end of string
291 // subtract one, as we want to get before the placeholder or end of string
293 // we got a tag name that we want to search for. escape any regex characters to prevent conflicts.
294 $regex .= preg_quote(substr($tag, $i, $offset - $i +
1), '/');
299 $regex .= '(?:$| ))'; // after the tag may only be a space or the end
304 * Returns the list of links associated with a given list of tags
306 * You can specify one or more tags, separated by space or a comma, e.g.
307 * print_r($mydb->filterTags('linux programming'));
309 * @param string $tags list of tags separated by commas or blank spaces.
310 * @param bool $casesensitive ignore case if false.
311 * @param string $visibility Optional: return only all/private/public links.
313 * @return array filtered links.
315 public function filterTags($tags, $casesensitive = false, $visibility = 'all')
317 // get single tags (we may get passed an array, even though the docs say different)
319 if (!is_array($tags)) {
320 // we got an input string, split tags
321 $inputTags = preg_split('/(?:\s+)|,/', $inputTags, -1, PREG_SPLIT_NO_EMPTY
);
324 if (!count($inputTags)) {
326 return $this->noFilter($visibility);
329 // build regex from all tags
330 $re = '/^' . implode(array_map("self::tag2regex", $inputTags)) . '.*$/';
331 if (!$casesensitive) {
332 // make regex case insensitive
336 // create resulting array
339 // iterate over each link
340 foreach ($this->links
as $key => $link) {
341 // check level of visibility
342 // ignore non private links when 'privateonly' is on.
343 if ($visibility !== 'all') {
344 if (!$link['private'] && $visibility === 'private') {
346 } elseif ($link['private'] && $visibility === 'public') {
350 $search = $link['tags']; // build search string, start with tags of current link
351 if (strlen(trim($link['description'])) && strpos($link['description'], '#') !== false) {
352 // description given and at least one possible tag found
354 // find all tags in the form of #tag in the description
356 '/(?<![' . self
::$HASHTAG_CHARS . '])#([' . self
::$HASHTAG_CHARS . ']+?)\b/sm',
357 $link['description'],
360 if (count($descTags[1])) {
361 // there were some tags in the description, add them to the search string
362 $search .= ' ' . implode(' ', $descTags[1]);
365 // match regular expression with search string
366 if (!preg_match($re, $search)) {
367 // this entry does _not_ match our regex
370 $filtered[$key] = $link;
376 * Return only links without any tag.
378 * @param string $visibility return only all/private/public links.
380 * @return array filtered links.
382 public function filterUntagged($visibility)
385 foreach ($this->links
as $key => $link) {
386 if ($visibility !== 'all') {
387 if (!$link['private'] && $visibility === 'private') {
389 } elseif ($link['private'] && $visibility === 'public') {
394 if (empty(trim($link['tags']))) {
395 $filtered[$key] = $link;
403 * Returns the list of articles for a given day, chronologically sorted
405 * Day must be in the form 'YYYYMMDD' (e.g. '20120125'), e.g.
406 * print_r($mydb->filterDay('20120125'));
408 * @param string $day day to filter.
410 * @return array all link matching given day.
412 * @throws Exception if date format is invalid.
414 public function filterDay($day)
416 if (!checkDateFormat('Ymd', $day)) {
417 throw new Exception('Invalid date format');
421 foreach ($this->links
as $key => $l) {
422 if ($l['created']->format('Ymd') == $day) {
423 $filtered[$key] = $l;
428 return array_reverse($filtered, true);
432 * Convert a list of tags (str) to an array. Also
433 * - handle case sensitivity.
434 * - accepts spaces commas as separator.
436 * @param string $tags string containing a list of tags.
437 * @param bool $casesensitive will convert everything to lowercase if false.
439 * @return array filtered tags string.
441 public static function tagsStrToArray($tags, $casesensitive)
443 // We use UTF-8 conversion to handle various graphemes (i.e. cyrillic, or greek)
444 $tagsOut = $casesensitive ? $tags : mb_convert_case($tags, MB_CASE_LOWER
, 'UTF-8');
445 $tagsOut = str_replace(',', ' ', $tagsOut);
447 return preg_split('/\s+/', $tagsOut, -1, PREG_SPLIT_NO_EMPTY
);