Faster tag list

This commit is contained in:
Marcin Kurczewski 2013-11-01 10:08:35 +01:00
parent fe99f97287
commit c3b2c68add
4 changed files with 6 additions and 23 deletions

View file

@ -12,12 +12,12 @@ class TagController
PrivilegesHelper::confirmWithException(Privilege::ListTags);
$suppliedFilter = InputHelper::get('filter');
$tags = Model_Tag::getEntities($suppliedFilter, null, null);
$tags = Model_Tag::getEntitiesRows($suppliedFilter, null, null);
$this->context->transport->tags = $tags;
if ($this->context->json)
$this->context->transport->tags = array_values(array_map(function($tag) {
return ['name' => $tag->name, 'count' => $tag->getPostCount()];
return ['name' => $tag['name'], 'count' => $tag['post_count']];
}, $this->context->transport->tags));
}

View file

@ -83,21 +83,4 @@ class Model_Tag extends AbstractModel
{
return 'Model_Tag_Querybuilder';
}
public static function getEntities($query, $perPage = null, $page = 1)
{
$table = static::getTableName();
$rows = self::getEntitiesRows($query, $perPage, $page);
$entities = R::convertToBeans($table, $rows);
$rowMap = [];
foreach ($rows as &$row)
$rowMap[$row['id']] = $row;
unset ($row);
foreach ($entities as $entity)
$entity->setMeta('post_count', $rowMap[$entity->id]['count']);
return $entities;
}
}

View file

@ -7,7 +7,7 @@ class model_Tag_QueryBuilder implements AbstractQueryBuilder
$limitQuery = false;
$dbQuery
->addSql(', COUNT(post_tag.post_id)')
->as('count')
->as('post_count')
->from('tag')
->innerJoin('post_tag')
->on('tag.id = post_tag.tag_id')

View file

@ -1,12 +1,12 @@
<?php $max = max([0]+array_map(function($x) { return $x->getPostCount(); }, $this->context->transport->tags)); ?>
<?php $max = max([0]+array_map(function($x) { return $x['post_count']; }, $this->context->transport->tags)); ?>
<?php $add = 0.25 ?>
<?php $mul = 0.75 / max(1, log(max(1, $max))) ?>
<?php $url = \Chibi\UrlHelper::route('post', 'list', ['query' => '{query}']) ?>
<div class="tags">
<ul>
<?php foreach ($this->context->transport->tags as $tag): ?>
<?php $name = $tag->name ?>
<?php $count = $tag->getPostCount() ?>
<?php $name = $tag['name'] ?>
<?php $count = $tag['post_count'] ?>
<li class="tag" title="<?php echo $name ?> (<?php echo $count ?>)">
<a href="<?php echo TextHelper::replaceTokens($url, ['query' => $name]) ?>" style="opacity: <?php printf('%.02f', $add + $mul * log($count)) ?>">
<?php echo $name . ' (' . $count . ')' ?>