PageRepository.php 102 KB
Newer Older
1
<?php
2

3
/*
4
 * This file is part of the TYPO3 CMS project.
5
 *
6
7
8
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
9
 *
10
11
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
12
 *
13
14
 * The TYPO3 project - inspiring people to share!
 */
15

16
17
namespace TYPO3\CMS\Core\Domain\Repository;

18
19
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
20
21
use TYPO3\CMS\Core\Cache\CacheManager;
use TYPO3\CMS\Core\Cache\Frontend\VariableFrontend;
22
use TYPO3\CMS\Core\Context\Context;
23
24
use TYPO3\CMS\Core\Context\Exception\AspectNotFoundException;
use TYPO3\CMS\Core\Context\LanguageAspect;
25
use TYPO3\CMS\Core\Context\UserAspect;
26
27
use TYPO3\CMS\Core\Context\VisibilityAspect;
use TYPO3\CMS\Core\Context\WorkspaceAspect;
28
use TYPO3\CMS\Core\Database\Connection;
29
30
31
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\QueryHelper;
use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
32
use TYPO3\CMS\Core\Database\Query\Restriction\EndTimeRestriction;
33
use TYPO3\CMS\Core\Database\Query\Restriction\FrontendGroupRestriction;
34
35
use TYPO3\CMS\Core\Database\Query\Restriction\FrontendRestrictionContainer;
use TYPO3\CMS\Core\Database\Query\Restriction\FrontendWorkspaceRestriction;
36
use TYPO3\CMS\Core\Database\Query\Restriction\HiddenRestriction;
37
use TYPO3\CMS\Core\Database\Query\Restriction\QueryRestrictionContainerInterface;
38
use TYPO3\CMS\Core\Database\Query\Restriction\StartTimeRestriction;
39
use TYPO3\CMS\Core\Database\Query\Restriction\WorkspaceRestriction;
40
use TYPO3\CMS\Core\Domain\Access\RecordAccessVoter;
41
use TYPO3\CMS\Core\Error\Http\ShortcutTargetPageNotFoundException;
42
use TYPO3\CMS\Core\Type\Bitmask\PageTranslationVisibility;
43
use TYPO3\CMS\Core\Utility\GeneralUtility;
44
use TYPO3\CMS\Core\Utility\MathUtility;
45
use TYPO3\CMS\Core\Versioning\VersionState;
46

47
48
/**
 * Page functions, a lot of sql/pages-related functions
49
50
51
52
 *
 * Mainly used in the frontend but also in some cases in the backend. It's
 * important to set the right $where_hid_del in the object so that the
 * functions operate properly
53
 * @see \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController::determineId()
54
 */
