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 include_once( 'lib/ezutils/classes/ezdebug.php' );
00041
00042 define( 'EZ_TEMPLATE_TREE_CACHE_CODE_DATE', 1044440833 );
00043
00044 class eZTemplateTreeCache
00045 {
00046
00047
00048
00049
00050 function &cacheTable()
00051 {
00052 $templateCache =& $GLOBALS['eZTemplateTreeCacheTable'];
00053 if ( !is_array( $templateCache ) )
00054 $templateCache = array();
00055 return $templateCache;
00056 }
00057
00058
00059
00060
00061
00062
00063 function internalKey( $key )
00064 {
00065 include_once( 'lib/ezutils/classes/ezini.php' );
00066 $ini =& eZINI::instance();
00067 $debug = $ini->variable( 'TemplateSettings', 'Debug' ) == 'enabled';
00068 if ( $debug )
00069 $key = $key . '-debug';
00070 else
00071 $key = $key . '-clean';
00072 return $key;
00073 }
00074
00075
00076
00077
00078
00079
00080 function cachedTree( $key, $uri, $res, $templatePath, &$extraParameters )
00081 {
00082 $templateCache =& eZTemplateTreeCache::cacheTable();
00083 $key = eZTemplateTreeCache::internalKey( $key );
00084 $root = null;
00085 if ( isset( $templateCache[$key] ) )
00086 {
00087 $root =& $templateCache[$key]['root'];
00088 eZDebugSetting::writeDebug( 'eztemplate-tree-cache', "Cache hit for uri '$uri' with key '$key'", 'eZTemplateTreeCache::cachedTree' );
00089 }
00090 else
00091 eZDebugSetting::writeDebug( 'eztemplate-tree-cache', "Cache miss for uri '$uri' with key '$key'", 'eZTemplateTreeCache::cachedTree' );
00092
00093 return $root;
00094 }
00095
00096
00097
00098
00099
00100 function setCachedTree( $originalKey, $uri, $res, $templatePath, &$extraParameters, &$root )
00101 {
00102 if ( $root === null )
00103 return;
00104 $templateCache =& eZTemplateTreeCache::cacheTable();
00105 $key = eZTemplateTreeCache::internalKey( $originalKey );
00106 if ( isset( $templateCache[$key] ) )
00107 {
00108 eZDebug::writeWarning( "Template cache for key '$key', created from uri '$uri', already exists", 'eZTemplateTreeCache::setCachedTree' );
00109 }
00110 else
00111 {
00112 $templateCache[$key] = array();
00113 }
00114 include_once( 'lib/ezutils/classes/ezini.php' );
00115 $ini =& eZINI::instance();
00116 $debug = $ini->variable( 'TemplateSettings', 'Debug' ) == 'enabled';
00117 $templateCache[$key]['root'] =& $root;
00118 $templateCache[$key]['info'] = array( 'original_key' => $originalKey,
00119 'key' => $key,
00120 'uri' => $uri,
00121 'debug' => $debug,
00122 'resource' => $res,
00123 'template_path' => $templatePath,
00124 'resource_parameters' => $extraParameters );
00125 }
00126
00127
00128
00129
00130
00131
00132 function isCacheEnabled()
00133 {
00134 if ( isset( $GLOBALS['eZSiteBasics'] ) )
00135 {
00136 $siteBasics = $GLOBALS['eZSiteBasics'];
00137 if ( isset( $siteBasics['no-cache-adviced'] ) and
00138 $siteBasics['no-cache-adviced'] )
00139 {
00140 return false;
00141 }
00142 }
00143
00144 include_once( 'lib/ezutils/classes/ezini.php' );
00145 $ini =& eZINI::instance();
00146 $cacheEnabled = $ini->variable( 'TemplateSettings', 'NodeTreeCaching' ) == 'enabled';
00147 return $cacheEnabled;
00148 }
00149
00150
00151
00152
00153
00154 function cacheDirectory()
00155 {
00156 $cacheDirectory =& $GLOBALS['eZTemplateTreeCacheDirectory'];
00157 if ( !isset( $cacheDirectory ) )
00158 {
00159 include_once( 'lib/ezfile/classes/ezdir.php' );
00160 include_once( 'lib/ezutils/classes/ezsys.php' );
00161 $cacheDirectory = eZDir::path( array( eZSys::cacheDirectory(), 'template/tree' ) );
00162 }
00163 return $cacheDirectory;
00164 }
00165
00166
00167
00168
00169
00170 function treeCacheFilename( $key, $templateFilepath )
00171 {
00172 $internalCharset = eZTextCodec::internalCharset();
00173 $extraName = '';
00174 if ( preg_match( "#^.+/(.*)\.tpl$#", $templateFilepath, $matches ) )
00175 $extraName = '-' . $matches[1];
00176 else if ( preg_match( "#^(.*)\.tpl$#", $templateFilepath, $matches ) )
00177 $extraName = '-' . $matches[1];
00178 $cacheFileKey = "$key-$internalCharset";
00179 $cacheFileName = md5( $cacheFileKey ) . $extraName . '.php';
00180 return $cacheFileName;
00181 }
00182
00183
00184
00185
00186
00187
00188
00189 function canRestoreCache( $key, $timestamp, $templateFilepath )
00190 {
00191 if ( !eZTemplateTreeCache::isCacheEnabled() )
00192 return false;
00193
00194 $templateCache =& eZTemplateTreeCache::cacheTable();
00195 $key = eZTemplateTreeCache::internalKey( $key );
00196 if ( isset( $templateCache[$key] ) )
00197 {
00198 return false;
00199 }
00200 $cacheFileName = eZTemplateTreeCache::treeCacheFilename( $key, $templateFilepath );
00201
00202 include_once( 'lib/ezutils/classes/ezphpcreator.php' );
00203
00204 $php = new eZPHPCreator( eZTemplateTreeCache::cacheDirectory(), $cacheFileName );
00205 return $php->canRestore( $timestamp );
00206 }
00207
00208
00209
00210
00211
00212
00213 function restoreCache( $key, $templateFilepath )
00214 {
00215 if ( !eZTemplateTreeCache::isCacheEnabled() )
00216 return false;
00217
00218 $templateCache =& eZTemplateTreeCache::cacheTable();
00219 $key = eZTemplateTreeCache::internalKey( $key );
00220 if ( isset( $templateCache[$key] ) )
00221 {
00222 eZDebug::writeWarning( "Template cache for key '$key' already exist, cannot restore cache", 'eZTemplateTreeCache::restoreCache' );
00223 return false;
00224 }
00225 $cacheFileName = eZTemplateTreeCache::treeCacheFilename( $key, $templateFilepath );
00226
00227 include_once( 'lib/ezutils/classes/ezphpcreator.php' );
00228
00229 $php = new eZPHPCreator( eZTemplateTreeCache::cacheDirectory(), $cacheFileName );
00230 $variables = $php->restore( array( 'info' => 'TemplateInfo',
00231 'root' => 'TemplateRoot',
00232 'cache-date' => 'eZTemplateTreeCacheCodeDate' ) );
00233 if ( $variables['cache-date'] != EZ_TEMPLATE_TREE_CACHE_CODE_DATE )
00234 return false;
00235 $cache =& $templateCache[$key];
00236 $cache['root'] =& $variables['root'];
00237 $cache['info'] =& $variables['info'];
00238 return true;
00239 }
00240
00241
00242
00243
00244
00245
00246 function storeCache( $key, $templateFilepath )
00247 {
00248 if ( !eZTemplateTreeCache::isCacheEnabled() )
00249 return false;
00250 $templateCache =& eZTemplateTreeCache::cacheTable();
00251 $key = eZTemplateTreeCache::internalKey( $key );
00252 if ( !isset( $templateCache[$key] ) )
00253 {
00254 eZDebug::writeWarning( "Template cache for key '$key' does not exist, cannot store cache", 'eZTemplateTreeCache::storeCache' );
00255 return;
00256 }
00257 $cacheFileName = eZTemplateTreeCache::treeCacheFilename( $key, $templateFilepath );
00258
00259 $cache =& $templateCache[$key];
00260
00261 include_once( 'lib/ezutils/classes/ezphpcreator.php' );
00262
00263 $php = new eZPHPCreator( eZTemplateTreeCache::cacheDirectory(), $cacheFileName );
00264 $php->addVariable( 'eZTemplateTreeCacheCodeDate', EZ_TEMPLATE_TREE_CACHE_CODE_DATE );
00265 $php->addSpace();
00266 $php->addVariable( 'TemplateInfo', $cache['info'] );
00267 $php->addSpace();
00268 $php->addVariable( 'TemplateRoot', $cache['root'] );
00269 $php->store();
00270 }
00271 }
00272
00273 ?>