Passed
Push — master ( 42d2d3...8f9ec7 )
by Timo
69:32 queued 48:37
created

ReIndexTask   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 90%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 2
dl 0
loc 112
ccs 36
cts 40
cp 0.9
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 14 3
B cleanUpIndex() 0 31 5
A getIndexingConfigurationsToReIndex() 0 4 1
A setIndexingConfigurationsToReIndex() 0 4 1
A getAdditionalInformation() 0 15 3
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Task;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2011-2015 Christoph Moeller <[email protected]>
8
 *  (c) 2012-2015 Ingo Renner <[email protected]>
9
 *
10
 *  All rights reserved
11
 *
12
 *  This script is part of the TYPO3 project. The TYPO3 project is
13
 *  free software; you can redistribute it and/or modify
14
 *  it under the terms of the GNU General Public License as published by
15
 *  the Free Software Foundation; either version 2 of the License, or
16
 *  (at your option) any later version.
17
 *
18
 *  The GNU General Public License can be found at
19
 *  http://www.gnu.org/copyleft/gpl.html.
20
 *
21
 *  This script is distributed in the hope that it will be useful,
22
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 *  GNU General Public License for more details.
25
 *
26
 *  This copyright notice MUST APPEAR in all copies of the script!
27
 ***************************************************************/
28
29
use ApacheSolrForTypo3\Solr\ConnectionManager;
30
use ApacheSolrForTypo3\Solr\IndexQueue\Queue;
31
use TYPO3\CMS\Core\Utility\GeneralUtility;
32
33
/**
34
 * Scheduler task to empty the indexes of a site and re-initialize the
35
 * Solr Index Queue thus making the indexer re-index the site.
36
 *
37
 * @author Christoph Moeller <[email protected]>
38
 */
39
class ReIndexTask extends AbstractSolrTask
40
{
41
42
    /**
43
     * Indexing configurations to re-initialize.
44
     *
45
     * @var array
46
     */
47
    protected $indexingConfigurationsToReIndex = [];
48
49
    /**
50
     * Purges/commits all Solr indexes, initializes the Index Queue
51
     * and returns TRUE if the execution was successful
52
     *
53
     * @return bool Returns TRUE on success, FALSE on failure.
54
     */
55 2
    public function execute()
56
    {
57
        // clean up
58 2
        $cleanUpResult = $this->cleanUpIndex();
59
60
        // initialize for re-indexing
61 2
        $indexQueue = GeneralUtility::makeInstance(Queue::class);
62 2
        $indexQueueInitializationResults = [];
63 2
        foreach ($this->indexingConfigurationsToReIndex as $indexingConfigurationName) {
64 2
            $indexQueueInitializationResults = $indexQueue->initialize($this->getSite(), $indexingConfigurationName);
65
        }
66
67 2
        return ($cleanUpResult && !in_array(false, $indexQueueInitializationResults));
68
    }
69
70
    /**
71
     * Removes documents of the selected types from the index.
72
     *
73
     * @return bool TRUE if clean up was successful, FALSE on error
74
     */
75 2
    protected function cleanUpIndex()
76
    {
77 2
        $cleanUpResult = true;
78 2
        $solrConfiguration = $this->getSite()->getSolrConfiguration();
79 2
        $solrServers = GeneralUtility::makeInstance(ConnectionManager::class)->getConnectionsBySite($this->getSite());
80 2
        $typesToCleanUp = [];
81 2
        $enableCommitsSetting = $solrConfiguration->getEnableCommits();
82
83 2
        foreach ($this->indexingConfigurationsToReIndex as $indexingConfigurationName) {
84 2
            $type = $solrConfiguration->getIndexQueueTableNameOrFallbackToConfigurationName($indexingConfigurationName);
85 2
            $typesToCleanUp[] = $type;
86
        }
87
88 2
        foreach ($solrServers as $solrServer) {
89 2
            $deleteQuery = 'type:(' . implode(' OR ', $typesToCleanUp) . ')' . ' AND siteHash:' . $this->getSite()->getSiteHash();
90 2
            $solrServer->getWriteService()->deleteByQuery($deleteQuery);
91
92 2
            if (!$enableCommitsSetting) {
93
                # Do not commit
94
                continue;
95
            }
96
97 2
            $response = $solrServer->getWriteService()->commit(false, false, false);
98 2
            if ($response->getHttpStatus() != 200) {
99
                $cleanUpResult = false;
100 2
                break;
101
            }
102
        }
103
104 2
        return $cleanUpResult;
105
    }
106
107
    /**
108
     * Gets the indexing configurations to re-index.
109
     *
110
     * @return array
111
     */
112
    public function getIndexingConfigurationsToReIndex()
113
    {
114
        return $this->indexingConfigurationsToReIndex;
115
    }
116
117
    /**
118
     * Sets the indexing configurations to re-index.
119
     *
120
     * @param array $indexingConfigurationsToReIndex
121
     */
122 3
    public function setIndexingConfigurationsToReIndex(array $indexingConfigurationsToReIndex)
123
    {
124 3
        $this->indexingConfigurationsToReIndex = $indexingConfigurationsToReIndex;
125 3
    }
126
127
    /**
128
     * This method is designed to return some additional information about the task,
129
     * that may help to set it apart from other tasks from the same class
130
     * This additional information is used - for example - in the Scheduler's BE module
131
     * This method should be implemented in most task classes
132
     *
133
     * @return string Information to display
134
     */
135 2
    public function getAdditionalInformation()
136
    {
137 2
        $site = $this->getSite();
138 2
        if (is_null($site)) {
139 1
            return 'Invalid site configuration for scheduler please re-create the task!';
140
        }
141
142 1
        $information = 'Site: ' . $this->getSite()->getLabel();
143 1
        if (!empty($this->indexingConfigurationsToReIndex)) {
144 1
            $information .= ', Indexing Configurations: ' . implode(', ',
145 1
                    $this->indexingConfigurationsToReIndex);
146
        }
147
148 1
        return $information;
149
    }
150
}
151