Fixed edge cases in searching
- search for number (1, 2, ...) - search for "0" (empty("0") returns true so searching for "0" was impossible) - search containing / character - search containing + character - adding tags containing + character
This commit is contained in:
parent
cc6b187aa0
commit
607ef63c2f
2 changed files with 16 additions and 12 deletions
|
@ -67,7 +67,7 @@ class PostController
|
||||||
/**
|
/**
|
||||||
* @route /posts
|
* @route /posts
|
||||||
* @route /posts/{page}
|
* @route /posts/{page}
|
||||||
* @route /posts/{query}
|
* @route /posts/{query}/
|
||||||
* @route /posts/{query}/{page}
|
* @route /posts/{query}/{page}
|
||||||
* @validate page \d*
|
* @validate page \d*
|
||||||
* @validate query [^\/]*
|
* @validate query [^\/]*
|
||||||
|
@ -81,14 +81,17 @@ class PostController
|
||||||
|
|
||||||
#redirect requests in form of /posts/?query=... to canonical address
|
#redirect requests in form of /posts/?query=... to canonical address
|
||||||
$formQuery = InputHelper::get('query');
|
$formQuery = InputHelper::get('query');
|
||||||
if (!empty($formQuery))
|
if ($formQuery !== null)
|
||||||
{
|
{
|
||||||
$url = \Chibi\UrlHelper::route('post', 'list', ['query' => $formQuery]);
|
$this->context->transport->searchQuery = $formQuery;
|
||||||
|
if (strpos($formQuery, '/') !== false)
|
||||||
|
throw new SimpleException('Search query contains invalid characters.');
|
||||||
|
$url = \Chibi\UrlHelper::route('post', 'list', ['query' => urlencode($formQuery)]);
|
||||||
\Chibi\UrlHelper::forward($url);
|
\Chibi\UrlHelper::forward($url);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$query = urldecode($query);
|
$query = trim(urldecode($query));
|
||||||
$page = intval($page);
|
$page = intval($page);
|
||||||
$postsPerPage = intval($this->config->browsing->postsPerPage);
|
$postsPerPage = intval($this->config->browsing->postsPerPage);
|
||||||
$this->context->subTitle = 'browsing posts';
|
$this->context->subTitle = 'browsing posts';
|
||||||
|
@ -112,7 +115,7 @@ class PostController
|
||||||
if (!PrivilegesHelper::confirm($this->context->user, Privilege::ListPosts, 'hidden'))
|
if (!PrivilegesHelper::confirm($this->context->user, Privilege::ListPosts, 'hidden'))
|
||||||
$dbQuery->andNot('hidden');
|
$dbQuery->andNot('hidden');
|
||||||
|
|
||||||
$tokens = array_filter(array_unique(explode(' ', $query)));
|
$tokens = array_filter(array_unique(explode(' ', $query)), function($x) { return $x != ''; });
|
||||||
if (count($tokens) > $this->config->browsing->maxSearchTokens)
|
if (count($tokens) > $this->config->browsing->maxSearchTokens)
|
||||||
throw new SimpleException('Too many search tokens (maximum: ' . $this->config->browsing->maxSearchTokens . ')');
|
throw new SimpleException('Too many search tokens (maximum: ' . $this->config->browsing->maxSearchTokens . ')');
|
||||||
foreach ($tokens as $token)
|
foreach ($tokens as $token)
|
||||||
|
@ -258,6 +261,7 @@ class PostController
|
||||||
$buildDbQuery($countDbQuery, $query);
|
$buildDbQuery($countDbQuery, $query);
|
||||||
$postCount = intval($countDbQuery->get('row')['count']);
|
$postCount = intval($countDbQuery->get('row')['count']);
|
||||||
$pageCount = ceil($postCount / $postsPerPage);
|
$pageCount = ceil($postCount / $postsPerPage);
|
||||||
|
$page = max(1, min($pageCount, $page));
|
||||||
|
|
||||||
$searchDbQuery = R::$f->begin();
|
$searchDbQuery = R::$f->begin();
|
||||||
$searchDbQuery->select('*');
|
$searchDbQuery->select('*');
|
||||||
|
@ -345,9 +349,9 @@ class PostController
|
||||||
|
|
||||||
|
|
||||||
/* tags */
|
/* tags */
|
||||||
$suppliedTags = InputHelper::get('tags');
|
$suppliedTags = trim(InputHelper::get('tags'));
|
||||||
$suppliedTags = preg_split('/[,;\s+]/', $suppliedTags);
|
$suppliedTags = preg_split('/[,;\s]+/', $suppliedTags);
|
||||||
$suppliedTags = array_filter($suppliedTags);
|
$suppliedTags = array_filter($suppliedTags, function($x) { return $x != ''; });
|
||||||
$suppliedTags = array_unique($suppliedTags);
|
$suppliedTags = array_unique($suppliedTags);
|
||||||
foreach ($suppliedTags as $tag)
|
foreach ($suppliedTags as $tag)
|
||||||
if (!preg_match('/^[a-zA-Z0-9_-]+$/i', $tag))
|
if (!preg_match('/^[a-zA-Z0-9_-]+$/i', $tag))
|
||||||
|
@ -420,7 +424,7 @@ class PostController
|
||||||
|
|
||||||
|
|
||||||
/* tags */
|
/* tags */
|
||||||
$suppliedTags = InputHelper::get('tags');
|
$suppliedTags = trim(InputHelper::get('tags'));
|
||||||
if ($suppliedTags !== null)
|
if ($suppliedTags !== null)
|
||||||
{
|
{
|
||||||
PrivilegesHelper::confirmWithException($this->context->user, Privilege::EditPostTags, $secondary);
|
PrivilegesHelper::confirmWithException($this->context->user, Privilege::EditPostTags, $secondary);
|
||||||
|
@ -428,8 +432,8 @@ class PostController
|
||||||
if (InputHelper::get('tags-token') != $currentToken)
|
if (InputHelper::get('tags-token') != $currentToken)
|
||||||
throw new SimpleException('Someone else has changed the tags in the meantime');
|
throw new SimpleException('Someone else has changed the tags in the meantime');
|
||||||
|
|
||||||
$suppliedTags = preg_split('/[,;\s+]/', $suppliedTags);
|
$suppliedTags = preg_split('/[,;\s]+/', $suppliedTags);
|
||||||
$suppliedTags = array_filter($suppliedTags);
|
$suppliedTags = array_filter($suppliedTags, function($x) { return $x != ''; });
|
||||||
$suppliedTags = array_unique($suppliedTags);
|
$suppliedTags = array_unique($suppliedTags);
|
||||||
foreach ($suppliedTags as $tag)
|
foreach ($suppliedTags as $tag)
|
||||||
if (!preg_match('/^[a-zA-Z0-9_-]+$/i', $tag))
|
if (!preg_match('/^[a-zA-Z0-9_-]+$/i', $tag))
|
||||||
|
|
|
@ -22,7 +22,7 @@ if (!function_exists('pageUrl'))
|
||||||
$page = min($context->transport->pageCount, $page);
|
$page = min($context->transport->pageCount, $page);
|
||||||
$params = [];
|
$params = [];
|
||||||
$params['page'] = $page;
|
$params['page'] = $page;
|
||||||
if (!empty($context->transport->searchQuery))
|
if ($context->transport->searchQuery != '')
|
||||||
{
|
{
|
||||||
$params['query'] = $context->transport->searchQuery;
|
$params['query'] = $context->transport->searchQuery;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue