Failed Conditions
Push — master ( 3056fe...4bd1a9 )
by Timo
26:15
created

ReIndexTask::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
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 3 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
            /** @var Queue $indexQueue */
62 2
        $indexQueue = GeneralUtility::makeInstance(Queue::class);
63 2
        $indexQueueInitializationResults = $indexQueue->getInitializationService()
64 2
            ->initializeBySiteAndIndexConfigurations($this->getSite(), $this->indexingConfigurationsToReIndex);
65
66 2
        return ($cleanUpResult && !in_array(false, $indexQueueInitializationResults));
67
    }
68
69
    /**
70
     * Removes documents of the selected types from the index.
71
     *
72
     * @return bool TRUE if clean up was successful, FALSE on error
73
     */
74 2
    protected function cleanUpIndex()
75
    {
76 2
        $cleanUpResult = true;
77 2
        $solrConfiguration = $this->getSite()->getSolrConfiguration();
78 2
        $solrServers = GeneralUtility::makeInstance(ConnectionManager::class)->getConnectionsBySite($this->getSite());
79 2
        $typesToCleanUp = [];
80 2
        $enableCommitsSetting = $solrConfiguration->getEnableCommits();
81
82 2
        foreach ($this->indexingConfigurationsToReIndex as $indexingConfigurationName) {
83 2
            $type = $solrConfiguration->getIndexQueueTableNameOrFallbackToConfigurationName($indexingConfigurationName);
84 2
            $typesToCleanUp[] = $type;
85
        }
86
87 2
        foreach ($solrServers as $solrServer) {
88 2
            $deleteQuery = 'type:(' . implode(' OR ', $typesToCleanUp) . ')' . ' AND siteHash:' . $this->getSite()->getSiteHash();
89 2
            $solrServer->getWriteService()->deleteByQuery($deleteQuery);
90
91 2
            if (!$enableCommitsSetting) {
92
                # Do not commit
93
                continue;
94
            }
95
96 2
            $response = $solrServer->getWriteService()->commit(false, false, false);
97 2
            if ($response->getHttpStatus() != 200) {
98
                $cleanUpResult = false;
99 2
                break;
100
            }
101
        }
102
103 2
        return $cleanUpResult;
104
    }
105
106
    /**
107
     * Gets the indexing configurations to re-index.
108
     *
109
     * @return array
110
     */
111
    public function getIndexingConfigurationsToReIndex()
112
    {
113
        return $this->indexingConfigurationsToReIndex;
114
    }
115
116
    /**
117
     * Sets the indexing configurations to re-index.
118
     *
119
     * @param array $indexingConfigurationsToReIndex
120
     */
121 3
    public function setIndexingConfigurationsToReIndex(array $indexingConfigurationsToReIndex)
122
    {
123 3
        $this->indexingConfigurationsToReIndex = $indexingConfigurationsToReIndex;
124 3
    }
125
126
    /**
127
     * This method is designed to return some additional information about the task,
128
     * that may help to set it apart from other tasks from the same class
129
     * This additional information is used - for example - in the Scheduler's BE module
130
     * This method should be implemented in most task classes
131
     *
132
     * @return string Information to display
133
     */
134 2
    public function getAdditionalInformation()
135
    {
136 2
        $site = $this->getSite();
137 2
        if (is_null($site)) {
138 1
            return 'Invalid site configuration for scheduler please re-create the task!';
139
        }
140
141 1
        $information = 'Site: ' . $this->getSite()->getLabel();
142 1
        if (!empty($this->indexingConfigurationsToReIndex)) {
143 1
            $information .= ', Indexing Configurations: ' . implode(', ',
144 1
                    $this->indexingConfigurationsToReIndex);
145
        }
146
147 1
        return $information;
148
    }
149
}
150