55
class PageRepository implements LoggerAwareInterface
56
{
57
    use LoggerAwareTrait;
58

59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
    /**
     * This is not the final clauses. There will normally be conditions for the
     * hidden, starttime and endtime fields as well. You MUST initialize the object
     * by the init() function
     *
     * @var string
     */
    public $where_hid_del = ' AND pages.deleted=0';

    /**
     * Clause for fe_group access
     *
     * @var string
     */
    public $where_groupAccess = '';

    /**
76
77
     * Can be migrated away later to use context API directly.
     *
78
79
     * @var int
     */
80
    protected $sys_language_uid = 0;
81
82

    /**
83
     * Can be migrated away later to use context API directly.
84
     * Workspace ID for preview
85
86
87
     * If > 0, versioning preview of other record versions is allowed. THIS MUST
     * ONLY BE SET IF the page is not cached and truly previewed by a backend
     * user!
88
89
90
     *
     * @var int
     */
91
    protected $versioningWorkspaceId = 0;
92
93
94
95
96
97

    /**
     * Computed properties that are added to database rows.
     *
     * @var array
     */
98
    protected $computedPropertyNames = [
99
100
101
102
        '_LOCALIZED_UID',
        '_MP_PARAM',
        '_ORIG_uid',
        '_ORIG_pid',
103
        '_SHORTCUT_ORIGINAL_PAGE_UID',
104
105
106
        '_PAGES_OVERLAY',
        '_PAGES_OVERLAY_UID',
        '_PAGES_OVERLAY_LANGUAGE',
107
        '_PAGES_OVERLAY_REQUESTEDLANGUAGE',
108
    ];
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129

    /**
     * Named constants for "magic numbers" of the field doktype
     */
    const DOKTYPE_DEFAULT = 1;
    const DOKTYPE_LINK = 3;
    const DOKTYPE_SHORTCUT = 4;
    const DOKTYPE_BE_USER_SECTION = 6;
    const DOKTYPE_MOUNTPOINT = 7;
    const DOKTYPE_SPACER = 199;
    const DOKTYPE_SYSFOLDER = 254;
    const DOKTYPE_RECYCLER = 255;

    /**
     * Named constants for "magic numbers" of the field shortcut_mode
     */
    const SHORTCUT_MODE_NONE = 0;
    const SHORTCUT_MODE_FIRST_SUBPAGE = 1;
    const SHORTCUT_MODE_RANDOM_SUBPAGE = 2;
    const SHORTCUT_MODE_PARENT_PAGE = 3;

130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
    /**
     * @var Context
     */
    protected $context;

    /**
     * PageRepository constructor to set the base context, this will effectively remove the necessity for
     * setting properties from the outside.
     *
     * @param Context $context
     */
    public function __construct(Context $context = null)
    {
        $this->context = $context ?? GeneralUtility::makeInstance(Context::class);
        $this->versioningWorkspaceId = $this->context->getPropertyFromAspect('workspace', 'id');
145
146
147
148
149
        // Only set up the where clauses for pages when TCA is set. This usually happens only in tests.
        // Once all tests are written very well, this can be removed again
        if (isset($GLOBALS['TCA']['pages'])) {
            $this->init($this->context->getPropertyFromAspect('visibility', 'includeHiddenPages'));
            $this->where_groupAccess = $this->getMultipleGroupsWhereClause('pages.fe_group', 'pages');
150
            $this->sys_language_uid = (int)$this->context->getPropertyFromAspect('language', 'id', 0);
151
        }
152
153
    }

154
155
156
157
158
159
160
    /**
     * init() MUST be run directly after creating a new template-object
     * This sets the internal variable $this->where_hid_del to the correct where
     * clause for page records taking deleted/hidden/starttime/endtime/t3ver_state
     * into account
     *
     * @param bool $show_hidden If $show_hidden is TRUE, the hidden-field is ignored!! Normally this should be FALSE. Is used for previewing.
161
     * @internal
162
     */
163
    protected function init($show_hidden)
164
    {
165
        $this->where_groupAccess = '';
166
167
168
169
170
171
172
173
        // As PageRepository may be used multiple times during the frontend request, and may
        // actually be used before the usergroups have been resolved, self::getMultipleGroupsWhereClause()
        // and the hook in ->enableFields() need to be reconsidered when the usergroup state changes.
        // When something changes in the context, a second runtime cache entry is built.
        // However, the PageRepository is generally in use for generating e.g. hundreds of links, so they would all use
        // the same cache identifier.
        $userAspect = $this->context->getAspect('frontend.user');
        $frontendUserIdentifier = 'user_' . (int)$userAspect->get('id') . '_groups_' . md5(implode(',', $userAspect->getGroupIds()));
174

175
176
177
        // We need to respect the date aspect as we might have subrequests with a different time (e.g. backend preview links)
        $dateTimeIdentifier = $this->context->getAspect('date')->get('timestamp');

178
        $cache = $this->getRuntimeCache();
179
        $cacheIdentifier = 'PageRepository_hidDelWhere' . ($show_hidden ? 'ShowHidden' : '') . '_' . (int)$this->versioningWorkspaceId . '_' . $frontendUserIdentifier . '_' . $dateTimeIdentifier;
180
181
182
        $cacheEntry = $cache->get($cacheIdentifier);
        if ($cacheEntry) {
            $this->where_hid_del = $cacheEntry;
183
        } else {
184
185
186
187
188
189
190
191
            $expressionBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
                ->getQueryBuilderForTable('pages')
                ->expr();
            if ($this->versioningWorkspaceId > 0) {
                // For version previewing, make sure that enable-fields are not
                // de-selecting hidden pages - we need versionOL() to unset them only
                // if the overlay record instructs us to.
                // Clear where_hid_del and restrict to live and current workspaces
192
                $this->where_hid_del = ' AND ' . $expressionBuilder->and(
193
                    $expressionBuilder->eq('pages.deleted', 0),
194
                    $expressionBuilder->or(
195
196
197
198
199
200
201
202
203
                        $expressionBuilder->eq('pages.t3ver_wsid', 0),
                        $expressionBuilder->eq('pages.t3ver_wsid', (int)$this->versioningWorkspaceId)
                    ),
                    $expressionBuilder->neq('pages.doktype', self::DOKTYPE_RECYCLER)
                );
            } else {
                // add starttime / endtime, and check for hidden/deleted
                // Filter out new/deleted place-holder pages in case we are NOT in a
                // versioning preview (that means we are online!)
204
                $this->where_hid_del = ' AND ' . (string)$expressionBuilder->and(
205
                    QueryHelper::stripLogicalOperatorPrefix(
206
                        $this->enableFields('pages', (int)$show_hidden, ['fe_group' => true])
207
208
209
210
211
                    ),
                    $expressionBuilder->neq('pages.doktype', self::DOKTYPE_RECYCLER)
                );
            }
            $cache->set($cacheIdentifier, $this->where_hid_del);
212
        }
213

214
        if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS'][self::class]['init'] ?? false)) {
215
            foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS'][self::class]['init'] as $classRef) {
216
217
                $hookObject = GeneralUtility::makeInstance($classRef);
                if (!$hookObject instanceof PageRepositoryInitHookInterface) {
218
                    throw new \UnexpectedValueException($classRef . ' must implement interface ' . PageRepositoryInitHookInterface::class, 1379579812);
219
220
221
222
223
224
225
226
227
228
229
230
231
                }
                $hookObject->init_postProcess($this);
            }
        }
    }

    /**************************
     *
     * Selecting page records
     *
     **************************/

    /**
232
     * Loads the full page record for the given page ID.
233
     *
234
235
236
     * The page record is either served from a first-level cache or loaded from the
     * database. If no page can be found, an empty array is returned.
     *
237
     * Language overlay and versioning overlay are applied. Mount Point
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
     * handling is not done, an overlaid Mount Point is not replaced.
     *
     * The result is conditioned by the public properties where_groupAccess
     * and where_hid_del that are preset by the init() method.
     *
     * @see PageRepository::where_groupAccess
     * @see PageRepository::where_hid_del
     *
     * By default the usergroup access check is enabled. Use the second method argument
     * to disable the usergroup access check.
     *
     * The given UID can be preprocessed by registering a hook class that is
     * implementing the PageRepositoryGetPageHookInterface into the configuration array
     * $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_page.php']['getPage'].
     *
     * @param int $uid The page id to look up
     * @param bool $disableGroupAccessCheck set to true to disable group access check
     * @return array The resulting page record with overlays or empty array
256
     * @throws \UnexpectedValueException
257
     * @see PageRepository::getPage_noCheck()
258
259
260
261
     */
    public function getPage($uid, $disableGroupAccessCheck = false)
    {
        // Hook to manipulate the page uid for special overlay handling
262
263
264
265
        foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_page.php']['getPage'] ?? [] as $className) {
            $hookObject = GeneralUtility::makeInstance($className);
            if (!$hookObject instanceof PageRepositoryGetPageHookInterface) {
                throw new \UnexpectedValueException($className . ' must implement interface ' . PageRepositoryGetPageHookInterface::class, 1251476766);
266
            }
267
            $hookObject->getPage_preProcess($uid, $disableGroupAccessCheck, $this);
268
        }
