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\Backend\SiteSelectorField; |
28
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Site\SiteRepository; |
29
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
30
|
|
|
use TYPO3\CMS\Scheduler\AdditionalFieldProviderInterface; |
31
|
|
|
use TYPO3\CMS\Scheduler\Controller\SchedulerModuleController; |
32
|
|
|
use TYPO3\CMS\Scheduler\Task\AbstractTask; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Additional field provider for the index queue worker task |
36
|
|
|
* |
37
|
|
|
* @author Ingo Renner <[email protected]> |
38
|
|
|
*/ |
39
|
|
|
class IndexQueueWorkerTaskAdditionalFieldProvider implements AdditionalFieldProviderInterface |
40
|
|
|
{ |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* SiteRepository |
44
|
|
|
* |
45
|
|
|
* @var SiteRepository |
46
|
|
|
*/ |
47
|
|
|
protected $siteRepository; |
48
|
|
|
|
49
|
|
|
public function __construct() |
50
|
|
|
{ |
51
|
|
|
$this->siteRepository = GeneralUtility::makeInstance(SiteRepository::class); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Used to define fields to provide the TYPO3 site to index and number of |
56
|
|
|
* items to index per run when adding or editing a task. |
57
|
|
|
* |
58
|
|
|
* @param array $taskInfo reference to the array containing the info used in the add/edit form |
59
|
|
|
* @param AbstractTask $task when editing, reference to the current task object. Null when adding. |
60
|
|
|
* @param SchedulerModuleController $schedulerModule : reference to the calling object (Scheduler's BE module) |
61
|
|
|
* @return array Array containing all the information pertaining to the additional fields |
62
|
|
|
* The array is multidimensional, keyed to the task class name and each field's id |
63
|
|
|
* For each field it provides an associative sub-array with the following: |
64
|
|
|
*/ |
65
|
|
|
public function getAdditionalFields( |
66
|
|
|
array &$taskInfo, |
67
|
|
|
$task, |
68
|
|
|
SchedulerModuleController $schedulerModule |
69
|
|
|
) { |
70
|
|
|
$additionalFields = []; |
71
|
|
|
$siteSelectorField = GeneralUtility::makeInstance(SiteSelectorField::class); |
72
|
|
|
|
73
|
|
|
if (!$this->isTaskInstanceofIndexQueueWorkerTask($task)) { |
74
|
|
|
return $additionalFields; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if ($schedulerModule->CMD == 'add') { |
78
|
|
|
$taskInfo['site'] = null; |
79
|
|
|
$taskInfo['documentsToIndexLimit'] = 50; |
80
|
|
|
$taskInfo['forcedWebRoot'] = ''; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if ($schedulerModule->CMD == 'edit') { |
84
|
|
|
$taskInfo['site'] = $task->getSite(); |
|
|
|
|
85
|
|
|
$taskInfo['documentsToIndexLimit'] = $task->getDocumentsToIndexLimit(); |
|
|
|
|
86
|
|
|
$taskInfo['forcedWebRoot'] = $task->getForcedWebRoot(); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$additionalFields['site'] = [ |
90
|
|
|
'code' => $siteSelectorField->getAvailableSitesSelector('tx_scheduler[site]', |
91
|
|
|
$taskInfo['site']), |
92
|
|
|
'label' => 'LLL:EXT:solr/Resources/Private/Language/locallang.xlf:field_site', |
93
|
|
|
'cshKey' => '', |
94
|
|
|
'cshLabel' => '' |
95
|
|
|
]; |
96
|
|
|
|
97
|
|
|
$additionalFields['documentsToIndexLimit'] = [ |
98
|
|
|
'code' => '<input type="number" class="form-control" name="tx_scheduler[documentsToIndexLimit]" value="' . htmlspecialchars($taskInfo['documentsToIndexLimit']) . '" />', |
99
|
|
|
'label' => 'LLL:EXT:solr/Resources/Private/Language/locallang.xlf:indexqueueworker_field_documentsToIndexLimit', |
100
|
|
|
'cshKey' => '', |
101
|
|
|
'cshLabel' => '' |
102
|
|
|
]; |
103
|
|
|
|
104
|
|
|
$additionalFields['forcedWebRoot'] = [ |
105
|
|
|
'code' => '<input type="text" class="form-control" name="tx_scheduler[forcedWebRoot]" value="' . htmlspecialchars($taskInfo['forcedWebRoot']) . '" />', |
106
|
|
|
'label' => 'LLL:EXT:solr/Resources/Private/Language/locallang.xlf:indexqueueworker_field_forcedWebRoot', |
107
|
|
|
'cshKey' => '', |
108
|
|
|
'cshLabel' => '' |
109
|
|
|
]; |
110
|
|
|
|
111
|
|
|
return $additionalFields; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Checks any additional data that is relevant to this task. If the task |
116
|
|
|
* class is not relevant, the method is expected to return TRUE |
117
|
|
|
* |
118
|
|
|
* @param array $submittedData reference to the array containing the data submitted by the user |
119
|
|
|
* @param SchedulerModuleController $schedulerModule reference to the calling object (Scheduler's BE module) |
120
|
|
|
* @return bool True if validation was ok (or selected class is not relevant), FALSE otherwise |
121
|
|
|
*/ |
122
|
|
|
public function validateAdditionalFields( |
123
|
|
|
array &$submittedData, |
124
|
|
|
SchedulerModuleController $schedulerModule |
125
|
|
|
) { |
126
|
|
|
$result = false; |
127
|
|
|
|
128
|
|
|
// validate site |
129
|
|
|
$sites = $this->siteRepository->getAvailableSites(); |
130
|
|
|
if (array_key_exists($submittedData['site'], $sites)) { |
131
|
|
|
$result = true; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
// escape limit |
135
|
|
|
$submittedData['documentsToIndexLimit'] = intval($submittedData['documentsToIndexLimit']); |
136
|
|
|
|
137
|
|
|
return $result; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Saves any additional input into the current task object if the task |
142
|
|
|
* class matches. |
143
|
|
|
* |
144
|
|
|
* @param array $submittedData array containing the data submitted by the user |
145
|
|
|
* @param AbstractTask $task reference to the current task object |
146
|
|
|
*/ |
147
|
|
|
public function saveAdditionalFields( |
148
|
|
|
array $submittedData, |
149
|
|
|
AbstractTask $task |
150
|
|
|
) { |
151
|
|
|
if (!$this->isTaskInstanceofIndexQueueWorkerTask($task)) { |
152
|
|
|
return; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
$task->setSite($this->siteRepository->getSiteByRootPageId($submittedData['site'])); |
|
|
|
|
156
|
|
|
$task->setDocumentsToIndexLimit($submittedData['documentsToIndexLimit']); |
|
|
|
|
157
|
|
|
$task->setForcedWebRoot($submittedData['forcedWebRoot']); |
|
|
|
|
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Check that a task is an instance of IndexQueueWorkerTask |
162
|
|
|
* |
163
|
|
|
* @param AbstractTask $task |
164
|
|
|
* @return boolean |
165
|
|
|
* @throws \LogicException |
166
|
|
|
*/ |
167
|
|
|
protected function isTaskInstanceofIndexQueueWorkerTask($task) |
168
|
|
|
{ |
169
|
|
|
if ((!is_null($task)) && (!($task instanceof IndexQueueWorkerTask))) { |
170
|
|
|
throw new \LogicException( |
171
|
|
|
'$task must be an instance of IndexQueueWorkerTask, ' |
172
|
|
|
.'other instances are not supported.', 1487499814 |
173
|
|
|
); |
174
|
|
|
} |
175
|
|
|
return true; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: