use Exception;
use Shaarli\Bookmark\Exception\BookmarkNotFoundException;
+use Shaarli\Config\ConfigManager;
/**
* Class LinkFilter.
*/
private $bookmarks;
+ /** @var ConfigManager */
+ protected $conf;
+
/**
* @param Bookmark[] $bookmarks initialization.
*/
- public function __construct($bookmarks)
+ public function __construct($bookmarks, ConfigManager $conf)
{
$this->bookmarks = $bookmarks;
+ $this->conf = $conf;
}
/**
$filtered = $this->bookmarks;
}
if (!empty($request[0])) {
- $filtered = (new BookmarkFilter($filtered))->filterTags($request[0], $casesensitive, $visibility);
+ $filtered = (new BookmarkFilter($filtered, $this->conf))
+ ->filterTags($request[0], $casesensitive, $visibility)
+ ;
}
if (!empty($request[1])) {
- $filtered = (new BookmarkFilter($filtered))->filterFulltext($request[1], $visibility);
+ $filtered = (new BookmarkFilter($filtered, $this->conf))
+ ->filterFulltext($request[1], $visibility)
+ ;
}
return $filtered;
case self::$FILTER_TEXT:
*
* @return string generated regex fragment
*/
- private static function tag2regex(string $tag): string
+ protected function tag2regex(string $tag): string
{
+ $tagsSeparator = $this->conf->get('general.tags_separator', ' ');
$len = strlen($tag);
if (!$len || $tag === "-" || $tag === "*") {
// nothing to search, return empty regex
$i = 0; // start at first character
$regex = '(?='; // use positive lookahead
}
- $regex .= '.*(?:^| )'; // before tag may only be a space or the beginning
+ // before tag may only be the separator or the beginning
+ $regex .= '.*(?:^|' . $tagsSeparator . ')';
// iterate over string, separating it into placeholder and content
for (; $i < $len; $i++) {
if ($tag[$i] === '*') {
// placeholder found
- $regex .= '[^ ]*?';
+ $regex .= '[^' . $tagsSeparator . ']*?';
} else {
// regular characters
$offset = strpos($tag, '*', $i);
$i = $offset;
}
}
- $regex .= '(?:$| ))'; // after the tag may only be a space or the end
+ // after the tag may only be the separator or the end
+ $regex .= '(?:$|' . $tagsSeparator . '))';
return $regex;
}
*/
public function filterTags($tags, bool $casesensitive = false, string $visibility = 'all')
{
+ $tagsSeparator = $this->conf->get('general.tags_separator', ' ');
// get single tags (we may get passed an array, even though the docs say different)
$inputTags = $tags;
if (!is_array($tags)) {
// we got an input string, split tags
- $inputTags = preg_split('/(?:\s+)|,/', $inputTags, -1, PREG_SPLIT_NO_EMPTY);
+ $inputTags = tags_str2array($inputTags, $tagsSeparator);
}
- if (!count($inputTags)) {
+ if (count($inputTags) === 0) {
// no input tags
return $this->noFilter($visibility);
}
}
// build regex from all tags
- $re = '/^' . implode(array_map("self::tag2regex", $inputTags)) . '.*$/';
+ $re = '/^' . implode(array_map([$this, 'tag2regex'], $inputTags)) . '.*$/';
if (!$casesensitive) {
// make regex case insensitive
$re .= 'i';
continue;
}
}
- $search = $link->getTagsString(); // build search string, start with tags of current link
+ // build search string, start with tags of current link
+ $search = $link->getTagsString($tagsSeparator);
if (strlen(trim($link->getDescription())) && strpos($link->getDescription(), '#') !== false) {
// description given and at least one possible tag found
$descTags = array();
);
if (count($descTags[1])) {
// there were some tags in the description, add them to the search string
- $search .= ' ' . implode(' ', $descTags[1]);
+ $search .= $tagsSeparator . tags_array2str($descTags[1], $tagsSeparator);
}
- };
+ }
// match regular expression with search string
if (!preg_match($re, $search)) {
// this entry does _not_ match our regex
}
}
- if (empty(trim($link->getTagsString()))) {
+ if (empty($link->getTags())) {
$filtered[$key] = $link;
}
}
*/
protected function buildFullTextSearchableLink(Bookmark $link, array &$lengths): string
{
+ $tagString = $link->getTagsString($this->conf->get('general.tags_separator', ' '));
$content = mb_convert_case($link->getTitle(), MB_CASE_LOWER, 'UTF-8') .'\\';
$content .= mb_convert_case($link->getDescription(), MB_CASE_LOWER, 'UTF-8') .'\\';
$content .= mb_convert_case($link->getUrl(), MB_CASE_LOWER, 'UTF-8') .'\\';
- $content .= mb_convert_case($link->getTagsString(), MB_CASE_LOWER, 'UTF-8') .'\\';
+ $content .= mb_convert_case($tagString, MB_CASE_LOWER, 'UTF-8') .'\\';
$lengths['title'] = ['start' => 0, 'end' => mb_strlen($link->getTitle())];
$nextField = $lengths['title']['end'] + 1;
$nextField = $lengths['description']['end'] + 1;
$lengths['url'] = ['start' => $nextField, 'end' => $nextField + mb_strlen($link->getUrl())];
$nextField = $lengths['url']['end'] + 1;
- $lengths['tags'] = ['start' => $nextField, 'end' => $nextField + mb_strlen($link->getTagsString())];
+ $lengths['tags'] = ['start' => $nextField, 'end' => $nextField + mb_strlen($tagString)];
return $content;
}