269
        $cacheIdentifier = 'PageRepository_getPage_' . md5(
270
271
272
            implode(
                '-',
                [
273
                    $uid,
274
                    $disableGroupAccessCheck ? '' : $this->where_groupAccess,
275
                    $this->where_hid_del,
276
                    $this->sys_language_uid,
277
278
279
                ]
            )
        );
280
281
282
283
        $cache = $this->getRuntimeCache();
        $cacheEntry = $cache->get($cacheIdentifier);
        if (is_array($cacheEntry)) {
            return $cacheEntry;
284
        }
285
        $result = [];
286
287
288
289
290
291
292
293
294
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
        $queryBuilder->getRestrictions()->removeAll();
        $queryBuilder->select('*')
            ->from('pages')
            ->where(
                $queryBuilder->expr()->eq('uid', (int)$uid),
                QueryHelper::stripLogicalOperatorPrefix($this->where_hid_del)
            );

295
        $originalWhereGroupAccess = '';
296
297
        if (!$disableGroupAccessCheck) {
            $queryBuilder->andWhere(QueryHelper::stripLogicalOperatorPrefix($this->where_groupAccess));
298
299
300
        } else {
            $originalWhereGroupAccess = $this->where_groupAccess;
            $this->where_groupAccess = '';
301
302
        }

303
        $row = $queryBuilder->executeQuery()->fetchAssociative();
304
        if ($row) {
305
306
307
            $this->versionOL('pages', $row);
            if (is_array($row)) {
                $result = $this->getPageOverlay($row);
308
309
            }
        }
310
311
312
313
314

        if ($disableGroupAccessCheck) {
            $this->where_groupAccess = $originalWhereGroupAccess;
        }

315
        $cache->set($cacheIdentifier, $result);
316
317
318
319
320
321
322
323
        return $result;
    }

    /**
     * Return the $row for the page with uid = $uid WITHOUT checking for
     * ->where_hid_del (start- and endtime or hidden). Only "deleted" is checked!
     *
     * @param int $uid The page id to look up
324
     * @return array The page row with overlaid localized fields. Empty array if no page.
325
326
327
328
     * @see getPage()
     */
    public function getPage_noCheck($uid)
    {
329
330
331
332
333
        $cache = $this->getRuntimeCache();
        $cacheIdentifier = 'PageRepository_getPage_noCheck_' . $uid . '_' . $this->sys_language_uid . '_' . $this->versioningWorkspaceId;
        $cacheEntry = $cache->get($cacheIdentifier);
        if ($cacheEntry !== false) {
            return $cacheEntry;
334
        }
335
336
337
338
339
340
341
342

        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
        $queryBuilder->getRestrictions()
            ->removeAll()
            ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
        $row = $queryBuilder->select('*')
            ->from('pages')
            ->where($queryBuilder->expr()->eq('uid', (int)$uid))
343
            ->executeQuery()
344
            ->fetchAssociative();
345

346
        $result = [];
347
348
349
350
351
352
        if ($row) {
            $this->versionOL('pages', $row);
            if (is_array($row)) {
                $result = $this->getPageOverlay($row);
            }
        }
353
        $cache->set($cacheIdentifier, $result);
354
355
356
        return $result;
    }

357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
    /**
     * Master helper method to overlay a record to a language.
     *
     * Be aware that for pages the languageId is taken, and for all other records the contentId.
     * This might change through a feature switch in the future.
     *
     * @param string $table the name of the table, should be a TCA table with localization enabled
     * @param array $row the current (full-fletched) record.
     * @return array|null
     */
    public function getLanguageOverlay(string $table, array $row)
    {
        // table is not localizable, so return directly
        if (!isset($GLOBALS['TCA'][$table]['ctrl']['languageField'])) {
            return $row;
        }
        try {
            /** @var LanguageAspect $languageAspect */
            $languageAspect = $this->context->getAspect('language');
            if ($languageAspect->doOverlays()) {
                if ($table === 'pages') {
                    return $this->getPageOverlay($row, $languageAspect->getId());
                }
                return $this->getRecordOverlay(
                    $table,
                    $row,
                    $languageAspect->getContentId(),
                    $languageAspect->getOverlayType() === $languageAspect::OVERLAYS_MIXED ? '1' : 'hideNonTranslated'
                );
            }
        } catch (AspectNotFoundException $e) {
            // no overlays
        }
        return $row;
    }

393
394
395
    /**
     * Returns the relevant page overlay record fields
     *
396
     * @param mixed $pageInput If $pageInput is an integer, it's the pid of the pageOverlay record and thus the page overlay record is returned. If $pageInput is an array, it's a page-record and based on this page record the language record is found and OVERLAID before the page record is returned.
397
     * @param int $languageUid anguage UID if you want to set an alternative value to $this->sys_language_uid which is default. Should be >=0
398
     * @throws \UnexpectedValueException
399
     * @return array Page row which is overlaid with language_overlay record (or the overlay record alone)
400
     */
401
    public function getPageOverlay($pageInput, $languageUid = null)
