From 34e220d465a380a550ac755d9fbdf1c5bda28f13 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Wed, 15 Oct 2014 19:53:21 +0200 Subject: [PATCH] Improved tag export performance --- src/Dao/TagDao.php | 46 +++++++++++++++++++++++++++++++++++++ src/Services/TagService.php | 29 ++--------------------- 2 files changed, 48 insertions(+), 27 deletions(-) diff --git a/src/Dao/TagDao.php b/src/Dao/TagDao.php index 0793a79d..22f2cb76 100644 --- a/src/Dao/TagDao.php +++ b/src/Dao/TagDao.php @@ -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(); diff --git a/src/Services/TagService.php b/src/Services/TagService.php index 9c18301b..f673475f 100644 --- a/src/Services/TagService.php +++ b/src/Services/TagService.php @@ -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); }