[FEATURE] New caching framework implementation for l10n
authorDominique Feyer <dominique.feyer@reelpeek.net>
Sun, 26 Jun 2011 22:39:15 +0000 (00:39 +0200)
committerXavier Perseguers <typo3@perseguers.ch>
Sat, 9 Jul 2011 18:28:37 +0000 (20:28 +0200)
Add custom caching table for parsed l10n file.

Change-Id: Ia59a77757112bccbf7a53ebeec71928f06ebe02a
Resolves: #25397
Reviewed-on: http://review.typo3.org/2930
Reviewed-by: Stefan Neufeind
Reviewed-by: Björn Pedersen
Tested-by: Björn Pedersen
Reviewed-by: Peter Beernink
Tested-by: Peter Beernink
Reviewed-by: Xavier Perseguers
Tested-by: Xavier Perseguers
t3lib/config_default.php
typo3/sysext/lang/classes/cache/class.tx_lang_cache_abstract.php [deleted file]
typo3/sysext/lang/classes/cache/class.tx_lang_cache_cachingframework.php [deleted file]
typo3/sysext/lang/classes/class.tx_lang_factory.php
typo3/sysext/lang/classes/class.tx_lang_store.php
typo3/sysext/lang/ext_autoload.php
typo3/sysext/lang/ext_localconf.php [new file with mode: 0644]
typo3/sysext/lang/hooks/clearcache/class.tx_lang_clearcache.php [new file with mode: 0644]
typo3/sysext/lang/hooks/clearcache/class.tx_lang_clearcachemenu.php [new file with mode: 0644]
typo3/sysext/lang/hooks/clearcache/locallang.xml [new file with mode: 0644]