402
    {
403
        $rows = $this->getPagesOverlay([$pageInput], $languageUid);
404
        // Always an array in return
405
        return $rows[0] ?? [];
406
407
408
409
410
    }

    /**
     * Returns the relevant page overlay record fields
     *
411
     * @param array $pagesInput Array of integers or array of arrays. If each value is an integer, it's the pids of the pageOverlay records and thus the page overlay records are returned. If each value is an array, it's page-records and based on this page records the language records are found and OVERLAID before the page records are returned.
412
     * @param int $languageUid Language UID if you want to set an alternative value to $this->sys_language_uid which is default. Should be >=0
413
     * @throws \UnexpectedValueException
414
     * @return array Page rows which are overlaid with language_overlay record.
415
416
417
418
     *               If the input was an array of integers, missing records are not
     *               included. If the input were page rows, untranslated pages
     *               are returned.
     */
419
    public function getPagesOverlay(array $pagesInput, $languageUid = null)
420
421
    {
        if (empty($pagesInput)) {
422
            return [];
423
        }
424
425
        if ($languageUid === null) {
            $languageUid = $this->sys_language_uid;
426
        }
427
428
429
430
431
        foreach ($pagesInput as &$origPage) {
            foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_page.php']['getPageOverlay'] ?? [] as $className) {
                $hookObject = GeneralUtility::makeInstance($className);
                if (!$hookObject instanceof PageRepositoryGetPageOverlayHookInterface) {
                    throw new \UnexpectedValueException($className . ' must implement interface ' . PageRepositoryGetPageOverlayHookInterface::class, 1269878881);
432
                }
433
                $hookObject->getPageOverlay_preProcess($origPage, $languageUid, $this);
434
435
            }
        }
436
        unset($origPage);
437
438

        $overlays = [];
439
        // If language UID is different from zero, do overlay:
440
        if ($languageUid) {
441
            $languageUids = array_merge([$languageUid], $this->getLanguageFallbackChain(null));
442

443
            $pageIds = [];
444
445
446
            foreach ($pagesInput as $origPage) {
                if (is_array($origPage)) {
                    // Was the whole record
447
                    $pageIds[] = (int)($origPage['uid'] ?? 0);
448
449
                } else {
                    // Was the id
450
                    $pageIds[] = (int)$origPage;
451
452
                }
            }
453
            $overlays = $this->getPageOverlaysForLanguageUids($pageIds, $languageUids);
454
        }
455

456
        // Create output:
457
        $pagesOutput = [];
458
459
460
        foreach ($pagesInput as $key => $origPage) {
            if (is_array($origPage)) {
                $pagesOutput[$key] = $origPage;
461
                if (isset($origPage['uid'], $overlays[$origPage['uid']])) {
462
463
464
                    // Overwrite the original field with the overlay
                    foreach ($overlays[$origPage['uid']] as $fieldName => $fieldValue) {
                        if ($fieldName !== 'uid' && $fieldName !== 'pid') {
465
                            $pagesOutput[$key][$fieldName] = $fieldValue;
466
467
468
469
470
471
472
473
474
475
476
477
                        }
                    }
                }
            } else {
                if (isset($overlays[$origPage])) {
                    $pagesOutput[$key] = $overlays[$origPage];
                }
            }
        }
        return $pagesOutput;
    }

478
479
480
481
482
483
    /**
     * Checks whether the passed (translated or default language) page is accessible with the given language settings.
     *
     * @param array $page the page translation record or the page in the default language
     * @param LanguageAspect $languageAspect
     * @return bool true if the given page translation record is suited for the given language ID
484
     * @internal
485
486
487
488
489
490
     */
    public function isPageSuitableForLanguage(array $page, LanguageAspect $languageAspect): bool
    {
        $languageUid = $languageAspect->getId();
        // Checks if the default language version can be shown
        // Block page is set, if l18n_cfg allows plus: 1) Either default language or 2) another language but NO overlay record set for page!
491
        $pageTranslationVisibility = new PageTranslationVisibility((int)($page['l18n_cfg'] ?? 0));
492
493
494
        if ((!$languageUid || !($page['_PAGES_OVERLAY'] ?? false))
            && $pageTranslationVisibility->shouldBeHiddenInDefaultLanguage()
        ) {
495
496
            return false;
        }
497
        if ($languageUid > 0 && $pageTranslationVisibility->shouldHideTranslationIfNoTranslatedRecordExists()) {
498
            if (!($page['_PAGES_OVERLAY'] ?? false) || (int)($page['_PAGES_OVERLAY_LANGUAGE'] ?? 0) !== $languageUid) {
499
500
501
502
                return false;
            }
        } elseif ($languageUid > 0) {
            $languageUids = array_merge([$languageUid], $this->getLanguageFallbackChain($languageAspect));
503
            return in_array((int)($page['sys_language_uid'] ?? 0), $languageUids, true);
504
505
506
507
508
509
510
511
512
513
514
515
516
        }
        return true;
    }

    /**
     * Returns the cleaned fallback chain from the current language aspect, if there is one.
     *
     * @param LanguageAspect|null $languageAspect
     * @return int[]
     */
    protected function getLanguageFallbackChain(?LanguageAspect $languageAspect): array
    {
        $languageAspect = $languageAspect ?? $this->context->getAspect('language');
517
        return array_filter($languageAspect->getFallbackChain(), static function ($item) {
518
519
520
521
522
523
524
525
526
527
528
529
530
531
            return MathUtility::canBeInterpretedAsInteger($item);
        });
    }

    /**
     * Returns the first match of overlays for pages in the passed languages.
     *
     * NOTE regarding the query restrictions:
     * Currently the visibility aspect within the FrontendRestrictionContainer will allow
     * page translation records to be selected as they are child-records of a page.
     * However you may argue that the visibility flag should determine this.
     * But that's not how it's done right now.
     *
     * @param array $pageUids
532
     * @param array $languageUids uid of site language, please note that the order is important here.
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
     * @return array
     */
    protected function getPageOverlaysForLanguageUids(array $pageUids, array $languageUids): array
    {
        // Remove default language ("0")
        $languageUids = array_filter($languageUids);
        $languageField = $GLOBALS['TCA']['pages']['ctrl']['languageField'];
        $overlays = [];

        foreach ($pageUids as $pageId) {
            // Create a map based on the order of values in $languageUids. Those entries reflect the order of the language + fallback chain.
            // We can't work with database ordering since there is no common SQL clause to order by e.g. [7, 1, 2].
            $orderedListByLanguages = array_flip($languageUids);

            $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
548
            $queryBuilder->setRestrictions(GeneralUtility::makeInstance(FrontendRestrictionContainer::class, $this->context));
549
550
            // Because "fe_group" is an exclude field, so it is synced between overlays, the group restriction is removed for language overlays of pages
            $queryBuilder->getRestrictions()->removeByType(FrontendGroupRestriction::class);
551
552
553
554
555
556
557
558
559
560
561
562
            $result = $queryBuilder->select('*')
                ->from('pages')
                ->where(
                    $queryBuilder->expr()->eq(
                        $GLOBALS['TCA']['pages']['ctrl']['transOrigPointerField'],
                        $queryBuilder->createNamedParameter($pageId, \PDO::PARAM_INT)
                    ),
                    $queryBuilder->expr()->in(
                        $GLOBALS['TCA']['pages']['ctrl']['languageField'],
                        $queryBuilder->createNamedParameter($languageUids, Connection::PARAM_INT_ARRAY)
                    )
                )
563
                ->executeQuery();
564
565

            // Create a list of rows ordered by values in $languageUids
566
            while ($row = $result->fetchAssociative()) {
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
                $orderedListByLanguages[$row[$languageField]] = $row;
            }

            foreach ($orderedListByLanguages as $languageUid => $row) {
                if (!is_array($row)) {
                    continue;
                }

                // Found a result for the current language id
                $this->versionOL('pages', $row);
                if (is_array($row)) {
                    $row['_PAGES_OVERLAY'] = true;
                    $row['_PAGES_OVERLAY_UID'] = $row['uid'];
                    $row['_PAGES_OVERLAY_LANGUAGE'] = $languageUid;
                    $row['_PAGES_OVERLAY_REQUESTEDLANGUAGE'] = $languageUids[0];
                    // Unset vital fields that are NOT allowed to be overlaid:
                    unset($row['uid'], $row['pid']);
                    $overlays[$pageId] = $row;

                    // Language fallback found, stop querying further languages
                    break;
                }
            }
        }

        return $overlays;
    }

