[TASK] Use ajax call in Install tool 'clear all cache' 18/52218/6
authorMarc Wessels <mwessels@me.com>
Tue, 28 Mar 2017 15:22:45 +0000 (17:22 +0200)
committerFrank Naegler <frank.naegler@typo3.org>
Tue, 28 Mar 2017 21:12:46 +0000 (23:12 +0200)
The 'clear all cache' buttons is switche to an ajax action
to be more conveniently used.

Resolves: #80525
Releases: master
Change-Id: I3f8c49308c185f5855fe3d393c3874c21f144e8b
Reviewed-on: https://review.typo3.org/52218
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Frank Naegler <frank.naegler@typo3.org>
Tested-by: Frank Naegler <frank.naegler@typo3.org>
typo3/sysext/install/Classes/Controller/Action/Ajax/ClearAllCache.php [new file with mode: 0644]
typo3/sysext/install/Classes/Controller/Action/Tool/ImportantActions.php
typo3/sysext/install/Classes/Controller/AjaxController.php
typo3/sysext/install/Resources/Private/Partials/Action/Tool/ImportantActions/ClearAllCache.html
typo3/sysext/install/Resources/Public/JavaScript/Install.js

diff --git a/typo3/sysext/install/Classes/Controller/Action/Ajax/ClearAllCache.php b/typo3/sysext/install/Classes/Controller/Action/Ajax/ClearAllCache.php
new file mode 100644 (file)
index 0000000..edaeedc
--- /dev/null
@@ -0,0 +1,97 @@
+<?php
+namespace TYPO3\CMS\Install\Controller\Action\Ajax;
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+use TYPO3\CMS\Core\Service\OpcodeCacheService;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Install\View\JsonView;
+
+/**
+ * Clear Cache
+ *
+ * This is an ajax wrapper for clearing all cache.
+ *
+ * @see \TYPO3\CMS\Install\Service\ClearCacheService
+ */
+class ClearAllCache extends AbstractAjaxAction
+{
+
+    /**
+     * @var \TYPO3\CMS\Install\View\JsonView
+     */
+    protected $view;
+
+    /**
+     * @param JsonView $view
+     */
+    public function __construct(JsonView $view = null)
+    {
+        $this->view = $view ?: GeneralUtility::makeInstance(JsonView::class);
+    }
+
+    /**
+     * Initialize the handle action, sets up fluid stuff and assigns default variables.
+     * @ToDo Refactor View Initialization for all Ajax Controllers
+     */
+    protected function initializeHandle()
+    {
+        // empty on purpose because AbstractAjaxAction still overwrites $this->view with StandaloneView
+    }
+
+    /**
+     * Executes the action
+     *
+     * @return array Rendered content
+     */
+    protected function executeAction()
+    {
+        $statusMessages[] = $this->clearAllCache();
+        $statusMessages[] = $this->clearOpcodeCache();
+
+        $this->view->assignMultiple([
+            'success' => true,
+            'status' => $statusMessages,
+        ]);
+        return $this->view->render();
+    }
+
+    /**
+     * Clear all caches
+     *
+     * @return \TYPO3\CMS\Install\Status\StatusInterface
+     */
+    protected function clearAllCache()
+    {
+        /** @var \TYPO3\CMS\Install\Service\ClearCacheService $clearCacheService */
+        $clearCacheService = GeneralUtility::makeInstance(\TYPO3\CMS\Install\Service\ClearCacheService::class);
+        $clearCacheService->clearAll();
+        $message = GeneralUtility::makeInstance(\TYPO3\CMS\Install\Status\OkStatus::class);
+        $message->setTitle('Successfully cleared all caches');
+        return $message;
+    }
+
+    /**
+     * Clear PHP opcode cache
+     *
+     * @return \TYPO3\CMS\Install\Status\StatusInterface
+     */
+    protected function clearOpcodeCache()
+    {
+        GeneralUtility::makeInstance(OpcodeCacheService::class)->clearAllActive();
+        $message = GeneralUtility::makeInstance(\TYPO3\CMS\Install\Status\OkStatus::class);
+        $message->setTitle('Successfully cleared all available opcode caches');
+        return $message;
+    }
+}
index 65de4d7..4a6f441 100644 (file)
@@ -50,10 +50,6 @@ class ImportantActions extends Action\AbstractAction
         if (isset($this->postValues['set']['createAdministrator'])) {
             $actionMessages[] = $this->createAdministrator();
         }