index c986b85..03d8792 100644 (file)
@@ -57,6 +57,9 @@ $TYPO3_CONF_VARS = array(
        ),
        'SYS' => array(                 // System related concerning both frontend and backend.
                'lang' => array(
+                       'cache' => array(
+                               'lifetime' => 86400
+                       ),
                        'format' => array(
                                'priority' => 'xlf,xml,php'
                        ),
diff --git a/typo3/sysext/lang/classes/cache/class.tx_lang_cache_abstract.php b/typo3/sysext/lang/classes/cache/class.tx_lang_cache_abstract.php
deleted file mode 100644 (file)
index a0f0677..0000000
+++ /dev/null
@@ -1,104 +0,0 @@
-<?php
-/***************************************************************
- *  Copyright notice
- *
- *  (c) 2011 Dominique Feyer <dfeyer@reelpeek.net>
- *  All rights reserved
- *
- *  This script is part of the TYPO3 project. The TYPO3 project is
- *  free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
- *  (at your option) any later version.
- *
- *  The GNU General Public License can be found at
- *  http://www.gnu.org/copyleft/gpl.html.
- *  A copy is found in the textfile GPL.txt and important notices to the license
- *  from the author is found in LICENSE.txt distributed with these scripts.
- *
- *
- *  This script is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  This copyright notice MUST APPEAR in all copies of the script!
- ***************************************************************/
-
-/**
- * Provides the abstract class for the caching subsystem.
- *
- * @package    TYPO3
- * @subpackage tx_lang
- * @author     Dominique Feyer <dfeyer@reelpeek.net>
- */
-abstract class tx_lang_cache_Abstract implements t3lib_Singleton {
-
-       /**
-        * Local serialize function
-        *
-        * @var string
-        */
-       protected $serialize;
-
-       /**
-        * Local unserialize function
-        *
-        * @var string
-        */
-       protected $unserialize;
-
-       /**
-        * Cache constructor.
-        *
-        * Detects is the current setup supports igbinary.
-        */
-       public function __construct() {
-               if (extension_loaded('igbinary')) {
-                       $this->serialize = 'igbinary_serialize';
-                       $this->unserialize = 'igbinary_unserialize';
-               } else {
-                       $this->serialize = 'serialize';
-                       $this->unserialize = 'unserialize';
-               }
-       }
-
-       /**
-        * Gets the local serializer function.
-        *
-        * @return string
-        */
-       protected function getSerialize() {
-               return $this->serialize;
-       }
-
-       /**
-        * Gets the local unserializer function.
-        *
-        * @return string
-        */
-       protected function getUnserialize() {
-               return $this->unserialize;
-       }
-
-       /**
-        * Gets a cached value.
-        *
-        * @param string $hash Cache hash
-        * @return bool|mixed
-        */
-       abstract public function get($hash);
-
-       /**
-        * Adds a value to the cache.
-        *
-        * @throws RuntimeException
-        * @param string $hash Cache hash
-        * @param mixed $data
-        * @return tx_lang_CacheAbstract This class to allow method chaining
-        */
-       abstract public function set($hash, $data);
-
-}
-
-?>
\ No newline at end of file
diff --git a/typo3/sysext/lang/classes/cache/class.tx_lang_cache_cachingframework.php b/typo3/sysext/lang/classes/cache/class.tx_lang_cache_cachingframework.php
deleted file mode 100644 (file)
index 56a6d9b..0000000
+++ /dev/null
@@ -1,80 +0,0 @@
-<?php
-/***************************************************************
- *  Copyright notice
- *
- *  (c) 2011 Dominique Feyer <dfeyer@reelpeek.net>
- *  All rights reserved
- *
- *  This script is part of the TYPO3 project. The TYPO3 project is
- *  free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
- *  (at your option) any later version.
- *
- *  The GNU General Public License can be found at
- *  http://www.gnu.org/copyleft/gpl.html.
- *  A copy is found in the textfile GPL.txt and important notices to the license
- *  from the author is found in LICENSE.txt distributed with these scripts.
- *
- *
- *  This script is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  This copyright notice MUST APPEAR in all copies of the script!
- ***************************************************************/
-
-/**
- * Provides a cache based on the caching framework.
- *
- * @package    TYPO3
- * @subpackage tx_lang
- * @author     Dominique Feyer <dfeyer@reelpeek.net>
- */
-class tx_lang_cache_CachingFramework extends tx_lang_cache_Abstract {
-
-       /**
-        * Gets a cached value.
-        *
-        * @param  string $hash Cache hash
-        * @return bool|mixed
-        */
-       public function get($hash) {
-               $cacheIdentifier = 'language-' . $hash;
-               $cacheHash = md5($cacheIdentifier);
-               $cache = t3lib_pageSelect::getHash($cacheHash);
-               $unserialize = $this->getUnserialize();
-
-               if ($cache) {
-                       $data = $unserialize($cache);
-               } else {
-                       return FALSE;
-               }
-               return $data;
-       }
-
-       /**
-        * Adds a value to the cache.
-        *
-        * @throws RuntimeException
-        * @param string $hash Cache hash
-        * @param mixed $data
-        * @return tx_lang_cache_CachingFramework This instance to allow method chaining
-        */
-       public function set($hash, $data) {
-               $cacheIdentifier = 'language-' . $hash;
-               $cacheHash = md5($cacheIdentifier);
-               $serialize = $this->getSerialize();
-
-               t3lib_pageSelect::storeHash(
-                       $cacheHash,
-                       $serialize($data),
-                       'language'
-               );
-
-               return $this;
-       }
-}
-
-?>
\ No newline at end of file
index 75cd2f3..f9162b3 100644 (file)
@@ -35,9 +35,9 @@
 class tx_lang_Factory implements t3lib_Singleton {
 
        /**
-        * @var tx_lang_cache_Abstract
+        * @var t3lib_cache_frontend_StringFrontend
         */
-       protected $cache;
+       protected $cacheInstance;
 
        /**
         * @var integer
@@ -53,20 +53,22 @@ class tx_lang_Factory implements t3lib_Singleton {
         * Class constructor
         */
        public function __construct() {
-               $this->supportedExtension = array();
-               $this->ressourceConfiguration = array();
+               $this->initialize();
+       }
+
+       protected function initialize() {
                $this->store = t3lib_div::makeInstance('tx_lang_Store');
 
-               $this->initialize();
+               $this->initializeCache();
        }
 
        /**
-        * Sets factory configuration.
+        * Initialize cache instance to be ready to use
         *
         * @return void
         */
-       public function initialize() {
-               $this->cache = t3lib_div::makeInstance('tx_lang_cache_CachingFramework');
+       protected function initializeCache() {
+               $this->cacheInstance = $GLOBALS['typo3CacheManager']->getCache('lang_l10n');
        }
 
        /**
@@ -79,52 +81,55 @@ class tx_lang_Factory implements t3lib_Singleton {
         * @return array|bool
         */
        public function getParsedData($fileReference, $languageKey, $charset, $errorMode) {
-               $hash = md5($fileReference . $languageKey . $charset);
-               $this->errorMode = $errorMode;
+               try {
+                       $hash = md5($fileReference . $languageKey . $charset);
+                       $this->errorMode = $errorMode;
 
-                       // English is the default language
-               $languageKey = ($languageKey === 'en') ? 'default' : $languageKey;
+                               // English is the default language
+                       $languageKey = ($languageKey === 'en') ? 'default' : $languageKey;
 
-                       // Check if the default language is processed before processing other language
-               if (!$this->store->hasData($fileReference, 'default') && $languageKey !== 'default') {
-                       $this->getParsedData($fileReference, 'default', $charset, $this->errorMode);
-               }
+                               // Check if the default language is processed before processing other language
+                       if (!$this->store->hasData($fileReference, 'default') && $languageKey !== 'default') {
+                               $this->getParsedData($fileReference, 'default', $charset, $this->errorMode);
+                       }
 
-                       // If the content is parsed (local cache), use it
-               if ($this->store->hasData($fileReference, $languageKey)) {
-                       return $this->store->getData($fileReference);
-               }
+                               // If the content is parsed (local cache), use it
+                       if ($this->store->hasData($fileReference, $languageKey)) {
+                               return $this->store->getData($fileReference);
+                       }
 
-                       // If the content is in cache (system cache), use it
-               if ($parsedData = $this->cache->get($hash)) {
-                       return $this->store->setData($fileReference, $languageKey, $parsedData)
-                                       ->getData($fileReference, $languageKey);
-               }
+                               // If the content is in cache (system cache), use it
+                       if ($this->cacheInstance->has($hash)) {
+                                       // Load data from the caching framework
+                               $this->store->setData($fileReference, $languageKey, $this->cacheInstance->get($hash));
+
+                               return $this->store->getData($fileReference, $languageKey);
+                       }
 
-               try {
                        $this->store->setConfiguration($fileReference, $languageKey, $charset);
 
                        /** @var $parser tx_lang_parser */
                        $parser = $this->store->getParserInstance($fileReference);
 
-                       try {
-                               $LOCAL_LANG = $parser->getParsedData(
-                                       $this->store->getAbsoluteFileReference($fileReference),
-                                       $languageKey,
-                                       $charset
-                               );
-                               $this->store->setData(
-                                       $fileReference,
-                                       $languageKey,
-                                       $LOCAL_LANG[$languageKey]
-                               );
-
-                                       // Cache processed data
-                               $this->cache->set($hash, $this->store->getDataByLanguage($fileReference, $languageKey));
-                       } catch (tx_lang_exception_FileNotFound $exception) {
-                                       // Target localization file not found
-                               $this->store->setData($fileReference, $languageKey, array());
-                       }
+                       $LOCAL_LANG = $parser->getParsedData(
+                               $this->store->getAbsoluteFileReference($fileReference),
+                               $languageKey,
+                               $charset
+                       );
+                       $this->store->setData(
+                               $fileReference,
+                               $languageKey,
+                               $LOCAL_LANG[$languageKey]
+                       );
+
+                               // Cache processed data
+                       $this->cacheInstance->set(
+                               $hash,
+                               $this->store->getDataByLanguage($fileReference, $languageKey),
+                               array(),
+                               $GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['cache']['lifetime']
+                       );
+
                } catch (tx_lang_exception_FileNotFound $exception) {
                                // Source localization file not found
                        $this->store->setData($fileReference, $languageKey, array());
index a6401a2..605e3ee 100644 (file)
@@ -220,6 +220,13 @@ class tx_lang_Store implements t3lib_Singleton {
                        );
                }
        }
+
+       /**
+        * @return array
+        */
+       public function getSupportedExtensions() {
+               return $this->supportedExtensions;
+       }
 }
 
 ?>
\ No newline at end of file
index 57f4e1b..152c979 100644 (file)
@@ -7,9 +7,6 @@ $extensionClassesPath = t3lib_extMgm::extPath('lang') . 'classes/';
 return array(
        'tx_lang_factory' => $extensionClassesPath . 'class.tx_lang_factory.php',
        'tx_lang_store' => $extensionClassesPath . 'class.tx_lang_store.php',
-       'tx_lang_cache_abstract' => $extensionClassesPath . 'cache/class.tx_lang_cache_abstract.php',
-       'tx_lang_cache_cachingframework' => $extensionClassesPath . 'cache/class.tx_lang_cache_cachingframework.php',
-       'tx_lang_cache_file' => $extensionClassesPath . 'cache/class.tx_lang_cache_file.php',
        'tx_lang_exception_filenotfound' => $extensionClassesPath . 'exception/class.tx_lang_exception_filenotfound.php',
        'tx_lang_exception_invalidparser' => $extensionClassesPath . 'exception/class.tx_lang_exception_invalidparser.php',
        'tx_lang_exception_invalidxmlfile' => $extensionClassesPath . 'exception/class.tx_lang_exception_invalidxmlfile.php',
diff --git a/typo3/sysext/lang/ext_localconf.php b/typo3/sysext/lang/ext_localconf.php
new file mode 100644 (file)
index 0000000..677de77
--- /dev/null
@@ -0,0 +1,21 @@
+<?php
+if (!defined ('TYPO3_MODE')) {
+       die ('Access denied.');
+}
+
+       // Register cache lang_l10n
+if (!is_array($TYPO3_CONF_VARS['SYS']['caching']['cacheConfigurations']['lang_l10n'])) {
+    $TYPO3_CONF_VARS['SYS']['caching']['cacheConfigurations']['lang_l10n'] = array();
+}
+
+if (!isset($TYPO3_CONF_VARS['SYS']['caching']['cacheConfigurations']['lang_l10n']['backend'])) {
+    $TYPO3_CONF_VARS['SYS']['caching']['cacheConfigurations']['lang_l10n']['backend'] = 't3lib_cache_backend_FileBackend';
+}
+
+       // Register Clear Cache Menu hook
+$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['additionalBackendItems']['cacheActions']['clearLangCache'] = 'EXT:lang/hooks/clearcache/class.tx_lang_clearcachemenu.php:&tx_lang_clearcachemenu';
+
+       // Register Ajax call
+$TYPO3_CONF_VARS['BE']['AJAX']['lang::clearCache'] = 'EXT:lang/hooks/clearcache/class.tx_lang_clearcache.php:tx_lang_clearcache->clearCache';
+
+?>
\ No newline at end of file
diff --git a/typo3/sysext/lang/hooks/clearcache/class.tx_lang_clearcache.php b/typo3/sysext/lang/hooks/clearcache/class.tx_lang_clearcache.php
new file mode 100644 (file)
index 0000000..3d12a7d
--- /dev/null
@@ -0,0 +1,68 @@
+<?php
+/***************************************************************
+*  Copyright notice
+*
+*  (c) 2008-2011 Dominique Feyer <dominique.feyer@reelpeek.net>
+*  All rights reserved
+*
+*  This script is part of the TYPO3 project. The TYPO3 project is
+*  free software; you can redistribute it and/or modify
+*  it under the terms of the GNU General Public License as published by
+*  the Free Software Foundation; either version 2 of the License, or
+*  (at your option) any later version.
+*
+*  The GNU General Public License can be found at
+*  http://www.gnu.org/copyleft/gpl.html.
+*  A copy is found in the textfile GPL.txt and important notices to the license
+*  from the author is found in LICENSE.txt distributed with these scripts.
+*
+*
+*  This script is distributed in the hope that it will be useful,
+*  but WITHOUT ANY WARRANTY; without even the implied warranty of
+*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+*  GNU General Public License for more details.
+*
+*  This copyright notice MUST APPEAR in all copies of the script!
+***************************************************************/
+/**
+ * Class to clear temp files of htmlArea RTE
+ *
+ * @author     Dominique Feyer <dominique.feyer@reelpeek.net>
+ * @package    TYPO3
+ * @subpackage tx_Lang
+ */
+class tx_lang_clearcache {
+
+       /**
+        * @var t3lib_cache_frontend_StringFrontend
+        */
+       protected $cacheInstance;
+
+       /**
+        * Constructor
+        */
+       public function __construct() {
+               $this->initializeCache();
+       }
+
+       /**
+        * Initialize cache instance to be ready to use
+        *
+        * @return void
+        */
+       protected function initializeCache() {
+               $this->cacheInstance = $GLOBALS['typo3CacheManager']->getCache('lang_l10n');
+       }
+
+       /**
+        * Flush the l10n cache
+        *
+        * @return void
+        */
+       public function clearCache() {
+               $this->cacheInstance->flush();
+       }
+
+}
+
+?>
\ No newline at end of file
diff --git a/typo3/sysext/lang/hooks/clearcache/class.tx_lang_clearcachemenu.php b/typo3/sysext/lang/hooks/clearcache/class.tx_lang_clearcachemenu.php
new file mode 100644 (file)
index 0000000..c977444
--- /dev/null
@@ -0,0 +1,60 @@
+<?php
+/***************************************************************
+*  Copyright notice
+*
+*  (c) 2007-2011 Dominique Feyer <dominique.feyer@reelpeek.net>
+*  All rights reserved
+*
+*  This script is part of the TYPO3 project. The TYPO3 project is
+*  free software; you can redistribute it and/or modify
+*  it under the terms of the GNU General Public License as published by
+*  the Free Software Foundation; either version 2 of the License, or
+*  (at your option) any later version.
+*
+*  The GNU General Public License can be found at
+*  http://www.gnu.org/copyleft/gpl.html.
+*  A copy is found in the textfile GPL.txt and important notices to the license
+*  from the author is found in LICENSE.txt distributed with these scripts.
+*
+*
+*  This script is distributed in the hope that it will be useful,
+*  but WITHOUT ANY WARRANTY; without even the implied warranty of
+*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+*  GNU General Public License for more details.
+*
+*  This copyright notice MUST APPEAR in all copies of the script!
+***************************************************************/
+
+/**
+ * Extending class to render the menu for the cache clearing actions, and adding Clear lang/l10n cache option
+ *
+ * @author     Dominique Feyer <dominique.feyer@reelpeek.net>
+ * @package    TYPO3
+ * @subpackage tx_Lang
+ */
+require_once (PATH_typo3 . 'interfaces/interface.backend_cacheActionsHook.php');
+class tx_lang_clearcachemenu implements backend_cacheActionsHook {
+
+       /**
+        * Add cache menu item
+        *
+        * @param array $cacheActions
+        * @param array $optionValues
+        * @return void
+        */
+       public function manipulateCacheActions(&$cacheActions, &$optionValues) {
+               if ($GLOBALS['BE_USER']->isAdmin() || $GLOBALS['BE_USER']->getTSConfigVal('options.clearCache.clearRTECache')) {
+                               // Add new cache menu item
+                       $title = $GLOBALS['LANG']->sL('LLL:EXT:lang/hooks/clearcache/locallang.xml:title');
+                       $cacheActions[] = array(
+                               'id'    => 'clearLangCache',
+                               'title' => $title,
+                               'href'  => $GLOBALS['BACK_PATH'] . 'ajax.php?ajaxID=lang::clearCache',
+                               'icon'  => '<span class="t3-icon t3-icon-actions t3-icon-actions-system t3-icon-system-cache-clear-impact-low"></span>'
+                       );
+                       $optionValues[] = 'clearLangCache';
+               }
+        }
+}
+
+?>
\ No newline at end of file
diff --git a/typo3/sysext/lang/hooks/clearcache/locallang.xml b/typo3/sysext/lang/hooks/clearcache/locallang.xml
new file mode 100644 (file)
index 0000000..09d1dab
--- /dev/null
@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
+<T3locallang>
+       <meta type="array">
+               <generator>ClearLangCache</generator>
+               <type>module</type>
+               <authorName>Dominique Feyer</authorName>
+               <authorEmail>dominique.feyer@reelpeek.net</authorEmail>
+               <description>Clears the l10n cache</description>
+       </meta>
+       <data type="array">
+               <languageKey index="default" type="array">
+                       <label index="title">Clear language cache</label>
+               </languageKey>
+       </data>
+</T3locallang>