595
596
597
598
599
    /**
     * Creates language-overlay for records in general (where translation is found
     * in records from the same table)
     *
     * @param string $table Table name
600
     * @param array $row Record to overlay. Must contain uid, pid and $table]['ctrl']['languageField']
601
     * @param int $sys_language_content Pointer to the site language id for content on the site.
602
     * @param string $OLmode Overlay mode. If "hideNonTranslated" then records without translation will not be returned  un-translated but unset (and return value is NULL)
603
     * @throws \UnexpectedValueException
604
     * @return mixed Returns the input record, possibly overlaid with a translation.  But if $OLmode is "hideNonTranslated" then it will return NULL if no translation is found.
605
606
607
     */
    public function getRecordOverlay($table, $row, $sys_language_content, $OLmode = '')
    {
608
609
610
611
        foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_page.php']['getRecordOverlay'] ?? [] as $className) {
            $hookObject = GeneralUtility::makeInstance($className);
            if (!$hookObject instanceof PageRepositoryGetRecordOverlayHookInterface) {
                throw new \UnexpectedValueException($className . ' must implement interface ' . PageRepositoryGetRecordOverlayHookInterface::class, 1269881658);
612
            }
613
            $hookObject->getRecordOverlay_preProcess($table, $row, $sys_language_content, $OLmode, $this);
614
        }
