diff --git a/public_html/dispatch.php b/public_html/dispatch.php index 175a08e4..334d5d60 100644 --- a/public_html/dispatch.php +++ b/public_html/dispatch.php @@ -126,10 +126,10 @@ $tagValidation = \Chibi\Router::register(['TagController', 'listView'], 'GET', '/tags/{page}', $tagValidation); \Chibi\Router::register(['TagController', 'listView'], 'GET', '/tags/{filter}/{page}', $tagValidation); \Chibi\Router::register(['TagController', 'autoCompleteView'], 'GET', '/tags-autocomplete', $tagValidation); +\Chibi\Router::register(['TagController', 'relatedView'], 'GET', '/tags-related', $tagValidation); foreach (['GET', 'POST'] as $method) { - \Chibi\Router::register(['TagController', 'relatedAction'], $method, '/tags-related', $tagValidation); \Chibi\Router::register(['TagController', 'mergeAction'], $method, '/tags-merge', $tagValidation); \Chibi\Router::register(['TagController', 'renameAction'], $method, '/tags-rename', $tagValidation); \Chibi\Router::register(['TagController', 'massTagRedirectAction'], $method, '/mass-tag-redirect', $tagValidation); diff --git a/src/Api/Jobs/ListRelatedTagsJob.php b/src/Api/Jobs/ListRelatedTagsJob.php new file mode 100644 index 00000000..20555809 --- /dev/null +++ b/src/Api/Jobs/ListRelatedTagsJob.php @@ -0,0 +1,23 @@ +getPageSize(); + $page = $this->getArgument(self::PAGE_NUMBER); + $tag = $this->getArgument(self::TAG_NAME); + $otherTags = $this->hasArgument(self::TAG_NAMES) ? $this->getArgument(self::TAG_NAMES) : []; + + $tags = TagSearchService::getRelatedTagRows($tag); + $tagCount = count($tags); + $tags = array_filter($tags, function($tag) use ($otherTags) { return !in_array($tag['name'], $otherTags); }); + $tags = array_slice($tags, 0, $pageSize); + + return $this->getPager($tags, $tagCount, $page, $pageSize); + } + + public function getDefaultPageSize() + { + return intval(getConfig()->browsing->tagsRelated); + } +} diff --git a/src/Controllers/TagController.php b/src/Controllers/TagController.php index bc612c98..36e40a0d 100644 --- a/src/Controllers/TagController.php +++ b/src/Controllers/TagController.php @@ -42,17 +42,20 @@ class TagController }, $ret->entities)); } - public function relatedAction() + public function relatedView() { + $otherTags = (array) InputHelper::get('context'); + $tag = InputHelper::get('tag'); + + $ret = Api::run( + (new ListRelatedTagsJob), + [ + ListRelatedTagsJob::TAG_NAME => $tag, + ListRelatedTagsJob::TAG_NAMES => $otherTags, + ListRelatedTagsJob::PAGE_NUMBER => 1 + ]); + $context = getContext(); - Access::assert(Privilege::ListTags); - - $suppliedContext = (array) InputHelper::get('context'); - $suppliedTag = InputHelper::get('tag'); - - $limit = intval(getConfig()->browsing->tagsRelated); - $tags = TagSearchService::getRelatedTagRows($suppliedTag, $suppliedContext, $limit); - $context->transport->tags = array_values(array_map( function($tag) @@ -61,7 +64,7 @@ class TagController 'name' => $tag['name'], 'count' => $tag['post_count'] ]; - }, $tags)); + }, $ret->entities)); } public function mergeAction() diff --git a/src/Models/SearchServices/TagSearchService.php b/src/Models/SearchServices/TagSearchService.php index 365e0a6c..593c592b 100644 --- a/src/Models/SearchServices/TagSearchService.php +++ b/src/Models/SearchServices/TagSearchService.php @@ -9,7 +9,7 @@ class TagSearchService extends AbstractSearchService $stmt->addColumn(new Sql\AliasFunctor(new Sql\CountFunctor('post_tag.post_id'), 'post_count')); } - public static function getRelatedTagRows($parentTagName, $context, $limit) + public static function getRelatedTagRows($parentTagName) { $parentTagEntity = TagModel::findByName($parentTagName, false); if (empty($parentTagEntity)) @@ -61,8 +61,6 @@ class TagSearchService extends AbstractSearchService } usort($rows, function($a, $b) { return intval($b['sort']) - intval($a['sort']); }); - $rows = array_filter($rows, function($row) use ($context) { return !in_array($row['name'], $context); }); - $rows = array_slice($rows, 0, $limit); return $rows; }