Improved search error messages

This commit is contained in:
Marcin Kurczewski 2015-05-23 10:46:17 +02:00
parent 5412ac14b9
commit 24d8bf5295
5 changed files with 71 additions and 41 deletions

View file

@ -149,7 +149,7 @@ class PostDao extends AbstractDao implements ICrudDao
return; return;
} }
elseif ($requirement->getType() === PostFilter::REQUIREMENT_COMMENT) elseif ($requirement->getType() === PostFilter::REQUIREMENT_COMMENT_AUTHOR)
{ {
foreach ($requirement->getValue()->getValues() as $userName) foreach ($requirement->getValue()->getValues() as $userName)
{ {

View file

@ -104,7 +104,12 @@ class EnumHelper
$key = trim(strtolower($enumString)); $key = trim(strtolower($enumString));
$lowerEnumMap = array_change_key_case($enumMap, \CASE_LOWER); $lowerEnumMap = array_change_key_case($enumMap, \CASE_LOWER);
if (!isset($lowerEnumMap[$key])) if (!isset($lowerEnumMap[$key]))
throw new \DomainException('Unrecognized value: ' . $enumString); {
throw new \DomainException(sprintf(
'Unrecognized value: %s.' . PHP_EOL . 'Possible values: %s',
$enumString,
join(', ', array_keys($lowerEnumMap))));
}
return $lowerEnumMap[$key]; return $lowerEnumMap[$key];
} }

View file

@ -3,8 +3,8 @@ namespace Szurubooru;
class NotSupportedException extends \BadMethodCallException class NotSupportedException extends \BadMethodCallException
{ {
public function __construct() public function __construct($message = null)
{ {
parent::__construct('Not supported'); parent::__construct($message === null ? 'Not supported' : $message);
} }
} }

View file

@ -25,7 +25,7 @@ class PostFilter extends BasicFilter implements IFilter
const REQUIREMENT_UPLOADER = 'uploader.name'; const REQUIREMENT_UPLOADER = 'uploader.name';
const REQUIREMENT_SAFETY = 'safety'; const REQUIREMENT_SAFETY = 'safety';
const REQUIREMENT_FAVORITE = 'favoritedBy.name'; const REQUIREMENT_FAVORITE = 'favoritedBy.name';
const REQUIREMENT_COMMENT = 'commentedBy.name'; const REQUIREMENT_COMMENT_AUTHOR = 'commentedBy.name';
const REQUIREMENT_TYPE = 'contentType'; const REQUIREMENT_TYPE = 'contentType';
const REQUIREMENT_USER_SCORE = 'userScore'; const REQUIREMENT_USER_SCORE = 'userScore';

View file

@ -43,7 +43,13 @@ class PostSearchParser extends AbstractSearchParser
$tokenKey = $token->getKey(); $tokenKey = $token->getKey();
$tokenValue = $token->getValue(); $tokenValue = $token->getValue();
$countAliases = ['tag_count' => 'tag', 'fav_count' => 'fav', 'score' => 'score']; $countAliases =
[
'tag_count' => 'tags',
'fav_count' => 'favs',
'score' => 'score',
'comment_count' => 'comments',
];
foreach ($countAliases as $realKey => $baseAlias) foreach ($countAliases as $realKey => $baseAlias)
{ {
if ($this->matches($tokenKey, [$baseAlias . '_min', $baseAlias . '_max'])) if ($this->matches($tokenKey, [$baseAlias . '_min', $baseAlias . '_max']))
@ -60,59 +66,49 @@ class PostSearchParser extends AbstractSearchParser
[['id'], [$this, 'addIdRequirement']], [['id'], [$this, 'addIdRequirement']],
[['hash', 'name'], [$this, 'addHashRequirement']], [['hash', 'name'], [$this, 'addHashRequirement']],
[['date', 'time'], [$this, 'addDateRequirement']], [['date', 'time'], [$this, 'addDateRequirement']],
[['tag_count'], [$this, 'addTagCountRequirement']], [['tag_count', 'tags'], [$this, 'addTagCountRequirement']],
[['fav_count'], [$this, 'addFavCountRequirement']], [['fav_count', 'favs'], [$this, 'addFavCountRequirement']],
[['comment_count'], [$this, 'addCommentCountRequirement']], [['comment_count', 'comments'], [$this, 'addCommentCountRequirement']],
[['score'], [$this, 'addScoreRequirement']], [['score'], [$this, 'addScoreRequirement']],
[['uploader', 'submit', 'up'], [$this, 'addUploaderRequirement']], [['uploader', 'uploader', 'uploaded', 'submit', 'submitter', 'submitted'], [$this, 'addUploaderRequirement']],
[['safety', 'rating'], [$this, 'addSafetyRequirement']], [['safety', 'rating'], [$this, 'addSafetyRequirement']],
[['fav'], [$this, 'addFavRequirement']], [['fav'], [$this, 'addFavRequirement']],
[['type'], [$this, 'addTypeRequirement']], [['type'], [$this, 'addTypeRequirement']],
[['comment'], [$this, 'addCommentRequirement']], [['comment', 'comment_author', 'commented'], [$this, 'addCommentAuthorRequirement']],
]; ];
foreach ($map as $item) foreach ($map as $item)
{ {
list ($aliases, $callback) = $item; list ($aliases, $callback) = $item;
if ($this->matches($tokenKey, $aliases)) if ($this->matches($tokenKey, $aliases))
{
return $callback($filter, $token); return $callback($filter, $token);
} }
}
if ($this->matches($tokenKey, ['special'])) if ($this->matches($tokenKey, ['special']))
{ {
if ($this->matches($tokenValue, ['liked'])) $specialMap =
[
[['liked'], [$this, 'addOwnLikedRequirement']],
[['disliked'], [$this, 'addOwnDislikedRequirement']],
[['fav'], [$this, 'addOwnFavRequirement']],
];
foreach ($specialMap as $item)
{ {
$this->privilegeService->assertLoggedIn(); list ($aliases, $callback) = $item;
return $this->addUserScoreRequirement( if ($this->matches($token->getValue(), $aliases))
$filter, return $callback($filter, $token);
$this->authService->getLoggedInUser()->getName(),
1,
$token->isNegated());
} }
if ($this->matches($tokenValue, ['disliked'])) throw new NotSupportedException(
{ 'Unknown value for special search term: ' . $token->getValue()
$this->privilegeService->assertLoggedIn(); . '. Possible search terms: '
return $this->addUserScoreRequirement( . join(', ', array_map(function($term) { return join('/', $term[0]); }, $specialMap)));
$filter,
$this->authService->getLoggedInUser()->getName(),
-1,
$token->isNegated());
} }
if ($this->matches($tokenValue, ['fav'])) throw new NotSupportedException('Unknown search term: ' . $token->getKey()
{ . '. Possible search terms: special, '
$this->privilegeService->assertLoggedIn(); . join(', ', array_map(function($term) { return join('/', $term[0]); }, $map)));
$token = new NamedSearchToken();
$token->setKey('fav');
$token->setValue($this->authService->getLoggedInUser()->getName());
return $this->decorateFilterFromNamedToken($filter, $token);
}
}
throw new NotSupportedException();
} }
protected function getOrderColumn($tokenText) protected function getOrderColumn($tokenText)
@ -153,6 +149,35 @@ class PostSearchParser extends AbstractSearchParser
throw new NotSupportedException(); throw new NotSupportedException();
} }
private function addOwnLikedRequirement($filter, $token)
{
$this->privilegeService->assertLoggedIn();
$this->addUserScoreRequirement(
$filter,
$this->authService->getLoggedInUser()->getName(),
1,
$token->isNegated());
}
private function addOwnDislikedRequirement($filter, $token)
{
$this->privilegeService->assertLoggedIn();
$this->addUserScoreRequirement(
$filter,
$this->authService->getLoggedInUser()->getName(),
-1,
$token->isNegated());
}
private function addOwnFavRequirement($filter, $token)
{
$this->privilegeService->assertLoggedIn();
$token = new NamedSearchToken();
$token->setKey('fav');
$token->setValue($this->authService->getLoggedInUser()->getName());
$this->decorateFilterFromNamedToken($filter, $token);
}
private function addIdRequirement($filter, $token) private function addIdRequirement($filter, $token)
{ {
$this->addRequirementFromToken( $this->addRequirementFromToken(
@ -280,12 +305,12 @@ class PostSearchParser extends AbstractSearchParser
}); });
} }
private function addCommentRequirement($filter, $token) private function addCommentAuthorRequirement($filter, $token)
{ {
$this->addRequirementFromToken( $this->addRequirementFromToken(
$filter, $filter,
$token, $token,
PostFilter::REQUIREMENT_COMMENT, PostFilter::REQUIREMENT_COMMENT_AUTHOR,
self::ALLOW_COMPOSITE); self::ALLOW_COMPOSITE);
} }