Added support for note count based post searches

This commit is contained in:
rr- 2015-08-04 19:31:57 +02:00
parent 7ca582186b
commit 8c87a93774
5 changed files with 60 additions and 0 deletions

View file

@ -114,6 +114,7 @@
{search: 'comment_count:3', description: 'having exactly three comments'}, {search: 'comment_count:3', description: 'having exactly three comments'},
{search: 'score:4', description: 'having score of 4'}, {search: 'score:4', description: 'having score of 4'},
{search: 'tag_count:7', description: 'tagged with exactly seven tags'}, {search: 'tag_count:7', description: 'tagged with exactly seven tags'},
{search: 'note_count:1..', description: 'having at least one post note'},
{search: 'date:today', description: 'posted today'}, {search: 'date:today', description: 'posted today'},
{search: 'date:yesterday', description: 'posted yesterday'}, {search: 'date:yesterday', description: 'posted yesterday'},
{search: 'date:2000', description: 'posted in year 2000'}, {search: 'date:2000', description: 'posted in year 2000'},

View file

@ -7,6 +7,7 @@ class PostFilter extends BasicFilter implements IFilter
const ORDER_FAV_COUNT = 'favCount'; const ORDER_FAV_COUNT = 'favCount';
const ORDER_TAG_COUNT = 'tagCount'; const ORDER_TAG_COUNT = 'tagCount';
const ORDER_COMMENT_COUNT = 'commentCount'; const ORDER_COMMENT_COUNT = 'commentCount';
const ORDER_NOTE_COUNT = 'noteCount';
const ORDER_SCORE = 'score'; const ORDER_SCORE = 'score';
const ORDER_LAST_EDIT_TIME = 'lastEditTime'; const ORDER_LAST_EDIT_TIME = 'lastEditTime';
const ORDER_FILE_SIZE = 'originalFileSize'; const ORDER_FILE_SIZE = 'originalFileSize';
@ -21,6 +22,7 @@ class PostFilter extends BasicFilter implements IFilter
const REQUIREMENT_TAG_COUNT = 'tagCount'; const REQUIREMENT_TAG_COUNT = 'tagCount';
const REQUIREMENT_FAV_COUNT = 'favCount'; const REQUIREMENT_FAV_COUNT = 'favCount';
const REQUIREMENT_COMMENT_COUNT = 'commentCount'; const REQUIREMENT_COMMENT_COUNT = 'commentCount';
const REQUIREMENT_NOTE_COUNT = 'noteCount';
const REQUIREMENT_SCORE = 'score'; const REQUIREMENT_SCORE = 'score';
const REQUIREMENT_UPLOADER = 'uploader.name'; const REQUIREMENT_UPLOADER = 'uploader.name';
const REQUIREMENT_SAFETY = 'safety'; const REQUIREMENT_SAFETY = 'safety';

View file

