ContentFetcher.php 11.5 KB
Newer Older
1
<?php
2

3
declare(strict_types=1);
4
5
6
7
8
9
10
11
12
13
14
15
16
17

/*
 * This file is part of the TYPO3 CMS project.
 *
 * 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.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 * The TYPO3 project - inspiring people to share!
 */

18
19
namespace TYPO3\CMS\Backend\View\BackendLayout;

20
use TYPO3\CMS\Backend\Utility\BackendUtility;
21
use TYPO3\CMS\Backend\View\PageLayoutContext;
22
use TYPO3\CMS\Backend\View\PageLayoutView;
23
24
use TYPO3\CMS\Core\Cache\CacheManager;
use TYPO3\CMS\Core\Cache\Frontend\VariableFrontend;
25
26
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
27
use TYPO3\CMS\Core\Database\Query\QueryHelper;
28
use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
29
use TYPO3\CMS\Core\Database\Query\Restriction\WorkspaceRestriction;
30
31
32
33
use TYPO3\CMS\Core\Localization\LanguageService;
use TYPO3\CMS\Core\Messaging\FlashMessage;
use TYPO3\CMS\Core\Messaging\FlashMessageService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
34
use TYPO3\CMS\Core\Versioning\VersionState;
35
36
37
38
39
40
41
42

/**
 * Class responsible for fetching the content data related to a BackendLayout
 *
 * - Reads content records
 * - Performs workspace overlay on records
 * - Capable of returning all records in active language as flat array
 * - Capable of returning records for a given column in a given (optional) language
43
 * - Capable of returning translation data (brief info about translation consistency)
44
45
 *
 * @internal this is experimental and subject to change in TYPO3 v10 / v11
46
47
48
49
 */
class ContentFetcher
{
    /**
50
     * @var PageLayoutContext
51
     */
52
    protected $context;
53
54
55
56
57
58

    /**
     * @var array
     */
    protected $fetchedContentRecords = [];

59
    public function __construct(PageLayoutContext $pageLayoutContext)
60
    {
61
62
        $this->context = $pageLayoutContext;
        $this->fetchedContentRecords = $this->getRuntimeCache()->get('ContentFetcher_fetchedContentRecords') ?: [];
63
64
65
66
67
68
69
70
71
72
    }

