Apply PSR-12 code style (#3886)
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / cache / builder / TagCloudCacheBuilder.class.php
CommitLineData
04c06e85 1<?php
a9229942 2
04c06e85 3namespace wcf\system\cache\builder;
a9229942 4
04c06e85
MW
5use wcf\data\object\type\ObjectTypeCache;
6use wcf\data\tag\Tag;
7use wcf\data\tag\TagCloudTag;
8use wcf\system\database\util\PreparedStatementConditionBuilder;
9use wcf\system\WCF;
10
11/**
12 * Caches the tag cloud.
a9229942
TD
13 *
14 * @author Marcel Werk
15 * @copyright 2001-2019 WoltLab GmbH
16 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
17 * @package WoltLabSuite\Core\System\Cache\Builder
04c06e85 18 */
a9229942
TD
19class TagCloudCacheBuilder extends AbstractCacheBuilder
20{
21 /**
22 * list of tags
23 * @var TagCloudTag[]
24 */
25 protected $tags = [];
26
27 /**
28 * language ids
29 * @var int
30 */
31 protected $languageIDs = [];
32
33 /**
34 * @inheritDoc
35 */
36 protected $maxLifetime = 3600;
37
38 /**
39 * object type ids
40 * @var int
41 */
42 protected $objectTypeIDs = [];
43
44 /**
45 * @inheritDoc
46 */
47 protected function rebuild(array $parameters)
48 {
49 $this->languageIDs = $this->parseLanguageIDs($parameters);
50
51 // get all taggable types
52 $objectTypes = ObjectTypeCache::getInstance()->getObjectTypes('com.woltlab.wcf.tagging.taggableObject');
53 foreach ($objectTypes as $objectType) {
54 $this->objectTypeIDs[] = $objectType->objectTypeID;
55 }
56
57 // get tags
58 $this->getTags();
59
60 return $this->tags;
61 }
62
63 /**
64 * Parses a list of language ids. If one given language id evaluates to '0' all ids will be discarded.
65 *
66 * @param int[] $parameters
67 * @return int[]
68 */
69 protected function parseLanguageIDs(array $parameters)
70 {
71 // handle special '0' value
72 if (\in_array(0, $parameters)) {
73 // discard all language ids
74 $parameters = [];
75 }
76
77 return $parameters;
78 }
79
80 /**
81 * Reads associated tags.
82 */
83 protected function getTags()
84 {
85 $this->tags = [];
86
87 if (!empty($this->objectTypeIDs)) {
88 // get tag ids
89 $conditionBuilder = new PreparedStatementConditionBuilder();
90 $conditionBuilder->add('object.objectTypeID IN (?)', [$this->objectTypeIDs]);
91 $conditionBuilder->add('object.languageID IN (?)', [$this->languageIDs]);
92 $sql = "SELECT COUNT(*) AS counter, object.tagID
93 FROM wcf" . WCF_N . "_tag_to_object object
94 " . $conditionBuilder . "
95 GROUP BY object.tagID
96 ORDER BY counter DESC";
97 $statement = WCF::getDB()->prepareStatement($sql, 500);
98 $statement->execute($conditionBuilder->getParameters());
99 $tagIDs = $statement->fetchMap('tagID', 'counter');
100
101 // get tags
102 if (!empty($tagIDs)) {
103 $conditionBuilder = new PreparedStatementConditionBuilder();
104 $conditionBuilder->add('tagID IN (?)', [\array_keys($tagIDs)]);
105 $sql = "SELECT *
106 FROM wcf" . WCF_N . "_tag
107 " . $conditionBuilder;
108 $statement = WCF::getDB()->prepareStatement($sql);
109 $statement->execute($conditionBuilder->getParameters());
110 while ($row = $statement->fetchArray()) {
111 $row['counter'] = $tagIDs[$row['tagID']];
112 $this->tags[$row['name']] = new TagCloudTag(new Tag(null, $row));
113 }
114
115 // sort by counter
116 \uasort($this->tags, ['self', 'compareTags']);
117 }
118 }
119 }
120
121 /**
122 * Compares the weight between two tags.
123 *
124 * @param TagCloudTag $tagA
125 * @param TagCloudTag $tagB
126 * @return int
127 */
128 protected static function compareTags($tagA, $tagB)
129 {
130 if ($tagA->counter > $tagB->counter) {
131 return -1;
132 }
133 if ($tagA->counter < $tagB->counter) {
134 return 1;
135 }
136
137 return 0;
138 }
04c06e85 139}