From 26414417f266d2d90ae8a3c34c82acb6094b49d3 Mon Sep 17 00:00:00 2001
From: fgerards <frank.gerards@gerards-gbr.de>
Date: Tue, 5 Apr 2016 13:45:51 +0200
Subject: [PATCH] adds command for clearing the processed files in typo3temp
 (DB and hdd/storage) based on method used in install-tool

---
 Classes/Command/CacheApiCommandController.php | 50 ++++++++++++++----
 Classes/Service/CacheApiService.php           | 52 ++++++++++++++-----
 2 files changed, 79 insertions(+), 23 deletions(-)

diff --git a/Classes/Command/CacheApiCommandController.php b/Classes/Command/CacheApiCommandController.php
index a610915..25ed4d0 100644
--- a/Classes/Command/CacheApiCommandController.php
+++ b/Classes/Command/CacheApiCommandController.php
@@ -33,7 +33,8 @@
  * @author Stefano Kowalke <blueduck@gmx.net>
  * @package Etobi\CoreAPI\Service\SiteApiService
  */
-class CacheApiCommandController extends CommandController {
+class CacheApiCommandController extends CommandController
+{
 	/**
 	 * @var \TYPO3\CMS\Core\Log\LogManager $logManager
 	 */
@@ -49,14 +50,16 @@ class CacheApiCommandController extends CommandController {
 	 *
 	 * @return void
 	 */
-	public function injectLogManager(\TYPO3\CMS\Core\Log\LogManager $logManager) {
+	public function injectLogManager(\TYPO3\CMS\Core\Log\LogManager $logManager)
+	{
 		$this->logManager = $logManager;
 	}
 
 	/**
 	 * Initialize the object
 	 */
-	public function initializeObject() {
+	public function initializeObject()
+	{
 		$this->logger = $this->objectManager->get('TYPO3\CMS\Core\Log\LogManager')->getLogger(__CLASS__);
 	}
 
@@ -70,7 +73,8 @@ public function initializeObject() {
 	 *
 	 * @param \Etobi\CoreAPI\Service\CacheApiService $cacheApiService
 	 */
-	public function injectCacheApiService(\Etobi\CoreAPI\Service\CacheApiService $cacheApiService) {
+	public function injectCacheApiService(\Etobi\CoreAPI\Service\CacheApiService $cacheApiService)
+	{
 		$this->cacheApiService = $cacheApiService;
 	}
 
@@ -81,7 +85,8 @@ public function injectCacheApiService(\Etobi\CoreAPI\Service\CacheApiService $ca
 	 * @param boolean $hard
 	 * @return void
 	 */
-	public function clearAllCachesCommand($hard = false) {
+	public function clearAllCachesCommand($hard = false)
+	{
 		$this->cacheApiService->clearAllCaches($hard);
 		$message = 'All caches have been cleared%s.';
 		$this->logger->info($message);
@@ -93,7 +98,8 @@ public function clearAllCachesCommand($hard = false) {
 	 *
 	 * @return void
 	 */
-	public function clearSystemCacheCommand() {
+	public function clearSystemCacheCommand()
+	{
 		$this->cacheApiService->clearSystemCache();
 		$message = 'System cache has been cleared';
 		$this->logger->info($message);
@@ -108,7 +114,8 @@ public function clearSystemCacheCommand() {
 	 *
 	 * @return void
 	 */
-	public function clearAllActiveOpcodeCacheCommand($fileAbsPath = NULL) {
+	public function clearAllActiveOpcodeCacheCommand($fileAbsPath = NULL)
+	{
 		$this->cacheApiService->clearAllActiveOpcodeCache($fileAbsPath);
 
 		if ($fileAbsPath !== NULL) {
@@ -127,7 +134,8 @@ public function clearAllActiveOpcodeCacheCommand($fileAbsPath = NULL) {
 	 *
 	 * @return void
 	 */
-	public function clearConfigurationCacheCommand() {
+	public function clearConfigurationCacheCommand()
+	{
 		$this->cacheApiService->clearConfigurationCache();
 		$message = 'Configuration cache has been cleared.';
 		$this->logger->info($message);
@@ -139,7 +147,8 @@ public function clearConfigurationCacheCommand() {
 	 *
 	 * @return void
 	 */
-	public function clearPageCacheCommand() {
+	public function clearPageCacheCommand()
+	{
 		$this->cacheApiService->clearPageCache();
 		$message = 'Page cache has been cleared.';
 		$this->logger->info($message);
@@ -184,10 +193,31 @@ public function clearRealUrlCacheCommand()
 	 *
 	 * @return void
 	 */
-	public function clearAllExceptPageCacheCommand() {
+	public function clearAllExceptPageCacheCommand()
+	{
 		$clearedCaches = $this->cacheApiService->clearAllExceptPageCache();
 		$message = 'Cleared caches: ' . implode(', ', $clearedCaches);
 		$this->logger->info($message);
 		$this->outputLine($message);
 	}
+
+	/**
+	 * Clear all processsed files (in DB and on disk)
+	 * This is especially useful on big sites when you can't just drop the page cache.
+	 *
+	 * @return void
+	 */
+	public function clearProcessedFilesCommand()
+	{
+		if ($this->cacheApiService->clearProcessedFiles()) {
+			$message = 'Cleared processed files in typo3temp and fileadmin.';
+			$this->logger->info($message);
+			$this->outputLine($message);
+		} else {
+			$message = 'Clearing processed files failed.';
+			$this->logger->info($message);
+			$this->outputLine($message);
+			$this->quit(1);
+		};
+	}
 }
\ No newline at end of file
diff --git a/Classes/Service/CacheApiService.php b/Classes/Service/CacheApiService.php
index 4257918..276128c 100644
--- a/Classes/Service/CacheApiService.php
+++ b/Classes/Service/CacheApiService.php
@@ -35,7 +35,8 @@
  * @author Stefano Kowalke <blueduck@gmx.net>
  * @package Etobi\CoreAPI\Service\SiteApiService
  */
-class CacheApiService {
+class CacheApiService
+{
 
 	/**
 	 * @var \TYPO3\CMS\Core\DataHandling\DataHandler
@@ -57,7 +58,8 @@ class CacheApiService {
 	 *
 	 * @return void
 	 */
-	public function injectDataHandler(\TYPO3\CMS\Core\DataHandling\DataHandler $dataHandler) {
+	public function injectDataHandler(\TYPO3\CMS\Core\DataHandling\DataHandler $dataHandler)
+	{
 		$this->dataHandler = $dataHandler;
 	}
 
@@ -66,7 +68,8 @@ public function injectDataHandler(\TYPO3\CMS\Core\DataHandling\DataHandler $data
 	 *
 	 * @return void
 	 */
-	public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManager $objectManager) {
+	public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManager $objectManager)
+	{
 		$this->objectManager = $objectManager;
 	}
 
@@ -75,7 +78,8 @@ public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManager $obj
 	 *
 	 * @return void
 	 */
-	public function injectInstallToolClearCacheService(\TYPO3\CMS\Install\Service\ClearCacheService $installToolClearCacheService) {
+	public function injectInstallToolClearCacheService(\TYPO3\CMS\Install\Service\ClearCacheService $installToolClearCacheService)
+	{
 		$this->installToolClearCacheService = $installToolClearCacheService;
 	}
 
@@ -84,7 +88,8 @@ public function injectInstallToolClearCacheService(\TYPO3\CMS\Install\Service\Cl
 	 *
 	 * @return void
 	 */
-	public function initializeObject() {
+	public function initializeObject()
+	{
 		// Create a fake admin user
 		$adminUser = $this->objectManager->get('TYPO3\\CMS\\Core\\Authentication\\BackendUserAuthentication');
 		$adminUser->user['uid'] = $GLOBALS['BE_USER']->user['uid'];
@@ -101,7 +106,8 @@ public function initializeObject() {
 	 * @param bool $hard
 	 * @return void
 	 */
-	public function clearAllCaches($hard = FALSE) {
+	public function clearAllCaches($hard = FALSE)
+	{
 		!$hard ? $this->dataHandler->clear_cacheCmd('all') : $this->installToolClearCacheService->clearAll();
 	}
 
@@ -110,13 +116,14 @@ public function clearAllCaches($hard = FALSE) {
 	 *
 	 * @return void
 	 */
-	public function clearPageCache() {
+	public function clearPageCache()
+	{
 		$this->dataHandler->clear_cacheCmd('pages');
 	}
 
 	/**
 	 * Clear the page cache by tag.
-	 * @param string $tag
+	 *
 	 * @return void
 	 */
 	public function clearPageCacheByTag($tag)
@@ -129,16 +136,19 @@ public function clearPageCacheByTag($tag)
 	 *
 	 * @return void
 	 */
-	public function clearConfigurationCache() {
+	public function clearConfigurationCache()
+	{
 		$this->dataHandler->clear_cacheCmd('temp_cached');
 	}
 
+
 	/**
 	 * Clear the system cache
 	 *
 	 * @return void
 	 */
-	public function clearSystemCache() {
+	public function clearSystemCache()
+	{
 		$this->dataHandler->clear_cacheCmd('system');
 	}
 
@@ -150,7 +160,8 @@ public function clearSystemCache() {
 	 *
 	 * @return void
 	 */
-	public function clearAllActiveOpcodeCache($fileAbsPath = NULL) {
+	public function clearAllActiveOpcodeCache($fileAbsPath = NULL)
+	{
 		$this->clearAllActiveOpcodeCacheWrapper($fileAbsPath);
 	}
 
@@ -161,7 +172,8 @@ public function clearAllActiveOpcodeCache($fileAbsPath = NULL) {
 	 *
 	 * @return array with list of cleared caches
 	 */
-	public function clearAllExceptPageCache() {
+	public function clearAllExceptPageCache()
+	{
 		$out = array();
 		$cacheKeys = array_keys($GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']);
 		$ignoredCaches = array('cache_pages', 'cache_pagesection');
@@ -181,6 +193,19 @@ public function clearAllExceptPageCache() {
 		return $toBeFlushed;
 	}
 
+	/**
+	 * Clear processed files
+	 * The sys_file_processedfile table is truncated and the physical files of local storages are deleted.
+	 * @return \TYPO3\CMS\Install\Status\StatusInterface
+	 */
+	public function clearProcessedFiles()
+	{
+		$repository = $this->objectManager->get('\\TYPO3\\CMS\\Core\\Resource\\ProcessedFileRepository');
+		$failedDeletions = $repository->removeAll();
+		return $failedDeletions;
+
+	}
+
 	/**
 	 * Clears the opcode cache. This just wraps the static call for testing purposes.
 	 *
@@ -189,7 +214,8 @@ public function clearAllExceptPageCache() {
 	 *
 	 * @return void
 	 */
-	protected function clearAllActiveOpcodeCacheWrapper($fileAbsPath) {
+	protected function clearAllActiveOpcodeCacheWrapper($fileAbsPath)
+	{
 		\TYPO3\CMS\Core\Utility\OpcodeCacheUtility::clearAllActive($fileAbsPath);
 	}
 }