Completed
Push — master ( cb9863...be434f )
by
unknown
40:07 queued 25:06
created

ContentFetcher   A

Complexity

Total Complexity 36

Size/Duplication

Total Lines 217
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 115
dl 0
loc 217
rs 9.52
c 2
b 0
f 0
wmc 36

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getRuntimeCache() 0 3 1
A __construct() 0 4 2
A getUnusedRecords() 0 22 5
C getTranslationData() 0 55 13
A getFlatContentRecords() 0 4 2
A getResult() 0 10 3
A getLanguageService() 0 3 1
A getQueryBuilder() 0 42 3
A getContentRecordsPerColumn() 0 33 6
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
namespace TYPO3\CMS\Backend\View\BackendLayout;
19
20
use Doctrine\DBAL\Driver\Statement;
21
use TYPO3\CMS\Backend\Utility\BackendUtility;
22
use TYPO3\CMS\Backend\View\PageLayoutContext;
23
use TYPO3\CMS\Backend\View\PageLayoutView;
24
use TYPO3\CMS\Core\Cache\CacheManager;
25
use TYPO3\CMS\Core\Cache\Frontend\VariableFrontend;
26
use TYPO3\CMS\Core\Database\ConnectionPool;
27
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
28
use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
29
use TYPO3\CMS\Core\Database\Query\Restriction\WorkspaceRestriction;
30
use TYPO3\CMS\Core\Localization\LanguageService;
31
use TYPO3\CMS\Core\Messaging\FlashMessage;
32
use TYPO3\CMS\Core\Messaging\FlashMessageService;
33
use TYPO3\CMS\Core\Utility\GeneralUtility;
34
35
/**
36
 * Class responsible for fetching the content data related to a BackendLayout
37
 *
38
 * - Reads content records
39
 * - Performs workspace overlay on records
40
 * - Capable of returning all records in active language as flat array
41
 * - Capable of returning records for a given column in a given (optional) language
42
 * - Capable of returning translation data (brief info about translation consistency)
43
 */