-        if (isset($this->postValues['set']['clearAllCache'])) {
-            $actionMessages[] = $this->clearAllCache();
-            $actionMessages[] = $this->clearOpcodeCache();
-        }
         if (isset($this->postValues['set']['dumpAutoload'])) {
             $actionMessages[] = $this->dumpAutoload();
         }
@@ -187,34 +183,6 @@ class ImportantActions extends Action\AbstractAction
     }
 
     /**
-     * Clear all caches
-     *
-     * @return \TYPO3\CMS\Install\Status\StatusInterface
-     */
-    protected function clearAllCache()
-    {
-        /** @var \TYPO3\CMS\Install\Service\ClearCacheService $clearCacheService */
-        $clearCacheService = GeneralUtility::makeInstance(\TYPO3\CMS\Install\Service\ClearCacheService::class);
-        $clearCacheService->clearAll();
-        $message = GeneralUtility::makeInstance(\TYPO3\CMS\Install\Status\OkStatus::class);
-        $message->setTitle('Successfully cleared all caches');
-        return $message;
-    }
-
-    /**
-     * Clear PHP opcode cache
-     *
-     * @return \TYPO3\CMS\Install\Status\StatusInterface
-     */
-    protected function clearOpcodeCache()
-    {
-        GeneralUtility::makeInstance(OpcodeCacheService::class)->clearAllActive();
-        $message = GeneralUtility::makeInstance(\TYPO3\CMS\Install\Status\OkStatus::class);
-        $message->setTitle('Successfully cleared all available opcode caches');
-        return $message;
-    }
-
-    /**
      * Dumps Extension Autoload Information
      *
      * @return \TYPO3\CMS\Install\Status\StatusInterface
index 42a94cd..cdd426d 100644 (file)
@@ -33,6 +33,7 @@ class AjaxController extends AbstractController
         'extensionCompatibilityTester',
         'uninstallExtension',
         'clearCache',
+        'clearAllCache',
         'coreUpdateUpdateVersionMatrix',
         'coreUpdateIsUpdateAvailable',
         'coreUpdateCheckPreConditions',
index befbd29..6062d73 100644 (file)
@@ -24,5 +24,7 @@
 
 <form method="post">
        <f:render partial="Action/Common/HiddenFormFields" arguments="{_all}" />
-       <f:render partial="Action/Common/SubmitButton" arguments="{name:'clearAllCache', text:'Clear all caches including PHP opcode cache'}"/>
-</form>
+       <div id="clearAllCache">
+               <f:render partial="Action/Common/SubmitButton" arguments="{name:'clearAllCache', text:'Clear all caches including PHP opcode cache'}"/>
+       </div>
+</form>
\ No newline at end of file
index 6c73558..955d400 100644 (file)
@@ -74,6 +74,53 @@ TYPO3.Install.FlashMessage = {
 
 TYPO3.Install.Cache = {
        /**
+        * output DOM Container
+        */
+       outputContainer: {},
+
+       /**
+        * Clone of the DOM object that contains the submit button
+        */
+       submitButton: {},
+
+       /**
+        * Default output messages
+        */
+       outputMessages: {
+               clearAllCache: {
+                       fatalTitle: 'Something went wrong',
+                       fatalMessage: '',
+                       loadingTitle: 'Loading…',
+                       loadingMessage: ''
+               }
+       },
+
+       /**
+       * Fetching the templates out of the DOM
+       *
+       * @param cacheCheckContainer DOM element id with all needed HTML in it
+       * @return boolean DOM container could be found and initialization finished
+       */
+       initialize: function(cacheCheckContainer) {
+               var success = false;
+               this.outputContainer[cacheCheckContainer] = $('#' + cacheCheckContainer);
+
+               if (this.outputContainer[cacheCheckContainer]) {
+                       // submit button: save and delete
+                       if(!this.submitButton[cacheCheckContainer]) {
+                               var submitButton = this.outputContainer[cacheCheckContainer].find('button[type="submit"]');
+                               this.submitButton[cacheCheckContainer] = submitButton.clone();
+                       }
+
+                       // clear all messages from the run before
+                       this.outputContainer[cacheCheckContainer].find('.typo3-message:visible ').remove();
+
+                       success = true;
+               }
+               return success;
+       },
+
+       /**
         * Ajax call to clear all caches.
         */
        clearCache: function() {
@@ -81,6 +128,74 @@ TYPO3.Install.Cache = {
                        url: location.href + '&install[controller]=ajax&install[action]=clearCache',
                        cache: false
                });
