Merge pull request #947 from thewilli/wildcardsearch

wildcard tag search support
This commit is contained in:
ArthurHoaro 2017-09-29 18:38:02 +02:00 committed by GitHub
commit a59bbf50d7
2 changed files with 167 additions and 47 deletions

View file

@ -249,6 +249,51 @@ private function filterFulltext($searchterms, $visibility = 'all')
return $filtered; return $filtered;
} }
/**
* generate a regex fragment out of a tag
* @param string $tag to to generate regexs from. may start with '-' to negate, contain '*' as wildcard
* @return string generated regex fragment
*/
private static function tag2regex($tag)
{
$len = strlen($tag);
if(!$len || $tag === "-" || $tag === "*"){
// nothing to search, return empty regex
return '';
}
if($tag[0] === "-") {
// query is negated
$i = 1; // use offset to start after '-' character
$regex = '(?!'; // create negative lookahead
} else {
$i = 0; // start at first character
$regex = '(?='; // use positive lookahead
}
$regex .= '.*(?:^| )'; // before tag may only be a space or the beginning
// iterate over string, separating it into placeholder and content
for(; $i < $len; $i++){
if($tag[$i] === '*'){
// placeholder found
$regex .= '[^ ]*?';
} else {
// regular characters
$offset = strpos($tag, '*', $i);
if($offset === false){
// no placeholder found, set offset to end of string
$offset = $len;
}
// subtract one, as we want to get before the placeholder or end of string
$offset -= 1;
// we got a tag name that we want to search for. escape any regex characters to prevent conflicts.
$regex .= preg_quote(substr($tag, $i, $offset - $i + 1), '/');
// move $i on
$i = $offset;
}
}
$regex .= '(?:$| ))'; // after the tag may only be a space or the end
return $regex;
}
/** /**
* Returns the list of links associated with a given list of tags * Returns the list of links associated with a given list of tags
* *
@ -263,20 +308,32 @@ private function filterFulltext($searchterms, $visibility = 'all')
*/ */
public function filterTags($tags, $casesensitive = false, $visibility = 'all') public function filterTags($tags, $casesensitive = false, $visibility = 'all')
{ {
// Implode if array for clean up. // get single tags (we may get passed an array, even though the docs say different)
$tags = is_array($tags) ? trim(implode(' ', $tags)) : $tags; $inputTags = $tags;
if (empty($tags)) { if(!is_array($tags)) {
// we got an input string, split tags
$inputTags = preg_split('/(?:\s+)|,/', $inputTags, -1, PREG_SPLIT_NO_EMPTY);
}
if(!count($inputTags)){
// no input tags
return $this->noFilter($visibility); return $this->noFilter($visibility);
} }
$searchtags = self::tagsStrToArray($tags, $casesensitive); // build regex from all tags
$filtered = array(); $re = '/^' . implode(array_map("self::tag2regex", $inputTags)) . '.*$/';
if (empty($searchtags)) { if(!$casesensitive) {
return $filtered; // make regex case insensitive
$re .= 'i';
} }
// create resulting array
$filtered = array();
// iterate over each link
foreach ($this->links as $key => $link) { foreach ($this->links as $key => $link) {
// ignore non private links when 'privatonly' is on. // check level of visibility
// ignore non private links when 'privateonly' is on.
if ($visibility !== 'all') { if ($visibility !== 'all') {
if (! $link['private'] && $visibility === 'private') { if (! $link['private'] && $visibility === 'private') {
continue; continue;
@ -284,26 +341,28 @@ public function filterTags($tags, $casesensitive = false, $visibility = 'all')
continue; continue;
} }
} }
$search = $link['tags']; // build search string, start with tags of current link
$linktags = self::tagsStrToArray($link['tags'], $casesensitive); if(strlen(trim($link['description'])) && strpos($link['description'], '#') !== false){
// description given and at least one possible tag found
$found = true; $descTags = array();
for ($i = 0 ; $i < count($searchtags) && $found; $i++) { // find all tags in the form of #tag in the description
// Exclusive search, quit if tag found. preg_match_all(
// Or, tag not found in the link, quit. '/(?<![' . self::$HASHTAG_CHARS . '])#([' . self::$HASHTAG_CHARS . ']+?)\b/sm',
if (($searchtags[$i][0] == '-' $link['description'],
&& $this->searchTagAndHashTag(substr($searchtags[$i], 1), $linktags, $link['description'])) $descTags
|| ($searchtags[$i][0] != '-') );
&& ! $this->searchTagAndHashTag($searchtags[$i], $linktags, $link['description']) if(count($descTags[1])){
) { // there were some tags in the description, add them to the search string
$found = false; $search .= ' ' . implode(' ', $descTags[1]);
} }
};
// match regular expression with search string
if(!preg_match($re, $search)){
// this entry does _not_ match our regex
continue;
} }
if ($found) {
$filtered[$key] = $link; $filtered[$key] = $link;
} }
}
return $filtered; return $filtered;
} }
@ -363,28 +422,6 @@ public function filterDay($day)
return array_reverse($filtered, true); return array_reverse($filtered, true);
} }
/**
* Check if a tag is found in the taglist, or as an hashtag in the link description.
*
* @param string $tag Tag to search.
* @param array $taglist List of tags for the current link.
* @param string $description Link description.
*
* @return bool True if found, false otherwise.
*/
protected function searchTagAndHashTag($tag, $taglist, $description)
{
if (in_array($tag, $taglist)) {
return true;
}
if (preg_match('/(^| )#'. $tag .'([^'. self::$HASHTAG_CHARS .']|$)/mui', $description) > 0) {
return true;
}
return false;
}
/** /**
* Convert a list of tags (str) to an array. Also * Convert a list of tags (str) to an array. Also
* - handle case sensitivity. * - handle case sensitivity.

View file

@ -367,6 +367,89 @@ public function testGetLinksSearchTags()
$this->assertEquals(1, count($data)); $this->assertEquals(1, count($data));
$this->assertEquals(41, $data[0]['id']); $this->assertEquals(41, $data[0]['id']);
$this->assertEquals(self::NB_FIELDS_LINK, count($data[0])); $this->assertEquals(self::NB_FIELDS_LINK, count($data[0]));
// wildcard: placeholder at the start
$env = Environment::mock([
'REQUEST_METHOD' => 'GET',
'QUERY_STRING' => 'searchtags=*Tuff',
]);
$request = Request::createFromEnvironment($env);
$response = $this->controller->getLinks($request, new Response());
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode((string) $response->getBody(), true);
$this->assertEquals(2, count($data));
$this->assertEquals(41, $data[0]['id']);
// wildcard: placeholder at the end
$env = Environment::mock([
'REQUEST_METHOD' => 'GET',
'QUERY_STRING' => 'searchtags=c*',
]);
$request = Request::createFromEnvironment($env);
$response = $this->controller->getLinks($request, new Response());
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode((string) $response->getBody(), true);
$this->assertEquals(4, count($data));
$this->assertEquals(6, $data[0]['id']);
// wildcard: placeholder at the middle
$env = Environment::mock([
'REQUEST_METHOD' => 'GET',
'QUERY_STRING' => 'searchtags=w*b',
]);
$request = Request::createFromEnvironment($env);
$response = $this->controller->getLinks($request, new Response());
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode((string) $response->getBody(), true);
$this->assertEquals(4, count($data));
$this->assertEquals(6, $data[0]['id']);
// wildcard: match all
$env = Environment::mock([
'REQUEST_METHOD' => 'GET',
'QUERY_STRING' => 'searchtags=*',
]);
$request = Request::createFromEnvironment($env);
$response = $this->controller->getLinks($request, new Response());
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode((string) $response->getBody(), true);
$this->assertEquals(9, count($data));
$this->assertEquals(41, $data[0]['id']);
// wildcard: optional ('*' does not need to expand)
$env = Environment::mock([
'REQUEST_METHOD' => 'GET',
'QUERY_STRING' => 'searchtags=*stuff*',
]);
$request = Request::createFromEnvironment($env);
$response = $this->controller->getLinks($request, new Response());
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode((string) $response->getBody(), true);
$this->assertEquals(2, count($data));
$this->assertEquals(41, $data[0]['id']);
// wildcard: exclusions
$env = Environment::mock([
'REQUEST_METHOD' => 'GET',
'QUERY_STRING' => 'searchtags=*a*+-*e*',
]);
$request = Request::createFromEnvironment($env);
$response = $this->controller->getLinks($request, new Response());
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode((string) $response->getBody(), true);
$this->assertEquals(1, count($data));
$this->assertEquals(41, $data[0]['id']); // finds '#hashtag' in descr.
// wildcard: exclude all
$env = Environment::mock([
'REQUEST_METHOD' => 'GET',
'QUERY_STRING' => 'searchtags=-*',
]);
$request = Request::createFromEnvironment($env);
$response = $this->controller->getLinks($request, new Response());
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode((string) $response->getBody(), true);
$this->assertEquals(0, count($data));
} }
/** /**