Moved listing tag relations to API

This commit is contained in:
Marcin Kurczewski 2014-05-04 09:31:03 +02:00
parent ebfa0a71aa
commit 70f187c431
4 changed files with 38 additions and 14 deletions

View file

@ -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);

View file

@ -0,0 +1,23 @@
<?php
class ListRelatedTagsJob extends ListTagsJob
{
public function execute()
{
$pageSize = $this->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);
}
}

View file

@ -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()

View file

@ -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;
}