+       },
+       clearAllCache: function(actionName) {
+               var self = this;
+               var url = location.href + '&install[controller]=ajax&install[action]=' + actionName;
+               var isInitialized = self.initialize(actionName);
+               if (isInitialized) {
+                       self.addMessage(
+                               TYPO3.Install.Severity.loading,
+                               self.outputMessages[actionName].loadingTitle,
+                               self.outputMessages[actionName].loadingMessage,
+                               actionName
+                       );
+                       $.ajax({
+                               url: url,
+                               cache: false,
+                               success: function(data) {
+                                       if (data.success === true && Array.isArray(data.status)) {
+                                               if (data.status.length > 0) {
+                                                       self.outputContainer[actionName].find('.alert-loading').hide();
+                                                       data.status.forEach((function (element) {
+                                                               //noinspection JSUnresolvedVariable
+                                                               self.addMessage(
+                                                                       element.severity,
+                                                                       element.title,
+                                                                       element.message,
+                                                                       actionName
+                                                               );
+                                                       }));
+                                               } else {
+                                                       self.outputContainer[actionName].find('.alert-loading').hide();
+                                                       self.addMessage(
+                                                               TYPO3.Install.Severity.ok,
+                                                               self.outputMessages[actionName].successTitle,
+                                                               self.outputMessages[actionName].successMessage,
+                                                               actionName
+                                                       );
+                                               }
+                                       } else if (data === 'unauthorized') {
+                                               location.reload();
+                                       }
+                               }
+                       });
+               }
+       },
+       /**
+        * Move the submit button to the end of the box
+        *
+        * @param cacheCheckContainer DOM container name
+        */
+       moveSubmitButtonFurtherDown: function(cacheCheckContainer) {
+               // first remove the currently visible button
+               this.outputContainer[cacheCheckContainer].find('button[type="submit"]').remove();
+               // then append the cloned template to the end
+               this.outputContainer[cacheCheckContainer].append(this.submitButton[cacheCheckContainer]);
+       },
+
+       /**
+        * Show a status message
+        *
+        * @param severity
+        * @param title
+        * @param message
+        * @param cacheCheckContainer DOM container name
+        */
+       addMessage: function(severity, title, message, cacheCheckContainer) {
+               var domMessage = TYPO3.Install.FlashMessage.render(severity, title, message);
+               this.outputContainer[cacheCheckContainer].append(domMessage);
+               this.moveSubmitButtonFurtherDown(cacheCheckContainer);
        }
 };
 
@@ -805,6 +920,15 @@ $(function() {
                }));
        }
 
+       // Handle clearAllCache
+       var $clearAllCacheSection = $('#clearAllCache');
+       if ($clearAllCacheSection) {
+               $clearAllCacheSection.on('click', 'button', (function(e) {
+                       TYPO3.Install.Cache.clearAllCache('clearAllCache');
+                       e.preventDefault();
+                       return false;
+               }));
+       }
        // Handle TCA ext_tables check
        var $tcaExtTablesCheckSection = $('#tcaExtTablesCheck');
        if ($tcaExtTablesCheckSection) {