Passed
Push — master ( 791909...fe60d3 )
by Timo
52s
created

Page::mountedPageExists()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.1481

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 9
cp 0.6667
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
crap 2.1481
1
<?php
2
namespace ApacheSolrForTypo3\Solr\IndexQueue\Initializer;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2011-2015 Ingo Renner <[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 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
 *  A copy is found in the textfile GPL.txt and important notices to the license
19
 *  from the author is found in LICENSE.txt distributed with these scripts.
20
 *
21
 *
22
 *  This script is distributed in the hope that it will be useful,
23
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
24
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25
 *  GNU General Public License for more details.
26
 *
27
 *  This copyright notice MUST APPEAR in all copies of the script!
28
 ***************************************************************/
29
30
use ApacheSolrForTypo3\Solr\Domain\Site\SiteRepository;
31
use ApacheSolrForTypo3\Solr\IndexQueue\Item;
32
use ApacheSolrForTypo3\Solr\IndexQueue\Queue;
33
use ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager;
34
use ApacheSolrForTypo3\Solr\Utility\DatabaseUtility;
35
use TYPO3\CMS\Backend\Utility\BackendUtility;
36
use TYPO3\CMS\Core\Messaging\FlashMessage;
37
use TYPO3\CMS\Core\Utility\GeneralUtility;
38
39
/**
40
 * Index Queue initializer for pages which also covers resolution of mount
41
 * pages.
42
 *
43
 * @author Ingo Renner <[email protected]>
44
 */
45
class Page extends AbstractInitializer
46
{
47
    /**
48
     * @var \ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager
49
     */
50
    protected $logger = null;
51
52
    /**
53
     * Constructor, sets type and indexingConfigurationName to "pages".
54
     *
55
     */
56 11
    public function __construct()
57
    {
58 11
        parent::__construct();
59
60 11
        $this->type = 'pages';
61 11
        $this->logger = GeneralUtility::makeInstance(SolrLogManager::class, __CLASS__);
62 11
    }
63
64
    /**
65
     * Overrides the general setType() implementation, forcing type to "pages".
66
     *
67
     * @param string $type Type to initialize (ignored).
68
     */
69 10
    public function setType($type)
70
    {
71 10
        $this->type = 'pages';
72 10
    }
73
74
    /**
75
     * Initializes Index Queue page items for a site. Includes regular pages
76
     * and mounted pages - no nested mount page structures though.
77
     *
78
     * @return bool TRUE if initialization was successful, FALSE on error.
79
     */
80 10
    public function initialize()
81
    {
82 10
        $pagesInitialized = parent::initialize();
83 10
        $mountPagesInitialized = $this->initializeMountPages();
84
85 10
        return ($pagesInitialized && $mountPagesInitialized);
86
    }
87
88
    /**
89
     * Initialize a single page that is part of a mounted tree.
90
     *
91
     * @param array $mountProperties Array of mount point properties mountPageSource, mountPageDestination, and mountPageOverlayed
92
     * @param int $mountPageId The ID of the mounted page
93
     */
94 1
    public function initializeMountedPage(array $mountProperties, $mountPageId)
95
    {
96 1
        $mountedPages = [$mountPageId];
97
98 1
        $this->addMountedPagesToIndexQueue($mountedPages, $mountProperties);
99 1
        $this->addIndexQueueItemIndexingProperties($mountProperties, $mountedPages);
100 1
    }
101
102
    /**
103
     * Initializes Mount Pages to be indexed through the Index Queue. The Mount
104
     * Pages are searched and their mounted virtual sub-trees are then resolved
105
     * and added to the Index Queue as if they were actually present below the
106
     * Mount Page.
107
     *
108
     * @return bool TRUE if initialization of the Mount Pages was successful, FALSE otherwise
109
     */
110 10
    protected function initializeMountPages()
111
    {
112 10
        $mountPagesInitialized = false;
113 10
        $mountPages = $this->findMountPages();
114
115 10
        if (empty($mountPages)) {
116 4
            $mountPagesInitialized = true;
117 4
            return $mountPagesInitialized;
118
        }
119
120 6
        foreach ($mountPages as $mountPage) {
121 6
            if (!$this->validateMountPage($mountPage)) {
122 3
                continue;
123
            }
124
125
126 6
            $mountedPages = $this->resolveMountPageTree($mountPage);
127
128
            // handling mount_pid_ol behavior
129 6
            if ($mountPage['mountPageOverlayed']) {
130
                // the page shows the mounted page's content
131 3
                $mountedPages[] = $mountPage['mountPageSource'];
132
            } else {
133
                // Add page like a regular page, as only the sub tree is
134
                // mounted. The page itself has its own content.
135 3
                $indexQueue = GeneralUtility::makeInstance(Queue::class);
136 3
                $indexQueue->updateItem($this->type, $mountPage['uid']);
137
            }
138
139
            // This can happen when the mount point does not show the content of the
140
            // mounted page and the mounted page does not have any subpages.
141 6
            if (empty($mountedPages)) {
142
                continue;
143
            }
144
145 6
            DatabaseUtility::transactionStart();
146
            try {
147 6
                $this->addMountedPagesToIndexQueue($mountedPages, $mountPage);
148 6
                $this->addIndexQueueItemIndexingProperties($mountPage, $mountedPages);
149
150 6
                DatabaseUtility::transactionCommit();
151 6
                $mountPagesInitialized = true;
152
            } catch (\Exception $e) {
153
                DatabaseUtility::transactionRollback();
154
155
                $this->logger->log(
156
                    SolrLogManager::ERROR,
157
                    'Index Queue initialization failed for mount pages',
158
                    [
159
                        $e->__toString()
160
                    ]
161
                );
162
                break;
163
            }
164
        }
165
166 6
        return $mountPagesInitialized;
167
    }
168
169
    /**
170
     * Checks whether a Mount Page is properly configured.
171
     *
172
     * @param array $mountPage A mount page
173
     * @return bool TRUE if the Mount Page is OK, FALSE otherwise
174
     */
175 6
    protected function validateMountPage(array $mountPage)
176
    {
177 6
        $isValidMountPage = true;
178
179 6
        if (empty($mountPage['mountPageSource'])) {
180 3
            $isValidMountPage = false;
181
182 3
            $flashMessage = GeneralUtility::makeInstance(
183 3
                FlashMessage::class,
184 3
                'Property "Mounted page" must not be empty. Invalid Mount Page configuration for page ID ' . $mountPage['uid'] . '.',
185 3
                'Failed to initialize Mount Page tree. ',
186 3
                FlashMessage::ERROR
187
            );
188 3
            $this->flashMessageQueue->addMessage($flashMessage);
189
        }
190
191 6
        if (!$this->mountedPageExists($mountPage['mountPageSource'])) {
192 3
            $isValidMountPage = false;
193
194 3
            $flashMessage = GeneralUtility::makeInstance(
195 3
                FlashMessage::class,
196
                'The mounted page must be accessible in the frontend. '
197
                . 'Invalid Mount Page configuration for page ID '
198 3
                . $mountPage['uid'] . ', the mounted page with ID '
199 3
                . $mountPage['mountPageSource']
200 3
                . ' is not accessible in the frontend.',
201 3
                'Failed to initialize Mount Page tree. ',
202 3
                FlashMessage::ERROR
203
            );
204 3
            $this->flashMessageQueue->addMessage($flashMessage);
205
        }
206
207 6
        return $isValidMountPage;
208
    }
209
210
    /**
211
     * Checks whether the mounted page (mount page source) exists. That is,
212
     * whether it accessible in the frontend. So the record must exist
213
     * (deleted = 0) and must not be hidden (hidden = 0).
214
     *
215
     * @param int $mountedPageId Mounted page ID
216
     * @return bool TRUE if the page is accessible in the frontend, FALSE otherwise.
217
     */
218 6
    protected function mountedPageExists($mountedPageId)
219
    {
220 6
        $mountedPageExists = false;
221
222 6
        $mountedPage = BackendUtility::getRecord('pages', $mountedPageId, 'uid', ' AND hidden = 0');
223 6
        if (!empty($mountedPage)) {
224 6
            $mountedPageExists = true;
225
        }
226
227 6
        return $mountedPageExists;
228
    }
229
230
    /**
231
     * Adds the virtual / mounted pages to the Index Queue as if they would
232
     * belong to the same site where they are mounted.
233
     *
234
     * @param array $mountedPages An array of mounted page IDs
235
     * @param array $mountProperties Array with mount point properties (mountPageSource, mountPageDestination, mountPageOverlayed)
236
     */
237 7
    protected function addMountedPagesToIndexQueue(array $mountedPages, array $mountProperties)
238
    {
239 7
        $mountPointPageIsWithExistingQueueEntry = $this->getPageIdsOfExistingMountPages($mountProperties);
240 7
        $mountedPagesThatNeedToBeAdded = array_diff($mountedPages, $mountPointPageIsWithExistingQueueEntry);
241
242 7
        if (count($mountedPagesThatNeedToBeAdded) === 0) {
243
            //nothing to do
244
            return;
245
        }
246
247 7
        $mountIdentifier = $this->getMountPointIdentifier($mountProperties);
248
        $initializationQuery = 'INSERT INTO tx_solr_indexqueue_item (root, item_type, item_uid, indexing_configuration, indexing_priority, changed, has_indexing_properties, pages_mountidentifier, errors) '
249 7
            . $this->buildSelectStatement() . ', 1, ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($mountIdentifier,
250 7
                'tx_solr_indexqueue_item') . ',""'
251
            . 'FROM pages '
252
            . 'WHERE '
253 7
            . 'uid IN(' . implode(',', $mountedPagesThatNeedToBeAdded) . ') '
254 7
            . $this->buildTcaWhereClause()
255 7
            . $this->buildUserWhereClause();
256
257 7
        $GLOBALS['TYPO3_DB']->sql_query($initializationQuery);
258 7
        $this->logInitialization($initializationQuery);
259 7
    }
260
261
    /**
262
     * Retrieves an array of pageIds from mountPoints that allready have a queue entry.
263
     *
264
     * @param array $mountProperties
265
     * @return array
266
     */
267 7
    protected function getPageIdsOfExistingMountPages($mountProperties)
268
    {
269 7
        $identifier = $this->getMountPointIdentifier($mountProperties);
270 7
        $queueItemsOfExistingMountPoints = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
271 7
            'COUNT(*) AS queueItemCount,item_uid',
272 7
            'tx_solr_indexqueue_item',
273 7
            'item_type="pages" AND pages_mountidentifier = '. $identifier,
274 7
            'item_uid',
275 7
            '',
276 7
            '',
277 7
            'item_uid'
278
        );
279
280 7
        $mountedPagesIdsWithQueueItems = [];
281 7
        foreach ($queueItemsOfExistingMountPoints as $id => $queueItemsOfExistingMountPoint) {
282
            if (((int)$queueItemsOfExistingMountPoint['queueItemCount']) > 0) {
283
                $mountedPagesIdsWithQueueItems[] = (int)$id;
284
            }
285
        }
286
287 7
        return $mountedPagesIdsWithQueueItems;
288
    }