    /**
     * Gets content records per column.
     * This is required for correct workspace overlays.
     *
     * @param int|null $columnNumber
     * @param int|null $languageId
     * @return array Associative array for each column (colPos) or for all columns if $columnNumber is null
     */
73
    public function getContentRecordsPerColumn(?int $columnNumber = null, ?int $languageId = null): array
74
    {
75
76
        $languageId = $languageId ?? $this->context->getSiteLanguage()->getLanguageId();

77
        if (empty($this->fetchedContentRecords)) {
78
            $isLanguageMode = $this->context->getDrawingConfiguration()->getLanguageMode();
79
            $queryBuilder = $this->getQueryBuilder();
80
81
            $result = $queryBuilder->execute();
            $records = $this->getResult($result);
82
83
84
            foreach ($records as $record) {
                $recordLanguage = (int)$record['sys_language_uid'];
                $recordColumnNumber = (int)$record['colPos'];
85
86
87
88
89
90
91
92
93
94
                if ($recordLanguage === -1) {
                    // Record is set to "all languages", place it according to view mode.
                    if ($isLanguageMode) {
                        // Force the record to only be shown in default language in "Languages" view mode.
                        $recordLanguage = 0;
                    } else {
                        // Force the record to be shown in the currently active language in "Columns" view mode.
                        $recordLanguage = $languageId;
                    }
                }
95
96
                $this->fetchedContentRecords[$recordLanguage][$recordColumnNumber][] = $record;
            }
97
            $this->getRuntimeCache()->set('ContentFetcher_fetchedContentRecords', $this->fetchedContentRecords);
98
99
100
101
102
103
104
105
106
107
108
        }

        $contentByLanguage = &$this->fetchedContentRecords[$languageId];

        if ($columnNumber === null) {
            return $contentByLanguage ?? [];
        }

        return $contentByLanguage[$columnNumber] ?? [];
    }

109
    public function getFlatContentRecords(int $languageId): iterable
110
111
112
113
114
    {
        $contentRecords = $this->getContentRecordsPerColumn(null, $languageId);
        return empty($contentRecords) ? [] : array_merge(...$contentRecords);
    }

115
116
117
118
119
    /**
     * A hook allows to decide whether a custom type has children which were rendered or should not be rendered.
     *
     * @return iterable
     */
120
121
122
    public function getUnusedRecords(): iterable
    {
        $unrendered = [];
123
        $hookArray = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['cms/layout/class.tx_cms_layout.php']['record_is_used'] ?? [];
124
        $pageLayoutView = PageLayoutView::createFromPageLayoutContext($this->context);
125

126
127
        $knownColumnPositionNumbers = $this->context->getBackendLayout()->getColumnPositionNumbers();
        $rememberer = GeneralUtility::makeInstance(RecordRememberer::class);
128
129
        $languageId = $this->context->getDrawingConfiguration()->getSelectedLanguageId();
        foreach ($this->getContentRecordsPerColumn(null, $languageId) as $contentRecordsInColumn) {
130
            foreach ($contentRecordsInColumn as $contentRecord) {
131
132
133
134
135
136
137
                $used = $rememberer->isRemembered((int)$contentRecord['uid']);
                // A hook mentioned that this record is used somewhere, so this is in fact "rendered" already
                foreach ($hookArray as $hookFunction) {
                    $_params = ['columns' => $knownColumnPositionNumbers, 'record' => $contentRecord, 'used' => $used];
                    $used = GeneralUtility::callUserFunction($hookFunction, $_params, $pageLayoutView);
                }
                if (!$used) {
138
139
140
141
142
143
144
145
146
147
148
149
150
                    $unrendered[] = $contentRecord;
                }
            }
        }
        return $unrendered;
    }

    public function getTranslationData(iterable $contentElements, int $language): array
    {
        if ($language === 0) {
            return [];
        }

151
152
153
154
155
        $languageTranslationInfo = $this->getRuntimeCache()->get('ContentFetcher_TranslationInfo_' . $language) ?: [];
        if (empty($languageTranslationInfo)) {
            $contentRecordsInDefaultLanguage = $this->getContentRecordsPerColumn(null, 0);
            if (!empty($contentRecordsInDefaultLanguage)) {
                $contentRecordsInDefaultLanguage = array_merge(...$contentRecordsInDefaultLanguage);
156
            }
157
158
159
160
161
162
163
164
165
            $untranslatedRecordUids = array_flip(
                array_column(
                    // Eliminate records with "-1" as sys_language_uid since they can not be translated
                    array_filter($contentRecordsInDefaultLanguage, static function (array $record): bool {
                        return (int)($record['sys_language_uid'] ?? 0) !== -1;
                    }),
                    'uid'
                )
            );
166

167
            foreach ($contentElements as $contentElement) {
168
169
170
                if ((int)$contentElement['sys_language_uid'] === -1) {
                    continue;
                }
171
                if ((int)$contentElement['l18n_parent'] === 0) {
172
173
                    $languageTranslationInfo['hasStandAloneContent'] = true;
                    $languageTranslationInfo['mode'] = 'free';
174
175
                }
                if ((int)$contentElement['l18n_parent'] > 0) {
176
177
                    $languageTranslationInfo['hasTranslations'] = true;
                    $languageTranslationInfo['mode'] = 'connected';
178
                }
179
180
181
                if ((int)$contentElement['l10n_source'] > 0) {
                    unset($untranslatedRecordUids[(int)$contentElement['l10n_source']]);
                }
182
            }
183
184
            if (!isset($languageTranslationInfo['hasTranslations'])) {
                $languageTranslationInfo['hasTranslations'] = false;
185
            }
186
            $languageTranslationInfo['untranslatedRecordUids'] = array_keys($untranslatedRecordUids);
187
188

            // Check for inconsistent translations, force "mixed" mode and dispatch a FlashMessage to user if such a case is encountered.
189
190
            if (isset($languageTranslationInfo['hasStandAloneContent'])
                && $languageTranslationInfo['hasTranslations']
191
            ) {
192
                $languageTranslationInfo['mode'] = 'mixed';
193
194
                $siteLanguage = $this->context->getSiteLanguage($language);

195
196
                $message = GeneralUtility::makeInstance(
                    FlashMessage::class,
197
                    $this->getLanguageService()->getLL('staleTranslationWarning'),
198
199
200
201
202
203
204
                    sprintf($this->getLanguageService()->getLL('staleTranslationWarningTitle'), $siteLanguage->getTitle()),
                    FlashMessage::WARNING
                );
                $service = GeneralUtility::makeInstance(FlashMessageService::class);
                $queue = $service->getMessageQueueByIdentifier();
                $queue->addMessage($message);
            }
205
206

            $this->getRuntimeCache()->set('ContentFetcher_TranslationInfo_' . $language, $languageTranslationInfo);
207
        }
208
        return $languageTranslationInfo;
209
210
211
212
213
214
215
216
217
218
    }

