Passed
Pull Request — master (#2666)
by Jens
35:27
created

OptimizeIndexTask::execute()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 16
ccs 0
cts 16
cp 0
rs 9.9
cc 4
nc 4
nop 0
crap 20
1
<?php
2
3
namespace ApacheSolrForTypo3\Solr\Task;
4
5
/***************************************************************
6
 *  Copyright notice
7
 *
8
 *  (c) 2020 Jens Jacobsen <[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 TYPO3\CMS\Core\Utility\GeneralUtility;
30
31
/**
32
 * Scheduler task to empty the indexes of a site and re-initialize the
33
 * Solr Index Queue thus making the indexer re-index the site.
34
 *
35
 * @author Jens Jacobsen <[email protected]>
36
 */
37
class OptimizeIndexTask extends AbstractSolrTask
38
{
39
    /**
40
     * Default language file of the extension linkvalidator
41
     *
42
     * @var string
43
     */
44
    protected $languageFile = 'LLL:EXT:solr/Resources/Private/Language/locallang.xlf';
45
46
    /**
47
     * Cores to optimize.
48
     *
49
     * @var array
50
     */
51
    protected $coresToOptimizeIndex = [];
52
53
    /**
54
     * Optimizes all Solr indexes for selected cores and returns TRUE if the execution was successful
55
     *
56
     * @return bool Returns TRUE on success, FALSE on failure.
57
     */
58
    public function execute()
59
    {
60
        $optimizeResult = true;
61
        $solrServers = GeneralUtility::makeInstance(ConnectionManager::class)->getConnectionsBySite($this->getSite());
62
        foreach ($solrServers as $solrServer) {
63
            $writeService = $solrServer->getWriteService();
64
            $corePath = $writeService->getCorePath();
65
            if (!in_array($corePath, $this->coresToOptimizeIndex)) {
66
                continue;
67
            }
68
            $result = $writeService->optimizeIndex();
69
            if ($result->getResponse()->getStatusCode() != 200) {
70
                $optimizeResult = false;
71
            }
72
        }
73
        return $optimizeResult;
74
    }
75
76
    /**
77
     * Gets the cores to optimize.
78
     *
79
     * @return array
80
     */
81
    public function getCoresToOptimizeIndex(): array
82
    {
83
        return $this->coresToOptimizeIndex;
84
    }
85
86
    /**
87
     * Sets the cores to optimize.
88
     *
89
     * @param array $coresToOptimizeIndex
90
     */
91
    public function setCoresToOptimizeIndex(array $coresToOptimizeIndex): void
92
    {
93
        $this->coresToOptimizeIndex = $coresToOptimizeIndex;
94
    }
95
96
    /**
97
     * This method is designed to return some additional information about the task,
98
     * that may help to set it apart from other tasks from the same class
99
     * This additional information is used - for example - in the Scheduler's BE module
100
     * This method should be implemented in most task classes
101
     *
102
     * @return string Information to display
103
     */
104 1
    public function getAdditionalInformation()
105
    {
106 1
        $site = $this->getSite();
107 1
        if (is_null($site)) {
108 1
            return 'Invalid site configuration for scheduler please re-create the task!';
109
        }
110
111
        $information = 'Site: ' . $this->getSite()->getLabel();
112
        if (!empty($this->coresToOptimizeIndex)) {
113
            $information .= PHP_EOL . 'Corepaths: ' . implode(', ', $this->coresToOptimizeIndex);
114
        }
115
        return $information;
116
    }
117
}
118