Implemented searching for a phrase in double-quotes or all words in no particular order.

+ unit tests
This commit is contained in:
Florian Voigt 2016-01-24 06:13:11 +00:00 committed by ArthurHoaro
parent db36b8812d
commit ebd8075a89
2 changed files with 48 additions and 14 deletions

View file

@ -136,16 +136,21 @@ private function filterSmallHash($smallHash)
*/ */
private function filterFulltext($searchterms, $privateonly = false) private function filterFulltext($searchterms, $privateonly = false)
{ {
// FIXME: explode(' ',$searchterms) and perform a AND search. $search = mb_convert_case(html_entity_decode($searchterms), MB_CASE_LOWER, 'UTF-8');
// FIXME: accept double-quotes to search for a string "as is"?
$filtered = array();
$search = mb_convert_case($searchterms, MB_CASE_LOWER, 'UTF-8');
$explodedSearch = explode(' ', trim($search)); $explodedSearch = explode(' ', trim($search));
$keys = array('title', 'description', 'url', 'tags'); $keys = array('title', 'description', 'url', 'tags');
$found = true;
$searchExactPhrase = false;
// Check if we're using double-quotes to search for the exact string
if ($search[0] == '"' && $search[strlen($search) - 1] == '"') {
$searchExactPhrase = true;
// Remove the double-quotes as they are not what we search for
$search = substr($search, 1, -1);
}
// Iterate over every stored link. // Iterate over every stored link.
foreach ($this->links as $link) { foreach ($this->links as $link) {
$found = false;
// ignore non private links when 'privatonly' is on. // ignore non private links when 'privatonly' is on.
if (! $link['private'] && $privateonly === true) { if (! $link['private'] && $privateonly === true) {
@ -154,14 +159,28 @@ private function filterFulltext($searchterms, $privateonly = false)
// Iterate over searchable link fields. // Iterate over searchable link fields.
foreach ($keys as $key) { foreach ($keys as $key) {
// Search full expression. // Be optimistic
if (strpos( $found = true;
mb_convert_case($link[$key], MB_CASE_LOWER, 'UTF-8'),
$search // FIXME: Find a better word for where you're searching in
) !== false) { $haystack = mb_convert_case($link[$key], MB_CASE_LOWER, 'UTF-8');
$found = true;
// When searching for the phrase, check if it's in the haystack...
if ( $searchExactPhrase && strpos($haystack, $search) !== false) {
break;
}
else {
// Iterate over keywords, if keyword is not found,
// no need to check for the others. We want all or nothing.
foreach($explodedSearch as $keyword) {
if(strpos($haystack, $keyword) === false) {
$found = false;
break;
}
}
} }
// One of the fields of the link matches, no need to check the other.
if ($found) { if ($found) {
break; break;
} }

View file

@ -173,6 +173,11 @@ public function testFilterFullTextURL()
2, 2,
count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'ars.userfriendly.org')) count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'ars.userfriendly.org'))
); );
$this->assertEquals(
2,
count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'ars org'))
);
} }
/** /**
@ -208,7 +213,17 @@ public function testFilterFullTextDescription()
{ {
$this->assertEquals( $this->assertEquals(
1, 1,
count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'media publishing')) count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'publishing media'))
);
$this->assertEquals(
1,
count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'mercurial w3c'))
);
$this->assertEquals(
2,
count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, '"free software"'))
); );
} }