From 5db571ab7fc26682987b86f060c869452c0c092b Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Fri, 3 Oct 2014 14:39:27 +0200 Subject: [PATCH] Added special:(dis)liked support to post searching --- TODO | 2 -- src/Dao/PostDao.php | 20 +++++++++++++++- src/SearchServices/Filters/PostFilter.php | 3 ++- .../Parsers/AbstractSearchParser.php | 2 +- .../Parsers/PostSearchParser.php | 24 +++++++++++++++++++ 5 files changed, 46 insertions(+), 5 deletions(-) diff --git a/TODO b/TODO index 88872da0..5a15cf4b 100644 --- a/TODO +++ b/TODO @@ -12,8 +12,6 @@ everything related to posts: - comment_count: 3..5 - file_size:3K..5M - image_size:huge/large/medium/small - - special:liked - - special:disliked - special:fav - search order - order:comment_count diff --git a/src/Dao/PostDao.php b/src/Dao/PostDao.php index 83be8011..28830ead 100644 --- a/src/Dao/PostDao.php +++ b/src/Dao/PostDao.php @@ -112,7 +112,8 @@ class PostDao extends AbstractDao implements ICrudDao elseif ($requirement->getType() === \Szurubooru\SearchServices\Filters\PostFilter::REQUIREMENT_FAVORITE) { - $query->innerJoin('favorites _fav ON _fav.postId = posts.id INNER JOIN users favorite ON favorite.id = _fav.userId'); + $query->innerJoin('favorites _fav ON _fav.postId = posts.id'); + $query->innerJoin('users favoritedBy ON favoritedBy.id = _fav.userId'); } elseif ($requirement->getType() === \Szurubooru\SearchServices\Filters\PostFilter::REQUIREMENT_UPLOADER) @@ -120,6 +121,23 @@ class PostDao extends AbstractDao implements ICrudDao $query->innerJoin('users uploader ON uploader.id = userId'); } + elseif ($requirement->getType() === \Szurubooru\SearchServices\Filters\PostFilter::REQUIREMENT_USER_SCORE) + { + $values = $requirement->getValue()->getValues(); + $userName = $values[0]; + $score = $values[1]; + $sql = 'EXISTS ( + SELECT 1 FROM postScores + INNER JOIN users ON postScores.userId = users.id + WHERE postScores.postId = posts.id + AND LOWER(users.name) = LOWER(?) + AND postScores.score = ?)'; + if ($requirement->isnegated()) + $sql = 'NOT ' . $sql; + $query->where($sql, $userName, $score); + return; + } + parent::decorateQueryFromRequirement($query, $requirement); } diff --git a/src/SearchServices/Filters/PostFilter.php b/src/SearchServices/Filters/PostFilter.php index f5b551ed..6d7a50bd 100644 --- a/src/SearchServices/Filters/PostFilter.php +++ b/src/SearchServices/Filters/PostFilter.php @@ -19,8 +19,9 @@ class PostFilter extends BasicFilter implements IFilter const REQUIREMENT_SCORE = 'score'; const REQUIREMENT_UPLOADER = 'uploader.name'; const REQUIREMENT_SAFETY = 'safety'; - const REQUIREMENT_FAVORITE = 'favorite.name'; + const REQUIREMENT_FAVORITE = 'favoritedBy.name'; const REQUIREMENT_TYPE = 'contentType'; + const REQUIREMENT_USER_SCORE = 'userScore'; public function __construct() { diff --git a/src/SearchServices/Parsers/AbstractSearchParser.php b/src/SearchServices/Parsers/AbstractSearchParser.php index f7b30b3b..d13c4549 100644 --- a/src/SearchServices/Parsers/AbstractSearchParser.php +++ b/src/SearchServices/Parsers/AbstractSearchParser.php @@ -81,7 +81,7 @@ abstract class AbstractSearchParser protected function addRequirementFromToken($filter, $token, $type, $flags, $valueDecorator = null) { $requirement = new \Szurubooru\SearchServices\Requirements\Requirement(); - $requirement->setType($type, $flags); + $requirement->setType($type); $requirement->setValue($this->createRequirementValue($token->getValue(), $flags, $valueDecorator)); $requirement->setNegated($token->isNegated()); $filter->addRequirement($requirement); diff --git a/src/SearchServices/Parsers/PostSearchParser.php b/src/SearchServices/Parsers/PostSearchParser.php index 43975724..0c1ed0e5 100644 --- a/src/SearchServices/Parsers/PostSearchParser.php +++ b/src/SearchServices/Parsers/PostSearchParser.php @@ -3,6 +3,13 @@ namespace Szurubooru\SearchServices\Parsers; class PostSearchParser extends AbstractSearchParser { + private $authService; + + public function __construct(\Szurubooru\Services\AuthService $authService) + { + $this->authService = $authService; + } + protected function createFilter() { return new \Szurubooru\SearchServices\Filters\PostFilter; @@ -49,6 +56,12 @@ class PostSearchParser extends AbstractSearchParser elseif ($token->getKey() === 'type') $this->addTypeRequirement($filter, $token); + elseif ($token->getKey() === 'special' and $token->getValue() === 'liked' and $this->authService->isLoggedIn()) + $this->addUserScoreRequirement($filter, $this->authService->getLoggedInUser()->getName(), 1, $token->isNegated()); + + elseif ($token->getKey() === 'special' and $token->getValue() === 'disliked' and $this->authService->isLoggedIn()) + $this->addUserScoreRequirement($filter, $this->authService->getLoggedInUser()->getName(), -1, $token->isNegated()); + else throw new \BadMethodCallException('Not supported'); } @@ -194,6 +207,17 @@ class PostSearchParser extends AbstractSearchParser }); } + private function addUserScoreRequirement($filter, $userName, $score, $isNegated) + { + $tokenValue = new \Szurubooru\SearchServices\Requirements\RequirementCompositeValue(); + $tokenValue->setValues([$userName, $score]); + $requirement = new \Szurubooru\SearchServices\Requirements\Requirement(); + $requirement->setType(\Szurubooru\SearchServices\Filters\PostFilter::REQUIREMENT_USER_SCORE); + $requirement->setValue($tokenValue); + $requirement->setNegated($isNegated); + $filter->addRequirement($requirement); + } + private function dateToTime($value) { $value = strtolower(trim($value));