Passed
Push — master ( 02f8b8...776b21 )
by Timo
22:25
created

IndexQueueWorkerTask::getForcedWebRoot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

206
        $indexService = GeneralUtility::makeInstance(IndexService::class, /** @scrutinizer ignore-type */ $site);
Loading history...
207 1
        $indexService->setContextTask($this);
208 1
        return $indexService;
209
    }
210
}
211