Made tag/title search unicode aware, fixes #75

This commit is contained in:
Florian Eula 2014-12-22 16:43:37 +01:00
parent a58240ce8d
commit 2e45fdd8ff

View file

@ -794,14 +794,16 @@ public function filterFulltext($searchterms)
{ {
// FIXME: explode(' ',$searchterms) and perform a AND search. // FIXME: explode(' ',$searchterms) and perform a AND search.
// FIXME: accept double-quotes to search for a string "as is"? // FIXME: accept double-quotes to search for a string "as is"?
// Using mb_convert_case($val, MB_CASE_LOWER, 'UTF-8') allows us to perform searches on
// Unicode text. See https://github.com/shaarli/Shaarli/issues/75 for examples.
$filtered=array(); $filtered=array();
$s = strtolower($searchterms); $s = mb_convert_case($searchterms, MB_CASE_LOWER, 'UTF-8');
foreach($this->links as $l) foreach($this->links as $l)
{ {
$found= (strpos(strtolower($l['title']),$s)!==false) $found= (strpos(mb_convert_case($l['title'], MB_CASE_LOWER, 'UTF-8'),$s) !== false)
|| (strpos(strtolower($l['description']),$s)!==false) || (strpos(mb_convert_case($l['description'], MB_CASE_LOWER, 'UTF-8'),$s) !== false)
|| (strpos(strtolower($l['url']),$s)!==false) || (strpos(mb_convert_case($l['url'], MB_CASE_LOWER, 'UTF-8'),$s) !== false)
|| (strpos(strtolower($l['tags']),$s)!==false); || (strpos(mb_convert_case($l['tags'], MB_CASE_LOWER, 'UTF-8'),$s) !== false);
if ($found) $filtered[$l['linkdate']] = $l; if ($found) $filtered[$l['linkdate']] = $l;
} }
krsort($filtered); krsort($filtered);
@ -813,12 +815,14 @@ public function filterFulltext($searchterms)
// e.g. print_r($mydb->filterTags('linux programming')); // e.g. print_r($mydb->filterTags('linux programming'));
public function filterTags($tags,$casesensitive=false) public function filterTags($tags,$casesensitive=false)
{ {
$t = str_replace(',',' ',($casesensitive?$tags:strtolower($tags))); // Same as above, we use UTF-8 conversion to handle various graphemes (i.e. cyrillic, or greek)
// TODO: is $casesensitive ever true ?
$t = str_replace(',',' ',($casesensitive?$tags:mb_convert_case($tags, MB_CASE_LOWER, 'UTF-8')));
$searchtags=explode(' ',$t); $searchtags=explode(' ',$t);
$filtered=array(); $filtered=array();
foreach($this->links as $l) foreach($this->links as $l)
{ {
$linktags = explode(' ',($casesensitive?$l['tags']:strtolower($l['tags']))); $linktags = explode(' ',($casesensitive?$l['tags']:mb_convert_case($l['tags'], MB_CASE_LOWER, 'UTF-8')));
if (count(array_intersect($linktags,$searchtags)) == count($searchtags)) if (count(array_intersect($linktags,$searchtags)) == count($searchtags))
$filtered[$l['linkdate']] = $l; $filtered[$l['linkdate']] = $l;
} }