Completed
Push — master ( c7f02f...d53d83 )
by Timo
14:49
created

ReIndexTask   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 58.2%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 4
dl 0
loc 143
ccs 39
cts 67
cp 0.582
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 16 3
A getSite() 0 4 1
A setSite() 0 4 1
A getIndexingConfigurationsToReIndex() 0 4 1
A setIndexingConfigurationsToReIndex() 0 5 1
A getAdditionalInformation() 0 15 3
B cleanUpIndex() 0 32 5
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 ApacheSolrForTypo3\Solr\Site;
32
use TYPO3\CMS\Core\Utility\GeneralUtility;
33
use TYPO3\CMS\Scheduler\Task\AbstractTask;
34
35
/**
36
 * Scheduler task to empty the indexes of a site and re-initialize the
37
 * Solr Index Queue thus making the indexer re-index the site.
38
 *
39
 * @author Christoph Moeller <[email protected]>
40
 */
41
class ReIndexTask extends AbstractTask
42
{
43
44
    /**
45
     * The site this task is supposed to initialize the index queue for.
46
     *
47
     * @var Site
48
     */
49
    protected $site;
50
51
    /**
52
     * Indexing configurations to re-initialize.
53
     *
54
     * @var array
55
     */
56
    protected $indexingConfigurationsToReIndex = [];
57
58
    /**
59
     * Purges/commits all Solr indexes, initializes the Index Queue
60
     * and returns TRUE if the execution was successful
61
     *
62
     * @return bool Returns TRUE on success, FALSE on failure.
63
     */
64 2
    public function execute()
65
    {
66
        // clean up
67 2
        $cleanUpResult = $this->cleanUpIndex();
68
69
        // initialize for re-indexing
70 2
        $indexQueue = GeneralUtility::makeInstance(Queue::class);
71 2
        $indexQueueInitializationResults = [];
72 2
        foreach ($this->indexingConfigurationsToReIndex as $indexingConfigurationName) {
73 2
            $indexQueueInitializationResults = $indexQueue->initialize($this->site,
74
                $indexingConfigurationName);
75
        }
76
77 2
        return ($cleanUpResult && !in_array(false,
78
                $indexQueueInitializationResults));
79
    }
80
81
    /**
82
     * Removes documents of the selected types from the index.
83
     *
84
     * @return bool TRUE if clean up was successful, FALSE on error
85
     */
86 2
    protected function cleanUpIndex()
87
    {
88 2
        $cleanUpResult = true;
89 2
        $solrConfiguration = $this->site->getSolrConfiguration();
90 2
        $solrServers = GeneralUtility::makeInstance(ConnectionManager::class)->getConnectionsBySite($this->site);
91 2
        $typesToCleanUp = [];
92 2
        $enableCommitsSetting = $solrConfiguration->getEnableCommits();
93
94 2
        foreach ($this->indexingConfigurationsToReIndex as $indexingConfigurationName) {
95 2
            $type = $solrConfiguration->getIndexQueueTableNameOrFallbackToConfigurationName($indexingConfigurationName);
96 2
            $typesToCleanUp[] = $type;
97
        }
98
99 2
        foreach ($solrServers as $solrServer) {
100 2
            $deleteQuery = 'type:(' . implode(' OR ', $typesToCleanUp) . ')'
101 2
                . ' AND siteHash:' . $this->site->getSiteHash();
102 2
            $solrServer->deleteByQuery($deleteQuery);
103
104 2
            if (!$enableCommitsSetting) {
105
                # Do not commit
106
                continue;
107
            }
108
109 2
            $response = $solrServer->commit(false, false, false);
110 2
            if ($response->getHttpStatus() != 200) {
111
                $cleanUpResult = false;
112 2
                break;
113
            }
114
        }
115
116 2
        return $cleanUpResult;
117
    }
118
119
    /**
120
     * Gets the site / the site's root page uid this task is running on.
121
     *
122
     * @return Site The site's root page uid this task is optimizing
123
     */
124
    public function getSite()
125
    {
126
        return $this->site;
127
    }
128
129
    /**
130
     * Sets the task's site.
131
     *
132
     * @param Site $site The site to be handled by this task
133
     */
134 3
    public function setSite(Site $site)
135
    {
136 3
        $this->site = $site;
137 3
    }
138
139
    /**
140
     * Gets the indexing configurations to re-index.
141
     *
142
     * @return array
143
     */
144
    public function getIndexingConfigurationsToReIndex()
145
    {
146
        return $this->indexingConfigurationsToReIndex;
147
    }
148
149
    /**
150
     * Sets the indexing configurations to re-index.
151
     *
152
     * @param array $indexingConfigurationsToReIndex
153
     */
154 3
    public function setIndexingConfigurationsToReIndex(
155
        array $indexingConfigurationsToReIndex
156
    ) {
157 3
        $this->indexingConfigurationsToReIndex = $indexingConfigurationsToReIndex;
158 3
    }
159
160
    /**
161
     * This method is designed to return some additional information about the task,
162
     * that may help to set it apart from other tasks from the same class
163
     * This additional information is used - for example - in the Scheduler's BE module
164
     * This method should be implemented in most task classes
165
     *
166
     * @return string Information to display
167
     */
168 1
    public function getAdditionalInformation()
169
    {
170 1
        $information = '';
171
172 1
        if ($this->site) {
173 1
            $information = 'Site: ' . $this->site->getLabel();
174
        }
175
176 1
        if (!empty($this->indexingConfigurationsToReIndex)) {
177 1
            $information .= ', Indexing Configurations: ' . implode(', ',
178 1
                    $this->indexingConfigurationsToReIndex);
179
        }
180
181 1
        return $information;
182
    }
183
}
184