| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace ApacheSolrForTypo3\Solr\Domain\Index\Queue\GarbageRemover; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /*************************************************************** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  *  Copyright notice | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  *  (c) 2018 - Timo Hund <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  *  All rights reserved | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  *  This script is part of the TYPO3 project. The TYPO3 project is | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  *  free software; you can redistribute it and/or modify | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  *  it under the terms of the GNU General Public License as published by | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  *  the Free Software Foundation; either version 3 of the License, or | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  *  (at your option) any later version. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  *  The GNU General Public License can be found at | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  *  http://www.gnu.org/copyleft/gpl.html. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  *  This script is distributed in the hope that it will be useful, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  *  but WITHOUT ANY WARRANTY; without even the implied warranty of | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  *  GNU General Public License for more details. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  *  This copyright notice MUST APPEAR in all copies of the script! | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  ***************************************************************/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | use ApacheSolrForTypo3\Solr\ConnectionManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | use ApacheSolrForTypo3\Solr\GarbageCollectorPostProcessor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | use ApacheSolrForTypo3\Solr\IndexQueue\Queue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | use ApacheSolrForTypo3\Solr\System\Solr\SolrConnection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | use TYPO3\CMS\Core\Utility\GeneralUtility; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  * An implementation ob a garbage remover strategy is responsible to remove all garbage from the index queue and | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  * the solr server for a certain table and uid combination. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  * @package ApacheSolrForTypo3\Solr\Domain\Index\Queue\GarbageRemover | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | abstract class AbstractStrategy { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @var Queue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     protected $queue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @var ConnectionManager | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     protected $connectionManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * AbstractStrategy constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * @param Queue|null $queue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @param ConnectionManager|null $connectionManager | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 11 |  |     public function __construct(Queue $queue = null, ConnectionManager $connectionManager = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 11 |  |         $this->queue = $queue ?? GeneralUtility::makeInstance(Queue::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 11 |  |         $this->connectionManager = $connectionManager ?? GeneralUtility::makeInstance(ConnectionManager::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 11 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * Call's the removal of the strategy and afterwards the garbagecollector post processing hook. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @param string $table | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @param int $uid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 11 |  |     public function removeGarbageOf($table, $uid) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 11 |  |         $this->removeGarbageOfByStrategy($table, $uid); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 11 |  |         $this->callPostProcessGarbageCollectorHook($table, $uid); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 11 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * A implementation of the GarbageCollection strategy is responsible to remove the garbage from | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * the indexqueue and from the solr server. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * @param string $table | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * @param int $uid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     abstract protected function removeGarbageOfByStrategy($table, $uid); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * Deletes a document from solr and from the index queue. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * @param string $table | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * @param integer $uid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 7 |  |     protected function deleteInSolrAndRemoveFromIndexQueue($table, $uid) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 7 |  |         $this->deleteIndexDocuments($table, $uid); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 7 |  |         $this->queue->deleteItem($table, $uid); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 7 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * Deletes a document from solr and updates the item in the index queue (e.g. on page content updates). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * @param string $table | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * @param integer $uid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 4 |  |     protected function deleteInSolrAndUpdateIndexQueue($table, $uid) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 4 |  |         $this->deleteIndexDocuments($table, $uid); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 4 |  |         $this->queue->updateItem($table, $uid); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 4 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * Deletes index documents for a given record identification. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * @param string $table The record's table name. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * @param int $uid The record's uid. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 11 |  |     protected function deleteIndexDocuments($table, $uid) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         // record can be indexed for multiple sites | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 11 |  |         $indexQueueItems = $this->queue->getItems($table, $uid); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 11 |  |         foreach ($indexQueueItems as $indexQueueItem) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 10 |  |             $site = $indexQueueItem->getSite(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 10 |  |             $enableCommitsSetting = $site->getSolrConfiguration()->getEnableCommits(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |             // a site can have multiple connections (cores / languages) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 10 |  |             $solrConnections = $this->connectionManager->getConnectionsBySite($site); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 10 |  |             $this->deleteRecordInAllSolrConnections($table, $uid, $solrConnections, $enableCommitsSetting); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 11 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      * Deletes the record in all solr connections from that site. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      * @param string $table | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      * @param int $uid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      * @param SolrConnection[] $solrConnections | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      * @param boolean $enableCommitsSetting | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 136 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 137 | 10 |  |     protected function deleteRecordInAllSolrConnections($table, $uid, $solrConnections, $enableCommitsSetting) | 
            
                                                                        
                            
            
                                    
            
            
                | 138 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 139 | 10 |  |         foreach ($solrConnections as $solr) { | 
            
                                                                        
                            
            
                                    
            
            
                | 140 | 10 |  |             $solr->getWriteService()->deleteByQuery('type:' . $table . ' AND uid:' . intval($uid)); | 
            
                                                                        
                            
            
                                    
            
            
                | 141 | 10 |  |             if ($enableCommitsSetting) { | 
            
                                                                        
                            
            
                                    
            
            
                | 142 | 10 |  |                 $solr->getWriteService()->commit(false, false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 | 10 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |      * Calls the registered post processing hooks after the garbageCollection. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      * @param string $table | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |      * @param int $uid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 | 11 |  |     protected function callPostProcessGarbageCollectorHook($table, $uid) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 | 11 |  |         if (!is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['postProcessGarbageCollector'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 | 10 |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 | 1 |  |         foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['postProcessGarbageCollector'] as $classReference) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 | 1 |  |             $garbageCollectorPostProcessor = GeneralUtility::makeInstance($classReference); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 | 1 |  |             if ($garbageCollectorPostProcessor instanceof GarbageCollectorPostProcessor) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 | 1 |  |                 $garbageCollectorPostProcessor->postProcessGarbageCollector($table, $uid); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |                 $message = get_class($garbageCollectorPostProcessor) . ' must implement interface ' . | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |                     GarbageCollectorPostProcessor::class; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 | 1 |  |                 throw new \UnexpectedValueException($message, 1345807460); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 170 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 171 |  |  | } |