From ffeefd06c67d502576de47b169fc6cc0863bee9b Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Fri, 2 May 2014 13:49:31 +0200 Subject: [PATCH] Moved post listing to API --- public_html/dispatch.php | 42 +++++++++++----------- src/Controllers/PostController.php | 58 +++++++++++++++--------------- src/Controllers/TagController.php | 2 +- src/CustomMarkdown.php | 4 +-- src/Jobs/Abstraction/JobArgs.php | 1 + src/Jobs/ListCommentsJob.php | 2 ++ src/Jobs/ListPostsJob.php | 41 +++++++++++++++++++++ src/Views/index-index.phtml | 2 +- src/Views/post-list-wrapper.phtml | 10 +++--- src/Views/post-upload.phtml | 2 +- src/Views/post-view.phtml | 6 ++-- src/Views/tag-list.phtml | 2 +- src/Views/top-navigation.phtml | 4 +-- 13 files changed, 110 insertions(+), 66 deletions(-) create mode 100644 src/Jobs/ListPostsJob.php diff --git a/public_html/dispatch.php b/public_html/dispatch.php index 18b24d3f..5702c688 100644 --- a/public_html/dispatch.php +++ b/public_html/dispatch.php @@ -45,6 +45,28 @@ $context->simpleActionName = null; $context->simpleActionName); }); +$postValidation = +[ + 'tag' => '[^\/]*', + 'enable' => '0|1', + 'source' => 'posts|mass-tag', + 'query' => '[^\/]*', + 'additionalInfo' => '[^\/]*', + 'score' => '-1|0|1', +]; + +\Chibi\Router::register(['PostController', 'listView'], 'GET', '/{source}', $postValidation); +\Chibi\Router::register(['PostController', 'listView'], 'GET', '/{source}/{query}', $postValidation); +\Chibi\Router::register(['PostController', 'listView'], 'GET', '/{source}/{query}/{page}', $postValidation); +\Chibi\Router::register(['PostController', 'listView'], 'GET', '/{source}/{additionalInfo}/{query}/{page}', $postValidation); + +\Chibi\Router::register(['PostController', 'randomView'], 'GET', '/random', $postValidation); +\Chibi\Router::register(['PostController', 'randomView'], 'GET', '/random/{page}', $postValidation); +\Chibi\Router::register(['PostController', 'favoritesView'], 'GET', '/favorites', $postValidation); +\Chibi\Router::register(['PostController', 'favoritesView'], 'GET', '/favorites/{page}', $postValidation); +\Chibi\Router::register(['PostController', 'upvotedView'], 'GET', '/upvoted', $postValidation); +\Chibi\Router::register(['PostController', 'upvotedView'], 'GET', '/upvoted/{page}', $postValidation); + \Chibi\Router::register(['CommentController', 'listView'], 'GET', '/comments'); \Chibi\Router::register(['CommentController', 'listView'], 'GET', '/comments/{page}', ['page' => '\d+']); \Chibi\Router::register(['CommentController', 'addAction'], 'POST', '/comment/add'); @@ -67,28 +89,8 @@ foreach (['GET', 'POST'] as $method) \Chibi\Router::register(['AuthController', 'loginAction'], 'POST', '/auth/login'); \Chibi\Router::register(['AuthController', 'logoutAction'], 'POST', '/auth/logout'); - $postValidation = - [ - 'tag' => '[^\/]*', - 'enable' => '0|1', - 'source' => 'posts|mass-tag', - 'query' => '[^\/]*', - 'additionalInfo' => '[^\/]*', - 'score' => '-1|0|1', - ]; - \Chibi\Router::register(['PostController', 'uploadAction'], $method, '/posts/upload', $postValidation); - \Chibi\Router::register(['PostController', 'listAction'], $method, '/{source}', $postValidation); - \Chibi\Router::register(['PostController', 'listAction'], $method, '/{source}/{query}', $postValidation); - \Chibi\Router::register(['PostController', 'listAction'], $method, '/{source}/{query}/{page}', $postValidation); - \Chibi\Router::register(['PostController', 'listAction'], $method, '/{source}/{additionalInfo}/{query}/{page}', $postValidation); \Chibi\Router::register(['PostController', 'toggleTagAction'], $method, '/post/{id}/toggle-tag/{tag}/{enable}', $postValidation); - \Chibi\Router::register(['PostController', 'favoritesAction'], $method, '/favorites', $postValidation); - \Chibi\Router::register(['PostController', 'favoritesAction'], $method, '/favorites/{page}', $postValidation); - \Chibi\Router::register(['PostController', 'upvotedAction'], $method, '/upvoted', $postValidation); - \Chibi\Router::register(['PostController', 'upvotedAction'], $method, '/upvoted/{page}', $postValidation); - \Chibi\Router::register(['PostController', 'randomAction'], $method, '/random', $postValidation); - \Chibi\Router::register(['PostController', 'randomAction'], $method, '/random/{page}', $postValidation); \Chibi\Router::register(['PostController', 'viewAction'], $method, '/post/{id}', $postValidation); \Chibi\Router::register(['PostController', 'retrieveAction'], $method, '/post/{name}/retrieve', $postValidation); \Chibi\Router::register(['PostController', 'thumbAction'], $method, '/post/{name}/thumb', $postValidation); diff --git a/src/Controllers/PostController.php b/src/Controllers/PostController.php index 2021e372..661a1d6c 100644 --- a/src/Controllers/PostController.php +++ b/src/Controllers/PostController.php @@ -1,7 +1,7 @@ viewName = 'post-list-wrapper'; @@ -18,7 +18,7 @@ class PostController if (strpos($formQuery, '/') !== false) throw new SimpleException('Search query contains invalid characters'); - $url = \Chibi\Router::linkTo(['PostController', 'listAction'], [ + $url = \Chibi\Router::linkTo(['PostController', 'listView'], [ 'source' => $source, 'additionalInfo' => $additionalInfo, 'query' => $formQuery]); @@ -27,11 +27,8 @@ class PostController } $query = trim($query); - $page = max(1, intval($page)); - $postsPerPage = intval(getConfig()->browsing->postsPerPage); $context->transport->searchQuery = $query; $context->transport->lastSearchQuery = $query; - Access::assert(Privilege::ListPosts); if ($source == 'mass-tag') { Access::assert(Privilege::MassTag); @@ -42,18 +39,34 @@ class PostController $query = trim($query . ' submit:' . Auth::getCurrentUser()->name); } - $posts = PostSearchService::getEntities($query, $postsPerPage, $page); - $postCount = PostSearchService::getEntityCount($query); - $pageCount = ceil($postCount / $postsPerPage); - $page = min($pageCount, $page); - PostModel::preloadTags($posts); + $ret = Api::run( + new ListPostsJob(), + [ + JobArgs::PAGE_NUMBER => $page, + JobArgs::QUERY => $query + ]); + $context->transport->posts = $ret->posts; $context->transport->paginator = new StdClass; - $context->transport->paginator->page = $page; - $context->transport->paginator->pageCount = $pageCount; - $context->transport->paginator->entityCount = $postCount; - $context->transport->paginator->entities = $posts; - $context->transport->posts = $posts; + $context->transport->paginator->page = $ret->page; + $context->transport->paginator->pageCount = $ret->pageCount; + $context->transport->paginator->entityCount = $ret->postCount; + $context->transport->paginator->entities = $ret->posts; + } + + public function favoritesView($page = 1) + { + $this->listView('favmin:1', $page); + } + + public function upvotedView($page = 1) + { + $this->listView('scoremin:1', $page); + } + + public function randomView($page = 1) + { + $this->listView('order:random', $page); } public function toggleTagAction($id, $tag, $enable) @@ -103,21 +116,6 @@ class PostController PostModel::save($post); } - public function favoritesAction($page = 1) - { - $this->listAction('favmin:1', $page); - } - - public function upvotedAction($page = 1) - { - $this->listAction('scoremin:1', $page); - } - - public function randomAction($page = 1) - { - $this->listAction('order:random', $page); - } - public function uploadAction() { $context = getContext(); diff --git a/src/Controllers/TagController.php b/src/Controllers/TagController.php index f8ff5cc2..368a5f3e 100644 --- a/src/Controllers/TagController.php +++ b/src/Controllers/TagController.php @@ -154,6 +154,6 @@ class TagController ]; if ($suppliedOldPage != 0 and $suppliedOldQuery == $suppliedQuery) $params['page'] = $suppliedOldPage; - \Chibi\Util\Url::forward(\Chibi\Router::linkTo(['PostController', 'listAction'], $params)); + \Chibi\Util\Url::forward(\Chibi\Router::linkTo(['PostController', 'listView'], $params)); } } diff --git a/src/CustomMarkdown.php b/src/CustomMarkdown.php index 2cc80f6c..cda304be 100644 --- a/src/CustomMarkdown.php +++ b/src/CustomMarkdown.php @@ -136,7 +136,7 @@ class CustomMarkdown extends \Michelf\MarkdownExtra protected function doTags($text) { - $link = \Chibi\Router::linkTo(['PostController', 'listAction'], ['query' => '_query_']); + $link = \Chibi\Router::linkTo(['PostController', 'listView'], ['query' => '_query_']); return preg_replace_callback('/(?:(?hashPart('' . $x[0] . ''); @@ -154,7 +154,7 @@ class CustomMarkdown extends \Michelf\MarkdownExtra protected function doSearchPermalinks($text) { - $link = \Chibi\Router::linkTo(['PostController', 'listAction'], ['query' => '_query_']); + $link = \Chibi\Router::linkTo(['PostController', 'listView'], ['query' => '_query_']); return preg_replace_callback('{\[search\]((?:[^\[]|\[(?!\/?search\]))+)\[\/search\]}is', function($x) use ($link) { return $this->hashPart('' . $x[1] . ''); diff --git a/src/Jobs/Abstraction/JobArgs.php b/src/Jobs/Abstraction/JobArgs.php index 0c33f3b6..80d849ce 100644 --- a/src/Jobs/Abstraction/JobArgs.php +++ b/src/Jobs/Abstraction/JobArgs.php @@ -5,4 +5,5 @@ class JobArgs const POST_ID = 'post-id'; const TEXT = 'text'; const PAGE_NUMBER = 'page-number'; + const QUERY = 'query'; } diff --git a/src/Jobs/ListCommentsJob.php b/src/Jobs/ListCommentsJob.php index 4a2e69d8..cda2bfb9 100644 --- a/src/Jobs/ListCommentsJob.php +++ b/src/Jobs/ListCommentsJob.php @@ -12,6 +12,8 @@ class ListCommentsJob extends AbstractJob $posts = PostSearchService::getEntities($searchQuery, $commentsPerPage, $page); $postCount = PostSearchService::getEntityCount($searchQuery); $pageCount = ceil($postCount / $commentsPerPage); + $page = min($pageCount, $page); + PostModel::preloadTags($posts); PostModel::preloadComments($posts); $comments = []; diff --git a/src/Jobs/ListPostsJob.php b/src/Jobs/ListPostsJob.php new file mode 100644 index 00000000..34928d39 --- /dev/null +++ b/src/Jobs/ListPostsJob.php @@ -0,0 +1,41 @@ +getArgument(JobArgs::PAGE_NUMBER); + $query = $this->getArgument(JobArgs::QUERY); + + $page = max(1, intval($page)); + $postsPerPage = intval(getConfig()->browsing->postsPerPage); + + $posts = PostSearchService::getEntities($query, $postsPerPage, $page); + $postCount = PostSearchService::getEntityCount($query); + $pageCount = ceil($postCount / $postsPerPage); + $page = min($pageCount, $page); + + PostModel::preloadTags($posts); + + $ret = new StdClass; + $ret->posts = $posts; + $ret->postCount = $postCount; + $ret->page = $page; + $ret->pageCount = $pageCount; + return $ret; + } + + public function requiresPrivilege() + { + return Privilege::ListPosts; + } + + public function requiresAuthentication() + { + return false; + } + + public function requiresConfirmedEmail() + { + return false; + } +} diff --git a/src/Views/index-index.phtml b/src/Views/index-index.phtml index e162bf2a..a1e9a5e1 100644 --- a/src/Views/index-index.phtml +++ b/src/Views/index-index.phtml @@ -29,7 +29,7 @@ Assets::addStylesheet('index-index.css'); name, $b->name); }) ?>
  • - + name ?>
  • diff --git a/src/Views/post-list-wrapper.phtml b/src/Views/post-list-wrapper.phtml index 0b9815a8..ff73c922 100644 --- a/src/Views/post-list-wrapper.phtml +++ b/src/Views/post-list-wrapper.phtml @@ -4,32 +4,32 @@ Assets::setSubTitle('posts'); $tabs = []; $activeTab = 0; if (Access::check(Privilege::ListPosts)) - $tabs []= ['All posts', \Chibi\Router::linkTo(['PostController', 'listAction'])]; + $tabs []= ['All posts', \Chibi\Router::linkTo(['PostController', 'listView'])]; if (Access::check(Privilege::ListPosts)) { - $tabs []= ['Random', \Chibi\Router::linkTo(['PostController', 'randomAction'])]; + $tabs []= ['Random', \Chibi\Router::linkTo(['PostController', 'randomView'])]; if ($this->context->simpleActionName == 'random') $activeTab = count($tabs) - 1; } if (Access::check(Privilege::ListPosts)) { - $tabs []= ['Favorites', \Chibi\Router::linkTo(['PostController', 'favoritesAction'])]; + $tabs []= ['Favorites', \Chibi\Router::linkTo(['PostController', 'favoritesView'])]; if ($this->context->simpleActionName == 'favorites') $activeTab = count($tabs) - 1; } if (Access::check(Privilege::ListPosts)) { - $tabs []= ['Upvoted', \Chibi\Router::linkTo(['PostController', 'upvotedAction'])]; + $tabs []= ['Upvoted', \Chibi\Router::linkTo(['PostController', 'upvotedView'])]; if ($this->context->simpleActionName == 'upvoted') $activeTab = count($tabs) - 1; } if (Access::check(Privilege::MassTag)) { - $tabs []= ['Mass tag', \Chibi\Router::linkTo(['PostController', 'listAction'], [ + $tabs []= ['Mass tag', \Chibi\Router::linkTo(['PostController', 'listView'], [ 'source' => 'mass-tag', 'query' => isset($this->context->transport->searchQuery) ? htmlspecialchars($this->context->transport->searchQuery) diff --git a/src/Views/post-upload.phtml b/src/Views/post-upload.phtml index 0b3b95c2..c33f9685 100644 --- a/src/Views/post-upload.phtml +++ b/src/Views/post-upload.phtml @@ -37,7 +37,7 @@ Assets::addScript('../lib/tagit/jquery.tagit.js');
    -
    +

    diff --git a/src/Views/post-view.phtml b/src/Views/post-view.phtml index d718cec4..910b36f4 100644 --- a/src/Views/post-view.phtml +++ b/src/Views/post-view.phtml @@ -60,7 +60,7 @@ $canEditAnything = count(array_filter($editPostPrivileges)) > 0;
    Current search:
    context->transport->lastSearchQuery ?> @@ -76,7 +76,7 @@ $canEditAnything = count(array_filter($editPostPrivileges)) > 0;
  • name ?> @@ -354,7 +354,7 @@ $canEditAnything = count(array_filter($editPostPrivileges)) > 0; ['PostController', 'deleteAction'], ['id' => $this->context->transport->post->id]), 'data-confirm-text' => 'Are you sure you want to delete this post?', - 'data-redirect-url' => \Chibi\Router::linkTo(['PostController', 'listAction']), + 'data-redirect-url' => \Chibi\Router::linkTo(['PostController', 'listView']), ]; } diff --git a/src/Views/tag-list.phtml b/src/Views/tag-list.phtml index 6dfdea54..a3eba47c 100644 --- a/src/Views/tag-list.phtml +++ b/src/Views/tag-list.phtml @@ -28,7 +28,7 @@ context->highestUsage ?> - '_query_']) ?> + '_query_']) ?>
      context->transport->tags as $tag): ?> diff --git a/src/Views/top-navigation.phtml b/src/Views/top-navigation.phtml index a9070cc2..608555d3 100644 --- a/src/Views/top-navigation.phtml +++ b/src/Views/top-navigation.phtml @@ -22,7 +22,7 @@ { $registerNavItem( 'Browse', - \Chibi\Router::linkTo(['PostController', 'listAction']), + \Chibi\Router::linkTo(['PostController', 'listView']), $activeController == 'post' and $activeAction != 'upload'); } @@ -139,7 +139,7 @@