diff --git a/public_html/templates/help.tpl b/public_html/templates/help.tpl index e00d6720..173c3c53 100644 --- a/public_html/templates/help.tpl +++ b/public_html/templates/help.tpl @@ -114,6 +114,7 @@ {search: 'comment_count:3', description: 'having exactly three comments'}, {search: 'score:4', description: 'having score of 4'}, {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:yesterday', description: 'posted yesterday'}, {search: 'date:2000', description: 'posted in year 2000'}, diff --git a/src/Search/Filters/PostFilter.php b/src/Search/Filters/PostFilter.php index a90491f7..52d9b626 100644 --- a/src/Search/Filters/PostFilter.php +++ b/src/Search/Filters/PostFilter.php @@ -7,6 +7,7 @@ class PostFilter extends BasicFilter implements IFilter const ORDER_FAV_COUNT = 'favCount'; const ORDER_TAG_COUNT = 'tagCount'; const ORDER_COMMENT_COUNT = 'commentCount'; + const ORDER_NOTE_COUNT = 'noteCount'; const ORDER_SCORE = 'score'; const ORDER_LAST_EDIT_TIME = 'lastEditTime'; const ORDER_FILE_SIZE = 'originalFileSize'; @@ -21,6 +22,7 @@ class PostFilter extends BasicFilter implements IFilter const REQUIREMENT_TAG_COUNT = 'tagCount'; const REQUIREMENT_FAV_COUNT = 'favCount'; const REQUIREMENT_COMMENT_COUNT = 'commentCount'; + const REQUIREMENT_NOTE_COUNT = 'noteCount'; const REQUIREMENT_SCORE = 'score'; const REQUIREMENT_UPLOADER = 'uploader.name'; const REQUIREMENT_SAFETY = 'safety'; diff --git a/src/Search/Parsers/PostSearchParser.php b/src/Search/Parsers/PostSearchParser.php index a8d33893..95544814 100644 --- a/src/Search/Parsers/PostSearchParser.php +++ b/src/Search/Parsers/PostSearchParser.php @@ -49,6 +49,7 @@ class PostSearchParser extends AbstractSearchParser 'fav_count' => 'favs', 'score' => 'score', 'comment_count' => 'comments', + 'note_count' => 'notes', ]; foreach ($countAliases as $realKey => $baseAlias) { @@ -69,6 +70,7 @@ class PostSearchParser extends AbstractSearchParser [['tag_count', 'tags'], [$this, 'addTagCountRequirement']], [['fav_count', 'favs'], [$this, 'addFavCountRequirement']], [['comment_count', 'comments'], [$this, 'addCommentCountRequirement']], + [['note_count', 'notes'], [$this, 'addNoteCountRequirement']], [['score'], [$this, 'addScoreRequirement']], [['uploader', 'uploader', 'uploaded', 'submit', 'submitter', 'submitted'], [$this, 'addUploaderRequirement']], [['safety', 'rating'], [$this, 'addSafetyRequirement']], @@ -137,6 +139,9 @@ class PostSearchParser extends AbstractSearchParser if ($this->matches($tokenText, ['comment_count', 'comments', 'comment'])) 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'])) return PostFilter::ORDER_LAST_FAV_TIME; @@ -252,6 +257,15 @@ class PostSearchParser extends AbstractSearchParser 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) { $this->addRequirementFromToken( diff --git a/src/Upgrades/Upgrade41.php b/src/Upgrades/Upgrade41.php new file mode 100644 index 00000000..26583679 --- /dev/null +++ b/src/Upgrades/Upgrade41.php @@ -0,0 +1,42 @@ +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)'); + } +} diff --git a/src/di.php b/src/di.php index c6068023..ced90405 100644 --- a/src/di.php +++ b/src/di.php @@ -57,6 +57,7 @@ return [ $container->get(\Szurubooru\Upgrades\Upgrade38::class), $container->get(\Szurubooru\Upgrades\Upgrade39::class), $container->get(\Szurubooru\Upgrades\Upgrade40::class), + $container->get(\Szurubooru\Upgrades\Upgrade41::class), ]; }),