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

IndexQueueWorkerTask::setSite()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Task;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2009-2015 Ingo Renner <[email protected]>
8
 *  All rights reserved
9
 *
10
 *  This script is part of the TYPO3 project. The TYPO3 project is
11
 *  free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 2 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  The GNU General Public License can be found at
17
 *  http://www.gnu.org/copyleft/gpl.html.
18
 *
19
 *  This script is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  This copyright notice MUST APPEAR in all copies of the script!
25
 ***************************************************************/
26
27
use ApacheSolrForTypo3\Solr\Domain\Index\IndexService;
28
use ApacheSolrForTypo3\Solr\System\Environment\CliEnvironment;
29
use TYPO3\CMS\Core\Utility\GeneralUtility;
30
use TYPO3\CMS\Scheduler\ProgressProviderInterface;
31
32
/**
33
 * A worker indexing the items in the index queue. Needs to be set up as one
34
 * task per root page.
35
 *
36
 * @author Ingo Renner <[email protected]>
37
 */
38
class IndexQueueWorkerTask extends AbstractSolrTask implements ProgressProviderInterface
39
{
40
    /**
41
     * @var int
42
     */
43
    protected $documentsToIndexLimit;
44
45
    /**
46
     * @var string
47
     */
48
    protected $forcedWebRoot = '';
49
50
    /**
51
     * Works through the indexing queue and indexes the queued items into Solr.
52
     *
53
     * @return bool Returns TRUE on success, FALSE if no items were indexed or none were found.
54
     */
55
    public function execute()
56
    {
57
        $cliEnvironment = null;
58
59
        // Wrapped the CliEnvironment to avoid defining TYPO3_PATH_WEB since this
60
        // should only be done in the case when running it from outside TYPO3 BE
61
        // @see #921 and #934 on https://github.com/TYPO3-Solr
62
        if (TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_CLI) {
63
            $cliEnvironment = GeneralUtility::makeInstance(CliEnvironment::class);
64
            $cliEnvironment->backup();
65 1
            $cliEnvironment->initialize($this->getWebRoot());
66
        }
67 1
68
        $indexService = $this->getInitializedIndexService();
69
        $indexService->indexItems($this->documentsToIndexLimit);
70
71
        if (TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_CLI) {
72 1
            $cliEnvironment->restore();
73 1
        }
74 1
75 1
        $executionSucceeded = true;
76
77
        return $executionSucceeded;
78 1
    }
79 1
80
    /**
81 1
     * In the cli context TYPO3 has chance to determine the webroot.
82 1
     * Since we need it for the TSFE related things we allow to set it
83
     * in the scheduler task and use the ###PATH_typo3### marker in the
84
     * setting to be able to define relative paths.
85 1
     *
86
     * @return string
87 1
     */
88
    public function getWebRoot()
89
    {
90
        if ($this->forcedWebRoot !== '') {
91
            return $this->replaceWebRootMarkers($this->forcedWebRoot);
92
        }
93
94
        // when nothing is configured, we use the constant PATH_site
95
        // which should fit in the most cases
96
        return PATH_site;
97
    }
98 3
99
    /**
100 3
     * @param string $webRoot
101 1
     * @return string
102
     */
103
    protected function replaceWebRootMarkers($webRoot)
104
    {
105
        if (strpos($webRoot, '###PATH_typo3###') !== false) {
106 3
            $webRoot = str_replace('###PATH_typo3###', PATH_typo3, $webRoot);
107
        }
108
109
        if (strpos($webRoot, '###PATH_site###') !== false) {
110
            $webRoot = str_replace('###PATH_site###', PATH_site, $webRoot);
111
        }
112
113 1
        return $webRoot;
114
    }
115 1
116
    /**
117
     * Returns some additional information about indexing progress, shown in
118
     * the scheduler's task overview list.
119 1
     *
120 1
     * @return string Information to display
121
     */
122
    public function getAdditionalInformation()
123 1
    {
124
        $message = 'Site: ' . $this->getSite()->getLabel();
125
126
        /** @var $indexService \ApacheSolrForTypo3\Solr\Domain\Index\IndexService */
127
        $indexService = $this->getInitializedIndexService();
128
        $failedItemsCount = $indexService->getFailCount();
129
130
        if ($failedItemsCount) {
131
            $message .= ' Failures: ' . $failedItemsCount;
132 1
        }
133
134 1
        $message .= ' / Using webroot: ' . htmlspecialchars($this->getWebRoot());
135
136
        return $message;
137 1
    }
138 1
139
    /**
140 1
     * Gets the indexing progress.
141
     *
142
     * @return float Indexing progress as a two decimal precision float. f.e. 44.87
143
     */
144 1
    public function getProgress()
145
    {
146 1
        /** @var $indexService \ApacheSolrForTypo3\Solr\Domain\Index\IndexService */
147
        $indexService = $this->getInitializedIndexService();
148
149
        return $indexService->getProgress();
150
    }
151
152
    /**
153
     * @return mixed
154 1
     */
155
    public function getDocumentsToIndexLimit()
156
    {
157 1
        return $this->documentsToIndexLimit;
158
    }
159 1
160
    /**
161
     * @param int $limit
162
     */
163
    public function setDocumentsToIndexLimit($limit)
164
    {
165
        $this->documentsToIndexLimit = $limit;
166
    }
167
168
    /**
169
     * @param string $forcedWebRoot
170
     */
171
    public function setForcedWebRoot($forcedWebRoot)
172
    {
173
        $this->forcedWebRoot = $forcedWebRoot;
174
    }
175
176
    /**
177
     * @return string
178 2
     */
179
    public function getForcedWebRoot()
180 2
    {
181 2
        return $this->forcedWebRoot;
182
    }
183
184
    /**
185
     * Returns the initialize IndexService instance.
186
     *
187
     * @return IndexService
188
     */
189
    protected function getInitializedIndexService()
190
    {
191
        $indexService = GeneralUtility::makeInstance(IndexService::class, $this->getSite());
192
        $indexService->setContextTask($this);
193
        return $indexService;
194 2
    }
195
}
196