Passed
Push — master ( 0c15ec...cd3ff1 )
by Timo
16:18
created

ReIndexTask::getSite()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
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
    public function execute()
56
    {
57
        // clean up
58
        $cleanUpResult = $this->cleanUpIndex();
59
60
        // initialize for re-indexing
61
        $indexQueue = GeneralUtility::makeInstance(Queue::class);
62
        $indexQueueInitializationResults = [];
63
        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 2
    /**
71 2
     * Removes documents of the selected types from the index.
72 2
     *
73 2
     * @return bool TRUE if clean up was successful, FALSE on error
74
     */
75
    protected function cleanUpIndex()
76
    {
77 2
        $cleanUpResult = true;
78
        $solrConfiguration = $this->getSite()->getSolrConfiguration();
79
        $solrServers = GeneralUtility::makeInstance(ConnectionManager::class)->getConnectionsBySite($this->getSite());
80
        $typesToCleanUp = [];
81
        $enableCommitsSetting = $solrConfiguration->getEnableCommits();
82
83
        foreach ($this->indexingConfigurationsToReIndex as $indexingConfigurationName) {
84
            $type = $solrConfiguration->getIndexQueueTableNameOrFallbackToConfigurationName($indexingConfigurationName);
85
            $typesToCleanUp[] = $type;
86 2
        }
87
88 2
        foreach ($solrServers as $solrServer) {
89 2
            $deleteQuery = 'type:(' . implode(' OR ', $typesToCleanUp) . ')' . ' AND siteHash:' . $this->getSite()->getSiteHash();
90 2
            $solrServer->deleteByQuery($deleteQuery);
91 2
92 2
            if (!$enableCommitsSetting) {
93
                # Do not commit
94 2
                continue;
95 2
            }
96 2
97
            $response = $solrServer->commit(false, false, false);
98
            if ($response->getHttpStatus() != 200) {
99 2
                $cleanUpResult = false;
100 2
                break;
101 2
            }
102 2
        }
103
104 2
        return $cleanUpResult;
105
    }
106
107
    /**
108
     * Gets the indexing configurations to re-index.
109 2
     *
110 2
     * @return array
111
     */
112 2
    public function getIndexingConfigurationsToReIndex()
113
    {
114
        return $this->indexingConfigurationsToReIndex;
115
    }
116 2
117
    /**
118
     * Sets the indexing configurations to re-index.
119
     *
120
     * @param array $indexingConfigurationsToReIndex
121
     */
122
    public function setIndexingConfigurationsToReIndex(array $indexingConfigurationsToReIndex)
123
    {
124
        $this->indexingConfigurationsToReIndex = $indexingConfigurationsToReIndex;
125
    }
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 3
     */
135
    public function getAdditionalInformation()
136 3
    {
137 3
        $information = '';
138
139
        if ($this->getSite()) {
140
            $information = 'Site: ' . $this->getSite()->getLabel();
141
        }
142
143
        if (!empty($this->indexingConfigurationsToReIndex)) {
144
            $information .= ', Indexing Configurations: ' . implode(', ',
145
                    $this->indexingConfigurationsToReIndex);
146
        }
147
148
        return $information;
149
    }
150
}
151