[BUGFIX] Wrong usage of array_unique()
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Utility / RootlineUtility.php
1 <?php
2 namespace TYPO3\CMS\Core\Utility;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2012 Steffen Ritter <steffen.ritter@typo3.org>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26 /**
27 * A utility resolving and Caching the Rootline generation
28 *
29 * @author Steffen Ritter <steffen.ritter@typo3.org>
30 */
31 class RootlineUtility {
32
33 /**
34 * @var integer
35 */
36 protected $pageUid;
37
38 /**
39 * @var string
40 */
41 protected $mountPointParameter;
42
43 /**
44 * @var array
45 */
46 protected $parsedMountPointParameters = array();
47
48 /**
49 * @var integer
50 */
51 protected $languageUid = 0;
52
53 /**
54 * @var integer
55 */
56 protected $workspaceUid = 0;
57
58 /**
59 * @var boolean
60 */
61 protected $versionPreview = FALSE;
62
63 /**
64 * @var \TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
65 */
66 static protected $cache = NULL;
67
68 /**
69 * @var array
70 */
71 static protected $localCache = array();
72
73 /**
74 * Fields to fetch when populating rootline data
75 *
76 * @var array
77 */
78 static protected $rootlineFields = array(
79 'pid',
80 'uid',
81 't3ver_oid',
82 't3ver_wsid',
83 't3ver_state',
84 'title',
85 'alias',
86 'nav_title',
87 'media',
88 'layout',
89 'hidden',
90 'starttime',
91 'endtime',
92 'fe_group',
93 'extendToSubpages',
94 'doktype',
95 'TSconfig',
96 'storage_pid',
97 'is_siteroot',
98 'mount_pid',
99 'mount_pid_ol',
100 'fe_login_mode',
101 'backend_layout_next_level'
102 );
103
104 /**
105 * Rootline Context
106 *
107 * @var \TYPO3\CMS\Frontend\Page\PageRepository
108 */
109 protected $pageContext;
110
111 /**
112 * @var array
113 */
114 static protected $pageRecordCache = array();
115
116 /**
117 * @var \TYPO3\CMS\Core\Database\DatabaseConnection
118 */
119 protected $databaseConnection;
120
121 /**
122 * @param int $uid
123 * @param string $mountPointParameter
124 * @param \TYPO3\CMS\Frontend\Page\PageRepository $context
125 * @throws \RuntimeException
126 */
127 public function __construct($uid, $mountPointParameter = '', \TYPO3\CMS\Frontend\Page\PageRepository $context = NULL) {
128 $this->pageUid = intval($uid);
129 $this->mountPointParameter = trim($mountPointParameter);
130 if ($context === NULL) {
131 if (isset($GLOBALS['TSFE']) && is_object($GLOBALS['TSFE']) && is_object($GLOBALS['TSFE']->sys_page)) {
132 $this->pageContext = $GLOBALS['TSFE']->sys_page;
133 } else {
134 $this->pageContext = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Frontend\\Page\\PageRepository');
135 }
136 } else {
137 $this->pageContext = $context;
138 }
139 $this->initializeObject();
140 }
141
142 /**
143 * Initialize a state to work with
144 *
145 * @throws \RuntimeException
146 * @return void
147 */
148 protected function initializeObject() {
149 $this->languageUid = intval($this->pageContext->sys_language_uid);
150 $this->workspaceUid = intval($this->pageContext->versioningWorkspaceId);
151 $this->versionPreview = $this->pageContext->versioningPreview;
152 if ($this->mountPointParameter !== '') {
153 if (!$GLOBALS['TYPO3_CONF_VARS']['FE']['enable_mount_pids']) {
154 throw new \RuntimeException('Mount-Point Pages are disabled for this installation. Cannot resolve a Rootline for a page with Mount-Points', 1343462896);
155 } else {
156 $this->parseMountPointParameter();
157 }
158 }
159 if (self::$cache === NULL) {
160 self::$cache = $GLOBALS['typo3CacheManager']->getCache('cache_rootline');
161 }
162 self::$rootlineFields = array_merge(self::$rootlineFields, \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $GLOBALS['TYPO3_CONF_VARS']['FE']['addRootLineFields'], TRUE));
163 self::$rootlineFields = array_unique(self::$rootlineFields);
164 $this->databaseConnection = $GLOBALS['TYPO3_DB'];
165 }
166
167 /**
168 * Constructs the cache Identifier
169 *
170 * @param integer $otherUid
171 * @return string
172 */
173 public function getCacheIdentifier($otherUid = NULL) {
174 return implode('_', array(
175 $otherUid !== NULL ? intval($otherUid) : $this->pageUid,
176 $this->mountPointParameter,
177 $this->languageUid,
178 $this->workspaceUid,
179 $this->versionPreview ? 1 : 0
180 ));
181 }
182
183 /**
184 * Returns the actual rootline
185 *
186 * @return array
187 */
188 public function get() {
189 $cacheIdentifier = $this->getCacheIdentifier();
190 if (!isset(self::$localCache[$cacheIdentifier])) {
191 if (!self::$cache->has($cacheIdentifier)) {
192 $this->generateRootlineCache();
193 } else {
194 self::$localCache[$cacheIdentifier] = self::$cache->get($cacheIdentifier);
195 }
196 }
197 return self::$localCache[$cacheIdentifier];
198 }
199
200 /**
201 * Queries the database for the page record and returns it.
202 *
203 * @param integer $uid Page id
204 * @throws \RuntimeException
205 * @return array
206 */
207 protected function getRecordArray($uid) {
208 if (!isset(self::$pageRecordCache[$this->getCacheIdentifier($uid)])) {
209 $row = $this->databaseConnection->exec_SELECTgetSingleRow(implode(',', self::$rootlineFields), 'pages', 'uid = ' . intval($uid) . ' AND pages.deleted = 0 AND pages.doktype <> ' . \TYPO3\CMS\Frontend\Page\PageRepository::DOKTYPE_RECYCLER);
210 if (empty($row)) {
211 throw new \RuntimeException('Could not fetch page data for uid ' . $uid . '.', 1343589451);
212 }
213 $this->pageContext->versionOL('pages', $row, FALSE, TRUE);
214 $this->pageContext->fixVersioningPid('pages', $row);
215 if (is_array($row)) {
216 if ($this->languageUid > 0) {
217 $row = $this->pageContext->getPageOverlay($row, $this->languageUid);
218 }
219 $row = $this->enrichWithRelationFields(isset($row['_PAGES_OVERLAY_UID']) ? $row['_PAGES_OVERLAY_UID'] : $uid, $row);
220 self::$pageRecordCache[$this->getCacheIdentifier($uid)] = $row;
221 }
222 }
223 if (!is_array(self::$pageRecordCache[$this->getCacheIdentifier($uid)])) {
224 throw new \RuntimeException('Broken rootline. Could not resolve page with uid ' . $uid . '.', 1343464101);
225 }
226 return self::$pageRecordCache[$this->getCacheIdentifier($uid)];
227 }
228
229 /**
230 * Resolve relations as defined in TCA and add them to the provided $pageRecord array.
231 *
232 * @param integer $uid Page id
233 * @param array $pageRecord Array with page data to add relation data to.
234 * @throws \RuntimeException
235 * @return array $pageRecord with additional relations
236 */
237 protected function enrichWithRelationFields($uid, array $pageRecord) {
238 foreach ($GLOBALS['TCA']['pages']['columns'] as $column => $configuration) {
239 if ($this->columnHasRelationToResolve($configuration)) {
240 $configuration = $configuration['config'];
241 if ($configuration['MM']) {
242 /** @var $loadDBGroup \TYPO3\CMS\Core\Database\RelationHandler */
243 $loadDBGroup = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Database\\RelationHandler');
244 $loadDBGroup->start($pageRecord[$column], $configuration['foreign_table'], $configuration['MM'], $uid, 'pages', $configuration);
245 $relatedUids = $loadDBGroup->tableArray[$configuration['foreign_table']];
246 } else {
247 $table = $configuration['foreign_table'];
248 $field = $configuration['foreign_field'];
249 $whereClauseParts = array($field . ' = ' . intval($uid));
250 if (isset($configuration['foreign_match_fields']) && is_array($configuration['foreign_match_fields'])) {
251 foreach ($configuration['foreign_match_fields'] as $field => $value) {
252 $whereClauseParts[] = $field . ' = ' . $this->databaseConnection->fullQuoteStr($value, $table);
253 }
254 }
255 if (isset($configuration['foreign_table_field'])) {
256 if (intval($this->languageUid) > 0) {
257 $whereClauseParts[] = trim($configuration['foreign_table_field']) . ' = \'pages_language_overlay\'';
258 } else {
259 $whereClauseParts[] = trim($configuration['foreign_table_field']) . ' = \'pages\'';
260 }
261 }
262 $whereClause = implode(' AND ', $whereClauseParts);
263 $whereClause .= $this->pageContext->deleteClause($table);
264 $rows = $this->databaseConnection->exec_SELECTgetRows('uid', $table, $whereClause);
265 if (!is_array($rows)) {
266 throw new \RuntimeException('Could to resolve related records for page ' . $uid . ' and foreign_table ' . htmlspecialchars($configuration['foreign_table']), 1343589452);
267 }
268 $relatedUids = array();
269 foreach ($rows as $row) {
270 $relatedUids[] = $row['uid'];
271 }
272 }
273 $pageRecord[$column] = implode(',', $relatedUids);
274 }
275 }
276 return $pageRecord;
277 }
278
279 /**
280 * Checks whether the TCA Configuration array of a column
281 * describes a relation which is not stored as CSV in the record
282 *
283 * @param array $configuration TCA configuration to check
284 * @return boolean TRUE, if it describes a non-CSV relation
285 */
286 protected function columnHasRelationToResolve(array $configuration) {
287 $configuration = $configuration['config'];
288 if (!empty($configuration['MM']) && !empty($configuration['type']) && in_array($configuration['type'], array('select', 'inline', 'group'))) {
289 return TRUE;
290 }
291 if (!empty($configuration['foreign_field']) && !empty($configuration['type']) && in_array($configuration['type'], array('select', 'inline'))) {
292 return TRUE;
293 }
294 return FALSE;
295 }
296
297 /**
298 * Actual function to generate the rootline and cache it
299 *
300 * @throws \RuntimeException
301 * @return void
302 */
303 protected function generateRootlineCache() {
304 $page = $this->getRecordArray($this->pageUid);
305 // If the current page is a mounted (according to the MP parameter) handle the mount-point
306 if ($this->isMountedPage()) {
307 $mountPoint = $this->getRecordArray($this->parsedMountPointParameters[$this->pageUid]);
308 $page = $this->processMountedPage($page, $mountPoint);
309 $parentUid = $mountPoint['pid'];
310 // Anyhow after reaching the mount-point, we have to go up that rootline
311 unset($this->parsedMountPointParameters[$this->pageUid]);
312 } else {
313 $parentUid = $page['pid'];
314 }
315 $cacheTags = array('pageId_' . $page['uid']);
316 if ($parentUid > 0) {
317 // Get rootline of (and including) parent page
318 $mountPointParameter = count($this->parsedMountPointParameters) > 0 ? $this->mountPointParameter : '';
319 /** @var $rootline \TYPO3\CMS\Core\Utility\RootlineUtility */
320 $rootline = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Utility\\RootlineUtility', $parentUid, $mountPointParameter, $this->pageContext);
321 $rootline = $rootline->get();
322 // retrieve cache tags of parent rootline
323 foreach ($rootline as $entry) {
324 $cacheTags[] = 'pageId_' . $entry['uid'];
325 if ($entry['uid'] == $this->pageUid) {
326 throw new \RuntimeException('Circular connection in rootline for page with uid ' . $this->pageUid . ' found. Check your mountpoint configuration.', 1343464103);
327 }
328 }
329 } else {
330 $rootline = array();
331 }
332 array_push($rootline, $page);
333 krsort($rootline);
334 self::$cache->set($this->getCacheIdentifier(), $rootline, $cacheTags);
335 self::$localCache[$this->getCacheIdentifier()] = $rootline;
336 }
337
338 /**
339 * Checks whether the current Page is a Mounted Page
340 * (according to the MP-URL-Parameter)
341 *
342 * @return boolean
343 */
344 public function isMountedPage() {
345 return in_array($this->pageUid, array_keys($this->parsedMountPointParameters));
346 }
347
348 /**
349 * Enhances with mount point information or replaces the node if needed
350 *
351 * @param array $mountedPageData page record array of mounted page
352 * @param array $mountPointPageData page record array of mount point page
353 * @throws \RuntimeException
354 * @return array
355 */
356 protected function processMountedPage(array $mountedPageData, array $mountPointPageData) {
357 if ($mountPointPageData['mount_pid'] != $mountedPageData['uid']) {
358 throw new \RuntimeException('Broken rootline. Mountpoint parameter does not match the actual rootline. mount_pid (' . $mountPointPageData['mount_pid'] . ') does not match page uid (' . $mountedPageData['uid'] . ').', 1343464100);
359 }
360 // Current page replaces the original mount-page
361 if ($mountPointPageData['mount_pid_ol']) {
362 $mountedPageData['_MOUNT_OL'] = TRUE;
363 $mountedPageData['_MOUNT_PAGE'] = array(
364 'uid' => $mountPointPageData['uid'],
365 'pid' => $mountPointPageData['pid'],
366 'title' => $mountPointPageData['title']
367 );
368 } else {
369 // The mount-page is not replaced, the mount-page itself has to be used
370 $mountedPageData = $mountPointPageData;
371 }
372 $mountedPageData['_MOUNTED_FROM'] = $this->pageUid;
373 $mountedPageData['_MP_PARAM'] = $this->pageUid . '-' . $mountPointPageData['uid'];
374 return $mountedPageData;
375 }
376
377 /**
378 * Parse the MountPoint Parameters
379 * Splits the MP-Param via "," for several nested mountpoints
380 * and afterwords registers the mountpoint configurations
381 *
382 * @return void
383 */
384 protected function parseMountPointParameter() {
385 $mountPoints = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $this->mountPointParameter);
386 foreach ($mountPoints as $mP) {
387 list($mountedPageUid, $mountPageUid) = \TYPO3\CMS\Core\Utility\GeneralUtility::intExplode('-', $mP);
388 $this->parsedMountPointParameters[$mountedPageUid] = $mountPageUid;
389 }
390 }
391
392 }
393
394
395 ?>