615
616
617
618
619

        $tableControl = $GLOBALS['TCA'][$table]['ctrl'] ?? [];

        if (!empty($tableControl['languageField'])
            // Return record for ALL languages untouched
620
            // @todo: Fix call stack to prevent this situation in the first place
621
622
623
624
625
626
627
628
629
630
631
632
            && (int)$row[$tableControl['languageField']] !== -1
            && !empty($tableControl['transOrigPointerField'])
            && $row['uid'] > 0
            && ($row['pid'] > 0 || in_array($tableControl['rootLevel'] ?? false, [true, 1, -1], true))) {
            // Will try to overlay a record only if the sys_language_content value is larger than zero.
            if ($sys_language_content > 0) {
                // Must be default language, otherwise no overlaying
                if ((int)$row[$tableControl['languageField']] === 0) {
                    // Select overlay record:
                    $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
                        ->getQueryBuilderForTable($table);
                    $queryBuilder->setRestrictions(
633
                        GeneralUtility::makeInstance(FrontendRestrictionContainer::class, $this->context)
634
                    );
635
636
637
638
639
640
                    if ($this->versioningWorkspaceId > 0) {
                        // If not in live workspace, remove query based "enable fields" checks, it will be done in versionOL()
                        // @see functional workspace test createLocalizedNotHiddenWorkspaceContentHiddenInLive()
                        $queryBuilder->getRestrictions()->removeByType(HiddenRestriction::class);
                        $queryBuilder->getRestrictions()->removeByType(StartTimeRestriction::class);
                        $queryBuilder->getRestrictions()->removeByType(EndTimeRestriction::class);
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
                        // We remove the FrontendWorkspaceRestriction in this case, because we need to get the LIVE record
                        // of the language record before doing the version overlay of the language again. WorkspaceRestriction
                        // does this for us, PLUS we need to ensure to get a possible LIVE record first (that's why
                        // the "orderBy" query is there, so the LIVE record is found first), as there might only be a
                        // versioned record (e.g. new version) or both (common for modifying, moving etc).
                        if ($this->hasTableWorkspaceSupport($table)) {
                            $queryBuilder->getRestrictions()->removeByType(FrontendWorkspaceRestriction::class);
                            $queryBuilder->getRestrictions()->add(GeneralUtility::makeInstance(WorkspaceRestriction::class, $this->versioningWorkspaceId));
                            $queryBuilder->orderBy('t3ver_wsid', 'ASC');
                        }
                    }

                    $pid = $row['pid'];
                    // When inside a workspace, the already versioned $row of the default language is coming in
                    // For moved versioned records, the PID MIGHT be different. However, the idea of this function is
                    // to get the language overlay of the LIVE default record, and afterwards get the versioned record
                    // the found (live) language record again, see the versionOL() call a few lines below.
                    // This means, we need to modify the $pid value for moved records, as they might be on a different
                    // page and use the PID of the LIVE version.
                    if (isset($row['_ORIG_pid']) && $this->hasTableWorkspaceSupport($table) && VersionState::cast($row['t3ver_state'] ?? 0)->equals(VersionState::MOVE_POINTER)) {
                        $pid = $row['_ORIG_pid'];
662
                    }
663
664
665
666
667
                    $olrow = $queryBuilder->select('*')
                        ->from($table)
                        ->where(
                            $queryBuilder->expr()->eq(
                                'pid',
668
                                $queryBuilder->createNamedParameter($pid, \PDO::PARAM_INT)
669
670
671
672
673
674
675
676
677
678
679
                            ),
                            $queryBuilder->expr()->eq(
                                $tableControl['languageField'],
                                $queryBuilder->createNamedParameter($sys_language_content, \PDO::PARAM_INT)
                            ),
                            $queryBuilder->expr()->eq(
                                $tableControl['transOrigPointerField'],
                                $queryBuilder->createNamedParameter($row['uid'], \PDO::PARAM_INT)
                            )
                        )
                        ->setMaxResults(1)
680
                        ->executeQuery()
681
                        ->fetchAssociative();
682
683
684
685
686
687

                    $this->versionOL($table, $olrow);
                    // Merge record content by traversing all fields:
                    if (is_array($olrow)) {
                        if (isset($olrow['_ORIG_uid'])) {
                            $row['_ORIG_uid'] = $olrow['_ORIG_uid'];
688
                        }
689
690
                        if (isset($olrow['_ORIG_pid'])) {
                            $row['_ORIG_pid'] = $olrow['_ORIG_pid'];
691
                        }
692
                        foreach ($row as $fN => $fV) {
693
                            if ($fN !== 'uid' && $fN !== 'pid' && array_key_exists($fN, $olrow)) {
694
695
696
697
698
699
700
701
                                $row[$fN] = $olrow[$fN];
                            } elseif ($fN === 'uid') {
                                $row['_LOCALIZED_UID'] = $olrow['uid'];
                            }
                        }
                    } elseif ($OLmode === 'hideNonTranslated' && (int)$row[$tableControl['languageField']] === 0) {
                        // Unset, if non-translated records should be hidden. ONLY done if the source
                        // record really is default language and not [All] in which case it is allowed.
702
                        $row = null;
703
                    }
704
                } elseif ($sys_language_content != $row[$tableControl['languageField']]) {
705
                    $row = null;
706
707
708
709
710
                }
            } else {
                // When default language is displayed, we never want to return a record carrying
                // another language!
                if ($row[$tableControl['languageField']] > 0) {
711
                    $row = null;
712
713
714
                }
            }
        }
715

716
717
718
719
        foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_page.php']['getRecordOverlay'] ?? [] as $className) {
            $hookObject = GeneralUtility::makeInstance($className);
            if (!$hookObject instanceof PageRepositoryGetRecordOverlayHookInterface) {
                throw new \UnexpectedValueException($className . ' must implement interface ' . PageRepositoryGetRecordOverlayHookInterface::class, 1269881659);
720
            }
721
            $hookObject->getRecordOverlay_postProcess($table, $row, $sys_language_content, $OLmode, $this);
722
        }
723

