Completed
Push — master ( d53d83...c35e42 )
by
unknown
14:48
created

IndexQueueWorkerTask   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 178
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 84.62%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 19
c 3
b 1
f 0
lcom 1
cbo 4
dl 0
loc 178
ccs 44
cts 52
cp 0.8462
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
B execute() 0 25 5
A getWebRoot() 0 10 2
A replaceWebRootMarkers() 0 12 3
A getAdditionalInformation() 0 17 2
A getProgress() 0 8 1
A getSite() 0 4 1
A setSite() 0 4 1
A getDocumentsToIndexLimit() 0 4 1
A setDocumentsToIndexLimit() 0 4 1
A setForcedWebRoot() 0 4 1
A getForcedWebRoot() 0 4 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
        }
77
78 1
        $indexService = GeneralUtility::makeInstance(IndexService::class, $this->site);
79 1
        $indexService->setContextTask($this);
80 1
        $indexService->indexItems($this->documentsToIndexLimit);
81
82 1
        if (TYPO3_REQUESTTYPE && TYPO3_REQUESTTYPE_CLI) {
83 1
            $cliEnvironment->restore();
84
        }
85
86 1
        $executionSucceeded = true;
87
88 1
        return $executionSucceeded;
89
    }
90
91
    /**
92
     * In the cli context TYPO3 has chance to determine the webroot.
93
     * Since we need it for the TSFE related things we allow to set it
94
     * in the scheduler task and use the ###PATH_typo3### marker in the
95
     * setting to be able to define relative paths.
96
     *
97
     * @return string
98
     */
99 3
    public function getWebRoot()
100
    {
101 3
        if ($this->forcedWebRoot !== '') {
102 1
            return $this->replaceWebRootMarkers($this->forcedWebRoot);
103
        }
104
105
        // when nothing is configured, we use the constant PATH_site
106
        // which should fit in the most cases
107 3
        return PATH_site;
108
    }
109
110
    /**
111
     * @param string $webRoot
112
     * @return string
113
     */
114 1
    protected function replaceWebRootMarkers($webRoot)
115
    {
116 1
        if (strpos($webRoot, '###PATH_typo3###') !== false) {
117
            $webRoot = str_replace('###PATH_typo3###', PATH_typo3, $webRoot);
118
        }
119
120 1
        if (strpos($webRoot, '###PATH_site###') !== false) {
121 1
            $webRoot = str_replace('###PATH_site###', PATH_site, $webRoot);
122
        }
123
124 1
        return $webRoot;
125
    }
126
127
    /**
128
     * Returns some additional information about indexing progress, shown in
129
     * the scheduler's task overview list.
130
     *
131
     * @return string Information to display
132
     */
133 1
    public function getAdditionalInformation()
134
    {
135 1
        $message = 'Site: ' . $this->site->getLabel();
136
137 1
        $failedItemsCount = $GLOBALS['TYPO3_DB']->exec_SELECTcountRows(
138 1
            'uid',
139 1
            'tx_solr_indexqueue_item',
140 1
            'root = ' . $this->site->getRootPageId() . ' AND errors != \'\''
141
        );
142 1
        if ($failedItemsCount) {
143
            $message .= ' Failures: ' . $failedItemsCount;
144
        }
145
146 1
        $message .=  ' / Using webroot: ' . htmlspecialchars($this->getWebRoot());
147
148 1
        return $message;
149
    }
150
151
    /**
152
     * Gets the indexing progress.
153
     *
154
     * @return float Indexing progress as a two decimal precision float. f.e. 44.87
155
     */
156 1
    public function getProgress()
157
    {
158
        /** @var $indexService \ApacheSolrForTypo3\Solr\Domain\Index\IndexService */
159 1
        $indexService = GeneralUtility::makeInstance(IndexService::class, $this->site);
160 1
        $indexService->setContextTask($this);
161
162 1
        return $indexService->getProgress();
163
    }
164
165
    /**
166
     * Gets the site / the site's root page uid this task is indexing.
167
     *
168
     * @return Site The site's root page uid this task is indexing
169
     */
170
    public function getSite()
171
    {
172
        return $this->site;
173
    }
174
175
    /**
176
     * Sets the task's site to indexing.
177
     *
178
     * @param Site $site The site to index by this task
179
     * @return void
180
     */
181 2
    public function setSite(Site $site)
182
    {
183 2
        $this->site = $site;
184 2
    }
185
186
    /**
187
     * @return mixed
188
     */
189
    public function getDocumentsToIndexLimit()
190
    {
191
        return $this->documentsToIndexLimit;
192
    }
193
194
    /**
195
     * @param int $limit
196
     */
197 2
    public function setDocumentsToIndexLimit($limit)
198
    {
199 2
        $this->documentsToIndexLimit = $limit;
200 2
    }
201
202
    /**
203
     * @param string $forcedWebRoot
204
     */
205 1
    public function setForcedWebRoot($forcedWebRoot)
206
    {
207 1
        $this->forcedWebRoot = $forcedWebRoot;
208 1
    }
209
210
    /**
211
     * @return string
212
     */
213
    public function getForcedWebRoot()
214
    {
215
        return $this->forcedWebRoot;
216
    }
217
}
218