Passed
Push — master ( 202215...3acdae )
by Timo
57s
created

IndexQueueWorkerTask::getProgress()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
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\Site;
29
use ApacheSolrForTypo3\Solr\System\Environment\CliEnvironment;
30
use TYPO3\CMS\Core\Utility\GeneralUtility;
31
use TYPO3\CMS\Scheduler\ProgressProviderInterface;
32
use TYPO3\CMS\Scheduler\Task\AbstractTask;
33
34
/**
35
 * A worker indexing the items in the index queue. Needs to be set up as one
36
 * task per root page.
37
 *
38
 * @author Ingo Renner <[email protected]>
39
 */
40
class IndexQueueWorkerTask extends AbstractTask implements ProgressProviderInterface
41
{
42
43
    /**
44
     * The site this task is indexing.
45
     *
46
     * @var Site
47
     */
48
    protected $site;
49
50
    /**
51
     * @var int
52
     */
53
    protected $documentsToIndexLimit;
54
55
    /**
56
     * @var string
57
     */
58
    protected $forcedWebRoot = '';
59
60
    /**
61
     * Works through the indexing queue and indexes the queued items into Solr.
62
     *
63
     * @return bool Returns TRUE on success, FALSE if no items were indexed or none were found.
64
     */
65 1
    public function execute()
66
    {
67 1
        $cliEnvironment = null;
68
69
        // Wrapped the CliEnvironment to avoid defining TYPO3_PATH_WEB since this
70
        // should only be done in the case when running it from outside TYPO3 BE
71
        // @see #921 and #934 on https://github.com/TYPO3-Solr
72 1
        if (TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_CLI) {
73 1
            $cliEnvironment = GeneralUtility::makeInstance(CliEnvironment::class);
74 1
            $cliEnvironment->backup();
75 1
            $cliEnvironment->initialize($this->getWebRoot());
76 1
        }
77
78 1
        $indexService = $this->getInitializedIndexService();
79 1
        $indexService->indexItems($this->documentsToIndexLimit);
80
81 1
        if (TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_CLI) {
82 1
            $cliEnvironment->restore();
83 1
        }
84
85 1
        $executionSucceeded = true;
86
87 1
        return $executionSucceeded;
88
    }
89
90
    /**
91
     * In the cli context TYPO3 has chance to determine the webroot.
92
     * Since we need it for the TSFE related things we allow to set it
93
     * in the scheduler task and use the ###PATH_typo3### marker in the
94
     * setting to be able to define relative paths.
95
     *
96
     * @return string
97
     */
98 3
    public function getWebRoot()
99
    {
100 3
        if ($this->forcedWebRoot !== '') {
101 1
            return $this->replaceWebRootMarkers($this->forcedWebRoot);
102
        }
103
104
        // when nothing is configured, we use the constant PATH_site
105
        // which should fit in the most cases
106 3
        return PATH_site;
107
    }
108
109
    /**
110
     * @param string $webRoot
111
     * @return string
112
     */
113 1
    protected function replaceWebRootMarkers($webRoot)
114
    {
115 1
        if (strpos($webRoot, '###PATH_typo3###') !== false) {
116
            $webRoot = str_replace('###PATH_typo3###', PATH_typo3, $webRoot);
117
        }
118
119 1
        if (strpos($webRoot, '###PATH_site###') !== false) {
120 1
            $webRoot = str_replace('###PATH_site###', PATH_site, $webRoot);
121 1
        }
122
123 1
        return $webRoot;
124
    }
125
126
    /**
127
     * Returns some additional information about indexing progress, shown in
128
     * the scheduler's task overview list.
129
     *
130
     * @return string Information to display
131
     */
132 1
    public function getAdditionalInformation()
133
    {
134 1
        $message = 'Site: ' . $this->site->getLabel();
135
136
        /** @var $indexService \ApacheSolrForTypo3\Solr\Domain\Index\IndexService */
137 1
        $indexService = $this->getInitializedIndexService();
138 1
        $failedItemsCount = $indexService->getFailCount();
139
140 1
        if ($failedItemsCount) {
141
            $message .= ' Failures: ' . $failedItemsCount;
142
        }
143
144 1
        $message .= ' / Using webroot: ' . htmlspecialchars($this->getWebRoot());
145
146 1
        return $message;
147
    }
148
149
    /**
150
     * Gets the indexing progress.
151
     *
152
     * @return float Indexing progress as a two decimal precision float. f.e. 44.87
153
     */
154 1
    public function getProgress()
155
    {
156
        /** @var $indexService \ApacheSolrForTypo3\Solr\Domain\Index\IndexService */
157 1
        $indexService = $this->getInitializedIndexService();
158
159 1
        return $indexService->getProgress();
160
    }
161
162
    /**
163
     * Gets the site / the site's root page uid this task is indexing.
164
     *
165
     * @return Site The site's root page uid this task is indexing
166
     */
167
    public function getSite()
168
    {
169
        return $this->site;
170
    }
171
172
    /**
173
     * Sets the task's site to indexing.
174
     *
175
     * @param Site $site The site to index by this task
176
     * @return void
177
     */
178 2
    public function setSite(Site $site)
179
    {
180 2
        $this->site = $site;
181 2
    }
182
183
    /**
184
     * @return mixed
185
     */
186
    public function getDocumentsToIndexLimit()
187
    {
188
        return $this->documentsToIndexLimit;
189
    }
190
191
    /**
192
     * @param int $limit
193
     */
194 2
    public function setDocumentsToIndexLimit($limit)
195
    {
196 2
        $this->documentsToIndexLimit = $limit;
197 2
    }
198
199
    /**
200
     * @param string $forcedWebRoot
201
     */
202 1
    public function setForcedWebRoot($forcedWebRoot)
203
    {
204 1
        $this->forcedWebRoot = $forcedWebRoot;
205 1
    }
206
207
    /**
208
     * @return string
209
     */
210
    public function getForcedWebRoot()
211
    {
212
        return $this->forcedWebRoot;
213
    }
214
215
    /**
216
     * Returns the initialize IndexService instance.
217
     *
218
     * @return IndexService
219
     */
220 2
    protected function getInitializedIndexService()
221
    {
222 2
        $indexService = GeneralUtility::makeInstance(IndexService::class, $this->site);
223 2
        $indexService->setContextTask($this);
224 2
        return $indexService;
225
    }
226
}
227