289
290
    /**
291
     * Adds Index Queue item indexing properties for mounted pages. The page
292
     * indexer later needs to know that he's dealing with a mounted page, the
293
     * indexing properties will let make it possible for the indexer to
294
     * distinguish the mounted pages.
295
     *
296
     * @param array $mountPage An array with information about the root/destination Mount Page
297
     * @param array $mountedPages An array of mounted page IDs
298
     */
299 7
    protected function addIndexQueueItemIndexingProperties(array $mountPage, array $mountedPages)
300
    {
301 7
        $mountIdentifier = $this->getMountPointIdentifier($mountPage);
302 7
        $mountPageItems = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
303 7
            '*',
304 7
            'tx_solr_indexqueue_item',
305 7
            'root = ' . intval($this->site->getRootPageId()) . ' '
306
            . 'AND item_type = \'pages\' '
307 7
            . 'AND item_uid IN(' . implode(',', $mountedPages) . ') '
308
            . 'AND has_indexing_properties = 1 '
309 7
            . 'AND pages_mountidentifier=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($mountIdentifier,
310 7
                'tx_solr_indexqueue_item')
311
        );
312
313 7
        if (!is_array($mountPageItems)) {
314
            return;
315
        }
316
317 7
        foreach ($mountPageItems as $mountPageItemRecord) {
318 7
            $mountPageItem = GeneralUtility::makeInstance(Item::class, $mountPageItemRecord);
319 7
            $mountPageItem->setIndexingProperty('mountPageSource', $mountPage['mountPageSource']);
320 7
            $mountPageItem->setIndexingProperty('mountPageDestination', $mountPage['mountPageDestination']);
321 7
            $mountPageItem->setIndexingProperty('isMountedPage', '1');
322
323 7
            $mountPageItem->storeIndexingProperties();
324
        }
