From 174fd80a735782a34244d2920e77db53c2bf1557 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Tue, 20 May 2014 21:17:39 +0200 Subject: [PATCH] Added name: keyword in post search --- data/help.md | 3 ++- src/Models/SearchParsers/PostSearchParser.php | 6 +++++ tests/Tests/JobTests/ListPostsJobTest.php | 22 +++++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/data/help.md b/data/help.md index c0ff3a65..03aa584c 100644 --- a/data/help.md +++ b/data/help.md @@ -37,10 +37,11 @@ Command | Description [search]datemin:...[/search] | posted on `...` or later (format like in `date:`) | `date_min` | [search]datemax:...[/search] | posted on `...` or earlier (format like in `date:`) | `date_max` | [search]id:1,2,3[/search] | having specific post ID | `ids` | +[search]name:...[/search] | having specific post name (hash in full URLs) | `names`, `hash`, `hashes` | [search]idmin:5[/search] | posts with ID greater than or equal to @5 | `id_min` | [search]idmax:5[/search] | posts with ID less than or equal to @5 | `id_max` | [search]type:img[/search] | only image posts | `type:image` | -[search]type:flash[/search] | only Flash posts | `type:swf` | +[search]type:flash[/search] | only Flash posts | `type:swf` | [search]type:yt[/search] | only Youtube posts | `type:youtube` | [search]special:liked[/search] | posts liked by currently logged in user | `special:likes`, `special:like` | [search]special:disliked[/search] | posts disliked by currently logged in user | `special:dislikes`, `special:dislike` | diff --git a/src/Models/SearchParsers/PostSearchParser.php b/src/Models/SearchParsers/PostSearchParser.php index bd86ce4b..25f17e0e 100644 --- a/src/Models/SearchParsers/PostSearchParser.php +++ b/src/Models/SearchParsers/PostSearchParser.php @@ -75,6 +75,12 @@ class PostSearchParser extends AbstractSearchParser return Sql\InFunctor::fromArray('post.id', Sql\Binding::fromArray($ids)); } + if (in_array($key, ['name', 'names', 'hash', 'hashes'])) + { + $ids = preg_split('/[;,]/', $value); + return Sql\InFunctor::fromArray('post.name', Sql\Binding::fromArray($ids)); + } + elseif (in_array($key, ['fav', 'favs', 'favd'])) { $user = UserModel::getByName($value); diff --git a/tests/Tests/JobTests/ListPostsJobTest.php b/tests/Tests/JobTests/ListPostsJobTest.php index d76d3d45..ccc6f732 100644 --- a/tests/Tests/JobTests/ListPostsJobTest.php +++ b/tests/Tests/JobTests/ListPostsJobTest.php @@ -218,6 +218,28 @@ class ListPostsJobTest extends AbstractTest } } + public function testNames() + { + $this->grantAccess('listPosts'); + $posts = $this->postMocker->mockMultiple(3); + + foreach (['hash', 'hashes', 'name', 'names'] as $alias) + { + $ret = $this->assert->doesNotThrow(function() use ($alias, $posts) + { + $query = sprintf('%s:9999,%s,%s', + $alias, + $posts[0]->getName(), + $posts[2]->getName()); + return Api::run(new ListPostsJob(), [JobArgs::ARG_QUERY => $query]); + }); + + $this->assert->areEqual(2, $ret->entityCount); + $this->assert->areEqual($posts[2]->getId(), $ret->entities[0]->getId()); + $this->assert->areEqual($posts[0]->getId(), $ret->entities[1]->getId()); + } + } + public function testFavs() { $this->grantAccess('listPosts');