|
eZ Publish
[4.0]
|
00001 <?php 00002 // 00003 // $Id$ 00004 // 00005 // Definition of eZTranslationCache class 00006 // 00007 // Gunnstein Lye <gl@ez.no> 00008 // Created on: <23-Jan-2003 10:19:26 gl> 00009 // 00010 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ## 00011 // SOFTWARE NAME: eZ Publish 00012 // SOFTWARE RELEASE: 4.0.x 00013 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS 00014 // SOFTWARE LICENSE: GNU General Public License v2.0 00015 // NOTICE: > 00016 // This program is free software; you can redistribute it and/or 00017 // modify it under the terms of version 2.0 of the GNU General 00018 // Public License as published by the Free Software Foundation. 00019 // 00020 // This program is distributed in the hope that it will be useful, 00021 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00022 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00023 // GNU General Public License for more details. 00024 // 00025 // You should have received a copy of version 2.0 of the GNU General 00026 // Public License along with this program; if not, write to the Free 00027 // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 00028 // MA 02110-1301, USA. 00029 // 00030 // 00031 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ## 00032 // 00033 00034 /*! \file eztranslationcache.php 00035 */ 00036 00037 /*! 00038 \class eZTranslationCache eztranslationcache.php 00039 \brief Cache handling for translations. 00040 00041 */ 00042 00043 require_once( 'lib/ezutils/classes/ezdebug.php' ); 00044 00045 class eZTranslationCache 00046 { 00047 const CODE_DATE = 1058863428; 00048 00049 /*! 00050 \static 00051 \return the cache table which has cache keys and cache data. 00052 */ 00053 static function cacheTable() 00054 { 00055 if ( !isset( $GLOBALS['eZTranslationCacheTable'] ) ) 00056 { 00057 $GLOBALS['eZTranslationCacheTable'] = array(); 00058 } 00059 return $GLOBALS['eZTranslationCacheTable']; 00060 } 00061 00062 /*! 00063 \static 00064 \return the cache translation context which is stored with the cache key \a $contextName. 00065 Returns \c null if no cache data was found. 00066 */ 00067 static function contextCache( $contextName ) 00068 { 00069 $translationCache = eZTranslationCache::cacheTable(); 00070 if ( isset( $translationCache[$contextName] ) ) 00071 { 00072 return $translationCache[$contextName]['root']; 00073 } 00074 return null; 00075 } 00076 00077 /*! 00078 \static 00079 Sets the translation context \a $context to be cached with the cache key $contextName. 00080 \note Trying to overwrite and existing cache key will give a warning and fail. 00081 */ 00082 static function setContextCache( $contextName, $context ) 00083 { 00084 if ( $context === null ) 00085 { 00086 return; 00087 } 00088 if ( isset( $GLOBALS['eZTranslationCacheTable'][$contextName] ) ) 00089 { 00090 eZDebug::writeWarning( "Translation cache for context '$contextName' already exists", 00091 'eZTranslationCache::setContextCache' ); 00092 } 00093 else 00094 { 00095 $GLOBALS['eZTranslationCacheTable'][$contextName] = array(); 00096 } 00097 $GLOBALS['eZTranslationCacheTable'][$contextName]['root'] = $context; 00098 $GLOBALS['eZTranslationCacheTable'][$contextName]['info'] = array( 'context' => $contextName ); 00099 } 00100 00101 /*! 00102 \static 00103 \return the cache directory for translation cache files. 00104 */ 00105 static function cacheDirectory() 00106 { 00107 $cacheDirectory =& $GLOBALS['eZTranslationCacheDirectory']; 00108 if ( !isset( $cacheDirectory ) ) 00109 { 00110 //include_once( 'lib/ezutils/classes/ezini.php' ); 00111 $ini = eZINI::instance(); 00112 $locale = $ini->variable( 'RegionalSettings', 'Locale' ); 00113 00114 $rootCacheDirectory = eZTranslationCache::rootCacheDirectory(); 00115 $cacheDirectory = eZDir::path( array( $rootCacheDirectory, $locale ) ); 00116 00117 } 00118 return $cacheDirectory; 00119 } 00120 00121 /*! 00122 \static 00123 */ 00124 static function rootCacheDirectory() 00125 { 00126 //include_once( 'lib/ezfile/classes/ezdir.php' ); 00127 //include_once( 'lib/ezutils/classes/ezsys.php' ); 00128 00129 $internalCharset = eZTextCodec::internalCharset(); 00130 00131 $ini = eZINI::instance(); 00132 $translationRepository = $ini->variable( 'RegionalSettings', 'TranslationRepository' ); 00133 $translationExtensions = $ini->variable( 'RegionalSettings', 'TranslationExtensions' ); 00134 00135 $uniqueParts = array( $internalCharset, $translationRepository, implode( ';', $translationExtensions ) ); 00136 $rootCacheDirectory = eZDir::path( array( eZSys::cacheDirectory(), 'translation', md5( implode( '-', $uniqueParts ) ) ) ); 00137 00138 return $rootCacheDirectory; 00139 } 00140 00141 /*! 00142 \static 00143 \return true if the cache with the key \a $key can be restored. 00144 A cache file is found restorable when it exists and has a timestamp 00145 higher or equal to \a $timestamp. 00146 */ 00147 static function canRestoreCache( $key, $timestamp ) 00148 { 00149 $translationCache = eZTranslationCache::cacheTable(); 00150 if ( isset( $translationCache[$key] ) ) 00151 { 00152 return false; 00153 } 00154 // $internalCharset = eZTextCodec::internalCharset(); 00155 // $cacheFileKey = "$key-$internalCharset"; 00156 $cacheFileKey = $key; 00157 $cacheFileName = md5( $cacheFileKey ) . '.php'; 00158 00159 //include_once( 'lib/ezutils/classes/ezphpcreator.php' ); 00160 00161 $php = new eZPHPCreator( eZTranslationCache::cacheDirectory(), $cacheFileName ); 00162 return $php->canRestore( $timestamp ); 00163 } 00164 00165 /*! 00166 \static 00167 Loads the cache with the key \a $key from a file and sets the result in the cache table. 00168 \return true if the cache was successfully restored. 00169 */ 00170 static function restoreCache( $key ) 00171 { 00172 $translationCache = eZTranslationCache::cacheTable(); 00173 if ( isset( $translationCache[$key] ) ) 00174 { 00175 eZDebug::writeWarning( "Translation cache for key '$key' already exist, cannot restore cache", 'eZTranslationCache::restoreCache' ); 00176 return false; 00177 } 00178 // $internalCharset = eZTextCodec::internalCharset(); 00179 // $cacheFileKey = "$key-$internalCharset"; 00180 $cacheFileKey = $key; 00181 $cacheFileName = md5( $cacheFileKey ) . '.php'; 00182 00183 //include_once( 'lib/ezutils/classes/ezphpcreator.php' ); 00184 00185 $php = new eZPHPCreator( eZTranslationCache::cacheDirectory(), $cacheFileName ); 00186 $variables = $php->restore( array( 'info' => 'TranslationInfo', 00187 'root' => 'TranslationRoot', 00188 'cache-date' => 'eZTranslationCacheCodeDate' ) ); 00189 if ( $variables['cache-date'] != self::CODE_DATE ) 00190 return false; 00191 eZTranslationCache::setContextCache( $key, $variables['root'] ); 00192 return true; 00193 } 00194 00195 /*! 00196 \static 00197 Stores the data of the cache with the key \a $key to a file. 00198 \return false if the cache does not exist. 00199 */ 00200 static function storeCache( $key ) 00201 { 00202 $translationCache = eZTranslationCache::cacheTable(); 00203 if ( !isset( $translationCache[$key] ) ) 00204 { 00205 eZDebug::writeWarning( "Translation cache for key '$key' does not exist, cannot store cache", 'eZTranslationCache::storeCache' ); 00206 return; 00207 } 00208 $internalCharset = eZTextCodec::internalCharset(); 00209 // $cacheFileKey = "$key-$internalCharset"; 00210 $cacheFileKey = $key; 00211 $cacheFileName = md5( $cacheFileKey ) . '.php'; 00212 00213 $cache =& $translationCache[$key]; 00214 00215 //include_once( 'lib/ezutils/classes/ezphpcreator.php' ); 00216 00217 if ( file_exists( eZTranslationCache::cacheDirectory() ) ) 00218 { 00219 eZDir::mkdir( eZTranslationCache::cacheDirectory(), false, true ); 00220 } 00221 $php = new eZPHPCreator( eZTranslationCache::cacheDirectory(), $cacheFileName ); 00222 $php->addRawVariable( 'eZTranslationCacheCodeDate', self::CODE_DATE ); 00223 $php->addSpace(); 00224 $php->addRawVariable( 'CacheInfo', array( 'charset' => $internalCharset ) ); 00225 $php->addRawVariable( 'TranslationInfo', $cache['info'] ); 00226 $php->addSpace(); 00227 $php->addRawVariable( 'TranslationRoot', $cache['root'] ); 00228 $php->store(); 00229 } 00230 00231 /*! 00232 \static 00233 Reset values strored in $GLOABLS variable 00234 */ 00235 static function resetGlobals() 00236 { 00237 unset( $GLOBALS['eZTranslationCacheDirectory'] ); 00238 unset( $GLOBALS['eZTranslationCacheTable'] ); 00239 } 00240 } 00241 00242 ?>