[TASK] Display info about registered Extractors in Task "Metadata Extract" 75/36675/5
authorFabien Udriot <fabien.udriot@ecodev.ch>
Thu, 5 Feb 2015 17:41:19 +0000 (18:41 +0100)
committerAndreas Fernandez <typo3@scripting-base.de>
Mon, 11 May 2015 07:37:52 +0000 (09:37 +0200)
In the context of the Scheduler, the task "Metadata Extract" will have no
effect if no Extractor is registered - which is the by default case.
This quite confusing for the End User since it would expect something
to happen upon running the task.

The solution is to display a message to the End User about the registered
Extractors in the Task itself. In case no Extractor is present, the
message will make it clear that the Task will have no effect.

Change-Id: Id2dff904ca90f3cc182189db57064cf2b6daa574
Resolves: #64884
Releases: master, 6.2
Reviewed-on: http://review.typo3.org/36675
Reviewed-by: Fabien Udriot <fabien.udriot@ecodev.ch>
Tested-by: Fabien Udriot <fabien.udriot@ecodev.ch>
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Tested-by: Wouter Wolters <typo3@wouterwolters.nl>
Reviewed-by: Andreas Fernandez <typo3@scripting-base.de>
Tested-by: Andreas Fernandez <typo3@scripting-base.de>
typo3/sysext/scheduler/Classes/Task/FileStorageExtractionAdditionalFieldProvider.php
typo3/sysext/scheduler/Resources/Private/Language/locallang.xlf

index db668d5..5ea9fa8 100644 (file)
@@ -14,29 +14,37 @@ namespace TYPO3\CMS\Scheduler\Task;
  * The TYPO3 project - inspiring people to share!
  */
 
+use TYPO3\CMS\Core\Messaging\FlashMessage;
+use TYPO3\CMS\Core\Resource\Index\ExtractorInterface;
+use TYPO3\CMS\Core\Resource\Index\ExtractorRegistry;
+use TYPO3\CMS\Core\Resource\ResourceFactory;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Core\Utility\MathUtility;
+use TYPO3\CMS\Scheduler\AdditionalFieldProviderInterface;
+use TYPO3\CMS\Scheduler\Controller\SchedulerModuleController;
 
 /**
  * Additional BE fields for task which extracts metadata from storage
  *
  */