@ -49,6 +49,7 @@ class PostSearchParser extends AbstractSearchParser
'fav_count' => 'favs', 'fav_count' => 'favs',
'score' => 'score', 'score' => 'score',
'comment_count' => 'comments', 'comment_count' => 'comments',
'note_count' => 'notes',
]; ];
foreach ($countAliases as $realKey => $baseAlias) foreach ($countAliases as $realKey => $baseAlias)
{ {
@ -69,6 +70,7 @@ class PostSearchParser extends AbstractSearchParser
[['tag_count', 'tags'], [$this, 'addTagCountRequirement']], [['tag_count', 'tags'], [$this, 'addTagCountRequirement']],
[['fav_count', 'favs'], [$this, 'addFavCountRequirement']], [['fav_count', 'favs'], [$this, 'addFavCountRequirement']],
[['comment_count', 'comments'], [$this, 'addCommentCountRequirement']], [['comment_count', 'comments'], [$this, 'addCommentCountRequirement']],
[['note_count', 'notes'], [$this, 'addNoteCountRequirement']],
[['score'], [$this, 'addScoreRequirement']], [['score'], [$this, 'addScoreRequirement']],
[['uploader', 'uploader', 'uploaded', 'submit', 'submitter', 'submitted'], [$this, 'addUploaderRequirement']], [['uploader', 'uploader', 'uploaded', 'submit', 'submitter', 'submitted'], [$this, 'addUploaderRequirement']],
[['safety', 'rating'], [$this, 'addSafetyRequirement']], [['safety', 'rating'], [$this, 'addSafetyRequirement']],
@ -137,6 +139,9 @@ class PostSearchParser extends AbstractSearchParser
if ($this->matches($tokenText, ['comment_count', 'comments', 'comment'])) if ($this->matches($tokenText, ['comment_count', 'comments', 'comment']))
return PostFilter::ORDER_COMMENT_COUNT; return PostFilter::ORDER_COMMENT_COUNT;
if ($this->matches($tokenText, ['note_count', 'notes', 'note']))
return PostFilter::ORDER_NOTE_COUNT;
if ($this->matches($tokenText, ['fav_time', 'fav_date'])) if ($this->matches($tokenText, ['fav_time', 'fav_date']))
return PostFilter::ORDER_LAST_FAV_TIME; return PostFilter::ORDER_LAST_FAV_TIME;
@ -252,6 +257,15 @@ class PostSearchParser extends AbstractSearchParser
self::ALLOW_COMPOSITE | self::ALLOW_RANGES); self::ALLOW_COMPOSITE | self::ALLOW_RANGES);
} }
private function addNoteCountRequirement($filter, $token)
{
$this->addRequirementFromToken(
$filter,
$token,
PostFilter::REQUIREMENT_NOTE_COUNT,
self::ALLOW_COMPOSITE | self::ALLOW_RANGES);
}
private function addScoreRequirement($filter, $token) private function addScoreRequirement($filter, $token)
{ {
$this->addRequirementFromToken( $this->addRequirementFromToken(

View file

@ -0,0 +1,42 @@
<?php
namespace Szurubooru\Upgrades;
use Szurubooru\DatabaseConnection;
class Upgrade41 implements IUpgrade
{
public function run(DatabaseConnection $databaseConnection)
{
$pdo = $databaseConnection->getPDO();
$driver = $databaseConnection->getDriver();
$pdo->exec('
CREATE TRIGGER postNotesDelete AFTER DELETE ON postNotes
FOR EACH ROW
BEGIN
UPDATE posts SET
noteCount = (SELECT COUNT(1) FROM postNotes WHERE postNotes.postId = posts.id)
WHERE posts.id = OLD.postId;
END');
$pdo->exec('
CREATE TRIGGER postNotesInsert AFTER INSERT ON postNotes
FOR EACH ROW
BEGIN
UPDATE posts SET
noteCount = (SELECT COUNT(1) FROM postNotes WHERE postNotes.postId = posts.id)
WHERE posts.id = NEW.postId;
END');
$pdo->exec('
CREATE TRIGGER postNotesUpdate AFTER UPDATE ON postNotes
FOR EACH ROW
BEGIN
UPDATE posts SET
noteCount = (SELECT COUNT(1) FROM postNotes WHERE postNotes.postId = posts.id)
WHERE posts.id IN (OLD.postId, NEW.postId);
END');
$pdo->exec('ALTER TABLE posts ADD COLUMN noteCount INTEGER NOT NULL DEFAULT 0');
$pdo->exec('UPDATE posts SET noteCount = (SELECT COUNT(1) FROM postNotes WHERE postNotes.postId = posts.id)');
}
}

View file

@ -57,6 +57,7 @@ return [
$container->get(\Szurubooru\Upgrades\Upgrade38::class), $container->get(\Szurubooru\Upgrades\Upgrade38::class),
$container->get(\Szurubooru\Upgrades\Upgrade39::class), $container->get(\Szurubooru\Upgrades\Upgrade39::class),
$container->get(\Szurubooru\Upgrades\Upgrade40::class), $container->get(\Szurubooru\Upgrades\Upgrade40::class),
$container->get(\Szurubooru\Upgrades\Upgrade41::class),
]; ];
}), }),