| 1 |  |  | <?php declare(strict_types = 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace ApacheSolrForTypo3\Solr\Domain\Index\Queue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | /*************************************************************** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  *  Copyright notice | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  *  (c) 2010-2017 dkd Internet Service GmbH <[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 ApacheSolrForTypo3\Solr\IndexQueue\InitializationPostProcessor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | use ApacheSolrForTypo3\Solr\IndexQueue\Queue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | use ApacheSolrForTypo3\Solr\Site; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | use TYPO3\CMS\Core\Utility\GeneralUtility; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  * The queue initialization service is responsible to run the initialization of the index queue for a combination of sites | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  * and index queue configurations. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  * @author Timo Hund <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  * @author Ingo Renner <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  * @package ApacheSolrForTypo3\Solr\Domain\Index\Queue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | class QueueInitializationService { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @var Queue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     protected $queue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * QueueInitializationService constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 107 |  |     public function __construct(Queue $queue) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 107 |  |         $this->queue = $queue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 107 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * Truncate and rebuild the tx_solr_indexqueue_item table. This is the most | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * complete way to force reindexing, or to build the Index Queue for the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * first time. The Index Queue initialization is site-specific. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @param Site $site The site to initialize | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @param string $indexingConfigurationName Name of a specific indexing configuration, when * is passed any is used | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @return array An array of booleans, each representing whether the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      *      initialization for an indexing configuration was successful | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 5 |  |     public function initializeBySiteAndIndexConfiguration(Site $site, $indexingConfigurationName = '*'): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 5 |  |         return $this->initializeBySiteAndIndexConfigurations($site, [$indexingConfigurationName]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * Truncates and rebuilds the tx_solr_indexqueue_item table for a set of sites and a set of index configurations. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @param array $sites The array of sites to initialize | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * @param array $indexingConfigurationNames the array of index configurations to initialize. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     public function initializeBySitesAndConfigurations(array $sites, array $indexingConfigurationNames = ['*']): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $initializationStatesBySiteId = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         foreach($sites as $site) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             /** @var  Site $site */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             $initializationResult = $this->initializeBySiteAndIndexConfigurations($site, $indexingConfigurationNames); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             $initializationStatesBySiteId[$site->getRootPageId()] = $initializationResult; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         return $initializationStatesBySiteId; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * Initializes a set index configurations for a given site. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @param Site $site | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * @param array $indexingConfigurationNames if one of the names is a * (wildcard) all configurations are used, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 7 |  |     public function initializeBySiteAndIndexConfigurations(Site $site, array $indexingConfigurationNames): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 7 |  |         $initializationStatus = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 7 |  |         $hasWildcardConfiguration = in_array('*', $indexingConfigurationNames); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 7 |  |         $indexingConfigurationNames = $hasWildcardConfiguration ? $site->getSolrConfiguration()->getEnabledIndexQueueConfigurationNames() : $indexingConfigurationNames; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 7 |  |         foreach ($indexingConfigurationNames as $indexingConfigurationName) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 7 |  |             $initializationStatus[$indexingConfigurationName] = $this->applyInitialization($site, (string)$indexingConfigurationName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 7 |  |         if (!is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['postProcessIndexQueueInitialization'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 7 |  |             return $initializationStatus; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['postProcessIndexQueueInitialization'] as $classReference) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             $indexQueueInitializationPostProcessor = GeneralUtility::makeInstance($classReference); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             if ($indexQueueInitializationPostProcessor instanceof InitializationPostProcessor) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |                 $indexQueueInitializationPostProcessor->postProcessIndexQueueInitialization($site, $indexingConfigurationNames, $initializationStatus); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |                 throw new \UnexpectedValueException(get_class($indexQueueInitializationPostProcessor) . ' must implement interface ' . InitializationPostProcessor::class, 1345815561); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         return $initializationStatus; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      * Initializes the Index Queue for a specific indexing configuration. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      * @param Site $site The site to initialize | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      * @param string $indexingConfigurationName name of a specific | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |      *      indexing configuration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |      * @return bool TRUE if the initialization was successful, FALSE otherwise | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 7 |  |     protected function applyInitialization(Site $site, $indexingConfigurationName): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         // clear queue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 7 |  |         $this->queue->deleteItemsBySite($site, $indexingConfigurationName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 7 |  |         $solrConfiguration = $site->getSolrConfiguration(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 7 |  |         $tableToIndex = $solrConfiguration->getIndexQueueTableNameOrFallbackToConfigurationName($indexingConfigurationName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 | 7 |  |         $initializerClass = $solrConfiguration->getIndexQueueInitializerClassByConfigurationName($indexingConfigurationName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 | 7 |  |         $indexConfiguration = $solrConfiguration->getIndexQueueConfigurationByName($indexingConfigurationName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 | 7 |  |         return $this->executeInitializer($site, $indexingConfigurationName, $initializerClass, $tableToIndex, $indexConfiguration); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |      * @param Site $site | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |      * @param string $indexingConfigurationName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |      * @param string $initializerClass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |      * @param string $tableToIndex | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |      * @param array $indexConfiguration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |      * @return bool | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 150 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 151 | 6 |  |     protected function executeInitializer(Site $site, $indexingConfigurationName, $initializerClass, $tableToIndex, $indexConfiguration): bool | 
            
                                                                        
                            
            
                                    
            
            
                | 152 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 153 | 6 |  |         $initializer = GeneralUtility::makeInstance($initializerClass); | 
            
                                                                        
                            
            
                                    
            
            
                | 154 |  |  |         /** @var $initializer \ApacheSolrForTypo3\Solr\IndexQueue\Initializer\AbstractInitializer */ | 
            
                                                                        
                            
            
                                    
            
            
                | 155 | 6 |  |         $initializer->setSite($site); | 
            
                                                                        
                            
            
                                    
            
            
                | 156 | 6 |  |         $initializer->setType($tableToIndex); | 
            
                                                                        
                            
            
                                    
            
            
                | 157 | 6 |  |         $initializer->setIndexingConfigurationName($indexingConfigurationName); | 
            
                                                                        
                            
            
                                    
            
            
                | 158 | 6 |  |         $initializer->setIndexingConfiguration($indexConfiguration); | 
            
                                                                        
                            
            
                                    
            
            
                | 159 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 160 | 6 |  |         return $initializer->initialize(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 163 |  |  | } |