724
725
726
727
728
729
730
731
732
733
734
        return $row;
    }

    /************************************************
     *
     * Page related: Menu, Domain record, Root line
     *
     ************************************************/

    /**
     * Returns an array with page rows for subpages of a certain page ID. This is used for menus in the frontend.
735
     * If there are mount points in overlay mode the _MP_PARAM field is set to the correct MPvar.
736
737
738
739
740
     *
     * If the $pageId being input does in itself require MPvars to define a correct
     * rootline these must be handled externally to this function.
     *
     * @param int|int[] $pageId The page id (or array of page ids) for which to fetch subpages (PID)
741
742
     * @param string $fields Fields to select, `*` is the default - If a custom list is set, make sure the list
     *                       contains the `uid` field. It's mandatory for further processing of the result row.
743
     * @param string $sortField The field to sort by. Default is "sorting
744
     * @param string $additionalWhereClause Optional additional where clauses. Like "AND title like '%some text%'" for instance.
745
     * @param bool $checkShortcuts Check if shortcuts exist, checks by default
746
     * @return array Array with key/value pairs; keys are page-uid numbers. values are the corresponding page records (with overlaid localized fields, if any)
747
748
     * @see getPageShortcut()
     * @see \TYPO3\CMS\Frontend\ContentObject\Menu\AbstractMenuContentObject::makeMenu()
749
750
751
     */
    public function getMenu($pageId, $fields = '*', $sortField = 'sorting', $additionalWhereClause = '', $checkShortcuts = true)
    {
752
753
754
755
756
        // @todo: Restricting $fields to a list like 'uid, title' here, leads to issues from methods like
        //        getSubpagesForPages() which access keys like 'doktype'. This is odd, select field list
        //        should be handled better here, probably at least containing fields that are used in the
        //        sub methods. In the end, it might be easier to drop argument $fields altogether and
        //        always select * ?
757
758
759
760
761
762
763
764
765
766
        return $this->getSubpagesForPages((array)$pageId, $fields, $sortField, $additionalWhereClause, $checkShortcuts);
    }

    /**
     * Returns an array with page-rows for pages with uid in $pageIds.
     *
     * This is used for menus. If there are mount points in overlay mode
     * the _MP_PARAM field is set to the correct MPvar.
     *
     * @param int[] $pageIds Array of page ids to fetch
767
768
     * @param string $fields Fields to select, `*` is the default - If a custom list is set, make sure the list
     *                       contains the `uid` field. It's mandatory for further processing of the result row.
769
     * @param string $sortField The field to sort by. Default is "sorting"
770
     * @param string $additionalWhereClause Optional additional where clauses. Like "AND title like '%some text%'" for instance.
771
     * @param bool $checkShortcuts Check if shortcuts exist, checks by default
772
     * @return array Array with key/value pairs; keys are page-uid numbers. values are the corresponding page records (with overlaid localized fields, if any)
773
774
775
776
777
778
779
     */
    public function getMenuForPages(array $pageIds, $fields = '*', $sortField = 'sorting', $additionalWhereClause = '', $checkShortcuts = true)
    {
        return $this->getSubpagesForPages($pageIds, $fields, $sortField, $additionalWhereClause, $checkShortcuts, false);
    }

    /**
780
     * Loads page records either by PIDs or by UIDs.
781
     *
782
783
     * By default the subpages of the given page IDs are loaded (as the method name suggests). If $parentPages is set
     * to FALSE, the page records for the given page IDs are loaded directly.
784
     *
785
786
787
788
789
790
791
792
793
794
795
796
     * Concerning the rationale, please see these two other methods:
     *
     * @see PageRepository::getMenu()
     * @see PageRepository::getMenuForPages()
     *
     * Version and language overlay are applied to the loaded records.
     *
     * If a record is a mount point in overlay mode, the the overlaying page record is returned in place of the
     * record. The record is enriched by the field _MP_PARAM containing the mount point mapping for the mount
     * point.
     *
     * The query can be customized by setting fields, sorting and additional WHERE clauses. If additional WHERE
797
     * clauses are given, the clause must start with an operator, i.e: "AND title like '%some text%'".
798
799
800
801
802
803
     *
     * The keys of the returned page records are the page UIDs.
     *
     * CAUTION: In case of an overlaid mount point, it is the original UID.
     *
     * @param int[] $pageIds PIDs or UIDs to load records for
804
805
     * @param string $fields Fields to select, `*` is the default - If a custom list is set, make sure the list
     *                       contains the `uid` field. It's mandatory for further processing of the result row.
806
807
808
809
810
811
     * @param string $sortField the field to sort by
     * @param string $additionalWhereClause optional additional WHERE clause
     * @param bool $checkShortcuts whether to check if shortcuts exist
     * @param bool $parentPages Switch to load pages (false) or child pages (true).
     * @return array page records
     *
812
     * @see self::getPageShortcut()
813
     * @see \TYPO3\CMS\Frontend\ContentObject\Menu\AbstractMenuContentObject::makeMenu()
814
     */
815
816
817
818
819
820
821
822
    protected function getSubpagesForPages(
        array $pageIds,
        string $fields = '*',
        string $sortField = 'sorting',
        string $additionalWhereClause = '',
        bool $checkShortcuts = true,
        bool $parentPages = true
    ): array {
823
        $relationField = $parentPages ? 'pid' : 'uid';
824
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
825
826
827
        $queryBuilder->getRestrictions()
            ->removeAll()
            ->add(GeneralUtility::makeInstance(WorkspaceRestriction::class, $this->versioningWorkspaceId));
828

829
        $res = $queryBuilder->select(...GeneralUtility::trimExplode(',', $fields, true))
830
831
            ->from('pages')
            ->where(
832
833
834
835
                $queryBuilder->expr()->in(
                    $relationField,
                    $queryBuilder->createNamedParameter($pageIds, Connection::PARAM_INT_ARRAY)
                ),
836
                $queryBuilder->expr()->eq(
837
                    $GLOBALS['TCA']['pages']['ctrl']['languageField'],
838
839
                    $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT)
                ),
840
841
842
                QueryHelper::stripLogicalOperatorPrefix($this->where_hid_del),
                QueryHelper::stripLogicalOperatorPrefix($this->where_groupAccess),
                QueryHelper::stripLogicalOperatorPrefix($additionalWhereClause)
843
844
845
            );

        if (!empty($sortField)) {
846
847
            $orderBy = QueryHelper::parseOrderBy($sortField);
            foreach ($orderBy as $order) {
848
                $res->addOrderBy($order[0], $order[1] ?? 'ASC');
849
            }
850
        }
851
        $result = $res->executeQuery();
852

853
        $pages = [];