-class FileStorageExtractionAdditionalFieldProvider implements \TYPO3\CMS\Scheduler\AdditionalFieldProviderInterface {
+class FileStorageExtractionAdditionalFieldProvider implements AdditionalFieldProviderInterface {
 
        /**
         * Add additional fields
         *
         * @param array $taskInfo Reference to the array containing the info used in the add/edit form
         * @param AbstractTask|NULL $task When editing, reference to the current task. NULL when adding.
-        * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject Reference to the calling object (Scheduler's BE module)
+        * @param SchedulerModuleController $parentObject Reference to the calling object (Scheduler's BE module)
         * @return array Array containing all the information pertaining to the additional fields
         * @throws \InvalidArgumentException
         */
-       public function getAdditionalFields(array &$taskInfo, $task, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject) {
+       public function getAdditionalFields(array &$taskInfo, $task, SchedulerModuleController $parentObject) {
                if ($task !== NULL && !$task instanceof FileStorageExtractionTask) {
                        throw new \InvalidArgumentException('Task not of type FileStorageExtractionTask', 1384275695);
                }
                $additionalFields['scheduler_fileStorageIndexing_storage'] = $this->getAllStoragesField($task);
                $additionalFields['scheduler_fileStorageIndexing_fileCount'] = $this->getFileCountField($task);
+               $additionalFields['scheduler_fileStorageIndexing_registeredExtractors'] = $this->getRegisteredExtractorsField($task);
                return $additionalFields;
        }
 
@@ -48,7 +56,7 @@ class FileStorageExtractionAdditionalFieldProvider implements \TYPO3\CMS\Schedul
         */
        protected function getAllStoragesField(FileStorageExtractionTask $task = NULL) {
                /** @var \TYPO3\CMS\Core\Resource\ResourceStorage[] $storages */
-               $storages = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\StorageRepository::class)->findAll();
+               $storages = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\StorageRepository::class)->findAll();
                $options = array();
                foreach ($storages as $storage) {
                        if ($task !== NULL && $task->storageUid === $storage->getUid()) {
@@ -93,19 +101,71 @@ class FileStorageExtractionAdditionalFieldProvider implements \TYPO3\CMS\Schedul
        }
 
        /**
+        * Returns a field configuration telling about the status of registered extractors.
+        *
+        * @param FileStorageExtractionTask $task When editing, reference to the current task object. NULL when adding.
+        * @return array Array containing all the information pertaining to the additional fields
+        */
+       protected function getRegisteredExtractorsField(FileStorageExtractionTask $task = NULL) {
+               $extractors = ExtractorRegistry::getInstance()->getExtractors();
+
+               if (empty($extractors)) {
+                       $labelKey = 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.fileStorageExtraction.registeredExtractors.without_extractors';
+                       /** @var FlashMessage $flashMessage */
+                       $flashMessage = GeneralUtility::makeInstance(
+                               FlashMessage::class,
+                               $this->getLanguageService()->sL($labelKey),
+                               '',
+                               FlashMessage::WARNING
+                       );
+                       $content = $flashMessage->render();
+               } else {
+                       // Assemble the extractor bullet list first.
+                       $labelKey = 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.fileStorageExtraction.registeredExtractors.extractor';
+                       $bullets = array();
+                       foreach ($extractors as $extractor) {
+                               $bullets[] = sprintf(
+                                       '<li title="%s">%s</li>',
+                                       get_class($extractor),
+                                       sprintf($this->getLanguageService()->sL($labelKey), $this->formatExtractorClassName($extractor), $extractor->getPriority())
+                               );
+                       }
+
+                       // Finalize content assembling.
+                       $labelKey = 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.fileStorageExtraction.registeredExtractors.with_extractors';
+                       /** @var FlashMessage $flashMessage */
+                       $flashMessage = GeneralUtility::makeInstance(
+                               FlashMessage::class,
+                               '<ul>' . implode(LF, $bullets) . '</ul>',
+                               $this->getLanguageService()->sL($labelKey),
+                               FlashMessage::INFO
+                       );
+                       $content = $flashMessage->render();
+               }
+
+               $fieldConfiguration = array(
+                       'code' => $content,
+                       'label' => 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.fileStorageExtraction.registeredExtractors',
+                       'cshKey' => '_MOD_system_txschedulerM1',
+                       'cshLabel' => 'scheduler_fileStorageIndexing_registeredExtractors'
+               );
+               return $fieldConfiguration;
+       }
+
+       /**
         * Validate additional fields
         *
         * @param array $submittedData Reference to the array containing the data submitted by the user
-        * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject Reference to the calling object (Scheduler's BE module)
+        * @param SchedulerModuleController $parentObject Reference to the calling object (Scheduler's BE module)
         * @return bool True if validation was ok (or selected class is not relevant), false otherwise
         */
-       public function validateAdditionalFields(array &$submittedData, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject) {
+       public function validateAdditionalFields(array &$submittedData, SchedulerModuleController $parentObject) {
                if (
                        !MathUtility::canBeInterpretedAsInteger($submittedData['scheduler_fileStorageIndexing_storage'])
                        || !MathUtility::canBeInterpretedAsInteger($submittedData['scheduler_fileStorageIndexing_fileCount'])
                ) {
                        return FALSE;
-               } elseif(\TYPO3\CMS\Core\Resource\ResourceFactory::getInstance()->getStorageObject($submittedData['scheduler_fileStorageIndexing_storage']) === NULL) {
+               } elseif (ResourceFactory::getInstance()->getStorageObject($submittedData['scheduler_fileStorageIndexing_storage']) === NULL) {
                        return FALSE;
                } elseif (!MathUtility::isIntegerInRange($submittedData['scheduler_fileStorageIndexing_fileCount'], 1, 9999)) {
                        return FALSE;
@@ -129,4 +189,22 @@ class FileStorageExtractionAdditionalFieldProvider implements \TYPO3\CMS\Schedul
                $task->maxFileCount = (int)$submittedData['scheduler_fileStorageIndexing_fileCount'];
        }
 
+       /**
+        * Since the class name can be very long considering the namespace, only take the final
+        * part for better readability. The FQN of the class will be displayed as tooltip.
+        *
+        * @param ExtractorInterface $extractor
+        * @return string
+        */
+       protected function formatExtractorClassName(ExtractorInterface $extractor) {
+               $extractorParts = explode('\\', get_class($extractor));
+               return array_pop($extractorParts);
+       }
+
+       /**
+        * @return \TYPO3\CMS\Lang\LanguageService
+        */
+       protected function getLanguageService() {
+               return $GLOBALS['LANG'];
+       }
 }
index 34c154d..003c896 100644 (file)
                        <trans-unit id="label.fileStorageExtraction.fileCount">
                                <source>Number of files per run</source>
                        </trans-unit>
+                       <trans-unit id="label.fileStorageExtraction.registeredExtractors">
+                               <source>Registered extractors</source>
+                       </trans-unit>
+                       <trans-unit id="label.fileStorageExtraction.registeredExtractors.extractor">
+                               <source>%1$s with priority %2$d</source>
+                       </trans-unit>
+                       <trans-unit id="label.fileStorageExtraction.registeredExtractors.with_extractors">
+                               <source>Following extractors have been registered and will be used when running this task:</source>
+                       </trans-unit>
+                       <trans-unit id="label.fileStorageExtraction.registeredExtractors.without_extractors">
+                               <source>There currently is no extractor registered. This task will have no effect!</source>
+                       </trans-unit>
                        <trans-unit id="label.tableGarbageCollection.allTables">
                                <source>Clean all available tables</source>
                        </trans-unit>