Passed
Pull Request — release-11.5.x (#3193)
by Markus
44:19
created

AbstractUpdateHandler::getSubPageIds()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 12
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php declare(strict_types = 1);
2
namespace ApacheSolrForTypo3\Solr\Domain\Index\Queue\UpdateHandler;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2021 Markus Friedrich <[email protected]>
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 3 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
use Doctrine\DBAL\Driver\Exception as DBALDriverException;
28
use TYPO3\CMS\Core\Utility\GeneralUtility;
29
use TYPO3\CMS\Core\Database\QueryGenerator;
30
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
31
use TYPO3\CMS\Core\Database\ConnectionPool;
32
use ApacheSolrForTypo3\Solr\IndexQueue\Queue;
33
use ApacheSolrForTypo3\Solr\Domain\Index\Queue\RecordMonitor\Helper\ConfigurationAwareRecordService;
34
use ApacheSolrForTypo3\Solr\FrontendEnvironment;
35
use ApacheSolrForTypo3\Solr\System\TCA\TCAService;
36
use ApacheSolrForTypo3\Solr\System\Records\Pages\PagesRepository;
37
38
/**
39
 * Abstract update handler
40
 *
41
 * Base class for Handling updates or deletions on potential
42
 * relevant records
43
 */
44
abstract class AbstractUpdateHandler
45
{
46
    /**
47
     * List of fields in the update field array that
48
     * are required for processing
49
     *
50
     * Note: For pages all fields except l10n_diffsource are
51
     *       kept, as additional fields can be configured in
52
     *       TypoScript, see AbstractDataUpdateEvent->__sleep.
53
     *
54
     * @var array
55
     */
56
    protected static $requiredUpdatedFields = [];
57
58
    /**
59
     * Configuration used to check if recursive updates are required
60
     *
61
     * Update handlers may need to determine which update combination
62
     * require a recursive change.
63
     *
64
     * The structure needs to be:
65
     *
66
     * [
67
     *      [
68
     *           'currentState' => ['fieldName1' => 'value1'],
69
     *           'changeSet' => ['fieldName1' => 'value1']
70
     *      ]
71
     * ]
72
     *
73
     * When the all values of the currentState AND all values of the changeSet match, a recursive update
74
     * will be triggered.
75
     *
76
     * @var array
77
     */
78
    protected $updateSubPagesRecursiveTriggerConfiguration = [];
79
    /**
80
     * @var ConfigurationAwareRecordService
81
     */
82
    protected $configurationAwareRecordService = null;
83
84
    /**
85
     * @var FrontendEnvironment
86
     */
87
    protected $frontendEnvironment = null;
88
89
    /**
90
     * @var TCAService
91
     */
92
    protected $tcaService = null;
93
94
    /**
95
     * @var Queue
96
     */
97
    protected $indexQueue;
98
99
    /**
100
     * @var PagesRepository
101
     */
102
    protected $pagesRepository = null;
103
104
    /**
105
     * @var QueryBuilder[]
106
     */
107
    protected $queryBuilders = [];
108
109
    /**
110
     * @param ConfigurationAwareRecordService $recordService
111
     * @param FrontendEnvironment $frontendEnvironment
112
     * @param TCAService $tcaService
113
     */
114
    public function __construct(
115
        ConfigurationAwareRecordService $recordService,
116
        FrontendEnvironment $frontendEnvironment,
117
        TCAService $tcaService,
118
        Queue $indexQueue
119
        ) {
120
            $this->configurationAwareRecordService = $recordService;
121
            $this->frontendEnvironment = $frontendEnvironment;
122
            $this->tcaService = $tcaService;
123
            $this->indexQueue = $indexQueue;
124
    }
125
126
    /**
127
     * Returns the required fields from the updated fields array
128
     *
129
     * @return array
130
     */
131
    public static function getRequiredUpdatedFields(): array
132
    {
133
        return static::$requiredUpdatedFields;
134
    }
135
136
    /**
137
     * Add required update field
138
     *
139
     * @param string $field
140
     */
141
    public static function addRequiredUpdatedField(string $field): void
142
    {
143
        static::$requiredUpdatedFields[] = $field;
144
    }
145
146
    /**
147
     * @return array
148
     */
149
    protected function getAllRelevantFieldsForCurrentState(): array
150
    {
151
        $allCurrentStateFieldnames = [];
152
153
        foreach ($this->getUpdateSubPagesRecursiveTriggerConfiguration() as $triggerConfiguration) {
154
            if (!isset($triggerConfiguration['currentState']) || !is_array($triggerConfiguration['currentState'])) {
155
                // when no "currentState" configuration for the trigger exists we can skip it
156
                continue;
157
            }
158
159
            // we collect the currentState fields to return a unique list of all fields
160
            $allCurrentStateFieldnames = array_merge(
161
                $allCurrentStateFieldnames,
162
                array_keys($triggerConfiguration['currentState'])
163
                );
164
        }
165
166
        return array_unique($allCurrentStateFieldnames);
167
    }
168
169
    /**
170
     * When the extend to subpages flag was set, we determine the affected subpages and return them.
171
     *
172
     * @param int $pageId
173
     * @return array
174
     */
175
    protected function getSubPageIds(int $pageId): array
176
    {
177
        // here we retrieve only the subpages of this page because the permission clause is not evaluated
178
        // on the root node.
179
        $permissionClause = ' 1 ' . $this->getPagesRepository()->getBackendEnableFields();
180
        $treePageIdList = (string)$this->getQueryGenerator()->getTreeList($pageId, 20, 0, $permissionClause);
181
        $treePageIds = array_map('intval', explode(',', $treePageIdList));
182
183
        // the first one can be ignored because this is the page itself
184
        array_shift($treePageIds);
185
186
        return $treePageIds;
187
    }
188
189
    /**
190
     * Checks if a page update will trigger a recursive update of pages
191
     *
192
     * This can either be the case if some $changedFields are part of the RecursiveUpdateTriggerConfiguration or
193
     * columns have explicitly been configured via plugin.tx_solr.index.queue.recursiveUpdateFields
194
     *
195
     * @param int $pageId
196
     * @param array $updatedFields
197
     * @return bool
198
     * @throws DBALDriverException
199
     */
200
    protected function isRecursivePageUpdateRequired(int $pageId, array $updatedFields): bool
201
    {
202
        // First check RecursiveUpdateTriggerConfiguration
203
        $isRecursiveUpdateRequired = $this->isRecursiveUpdateRequired($pageId, $updatedFields);
204
        // If RecursiveUpdateTriggerConfiguration is false => check if changeFields are part of recursiveUpdateFields
205
        if ($isRecursiveUpdateRequired === false) {
206
            $solrConfiguration = $this->frontendEnvironment->getSolrConfigurationFromPageId($pageId);
207
            $indexQueueConfigurationName = $this->configurationAwareRecordService->getIndexingConfigurationName(
208
                'pages',
209
                $pageId,
210
                $solrConfiguration
211
                );
212
            if ($indexQueueConfigurationName === null) {
213
                return false;
214
            }
215
            $updateFields = $solrConfiguration->getIndexQueueConfigurationRecursiveUpdateFields(
216
                $indexQueueConfigurationName
217
                );
218
219
            // Check if no additional fields have been defined and then skip recursive update
220
            if (empty($updateFields)) {
221
                return false;
222
            }
223
            // If the recursiveUpdateFields configuration is not part of the $changedFields skip recursive update
224
            if (!array_intersect_key($updatedFields, $updateFields)) {
225
                return false;
226
            }
227
        }
228
229
        return true;
230
    }
231
232
    /**
233
     * @param int $pageId
234
     * @param array $updatedFields
235
     * @return bool
236
     */
237
    protected function isRecursiveUpdateRequired(int $pageId, array $updatedFields): bool
238
    {
239
        $fieldsForCurrentState = $this->getAllRelevantFieldsForCurrentState();
240
        $fieldListToRetrieve = implode(',', $fieldsForCurrentState);
241
        $page = $this->getPagesRepository()->getPage($pageId, $fieldListToRetrieve, '', false);
242
        foreach ($this->getUpdateSubPagesRecursiveTriggerConfiguration() as $triggerConfiguration) {
243
            $allCurrentStateFieldsMatch = $this->getAllCurrentStateFieldsMatch($triggerConfiguration, $page);
0 ignored issues
show
Bug introduced by
It seems like $page can also be of type null; however, parameter $pageRecord of ApacheSolrForTypo3\Solr\...rrentStateFieldsMatch() does only seem to accept array, 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

243
            $allCurrentStateFieldsMatch = $this->getAllCurrentStateFieldsMatch($triggerConfiguration, /** @scrutinizer ignore-type */ $page);
Loading history...
244
            $allChangeSetValuesMatch = $this->getAllChangeSetValuesMatch($triggerConfiguration, $updatedFields);
245
246
            $aMatchingTriggerHasBeenFound = $allCurrentStateFieldsMatch && $allChangeSetValuesMatch;
247
            if ($aMatchingTriggerHasBeenFound) {
248
                return true;
249
            }
250
        }
251
252
        return false;
253
    }
254
255
    /**
256
     * @param array $triggerConfiguration
257
     * @param array $pageRecord
258
     * @return bool
259
     */
260
    protected function getAllCurrentStateFieldsMatch(array $triggerConfiguration, array $pageRecord): bool
261
    {
262
        $triggerConfigurationHasNoCurrentStateConfiguration = !array_key_exists('currentState', $triggerConfiguration);
263
        if ($triggerConfigurationHasNoCurrentStateConfiguration) {
264
            return true;
265
        }
266
        $diff = array_diff_assoc($triggerConfiguration['currentState'], $pageRecord);
267
        return empty($diff);
268
    }
269
270
    /**
271
     * @param array $triggerConfiguration
272
     * @param array $changedFields
273
     * @return bool
274
     */
275
    protected function getAllChangeSetValuesMatch(array $triggerConfiguration, array $changedFields): bool
276
    {
277
        $triggerConfigurationHasNoChangeSetStateConfiguration = !array_key_exists('changeSet', $triggerConfiguration);
278
        if ($triggerConfigurationHasNoChangeSetStateConfiguration) {
279
            return true;
280
        }
281
282
        $diff = array_diff_assoc($triggerConfiguration['changeSet'], $changedFields);
283
        return empty($diff);
284
    }
285
286
    /**
287
     * The implementation of this method need to retrieve a configuration to determine which record data
288
     * and change combination required a recursive change.
289
     *
290
     * The structure needs to be:
291
     *
292
     * [
293
     *      [
294
     *           'currentState' => ['fieldName1' => 'value1'],
295
     *           'changeSet' => ['fieldName1' => 'value1']
296
     *      ]
297
     * ]
298
     *
299
     * When the all values of the currentState AND all values of the changeSet match, a recursive update
300
     * will be triggered.
301
     *
302
     * @return array
303
     */
304
    protected function getUpdateSubPagesRecursiveTriggerConfiguration(): array
305
    {
306
        return $this->updateSubPagesRecursiveTriggerConfiguration;
307
    }
308
309
    /**
310
     * @return QueryGenerator
311
     */
312
    protected function getQueryGenerator(): QueryGenerator
313
    {
314
        return GeneralUtility::makeInstance(QueryGenerator::class);
315
    }
316
317
    /**
318
     * @return PagesRepository
319
     */
320
    protected function getPagesRepository(): PagesRepository
321
    {
322
        if (!isset($this->pagesRepository)) {
323
            $this->pagesRepository = GeneralUtility::makeInstance(PagesRepository::class);
324
        }
325
326
        return $this->pagesRepository;
327
    }
328
329
    /**
330
     * Returns the prepared QueryBuilder for given table
331
     *
332
     * @param string $table
333
     * @return QueryBuilder
334
     */
335
    protected function getQueryBuilderForTable(string $table): QueryBuilder
336
    {
337
        if (!isset($this->queryBuilders[$table])) {
338
            $this->queryBuilders[$table] = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
339
        }
340
341
        return $this->queryBuilders[$table];
342
    }
343
}
344