854
        while ($page = $result->fetchAssociative()) {
855
856
857
858
            $originalUid = $page['uid'];

            // Versioning Preview Overlay
            $this->versionOL('pages', $page, true);
859
            // Skip if page got disabled due to version overlay (might be delete placeholder)
860
            if (empty($page)) {
861
862
                continue;
            }
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882

            // Add a mount point parameter if needed
            $page = $this->addMountPointParameterToPage((array)$page);

            // If shortcut, look up if the target exists and is currently visible
            if ($checkShortcuts) {
                $page = $this->checkValidShortcutOfPage((array)$page, $additionalWhereClause);
            }

            // If the page still is there, we add it to the output
            if (!empty($page)) {
                $pages[$originalUid] = $page;
            }
        }

        // Finally load language overlays
        return $this->getPagesOverlay($pages);
    }

    /**
883
     * Replaces the given page record with mounted page if required
884
     *
885
886
887
888
889
890
891
892
893
894
895
     * If the given page record is a mount point in overlay mode, the page
     * record is replaced by the record of the overlaying page. The overlay
     * record is enriched by setting the mount point mapping into the field
     * _MP_PARAM as string for example '23-14'.
     *
     * In all other cases the given page record is returned as is.
     *
     * @todo Find a better name. The current doesn't hit the point.
     *
     * @param array $page The page record to handle.
     * @return array The given page record or it's replacement.
896
     */
897
    protected function addMountPointParameterToPage(array $page): array
898
899
900
901
902
903
904
905
    {
        if (empty($page)) {
            return [];
        }

        // $page MUST have "uid", "pid", "doktype", "mount_pid", "mount_pid_ol" fields in it
        $mountPointInfo = $this->getMountPointInfo($page['uid'], $page);

906
        // There is a valid mount point in overlay mode.
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
        if (is_array($mountPointInfo) && $mountPointInfo['overlay']) {

            // Using "getPage" is OK since we need the check for enableFields AND for type 2
            // of mount pids we DO require a doktype < 200!
            $mountPointPage = $this->getPage($mountPointInfo['mount_pid']);

            if (!empty($mountPointPage)) {
                $page = $mountPointPage;
                $page['_MP_PARAM'] = $mountPointInfo['MPvar'];
            } else {
                $page = [];
            }
        }
        return $page;
    }

    /**
     * If shortcut, look up if the target exists and is currently visible
     *
     * @param array $page The page to check
927
     * @param string $additionalWhereClause Optional additional where clauses. Like "AND title like '%some text%'" for instance.
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
     * @return array
     */
    protected function checkValidShortcutOfPage(array $page, $additionalWhereClause)
    {
        if (empty($page)) {
            return [];
        }

        $dokType = (int)$page['doktype'];
        $shortcutMode = (int)$page['shortcut_mode'];

        if ($dokType === self::DOKTYPE_SHORTCUT && ($page['shortcut'] || $shortcutMode)) {
            if ($shortcutMode === self::SHORTCUT_MODE_NONE) {
                // No shortcut_mode set, so target is directly set in $page['shortcut']
                $searchField = 'uid';
                $searchUid = (int)$page['shortcut'];
            } elseif ($shortcutMode === self::SHORTCUT_MODE_FIRST_SUBPAGE || $shortcutMode === self::SHORTCUT_MODE_RANDOM_SUBPAGE) {
                // Check subpages - first subpage or random subpage
                $searchField = 'pid';
947
                // If a shortcut mode is set and no valid page is given to select subpages
948
949
950
951
952
953
954
955
956
957
958
                // from use the actual page.
                $searchUid = (int)$page['shortcut'] ?: $page['uid'];
            } elseif ($shortcutMode === self::SHORTCUT_MODE_PARENT_PAGE) {
                // Shortcut to parent page
                $searchField = 'uid';
                $searchUid = $page['pid'];
            } else {
                $searchField = '';
                $searchUid = 0;
            }

959
960
961
962
963
            $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
            $queryBuilder->getRestrictions()->removeAll();
            $count = $queryBuilder->count('uid')
                ->from('pages')
                ->where(
964
965
966
967
                    $queryBuilder->expr()->eq(
                        $searchField,
                        $queryBuilder->createNamedParameter($searchUid, \PDO::PARAM_INT)
                    ),
968
969
970
971
                    QueryHelper::stripLogicalOperatorPrefix($this->where_hid_del),
                    QueryHelper::stripLogicalOperatorPrefix($this->where_groupAccess),
                    QueryHelper::stripLogicalOperatorPrefix($additionalWhereClause)
                )
972
                ->executeQuery()
973
                ->fetchOne();
974
975
976
977
978
979
980
981
982
983

            if (!$count) {
                $page = [];
            }
        } elseif ($dokType === self::DOKTYPE_SHORTCUT) {
            // Neither shortcut target nor mode is set. Remove the page from the menu.
            $page = [];
        }
        return $page;
    }
984

985
986
987
    /**
     * Get page shortcut; Finds the records pointed to by input value $SC (the shortcut value)
     *
988
     * @param string $shortcutFieldValue The value of the "shortcut" field from the pages record
989
990
991
992
993
     * @param int $shortcutMode The shortcut mode: 1 will select first subpage, 2 a random subpage, 3 the parent page; default is the page pointed to by $SC
     * @param int $thisUid The current page UID of the page which is a shortcut
     * @param int $iteration Safety feature which makes sure that the function is calling itself recursively max 20 times (since this function can find shortcuts to other shortcuts to other shortcuts...)
     * @param array $pageLog An array filled with previous page uids tested by the function - new page uids are evaluated against this to avoid going in circles.
     * @param bool $disableGroupCheck If true, the group check is disabled when fetching the target page (needed e.g. for menu generation)
994
     * @param bool $resolveRandomPageShortcuts If true (default) this will also resolve shortcut to random subpages. In case of linking from a page to a shortcut page, we do not want to cache the "random" logic.
995
996
997
     *
     * @throws \RuntimeException
     * @throws ShortcutTargetPageNotFoundException
998
     * @return mixed Returns the page record of the page that the shortcut pointed to. If $resolveRandomPageShortcuts = false, and the shortcut page is configured to point to a random shortcut then an empty array is returned
999
     * @internal
1000
     * @see getPageAndRootline()
For faster browsing, not all history is shown. View entire blame