00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 include_once( 'lib/ezutils/classes/ezdebug.php' );
00044
00045 define( 'EZ_TRANSLATION_CACHE_CODE_DATE', 1058863428 );
00046
00047 class eZTranslationCache
00048 {
00049
00050
00051
00052
00053 function &cacheTable()
00054 {
00055 $translationCache =& $GLOBALS['eZTranslationCacheTable'];
00056 if ( !is_array( $translationCache ) )
00057 $translationCache = array();
00058 return $translationCache;
00059 }
00060
00061
00062
00063
00064
00065
00066 function contextCache( $contextName )
00067 {
00068 $translationCache =& eZTranslationCache::cacheTable();
00069 $context = null;
00070 if ( isset( $translationCache[$contextName] ) )
00071 {
00072 $context =& $translationCache[$contextName]['root'];
00073
00074
00075 }
00076
00077
00078
00079 return $context;
00080 }
00081
00082
00083
00084
00085
00086
00087 function setContextCache( $contextName, $context )
00088 {
00089 if ( $context === null )
00090 return;
00091 $translationCache =& eZTranslationCache::cacheTable();
00092 if ( isset( $translationCache[$contextName] ) )
00093 {
00094 eZDebug::writeWarning( "Translation cache for context '$contextName' already exists",
00095 'eZTranslationCache::setContextCache' );
00096 }
00097 else
00098 {
00099 $translationCache[$contextName] = array();
00100 }
00101 $translationCache[$contextName]['root'] =& $context;
00102 $translationCache[$contextName]['info'] = array( 'context' => $contextName );
00103 }
00104
00105
00106
00107
00108
00109 function cacheDirectory()
00110 {
00111 $cacheDirectory =& $GLOBALS['eZTranslationCacheDirectory'];
00112 if ( !isset( $cacheDirectory ) )
00113 {
00114 include_once( 'lib/ezutils/classes/ezini.php' );
00115 $ini =& eZINI::instance();
00116 $locale = $ini->variable( 'RegionalSettings', 'Locale' );
00117
00118 $rootCacheDirectory = eZTranslationCache::rootCacheDirectory();
00119 $cacheDirectory = eZDir::path( array( $rootCacheDirectory, $locale ) );
00120
00121 }
00122 return $cacheDirectory;
00123 }
00124
00125
00126
00127
00128 function rootCacheDirectory()
00129 {
00130 include_once( 'lib/ezfile/classes/ezdir.php' );
00131 include_once( 'lib/ezutils/classes/ezsys.php' );
00132
00133 $internalCharset = eZTextCodec::internalCharset();
00134 $rootName = 'root-' . md5( $internalCharset );
00135 $rootCacheDirectory = eZDir::path( array( eZSys::cacheDirectory(), 'translation', $rootName ) );
00136
00137 return $rootCacheDirectory;
00138 }
00139
00140
00141
00142
00143
00144
00145
00146 function canRestoreCache( $key, $timestamp )
00147 {
00148 $translationCache =& eZTranslationCache::cacheTable();
00149 if ( isset( $translationCache[$key] ) )
00150 {
00151 return false;
00152 }
00153
00154
00155 $cacheFileKey = $key;
00156 $cacheFileName = md5( $cacheFileKey ) . '.php';
00157
00158 include_once( 'lib/ezutils/classes/ezphpcreator.php' );
00159
00160 $php = new eZPHPCreator( eZTranslationCache::cacheDirectory(), $cacheFileName );
00161 return $php->canRestore( $timestamp );
00162 }
00163
00164
00165
00166
00167
00168
00169 function restoreCache( $key )
00170 {
00171 $translationCache =& eZTranslationCache::cacheTable();
00172 if ( isset( $translationCache[$key] ) )
00173 {
00174 eZDebug::writeWarning( "Translation cache for key '$key' already exist, cannot restore cache", 'eZTranslationCache::restoreCache' );
00175 return false;
00176 }
00177
00178
00179 $cacheFileKey = $key;
00180 $cacheFileName = md5( $cacheFileKey ) . '.php';
00181
00182 include_once( 'lib/ezutils/classes/ezphpcreator.php' );
00183
00184 $php = new eZPHPCreator( eZTranslationCache::cacheDirectory(), $cacheFileName );
00185 $variables = $php->restore( array( 'info' => 'TranslationInfo',
00186 'root' => 'TranslationRoot',
00187 'cache-date' => 'eZTranslationCacheCodeDate' ) );
00188 if ( $variables['cache-date'] != EZ_TRANSLATION_CACHE_CODE_DATE )
00189 return false;
00190 $cache =& $translationCache[$key];
00191 $cache['root'] =& $variables['root'];
00192 $cache['info'] =& $variables['info'];
00193 return true;
00194 }
00195
00196
00197
00198
00199
00200
00201 function storeCache( $key )
00202 {
00203 $translationCache =& eZTranslationCache::cacheTable();
00204 if ( !isset( $translationCache[$key] ) )
00205 {
00206 eZDebug::writeWarning( "Translation cache for key '$key' does not exist, cannot store cache", 'eZTranslationCache::storeCache' );
00207 return;
00208 }
00209 $internalCharset = eZTextCodec::internalCharset();
00210
00211 $cacheFileKey = $key;
00212 $cacheFileName = md5( $cacheFileKey ) . '.php';
00213
00214 $cache =& $translationCache[$key];
00215
00216 include_once( 'lib/ezutils/classes/ezphpcreator.php' );
00217
00218 if ( file_exists( eZTranslationCache::cacheDirectory() ) )
00219 {
00220 eZDir::mkdir( eZTranslationCache::cacheDirectory(), eZDir::directoryPermission(), true );
00221 }
00222 $php = new eZPHPCreator( eZTranslationCache::cacheDirectory(), $cacheFileName );
00223 $php->addRawVariable( 'eZTranslationCacheCodeDate', EZ_TRANSLATION_CACHE_CODE_DATE );
00224 $php->addSpace();
00225 $php->addRawVariable( 'CacheInfo', array( 'charset' => $internalCharset ) );
00226 $php->addRawVariable( 'TranslationInfo', $cache['info'] );
00227 $php->addSpace();
00228 $php->addRawVariable( 'TranslationRoot', $cache['root'] );
00229 $php->store();
00230 }
00231
00232
00233
00234
00235
00236 function resetGlobals()
00237 {
00238 unset( $GLOBALS['eZTranslationCacheDirectory'] );
00239 unset( $GLOBALS['eZTranslationCacheTable'] );
00240 }
00241 }
00242
00243 ?>