[TASK] Display info about registered Extractors in Task "Metadata Extract"
[Packages/TYPO3.CMS.git] / typo3 / sysext / scheduler / Classes / Task / FileStorageExtractionAdditionalFieldProvider.php
1 <?php
2 namespace TYPO3\CMS\Scheduler\Task;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Messaging\FlashMessage;
18 use TYPO3\CMS\Core\Resource\Index\ExtractorInterface;
19 use TYPO3\CMS\Core\Resource\Index\ExtractorRegistry;
20 use TYPO3\CMS\Core\Resource\ResourceFactory;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22 use TYPO3\CMS\Core\Utility\MathUtility;
23 use TYPO3\CMS\Scheduler\AdditionalFieldProviderInterface;
24 use TYPO3\CMS\Scheduler\Controller\SchedulerModuleController;
25
26 /**
27 * Additional BE fields for task which extracts metadata from storage
28 *
29 */
30 class FileStorageExtractionAdditionalFieldProvider implements AdditionalFieldProviderInterface {
31
32 /**
33 * Add additional fields
34 *
35 * @param array $taskInfo Reference to the array containing the info used in the add/edit form
36 * @param AbstractTask|NULL $task When editing, reference to the current task. NULL when adding.
37 * @param SchedulerModuleController $parentObject Reference to the calling object (Scheduler's BE module)
38 * @return array Array containing all the information pertaining to the additional fields
39 * @throws \InvalidArgumentException
40 */
41 public function getAdditionalFields(array &$taskInfo, $task, SchedulerModuleController $parentObject) {
42 if ($task !== NULL && !$task instanceof FileStorageExtractionTask) {
43 throw new \InvalidArgumentException('Task not of type FileStorageExtractionTask', 1384275695);
44 }
45 $additionalFields['scheduler_fileStorageIndexing_storage'] = $this->getAllStoragesField($task);
46 $additionalFields['scheduler_fileStorageIndexing_fileCount'] = $this->getFileCountField($task);
47 $additionalFields['scheduler_fileStorageIndexing_registeredExtractors'] = $this->getRegisteredExtractorsField($task);
48 return $additionalFields;
49 }
50
51 /**
52 * Returns a field configuration including a selectbox for available storages
53 *
54 * @param FileStorageExtractionTask $task When editing, reference to the current task object. NULL when adding.
55 * @return array Array containing all the information pertaining to the additional fields
56 */
57 protected function getAllStoragesField(FileStorageExtractionTask $task = NULL) {
58 /** @var \TYPO3\CMS\Core\Resource\ResourceStorage[] $storages */
59 $storages = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\StorageRepository::class)->findAll();
60 $options = array();
61 foreach ($storages as $storage) {
62 if ($task !== NULL && $task->storageUid === $storage->getUid()) {
63 $options[] = '<option value="' . $storage->getUid() . '" selected="selected">' . $storage->getName() . '</option>';
64 } else {
65 $options[] = '<option value="' . $storage->getUid() . '">' . $storage->getName() . '</option>';
66 }
67 }
68
69 $fieldName = 'tx_scheduler[scheduler_fileStorageIndexing_storage]';
70 $fieldId = 'scheduler_fileStorageIndexing_storage';
71 $fieldHtml = '<select class="form-control" name="' . $fieldName . '" id="' . $fieldId . '">' . implode("\n", $options) . '</select>';
72
73 $fieldConfiguration = array(
74 'code' => $fieldHtml,
75 'label' => 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.fileStorageIndexing.storage',
76 'cshKey' => '_MOD_system_txschedulerM1',
77 'cshLabel' => $fieldId
78 );
79 return $fieldConfiguration;
80 }
81
82 /**
83 * Returns a field configuration including a input field for the file count
84 *
85 * @param FileStorageExtractionTask $task When editing, reference to the current task object. NULL when adding.
86 * @return array Array containing all the information pertaining to the additional fields
87 */
88 protected function getFileCountField(FileStorageExtractionTask $task = NULL) {
89 $fieldName = 'tx_scheduler[scheduler_fileStorageIndexing_fileCount]';
90 $fieldId = 'scheduler_fileStorageIndexing_fileCount';
91 $fieldValue = $task !== NULL ? (int)$task->maxFileCount : 100;
92 $fieldHtml = '<input type="text" class="form-control" name="' . $fieldName . '" id="' . $fieldId . '" value="' . htmlspecialchars($fieldValue) . '">';
93
94 $fieldConfiguration = array(
95 'code' => $fieldHtml,
96 'label' => 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.fileStorageExtraction.fileCount',
97 'cshKey' => '_MOD_system_txschedulerM1',
98 'cshLabel' => $fieldId
99 );
100 return $fieldConfiguration;
101 }
102
103 /**
104 * Returns a field configuration telling about the status of registered extractors.
105 *
106 * @param FileStorageExtractionTask $task When editing, reference to the current task object. NULL when adding.
107 * @return array Array containing all the information pertaining to the additional fields
108 */
109 protected function getRegisteredExtractorsField(FileStorageExtractionTask $task = NULL) {
110 $extractors = ExtractorRegistry::getInstance()->getExtractors();
111
112 if (empty($extractors)) {
113 $labelKey = 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.fileStorageExtraction.registeredExtractors.without_extractors';
114 /** @var FlashMessage $flashMessage */
115 $flashMessage = GeneralUtility::makeInstance(
116 FlashMessage::class,
117 $this->getLanguageService()->sL($labelKey),
118 '',
119 FlashMessage::WARNING
120 );
121 $content = $flashMessage->render();
122 } else {
123 // Assemble the extractor bullet list first.
124 $labelKey = 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.fileStorageExtraction.registeredExtractors.extractor';
125 $bullets = array();
126 foreach ($extractors as $extractor) {
127 $bullets[] = sprintf(
128 '<li title="%s">%s</li>',
129 get_class($extractor),
130 sprintf($this->getLanguageService()->sL($labelKey), $this->formatExtractorClassName($extractor), $extractor->getPriority())
131 );
132 }
133
134 // Finalize content assembling.
135 $labelKey = 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.fileStorageExtraction.registeredExtractors.with_extractors';
136 /** @var FlashMessage $flashMessage */
137 $flashMessage = GeneralUtility::makeInstance(
138 FlashMessage::class,
139 '<ul>' . implode(LF, $bullets) . '</ul>',
140 $this->getLanguageService()->sL($labelKey),
141 FlashMessage::INFO
142 );
143 $content = $flashMessage->render();
144 }
145
146 $fieldConfiguration = array(
147 'code' => $content,
148 'label' => 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.fileStorageExtraction.registeredExtractors',
149 'cshKey' => '_MOD_system_txschedulerM1',
150 'cshLabel' => 'scheduler_fileStorageIndexing_registeredExtractors'
151 );
152 return $fieldConfiguration;
153 }
154
155 /**
156 * Validate additional fields
157 *
158 * @param array $submittedData Reference to the array containing the data submitted by the user
159 * @param SchedulerModuleController $parentObject Reference to the calling object (Scheduler's BE module)
160 * @return bool True if validation was ok (or selected class is not relevant), false otherwise
161 */
162 public function validateAdditionalFields(array &$submittedData, SchedulerModuleController $parentObject) {
163 if (
164 !MathUtility::canBeInterpretedAsInteger($submittedData['scheduler_fileStorageIndexing_storage'])
165 || !MathUtility::canBeInterpretedAsInteger($submittedData['scheduler_fileStorageIndexing_fileCount'])
166 ) {
167 return FALSE;
168 } elseif (ResourceFactory::getInstance()->getStorageObject($submittedData['scheduler_fileStorageIndexing_storage']) === NULL) {
169 return FALSE;
170 } elseif (!MathUtility::isIntegerInRange($submittedData['scheduler_fileStorageIndexing_fileCount'], 1, 9999)) {
171 return FALSE;
172 }
173 return TRUE;
174 }
175
176 /**
177 * Save additional field in task
178 *
179 * @param array $submittedData Contains data submitted by the user
180 * @param \TYPO3\CMS\Scheduler\Task\AbstractTask $task Reference to the current task object
181 * @return void
182 * @throws \InvalidArgumentException
183 */
184 public function saveAdditionalFields(array $submittedData, \TYPO3\CMS\Scheduler\Task\AbstractTask $task) {
185 if ($task !== NULL && !$task instanceof FileStorageExtractionTask) {
186 throw new \InvalidArgumentException('Task not of type FileStorageExtractionTask', 1384275698);
187 }
188 $task->storageUid = (int)$submittedData['scheduler_fileStorageIndexing_storage'];
189 $task->maxFileCount = (int)$submittedData['scheduler_fileStorageIndexing_fileCount'];
190 }
191
192 /**
193 * Since the class name can be very long considering the namespace, only take the final
194 * part for better readability. The FQN of the class will be displayed as tooltip.
195 *
196 * @param ExtractorInterface $extractor
197 * @return string
198 */
199 protected function formatExtractorClassName(ExtractorInterface $extractor) {
200 $extractorParts = explode('\\', get_class($extractor));
201 return array_pop($extractorParts);
202 }
203
204 /**
205 * @return \TYPO3\CMS\Lang\LanguageService
206 */
207 protected function getLanguageService() {
208 return $GLOBALS['LANG'];
209 }
210 }