325 7
    }
326
327
    /**
328
     * Builds an identifier of the given mount point properties.
329
     *
330
     * @param array $mountProperties Array with mount point properties (mountPageSource, mountPageDestination, mountPageOverlayed)
331
     * @return string String consisting of mountPageSource-mountPageDestination-mountPageOverlayed
332
     */
333 7
    protected function getMountPointIdentifier(array $mountProperties)
334
    {
335 7
        return $mountProperties['mountPageSource']
336 7
        . '-' . $mountProperties['mountPageDestination']
337 7
        . '-' . $mountProperties['mountPageOverlayed'];
338
    }
339
340
    // Mount Page resolution
341
342
    /**
343
     * Finds the mount pages in the current site.
344
     *
345
     * @return array An array of mount pages
346
     */
347 10
    protected function findMountPages()
348
    {
349 10
        return $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
350 10
            'uid,
351
				\'1\'        as isMountPage,
352
				mount_pid    as mountPageSource,
353
				uid          as mountPageDestination,
354
				mount_pid_ol as mountPageOverlayed',
355 10
            'pages',
356 10
            $this->buildPagesClause()
357 10
            . $this->buildTcaWhereClause()
358 10
            . ' AND doktype = 7 AND no_search = 0'
359
        );
360
    }
361
362
    /**
363
     * Gets all the pages from a mounted page tree.
364
     *
365
     * @param array $mountPage
366
     * @return array An array of page IDs in the mounted page tree
367
     */
368 6
    protected function resolveMountPageTree($mountPage)
369
    {
370 6
        $mountPageSourceId = $mountPage['mountPageSource'];
371 6
        $mountPageIdentifier = $this->getMountPointIdentifier($mountPage);
372
373 6
        $siteRepository = GeneralUtility::makeInstance(SiteRepository::class);
374
        /* @var $siteRepository SiteRepository */
375 6
        $mountedSite = $siteRepository->getSiteByPageId($mountPageSourceId, $mountPageIdentifier);
376
377 6
        return $mountedSite->getPages($mountPageSourceId);
378
    }
379
}
380