From 97c17c68a0567408e7c3d609202377cfdfa6f864 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Sun, 4 May 2014 08:42:18 +0200 Subject: [PATCH] Moved tag listing to API --- public_html/dispatch.php | 20 +++++++------- src/Api/Jobs/ListTagsJob.php | 39 ++++++++++++++++++++++++++ src/Controllers/TagController.php | 46 +++++++++++-------------------- src/Views/tag-list-wrapper.phtml | 2 +- src/Views/tag-list.phtml | 2 +- src/Views/top-navigation.phtml | 2 +- 6 files changed, 68 insertions(+), 43 deletions(-) create mode 100644 src/Api/Jobs/ListTagsJob.php diff --git a/public_html/dispatch.php b/public_html/dispatch.php index ca1aeeb3..0b7710b3 100644 --- a/public_html/dispatch.php +++ b/public_html/dispatch.php @@ -116,18 +116,18 @@ $commentValidation = \Chibi\Router::register(['CommentController', 'editView'], 'GET', '/comment/{id}/edit', $commentValidation); \Chibi\Router::register(['CommentController', 'editAction'], 'POST', '/comment/{id}/edit', $commentValidation); +$tagValidation = +[ + 'page' => '\d*', + 'filter' => '[^\/]+', +]; + +\Chibi\Router::register(['TagController', 'listView'], 'GET', '/tags', $tagValidation); +\Chibi\Router::register(['TagController', 'listView'], 'GET', '/tags/{page}', $tagValidation); +\Chibi\Router::register(['TagController', 'listView'], 'GET', '/tags/{filter}/{page}', $tagValidation); + foreach (['GET', 'POST'] as $method) { - $tagValidation = - [ - 'page' => '\d*', - 'filter' => '[^\/]+', - ]; - - \Chibi\Router::register(['TagController', 'listAction'], $method, '/tags', $tagValidation); - \Chibi\Router::register(['TagController', 'listAction'], $method, '/tags/{filter}', $tagValidation); - \Chibi\Router::register(['TagController', 'listAction'], $method, '/tags/{page}', $tagValidation); - \Chibi\Router::register(['TagController', 'listAction'], $method, '/tags/{filter}/{page}', $tagValidation); \Chibi\Router::register(['TagController', 'autoCompleteAction'], $method, '/tags-autocomplete', $tagValidation); \Chibi\Router::register(['TagController', 'relatedAction'], $method, '/tags-related', $tagValidation); \Chibi\Router::register(['TagController', 'mergeAction'], $method, '/tags-merge', $tagValidation); diff --git a/src/Api/Jobs/ListTagsJob.php b/src/Api/Jobs/ListTagsJob.php new file mode 100644 index 00000000..2ebf6b83 --- /dev/null +++ b/src/Api/Jobs/ListTagsJob.php @@ -0,0 +1,39 @@ +getArgument(self::PAGE_NUMBER); + $query = $this->getArgument(self::QUERY); + + $page = max(1, intval($page)); + $tagsPerPage = intval(getConfig()->browsing->tagsPerPage); + + $tags = TagSearchService::getEntitiesRows($query, $tagsPerPage, $page); + $tagCount = TagSearchService::getEntityCount($query); + $pageCount = ceil($tagCount / $tagsPerPage); + $page = min($pageCount, $page); + + $ret = new StdClass; + $ret->tags = $tags; + $ret->tagCount = $tagCount; + $ret->page = $page; + $ret->pageCount = $pageCount; + return $ret; + } + + public function requiresPrivilege() + { + return Privilege::ListTags; + } + + public function requiresAuthentication() + { + return false; + } + + public function requiresConfirmedEmail() + { + return false; + } +} diff --git a/src/Controllers/TagController.php b/src/Controllers/TagController.php index c569d992..f57185f8 100644 --- a/src/Controllers/TagController.php +++ b/src/Controllers/TagController.php @@ -1,39 +1,25 @@ $page, + ListTagsJob::QUERY => $filter, + ]); + $context = getContext(); $context->viewName = 'tag-list-wrapper'; - Access::assert(Privilege::ListTags); - - $suppliedFilter = $filter ?: 'order:alpha,asc'; - $page = max(1, intval($page)); - $tagsPerPage = intval(getConfig()->browsing->tagsPerPage); - - $tags = TagSearchService::getEntitiesRows($suppliedFilter, $tagsPerPage, $page); - $tagCount = TagSearchService::getEntityCount($suppliedFilter); - $pageCount = ceil($tagCount / $tagsPerPage); - $page = min($pageCount, $page); - - $context->filter = $suppliedFilter; - $context->transport->tags = $tags; - - if ($context->json) - { - $context->transport->tags = array_values(array_map(function($tag) { - return ['name' => $tag['name'], 'count' => $tag['post_count']]; - }, $context->transport->tags)); - } - else - { - $context->highestUsage = TagSearchService::getMostUsedTag()['post_count']; - $context->transport->paginator = new StdClass; - $context->transport->paginator->page = $page; - $context->transport->paginator->pageCount = $pageCount; - $context->transport->paginator->entityCount = $tagCount; - $context->transport->paginator->entities = $tags; - } + $context->highestUsage = TagSearchService::getMostUsedTag()['post_count']; + $context->filter = $filter; + $context->transport->tags = $ret->tags; + $context->transport->paginator = new StdClass; + $context->transport->paginator->page = $ret->page; + $context->transport->paginator->pageCount = $ret->pageCount; + $context->transport->paginator->entityCount = $ret->tagCount; + $context->transport->paginator->entities = $ret->tags; } public function autoCompleteAction() diff --git a/src/Views/tag-list-wrapper.phtml b/src/Views/tag-list-wrapper.phtml index 3863c515..99866944 100644 --- a/src/Views/tag-list-wrapper.phtml +++ b/src/Views/tag-list-wrapper.phtml @@ -3,7 +3,7 @@ Assets::setSubTitle('tags'); Assets::addStylesheet('tag-list.css'); $tabs = []; -if (Access::check(Privilege::ListTags)) $tabs['list'] = ['List', 'listAction']; +if (Access::check(Privilege::ListTags)) $tabs['list'] = ['List', 'listView']; if (Access::check(Privilege::RenameTags)) $tabs['rename'] = ['Rename', 'renameAction']; if (Access::check(Privilege::MergeTags)) $tabs['merge'] = ['Merge', 'mergeAction']; if (Access::check(Privilege::MassTag)) $tabs['mass-tag-redirect'] = ['Mass tag', 'massTagRedirectAction']; diff --git a/src/Views/tag-list.phtml b/src/Views/tag-list.phtml index a3eba47c..7c3c1b29 100644 --- a/src/Views/tag-list.phtml +++ b/src/Views/tag-list.phtml @@ -16,7 +16,7 @@
  • - +
  • diff --git a/src/Views/top-navigation.phtml b/src/Views/top-navigation.phtml index 2af70ccc..c1d16774 100644 --- a/src/Views/top-navigation.phtml +++ b/src/Views/top-navigation.phtml @@ -46,7 +46,7 @@ { $registerNavItem( 'Tags', - \Chibi\Router::linkTo(['TagController', 'listAction']), + \Chibi\Router::linkTo(['TagController', 'listView']), $activeController == 'tag'); }