44
class ContentFetcher
45
{
46
    /**
47
     * @var PageLayoutContext
48
     */
49
    protected $context;
50
51
    /**
52
     * @var array
53
     */
54
    protected $fetchedContentRecords = [];
55
56
    public function __construct(PageLayoutContext $pageLayoutContext)
57
    {
58
        $this->context = $pageLayoutContext;
59
        $this->fetchedContentRecords = $this->getRuntimeCache()->get('ContentFetcher_fetchedContentRecords') ?: [];
60
    }
61
62
    /**
63
     * Gets content records per column.
64
     * This is required for correct workspace overlays.
65
     *
66
     * @param int|null $columnNumber
67
     * @param int|null $languageId
68
     * @return array Associative array for each column (colPos) or for all columns if $columnNumber is null
69
     */
70
    public function getContentRecordsPerColumn(?int $columnNumber = null, ?int $languageId = null): iterable
71
    {
72
        $languageId = $languageId ?? $this->context->getSiteLanguage()->getLanguageId();
73
74
        if (empty($this->fetchedContentRecords)) {
75
            $isLanguageMode = $this->context->getDrawingConfiguration()->getLanguageMode();
76
            $queryBuilder = $this->getQueryBuilder();
77
            $records = $this->getResult($queryBuilder->execute());
0 ignored issues
show
Bug introduced by
It seems like $queryBuilder->execute() can also be of type integer; however, parameter $result of TYPO3\CMS\Backend\View\B...entFetcher::getResult() does only seem to accept Doctrine\DBAL\Driver\Statement, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

77
            $records = $this->getResult(/** @scrutinizer ignore-type */ $queryBuilder->execute());
Loading history...
78
            foreach ($records as $record) {
79
                $recordLanguage = (int)$record['sys_language_uid'];
80
                $recordColumnNumber = (int)$record['colPos'];
81
                if ($recordLanguage === -1) {
82
                    // Record is set to "all languages", place it according to view mode.
83
                    if ($isLanguageMode) {
84
                        // Force the record to only be shown in default language in "Languages" view mode.
85
                        $recordLanguage = 0;
86
                    } else {
87
                        // Force the record to be shown in the currently active language in "Columns" view mode.
88
                        $recordLanguage = $languageId;
89
                    }
90
                }
91
                $this->fetchedContentRecords[$recordLanguage][$recordColumnNumber][] = $record;
92
            }
93
            $this->getRuntimeCache()->set('ContentFetcher_fetchedContentRecords', $this->fetchedContentRecords);
94
        }
95
96
        $contentByLanguage = &$this->fetchedContentRecords[$languageId];
97
98
        if ($columnNumber === null) {
99
            return $contentByLanguage ?? [];
100
        }
101
102
        return $contentByLanguage[$columnNumber] ?? [];
103
    }
104
105
    public function getFlatContentRecords(int $languageId): iterable
106
    {
107
        $contentRecords = $this->getContentRecordsPerColumn(null, $languageId);
108
        return empty($contentRecords) ? [] : array_merge(...$contentRecords);
109
    }
110
111
    /**
112
     * A hook allows to decide whether a custom type has children which were rendered or should not be rendered.
113
     *
114
     * @return iterable
115
     */
116
    public function getUnusedRecords(): iterable
117
    {
118
        $unrendered = [];
119
        $hookArray = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['cms/layout/class.tx_cms_layout.php']['record_is_used'] ?? [];
120
        $pageLayoutView = PageLayoutView::createFromPageLayoutContext($this->context);
121
122
        $knownColumnPositionNumbers = $this->context->getBackendLayout()->getColumnPositionNumbers();
123
        $rememberer = GeneralUtility::makeInstance(RecordRememberer::class);
124
        foreach ($this->getContentRecordsPerColumn(null, 0) as $contentRecordsInColumn) {
125
            foreach ($contentRecordsInColumn as $contentRecord) {
126
                $used = $rememberer->isRemembered((int)$contentRecord['uid']);
127
                // A hook mentioned that this record is used somewhere, so this is in fact "rendered" already
128
                foreach ($hookArray as $hookFunction) {
129
                    $_params = ['columns' => $knownColumnPositionNumbers, 'record' => $contentRecord, 'used' => $used];
130
                    $used = GeneralUtility::callUserFunction($hookFunction, $_params, $pageLayoutView);
131
                }
132
                if (!$used) {
133
                    $unrendered[] = $contentRecord;
134
                }
135
            }
136
        }
137
        return $unrendered;
138
    }
139
140
    public function getTranslationData(iterable $contentElements, int $language): array
141
    {
142
        if ($language === 0) {
143
            return [];
144
        }
145
146
        $languageTranslationInfo = $this->getRuntimeCache()->get('ContentFetcher_TranslationInfo_' . $language) ?: [];
147
        if (empty($languageTranslationInfo)) {
148
            $contentRecordsInDefaultLanguage = $this->getContentRecordsPerColumn(null, 0);
149
            if (!empty($contentRecordsInDefaultLanguage)) {
150
                $contentRecordsInDefaultLanguage = array_merge(...$contentRecordsInDefaultLanguage);
151
            }
152
            $untranslatedRecordUids = array_flip(array_column($contentRecordsInDefaultLanguage, 'uid'));
153
154
            foreach ($contentElements as $contentElement) {
155
                if ((int)$contentElement['sys_language_uid'] === -1) {
156
                    continue;
157
                }
158
                if ((int)$contentElement['l18n_parent'] === 0) {
159
                    $languageTranslationInfo['hasStandAloneContent'] = true;
160
                    $languageTranslationInfo['mode'] = 'free';
161
                }
162
                if ((int)$contentElement['l18n_parent'] > 0) {
163
                    $languageTranslationInfo['hasTranslations'] = true;
164
                    $languageTranslationInfo['mode'] = 'connected';
165
                }
166
                if ((int)$contentElement['l10n_source'] > 0) {
167
                    unset($untranslatedRecordUids[(int)$contentElement['l10n_source']]);
168
                }
169
            }
170
            if (!isset($languageTranslationInfo['hasTranslations'])) {
171
                $languageTranslationInfo['hasTranslations'] = false;
172
            }
173
            $languageTranslationInfo['untranslatedRecordUids'] = array_keys($untranslatedRecordUids);
174
175
            // Check for inconsistent translations, force "mixed" mode and dispatch a FlashMessage to user if such a case is encountered.
176
            if (isset($languageTranslationInfo['hasStandAloneContent'])
177
                && $languageTranslationInfo['hasTranslations']
178
            ) {
179
                $languageTranslationInfo['mode'] = 'mixed';
180
                $siteLanguage = $this->context->getSiteLanguage();
181
                $message = GeneralUtility::makeInstance(
182
                    FlashMessage::class,
183
                    sprintf($this->getLanguageService()->getLL('staleTranslationWarning'), $siteLanguage->getTitle()),
184
                    sprintf($this->getLanguageService()->getLL('staleTranslationWarningTitle'), $siteLanguage->getTitle()),
185
                    FlashMessage::WARNING
186
                );
187
                $service = GeneralUtility::makeInstance(FlashMessageService::class);
188
                $queue = $service->getMessageQueueByIdentifier();
189
                $queue->addMessage($message);
190
            }
191
192
            $this->getRuntimeCache()->set('ContentFetcher_TranslationInfo_' . $language, $languageTranslationInfo);
193
        }
194
        return $languageTranslationInfo;
195
    }
196
197
    protected function getQueryBuilder(): QueryBuilder
198
    {
199
        $fields = ['*'];
200
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
201
            ->getQueryBuilderForTable('tt_content');
202
        $queryBuilder->getRestrictions()
203
            ->removeAll()
204
            ->add(GeneralUtility::makeInstance(DeletedRestriction::class))
205
            ->add(GeneralUtility::makeInstance(WorkspaceRestriction::class, (int)$GLOBALS['BE_USER']->workspace));
206
        $queryBuilder
207
            ->select(...$fields)
208
            ->from('tt_content');
209
210
        $queryBuilder->andWhere(
211
            $queryBuilder->expr()->eq(
212
                'tt_content.pid',
213
                $queryBuilder->createNamedParameter($this->context->getPageId(), \PDO::PARAM_INT)
214
            )
215
        );
216
217
        $additionalConstraints = [];
218
        $parameters = [
219
            'table' => 'tt_content',
220
            'fields' => $fields,
221
            'groupBy' => null,
222
            'orderBy' => null
223
        ];
224
        foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS'][PageLayoutView::class]['modifyQuery'] ?? [] as $className) {
225
            $hookObject = GeneralUtility::makeInstance($className);
226
            if (method_exists($hookObject, 'modifyQuery')) {
227
                $hookObject->modifyQuery(
228
                    $parameters,
229
                    'tt_content',
230
                    $this->context->getPageId(),
231
                    $additionalConstraints,
232
                    $fields,
233
                    $queryBuilder
234
                );
235
            }
236
        }
237
238
        return $queryBuilder;
239
    }
240
241
    protected function getResult(Statement $result): array
242
    {
243
        $output = [];
244
        while ($row = $result->fetch()) {
245
            BackendUtility::workspaceOL('tt_content', $row, -99, true);
246
            if ($row) {
247
                $output[] = $row;
248
            }
249
        }
250
        return $output;
251
    }
252
253
    protected function getLanguageService(): LanguageService
254
    {
255
        return $GLOBALS['LANG'];
256
    }
257
258
    protected function getRuntimeCache(): VariableFrontend
259
    {
260
        return GeneralUtility::makeInstance(CacheManager::class)->getCache('cache_runtime');
261
    }
262
}
263