3 namespace Shaarli\Legacy
;
6 use Shaarli\Bookmark\Exception\BookmarkNotFoundException
;
11 * Perform search and filter operation on link data list.
15 class LegacyLinkFilter
18 * @var string permalinks.
20 public static $FILTER_HASH = 'permalink';
23 * @var string text search.
25 public static $FILTER_TEXT = 'fulltext';
28 * @var string tag filter.
30 public static $FILTER_TAG = 'tags';
33 * @var string filter by day.
35 public static $FILTER_DAY = 'FILTER_DAY';
38 * @var string Allowed characters for hashtags (regex syntax).
40 public static $HASHTAG_CHARS = '\p{Pc}\p{N}\p{L}\p{Mn}';
43 * @var LegacyLinkDB all available links.
48 * @param LegacyLinkDB $links initialization.
50 public function __construct($links)
52 $this->links
= $links;
56 * Filter links according to parameters.
58 * @param string $type Type of filter (eg. tags, permalink, etc.).
59 * @param mixed $request Filter content.
60 * @param bool $casesensitive Optional: Perform case sensitive filter if true.
61 * @param string $visibility Optional: return only all/private/public links
62 * @param string $untaggedonly Optional: return only untagged links. Applies only if $type includes FILTER_TAG
64 * @return array filtered link list.
66 public function filter($type, $request, $casesensitive = false, $visibility = 'all', $untaggedonly = false)
68 if (!in_array($visibility, ['all', 'public', 'private'])) {
73 case self
::$FILTER_HASH:
74 return $this->filterSmallHash($request);
75 case self
::$FILTER_TAG | self
::$FILTER_TEXT: // == "vuotext"
76 $noRequest = empty($request) || (empty($request[0]) && empty($request[1]));
79 return $this->filterUntagged($visibility);
81 return $this->noFilter($visibility);
84 $filtered = $this->filterUntagged($visibility);
86 $filtered = $this->links
;
88 if (!empty($request[0])) {
89 $filtered = (new LegacyLinkFilter($filtered))->filterTags($request[0], $casesensitive, $visibility);
91 if (!empty($request[1])) {
92 $filtered = (new LegacyLinkFilter($filtered))->filterFulltext($request[1], $visibility);
95 case self
::$FILTER_TEXT:
96 return $this->filterFulltext($request, $visibility);
97 case self
::$FILTER_TAG:
99 return $this->filterUntagged($visibility);
101 return $this->filterTags($request, $casesensitive, $visibility);
103 case self
::$FILTER_DAY:
104 return $this->filterDay($request);
106 return $this->noFilter($visibility);
111 * Unknown filter, but handle private only.
113 * @param string $visibility Optional: return only all/private/public links
115 * @return array filtered links.
117 private function noFilter($visibility = 'all')
119 if ($visibility === 'all') {
124 foreach ($this->links
as $key => $value) {
125 if ($value['private'] && $visibility === 'private') {
127 } elseif (!$value['private'] && $visibility === 'public') {
136 * Returns the shaare corresponding to a smallHash.
138 * @param string $smallHash permalink hash.
140 * @return array $filtered array containing permalink data.
142 * @throws BookmarkNotFoundException if the smallhash doesn't match any link.
144 private function filterSmallHash($smallHash)
147 foreach ($this->links
as $key => $l) {
148 if ($smallHash == $l['shorturl']) {
149 // Yes, this is ugly and slow
150 $filtered[$key] = $l;
155 if (empty($filtered)) {
156 throw new BookmarkNotFoundException();
163 * Returns the list of links corresponding to a full-text search
166 * - in the URLs, title and description;
167 * - are case-insensitive;
168 * - terms surrounded by quotes " are exact terms search.
169 * - terms starting with a dash - are excluded (except exact terms).
172 * print_r($mydb->filterFulltext('hollandais'));
174 * mb_convert_case($val, MB_CASE_LOWER, 'UTF-8')
175 * - allows to perform searches on Unicode text
176 * - see https://github.com/shaarli/Shaarli/issues/75 for examples
178 * @param string $searchterms search query.
179 * @param string $visibility Optional: return only all/private/public links.
181 * @return array search results.
183 private function filterFulltext($searchterms, $visibility = 'all')
185 if (empty($searchterms)) {
186 return $this->noFilter($visibility);
190 $search = mb_convert_case(html_entity_decode($searchterms), MB_CASE_LOWER
, 'UTF-8');
191 $exactRegex = '/"([^"]+)"/';
192 // Retrieve exact search terms.
193 preg_match_all($exactRegex, $search, $exactSearch);
194 $exactSearch = array_values(array_filter($exactSearch[1]));
196 // Remove exact search terms to get AND terms search.
197 $explodedSearchAnd = explode(' ', trim(preg_replace($exactRegex, '', $search)));
198 $explodedSearchAnd = array_values(array_filter($explodedSearchAnd));
200 // Filter excluding terms and update andSearch.
201 $excludeSearch = array();
202 $andSearch = array();
203 foreach ($explodedSearchAnd as $needle) {
204 if ($needle[0] == '-' && strlen($needle) > 1) {
205 $excludeSearch[] = substr($needle, 1);
207 $andSearch[] = $needle;
211 $keys = array('title', 'description', 'url', 'tags');
213 // Iterate over every stored link.
214 foreach ($this->links
as $id => $link) {
215 // ignore non private links when 'privatonly' is on.
216 if ($visibility !== 'all') {
217 if (!$link['private'] && $visibility === 'private') {
219 } elseif ($link['private'] && $visibility === 'public') {
224 // Concatenate link fields to search across fields.
225 // Adds a '\' separator for exact search terms.
227 foreach ($keys as $key) {
228 $content .= mb_convert_case($link[$key], MB_CASE_LOWER
, 'UTF-8') . '\\';
234 // First, we look for exact term search
235 for ($i = 0; $i < count($exactSearch) && $found; $i++
) {
236 $found = strpos($content, $exactSearch[$i]) !== false;
239 // Iterate over keywords, if keyword is not found,
240 // no need to check for the others. We want all or nothing.
241 for ($i = 0; $i < count($andSearch) && $found; $i++
) {
242 $found = strpos($content, $andSearch[$i]) !== false;
246 for ($i = 0; $i < count($excludeSearch) && $found; $i++
) {
247 $found = strpos($content, $excludeSearch[$i]) === false;
251 $filtered[$id] = $link;
259 * generate a regex fragment out of a tag
261 * @param string $tag to to generate regexs from. may start with '-' to negate, contain '*' as wildcard
263 * @return string generated regex fragment
265 private static function tag2regex($tag)
268 if (!$len || $tag === "-" || $tag === "*") {
269 // nothing to search, return empty regex
272 if ($tag[0] === "-") {
274 $i = 1; // use offset to start after '-' character
275 $regex = '(?!'; // create negative lookahead
277 $i = 0; // start at first character
278 $regex = '(?='; // use positive lookahead
280 $regex .= '.*(?:^| )'; // before tag may only be a space or the beginning
281 // iterate over string, separating it into placeholder and content
282 for (; $i < $len; $i++
) {
283 if ($tag[$i] === '*') {
287 // regular characters
288 $offset = strpos($tag, '*', $i);
289 if ($offset === false) {
290 // no placeholder found, set offset to end of string
293 // subtract one, as we want to get before the placeholder or end of string
295 // we got a tag name that we want to search for. escape any regex characters to prevent conflicts.
296 $regex .= preg_quote(substr($tag, $i, $offset - $i +
1), '/');
301 $regex .= '(?:$| ))'; // after the tag may only be a space or the end
306 * Returns the list of links associated with a given list of tags
308 * You can specify one or more tags, separated by space or a comma, e.g.
309 * print_r($mydb->filterTags('linux programming'));
311 * @param string $tags list of tags separated by commas or blank spaces.
312 * @param bool $casesensitive ignore case if false.
313 * @param string $visibility Optional: return only all/private/public links.
315 * @return array filtered links.
317 public function filterTags($tags, $casesensitive = false, $visibility = 'all')
319 // get single tags (we may get passed an array, even though the docs say different)
321 if (!is_array($tags)) {
322 // we got an input string, split tags
323 $inputTags = preg_split('/(?:\s+)|,/', $inputTags, -1, PREG_SPLIT_NO_EMPTY
);
326 if (!count($inputTags)) {
328 return $this->noFilter($visibility);
331 // build regex from all tags
332 $re = '/^' . implode(array_map("self::tag2regex", $inputTags)) . '.*$/';
333 if (!$casesensitive) {
334 // make regex case insensitive
338 // create resulting array
341 // iterate over each link
342 foreach ($this->links
as $key => $link) {
343 // check level of visibility
344 // ignore non private links when 'privateonly' is on.
345 if ($visibility !== 'all') {
346 if (!$link['private'] && $visibility === 'private') {
348 } elseif ($link['private'] && $visibility === 'public') {
352 $search = $link['tags']; // build search string, start with tags of current link
353 if (strlen(trim($link['description'])) && strpos($link['description'], '#') !== false) {
354 // description given and at least one possible tag found
356 // find all tags in the form of #tag in the description
358 '/(?<![' . self
::$HASHTAG_CHARS . '])#([' . self
::$HASHTAG_CHARS . ']+?)\b/sm',
359 $link['description'],
362 if (count($descTags[1])) {
363 // there were some tags in the description, add them to the search string
364 $search .= ' ' . implode(' ', $descTags[1]);
367 // match regular expression with search string
368 if (!preg_match($re, $search)) {
369 // this entry does _not_ match our regex
372 $filtered[$key] = $link;
378 * Return only links without any tag.
380 * @param string $visibility return only all/private/public links.
382 * @return array filtered links.
384 public function filterUntagged($visibility)
387 foreach ($this->links
as $key => $link) {
388 if ($visibility !== 'all') {
389 if (!$link['private'] && $visibility === 'private') {
391 } elseif ($link['private'] && $visibility === 'public') {
396 if (empty(trim($link['tags']))) {
397 $filtered[$key] = $link;
405 * Returns the list of articles for a given day, chronologically sorted
407 * Day must be in the form 'YYYYMMDD' (e.g. '20120125'), e.g.
408 * print_r($mydb->filterDay('20120125'));
410 * @param string $day day to filter.
412 * @return array all link matching given day.
414 * @throws Exception if date format is invalid.
416 public function filterDay($day)
418 if (!checkDateFormat('Ymd', $day)) {
419 throw new Exception('Invalid date format');
423 foreach ($this->links
as $key => $l) {
424 if ($l['created']->format('Ymd') == $day) {
425 $filtered[$key] = $l;
430 return array_reverse($filtered, true);
434 * Convert a list of tags (str) to an array. Also
435 * - handle case sensitivity.
436 * - accepts spaces commas as separator.
438 * @param string $tags string containing a list of tags.
439 * @param bool $casesensitive will convert everything to lowercase if false.
441 * @return array filtered tags string.
443 public static function tagsStrToArray($tags, $casesensitive)
445 // We use UTF-8 conversion to handle various graphemes (i.e. cyrillic, or greek)
446 $tagsOut = $casesensitive ? $tags : mb_convert_case($tags, MB_CASE_LOWER
, 'UTF-8');
447 $tagsOut = str_replace(',', ' ', $tagsOut);
449 return preg_split('/\s+/', $tagsOut, -1, PREG_SPLIT_NO_EMPTY
);