|
eZ Publish
[trunk]
|
00001 <?php 00002 /** 00003 * File containing the eZTranslationCache class. 00004 * 00005 * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved. 00006 * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2 00007 * @version //autogentag// 00008 * @package lib 00009 */ 00010 00011 /*! 00012 \class eZTranslationCache eztranslationcache.php 00013 \brief Cache handling for translations. 00014 00015 */ 00016 00017 class eZTranslationCache 00018 { 00019 const CODE_DATE = 1058863428; 00020 00021 /*! 00022 \static 00023 \return the cache table which has cache keys and cache data. 00024 */ 00025 static function cacheTable() 00026 { 00027 if ( !isset( $GLOBALS['eZTranslationCacheTable'] ) ) 00028 { 00029 $GLOBALS['eZTranslationCacheTable'] = array(); 00030 } 00031 return $GLOBALS['eZTranslationCacheTable']; 00032 } 00033 00034 /*! 00035 \static 00036 \return the cache translation context which is stored with the cache key \a $contextName. 00037 Returns \c null if no cache data was found. 00038 */ 00039 static function contextCache( $contextName ) 00040 { 00041 $translationCache = eZTranslationCache::cacheTable(); 00042 if ( isset( $translationCache[$contextName] ) ) 00043 { 00044 return $translationCache[$contextName]['root']; 00045 } 00046 return null; 00047 } 00048 00049 /*! 00050 \static 00051 Sets the translation context \a $context to be cached with the cache key $contextName. 00052 \note Trying to overwrite and existing cache key will give a warning and fail. 00053 */ 00054 static function setContextCache( $contextName, $context ) 00055 { 00056 if ( $context === null ) 00057 { 00058 return; 00059 } 00060 if ( isset( $GLOBALS['eZTranslationCacheTable'][$contextName] ) ) 00061 { 00062 eZDebug::writeWarning( "Translation cache for context '$contextName' already exists", __METHOD__ ); 00063 } 00064 else 00065 { 00066 $GLOBALS['eZTranslationCacheTable'][$contextName] = array(); 00067 } 00068 $GLOBALS['eZTranslationCacheTable'][$contextName]['root'] = $context; 00069 $GLOBALS['eZTranslationCacheTable'][$contextName]['info'] = array( 'context' => $contextName ); 00070 } 00071 00072 /*! 00073 \static 00074 \return the cache directory for translation cache files. 00075 */ 00076 static function cacheDirectory() 00077 { 00078 $cacheDirectory =& $GLOBALS['eZTranslationCacheDirectory']; 00079 if ( !isset( $cacheDirectory ) ) 00080 { 00081 $ini = eZINI::instance(); 00082 $locale = $ini->variable( 'RegionalSettings', 'Locale' ); 00083 00084 $rootCacheDirectory = eZTranslationCache::rootCacheDirectory(); 00085 $cacheDirectory = eZDir::path( array( $rootCacheDirectory, $locale ) ); 00086 00087 } 00088 return $cacheDirectory; 00089 } 00090 00091 /*! 00092 \static 00093 */ 00094 static function rootCacheDirectory() 00095 { 00096 $internalCharset = eZTextCodec::internalCharset(); 00097 00098 $ini = eZINI::instance(); 00099 $translationRepository = $ini->variable( 'RegionalSettings', 'TranslationRepository' ); 00100 $translationExtensions = $ini->variable( 'RegionalSettings', 'TranslationExtensions' ); 00101 00102 $uniqueParts = array( $internalCharset, $translationRepository, implode( ';', $translationExtensions ) ); 00103 00104 $sharedTsCacheDir = $ini->hasVariable( 'RegionalSettings', 'SharedTranslationCacheDir' ) ? 00105 trim( $ini->variable( 'RegionalSettings', 'SharedTranslationCacheDir' ) ) : 00106 ''; 00107 if ( $sharedTsCacheDir !== '') 00108 { 00109 $rootCacheDirectory = eZDir::path( array( $sharedTsCacheDir, md5( implode( '-', $uniqueParts ) ) ) ); 00110 } 00111 else 00112 { 00113 $rootCacheDirectory = eZDir::path( array( eZSys::cacheDirectory(), 'translation', md5( implode( '-', $uniqueParts ) ) ) ); 00114 } 00115 return $rootCacheDirectory; 00116 } 00117 00118 /*! 00119 \static 00120 \return true if the cache with the key \a $key can be restored. 00121 A cache file is found restorable when it exists and has a timestamp 00122 higher or equal to \a $timestamp. 00123 */ 00124 static function canRestoreCache( $key, $timestamp ) 00125 { 00126 $translationCache = eZTranslationCache::cacheTable(); 00127 if ( isset( $translationCache[$key] ) ) 00128 { 00129 return false; 00130 } 00131 // $internalCharset = eZTextCodec::internalCharset(); 00132 // $cacheFileKey = "$key-$internalCharset"; 00133 $cacheFileKey = $key; 00134 $cacheFileName = md5( $cacheFileKey ) . '.php'; 00135 00136 $php = new eZPHPCreator( eZTranslationCache::cacheDirectory(), $cacheFileName ); 00137 return $php->canRestore( $timestamp ); 00138 } 00139 00140 /*! 00141 \static 00142 Loads the cache with the key \a $key from a file and sets the result in the cache table. 00143 \return true if the cache was successfully restored. 00144 */ 00145 static function restoreCache( $key ) 00146 { 00147 $translationCache = eZTranslationCache::cacheTable(); 00148 if ( isset( $translationCache[$key] ) ) 00149 { 00150 eZDebug::writeWarning( "Translation cache for key '$key' already exist, cannot restore cache", __METHOD__ ); 00151 return false; 00152 } 00153 // $internalCharset = eZTextCodec::internalCharset(); 00154 // $cacheFileKey = "$key-$internalCharset"; 00155 $cacheFileKey = $key; 00156 $cacheFileName = md5( $cacheFileKey ) . '.php'; 00157 00158 $php = new eZPHPCreator( eZTranslationCache::cacheDirectory(), $cacheFileName ); 00159 $variables = $php->restore( array( 'info' => 'TranslationInfo', 00160 'root' => 'TranslationRoot', 00161 'cache-date' => 'eZTranslationCacheCodeDate' ) ); 00162 if ( !isset($variables['cache-date']) || $variables['cache-date'] != self::CODE_DATE ) 00163 return false; 00164 eZTranslationCache::setContextCache( $key, $variables['root'] ); 00165 return true; 00166 } 00167 00168 /*! 00169 \static 00170 Stores the data of the cache with the key \a $key to a file. 00171 \return false if the cache does not exist. 00172 */ 00173 static function storeCache( $key ) 00174 { 00175 $translationCache = eZTranslationCache::cacheTable(); 00176 if ( !isset( $translationCache[$key] ) ) 00177 { 00178 eZDebug::writeWarning( "Translation cache for key '$key' does not exist, cannot store cache", __METHOD__ ); 00179 return; 00180 } 00181 $internalCharset = eZTextCodec::internalCharset(); 00182 // $cacheFileKey = "$key-$internalCharset"; 00183 $cacheFileKey = $key; 00184 $cacheFileName = md5( $cacheFileKey ) . '.php'; 00185 00186 $cache =& $translationCache[$key]; 00187 00188 if ( !file_exists( eZTranslationCache::cacheDirectory() ) ) 00189 { 00190 eZDir::mkdir( eZTranslationCache::cacheDirectory(), false, true ); 00191 } 00192 $php = new eZPHPCreator( eZTranslationCache::cacheDirectory(), $cacheFileName ); 00193 $php->addRawVariable( 'eZTranslationCacheCodeDate', self::CODE_DATE ); 00194 $php->addSpace(); 00195 $php->addRawVariable( 'CacheInfo', array( 'charset' => $internalCharset ) ); 00196 $php->addRawVariable( 'TranslationInfo', $cache['info'] ); 00197 $php->addSpace(); 00198 $php->addRawVariable( 'TranslationRoot', $cache['root'] ); 00199 $php->store(); 00200 } 00201 00202 /*! 00203 \static 00204 Reset values strored in $GLOABLS variable 00205 */ 00206 static function resetGlobals() 00207 { 00208 unset( $GLOBALS['eZTranslationCacheDirectory'] ); 00209 unset( $GLOBALS['eZTranslationCacheTable'] ); 00210 } 00211 } 00212 00213 ?>