    protected function getQueryBuilder(): QueryBuilder
    {
        $fields = ['*'];
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
            ->getQueryBuilderForTable('tt_content');
        $queryBuilder->getRestrictions()
            ->removeAll()
            ->add(GeneralUtility::makeInstance(DeletedRestriction::class))
219
            ->add(GeneralUtility::makeInstance(WorkspaceRestriction::class, (int)$GLOBALS['BE_USER']->workspace));
220
221
222
223
224
225
226
        $queryBuilder
            ->select(...$fields)
            ->from('tt_content');

        $queryBuilder->andWhere(
            $queryBuilder->expr()->eq(
                'tt_content.pid',
227
                $queryBuilder->createNamedParameter($this->context->getPageId(), \PDO::PARAM_INT)
228
229
230
231
232
233
234
235
236
237
            )
        );

        $additionalConstraints = [];
        $parameters = [
            'table' => 'tt_content',
            'fields' => $fields,
            'groupBy' => null,
            'orderBy' => null
        ];
238
239
240
241
242
243

        $sortBy = (string)($GLOBALS['TCA']['tt_content']['ctrl']['sortby'] ?: $GLOBALS['TCA']['tt_content']['ctrl']['default_sortby']);
        foreach (QueryHelper::parseOrderBy($sortBy) as $orderBy) {
            $queryBuilder->addOrderBy($orderBy[0], $orderBy[1]);
        }

244
245
246
247
248
249
        foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS'][PageLayoutView::class]['modifyQuery'] ?? [] as $className) {
            $hookObject = GeneralUtility::makeInstance($className);
            if (method_exists($hookObject, 'modifyQuery')) {
                $hookObject->modifyQuery(
                    $parameters,
                    'tt_content',
250
                    $this->context->getPageId(),
251
252
253
254
255
256
257
258
259
260
                    $additionalConstraints,
                    $fields,
                    $queryBuilder
                );
            }
        }

        return $queryBuilder;
    }

261
    protected function getResult($result): array
262
263
    {
        $output = [];
264
        while ($row = $result->fetchAssociative()) {
265
            BackendUtility::workspaceOL('tt_content', $row, -99, true);
266
            if ($row && !VersionState::cast($row['t3ver_state'] ?? 0)->equals(VersionState::DELETE_PLACEHOLDER)) {
267
268
269
270
271
272
273
274
275
276
                $output[] = $row;
            }
        }
        return $output;
    }

    protected function getLanguageService(): LanguageService
    {
        return $GLOBALS['LANG'];
    }
277
278
279

    protected function getRuntimeCache(): VariableFrontend
    {
280
        return GeneralUtility::makeInstance(CacheManager::class)->getCache('runtime');
281
    }
282
}