1 | <?php |
||
41 | class Site |
||
42 | { |
||
43 | /** |
||
44 | * Cache for ApacheSolrForTypo3\Solr\Site objects |
||
45 | * |
||
46 | * @var array |
||
47 | */ |
||
48 | protected static $sitesCache = []; |
||
49 | |||
50 | /** |
||
51 | * Small cache for the list of pages in a site, so that the results of this |
||
52 | * rather expensive operation can be used by all initializers without having |
||
53 | * each initializer do it again. |
||
54 | * |
||
55 | * TODO Move to caching framework once TYPO3 4.6 is the minimum required |
||
56 | * version. |
||
57 | * |
||
58 | * @var array |
||
59 | */ |
||
60 | protected static $sitePagesCache = []; |
||
61 | |||
62 | /** |
||
63 | * Root page record. |
||
64 | * |
||
65 | * @var array |
||
66 | */ |
||
67 | protected $rootPage = []; |
||
68 | |||
69 | /** |
||
70 | * The site's sys_language_mode |
||
71 | * |
||
72 | * @var string |
||
73 | */ |
||
74 | protected $sysLanguageMode = null; |
||
75 | |||
76 | /** |
||
77 | * Constructor. |
||
78 | * |
||
79 | * @param int $rootPageId Site root page ID (uid). The page must be marked as site root ("Use as Root Page" flag). |
||
80 | */ |
||
81 | 113 | public function __construct($rootPageId) |
|
103 | |||
104 | /** |
||
105 | * Clears the $sitePagesCache |
||
106 | * |
||
107 | */ |
||
108 | public static function clearSitePagesCache() |
||
109 | { |
||
110 | self::$sitePagesCache = []; |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Takes an pagerecord and checks whether the page is marked as root page. |
||
115 | * |
||
116 | * @param array $page pagerecord |
||
117 | * @return bool true if the page is marked as root page, false otherwise |
||
118 | */ |
||
119 | public static function isRootPage($page) |
||
127 | |||
128 | /** |
||
129 | * Gets the site's root page ID (uid). |
||
130 | * |
||
131 | * @return int The site's root page ID. |
||
132 | */ |
||
133 | public function getRootPageId() |
||
134 | { |
||
135 | return $this->rootPage['uid']; |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * Gets the site's label. The label is build from the the site title and root |
||
140 | * page ID (uid). |
||
141 | * |
||
142 | * @return string The site's label. |
||
143 | */ |
||
144 | public function getLabel() |
||
145 | { |
||
146 | $rootlineTitles = []; |
||
147 | $rootLine = BackendUtility::BEgetRootLine($this->rootPage['uid']); |
||
148 | // Remove last |
||
149 | array_pop($rootLine); |
||
150 | $rootLine = array_reverse($rootLine); |
||
151 | foreach ($rootLine as $rootLineItem) { |
||
152 | $rootlineTitles[] = $rootLineItem['title']; |
||
153 | } |
||
154 | return implode(' - ', $rootlineTitles) . ', Root Page ID: ' . $this->rootPage['uid']; |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * Gets the site's Solr TypoScript configuration (plugin.tx_solr.*) |
||
159 | * |
||
160 | * @return \ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration The Solr TypoScript configuration |
||
161 | */ |
||
162 | public function getSolrConfiguration() |
||
163 | { |
||
164 | return Util::getSolrConfigurationFromPageId($this->rootPage['uid']); |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * Gets the site's default language as configured in |
||
169 | * config.sys_language_uid. If sys_language_uid is not set, 0 is assumed to |
||
170 | * be the default. |
||
171 | * |
||
172 | * @return int The site's default language. |
||
173 | */ |
||
174 | public function getDefaultLanguage() |
||
189 | 114 | ||
190 | /** |
||
191 | * Generates a list of page IDs in this site. Attention, this includes |
||
192 | 56 | * all page types! Deleted pages are not included. |
|
193 | * |
||
194 | * @param int|string $rootPageId Page ID from where to start collection sub pages |
||
195 | * @param int $maxDepth Maximum depth to descend into the site tree |
||
196 | * @return array Array of pages (IDs) in this site |
||
197 | */ |
||
198 | public function getPages($rootPageId = 'SITE_ROOT', $maxDepth = 999) |
||
199 | { |
||
200 | 36 | $pageIds = []; |
|
201 | $maxDepth = intval($maxDepth); |
||
202 | 36 | ||
203 | if ($rootPageId == 'SITE_ROOT') { |
||
204 | $rootPageId = $this->rootPage['uid']; |
||
205 | $pageIds[] = (int)$this->rootPage['uid']; |
||
206 | } |
||
207 | |||
208 | $recursionRootPageId = intval($rootPageId); |
||
209 | |||
210 | // when we have a cached value, we can return it. |
||
211 | 10 | if (!empty(self::$sitePagesCache[$rootPageId])) { |
|
212 | return self::$sitePagesCache[$rootPageId]; |
||
213 | 10 | } |
|
214 | 10 | ||
215 | if ($maxDepth <= 0) { |
||
216 | 10 | // exiting the recursion loop, may write to cache now |
|
217 | 10 | self::$sitePagesCache[$rootPageId] = $pageIds; |
|
218 | 10 | return $pageIds; |
|
219 | 10 | } |
|
220 | 10 | ||
221 | 10 | // get the page ids of the current level and if needed call getPages recursive |
|
222 | $pageIds = $this->getPageIdsFromCurrentDepthAndCallRecursive($maxDepth, $recursionRootPageId, $pageIds); |
||
223 | |||
224 | // exiting the recursion loop, may write to cache now |
||
225 | self::$sitePagesCache[$rootPageId] = $pageIds; |
||
226 | return $pageIds; |
||
227 | } |
||
228 | |||
229 | 53 | /** |
|
230 | * This method retrieves the pages ids from the current tree level an calls getPages recursive, |
||
231 | 53 | * when the maxDepth has not been reached. |
|
232 | * |
||
233 | * @param int $maxDepth |
||
234 | * @param int $recursionRootPageId |
||
235 | * @param array $pageIds |
||
236 | * @return array |
||
237 | */ |
||
238 | protected function getPageIdsFromCurrentDepthAndCallRecursive($maxDepth, $recursionRootPageId, $pageIds) |
||
239 | { |
||
240 | static $initialPagesAdditionalWhereClause; |
||
241 | |||
242 | // Only fetch $initialPagesAdditionalWhereClause on first call |
||
243 | if (empty($initialPagesAdditionalWhereClause)) { |
||
244 | $configurationAwareRecordService = GeneralUtility::makeInstance(ConfigurationAwareRecordService::class); |
||
245 | // Fetch configuration in order to be able to read initialPagesAdditionalWhereClause |
||
246 | $solrConfiguration = $this->getSolrConfiguration(); |
||
247 | $indexQueueConfigurationName = $configurationAwareRecordService->getIndexingConfigurationName('pages', $this->rootPage['uid'], $solrConfiguration); |
||
248 | $initialPagesAdditionalWhereClause = $solrConfiguration->getInitialPagesAdditionalWhereClause($indexQueueConfigurationName); |
||
249 | } |
||
250 | |||
251 | $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid', 'pages', 'pid = ' . $recursionRootPageId . ' ' . BackendUtility::deleteClause('pages') . $initialPagesAdditionalWhereClause); |
||
252 | |||
253 | while ($page = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result)) { |
||
254 | $pageIds[] = (int)$page['uid']; |
||
255 | 1 | ||
256 | if ($maxDepth > 1) { |
||
257 | 1 | $pageIds = array_merge($pageIds, $this->getPages($page['uid'], $maxDepth - 1)); |
|
258 | } |
||
259 | 1 | } |
|
260 | 1 | $GLOBALS['TYPO3_DB']->sql_free_result($result); |
|
261 | return $pageIds; |
||
262 | 1 | } |
|
263 | |||
264 | 1 | /** |
|
265 | * Generates the site's unique Site Hash. |
||
266 | 1 | * |
|
267 | * The Site Hash is build from the site's main domain, the system encryption |
||
268 | 1 | * key, and the extension "tx_solr". These components are concatenated and |
|
269 | * sha1-hashed. |
||
270 | * |
||
271 | * @return string Site Hash. |
||
272 | */ |
||
273 | public function getSiteHash() |
||
279 | 9 | ||
280 | /** |
||
281 | 9 | * Gets the site's main domain. More specifically the first domain record in |
|
282 | 9 | * the site tree. |
|
283 | * |
||
284 | 9 | * @return string The site's main domain. |
|
285 | 9 | */ |
|
286 | 9 | public function getDomain() |
|
287 | 9 | { |
|
288 | $pageSelect = GeneralUtility::makeInstance(PageRepository::class); |
||
289 | 9 | $rootLine = $pageSelect->getRootLine($this->rootPage['uid']); |
|
290 | |||
291 | return BackendUtility::firstDomainRecord($rootLine); |
||
292 | 9 | } |
|
293 | 7 | ||
294 | /** |
||
295 | * Gets the site's root page. |
||
296 | 9 | * |
|
297 | * @return array The site's root page. |
||
298 | */ |
||
299 | public function getRootPage() |
||
303 | 9 | ||
304 | /** |
||
305 | * Gets the site's root page's title. |
||
306 | 9 | * |
|
307 | 9 | * @return string The site's root page's title |
|
308 | */ |
||
309 | public function getTitle() |
||
313 | |||
314 | /** |
||
315 | * Gets the site's config.sys_language_mode setting |
||
316 | * |
||
317 | * @return string The site's config.sys_language_mode |
||
318 | */ |
||
319 | 9 | public function getSysLanguageMode() |
|
328 | } |
||
329 |