Fixes presence of empty tags for private tags and in search results

* Private tags: make sure empty tags are properly filtered
  * Search results:
    * Use preg_split instead of function combination
    * Add normalize_spaces to remove extra whitespaces displaying empty tags search
This commit is contained in:
ArthurHoaro 2016-12-20 11:06:22 +01:00
parent e0177549c7
commit b3051a6aae
4 changed files with 30 additions and 5 deletions

View file

@ -348,7 +348,7 @@ class LinkFilter
$tagsOut = $casesensitive ? $tags : mb_convert_case($tags, MB_CASE_LOWER, 'UTF-8');
$tagsOut = str_replace(',', ' ', $tagsOut);
return array_values(array_filter(explode(' ', trim($tagsOut)), 'strlen'));
return preg_split('/\s+/', $tagsOut, -1, PREG_SPLIT_NO_EMPTY);
}
}

View file

@ -257,3 +257,16 @@ function generate_api_secret($username, $salt)
return str_shuffle(substr(hash_hmac('sha512', uniqid($salt), $username), 10, 12));
}
/**
* Trim string, replace sequences of whitespaces by a single space.
* PHP equivalent to `normalize-space` XSLT function.
*
* @param string $string Input string.
*
* @return mixed Normalized string.
*/
function normalize_spaces($string)
{
return preg_replace('/\s{2,}/', ' ', trim($string));
}

View file

@ -1601,8 +1601,8 @@ function renderPage($conf, $pluginManager, $LINKSDB)
function buildLinkList($PAGE,$LINKSDB, $conf, $pluginManager)
{
// Used in templates
$searchtags = !empty($_GET['searchtags']) ? escape($_GET['searchtags']) : '';
$searchterm = !empty($_GET['searchterm']) ? escape($_GET['searchterm']) : '';
$searchtags = !empty($_GET['searchtags']) ? escape(normalize_spaces($_GET['searchtags'])) : '';
$searchterm = !empty($_GET['searchterm']) ? escape(normalize_spaces($_GET['searchterm'])) : '';
// Smallhash filter
if (! empty($_SERVER['QUERY_STRING'])
@ -1649,7 +1649,7 @@ function buildLinkList($PAGE,$LINKSDB, $conf, $pluginManager)
} else {
$link['updated_timestamp'] = '';
}
$taglist = explode(' ', $link['tags']);
$taglist = preg_split('/\s+/', $link['tags'], -1, PREG_SPLIT_NO_EMPTY);
uasort($taglist, 'strcasecmp');
$link['taglist'] = $taglist;
// Check for both signs of a note: starting with ? and 7 chars long.

View file

@ -253,7 +253,7 @@ class UtilsTest extends PHPUnit_Framework_TestCase
is_session_id_valid('c0ZqcWF3VFE2NmJBdm1HMVQ0ZHJ3UmZPbTFsNGhkNHI=')
);
}
/**
* Test generateSecretApi.
*/
@ -270,4 +270,16 @@ class UtilsTest extends PHPUnit_Framework_TestCase
$this->assertFalse(generate_api_secret('', ''));
$this->assertFalse(generate_api_secret(false, false));
}
/**
* Test normalize_spaces.
*/
public function testNormalizeSpace()
{
$str = ' foo bar is important ';
$this->assertEquals('foo bar is important', normalize_spaces($str));
$this->assertEquals('foo', normalize_spaces('foo'));
$this->assertEquals('', normalize_spaces(''));
$this->assertEquals(null, normalize_spaces(null));
}
}