Improved tag export performance

This commit is contained in:
Marcin Kurczewski 2014-10-15 19:53:21 +02:00
parent 790c3e10c6
commit 34e220d465
2 changed files with 48 additions and 27 deletions

View file

@ -137,6 +137,52 @@ class TagDao extends AbstractDao implements ICrudDao
}
}
public function export()
{
$exported = [];
foreach ($this->fpdo->from('tags') as $arrayEntity)
{
$exported[$arrayEntity['id']] = [
'name' => $arrayEntity['name'],
'usages' => intval($arrayEntity['usages']),
'banned' => boolval($arrayEntity['banned'])
];
}
//upgrades on old databases
try
{
$relations = iterator_to_array($this->fpdo->from('tagRelations'));
}
catch (\Exception $e)
{
$relations = [];
}
foreach ($relations as $arrayEntity)
{
$key1 = $arrayEntity['tag1id'];
$key2 = $arrayEntity['tag2id'];
$type = intval($arrayEntity['type']);
if ($type === self::TAG_RELATION_IMPLICATION)
$target = 'implications';
elseif ($type === self::TAG_RELATION_SUGGESTION)
$target = 'suggestions';
else
continue;
if (!isset($exported[$key1]) or !isset($exported[$key2]))
continue;
if (!isset($exported[$key1][$target]))
$exported[$key1][$target] = [];
$exported[$key1][$target][] = $exported[$key2]['name'];
}
return array_values($exported);
}
private function findRelatedTagsByType(Tag $tag, $type)
{
$tagId = $tag->getId();

View file

@ -70,33 +70,8 @@ class TagService
public function exportJson()
{
$tags = [];
foreach ($this->tagDao->findAll() as $tag)
{
$item = [
'name' => $tag->getName(),
'usages' => $tag->getUsages(),
'banned' => $tag->isBanned(),
'implications' => array_values(array_map(
function ($subTag)
{
return $subTag->getName();
},
$tag->getImpliedTags())),
'suggestions' => array_values(array_map(
function ($subTag)
{
return $subTag->getName();
},
$tag->getSuggestedTags())),
];
if (empty($item['implications']))
unset($item['implications']);
if (empty($item['suggestions']))
unset($item['suggestions']);
$tags[] = $item;
}
$json = json_encode($tags);
$exported = $this->tagDao->export();
$json = json_encode($exported);
$this->fileDao->save('tags.json', $json);
}