eZ Publish  [4.0]
eztemplatetreecache.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZTemplateTreeCache class
00004 //
00005 // Created on: <28-Nov-2002 07:44:29 amos>
00006 //
00007 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00008 // SOFTWARE NAME: eZ Publish
00009 // SOFTWARE RELEASE: 4.0.x
00010 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00011 // SOFTWARE LICENSE: GNU General Public License v2.0
00012 // NOTICE: >
00013 //   This program is free software; you can redistribute it and/or
00014 //   modify it under the terms of version 2.0  of the GNU General
00015 //   Public License as published by the Free Software Foundation.
00016 //
00017 //   This program is distributed in the hope that it will be useful,
00018 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 //   GNU General Public License for more details.
00021 //
00022 //   You should have received a copy of version 2.0 of the GNU General
00023 //   Public License along with this program; if not, write to the Free
00024 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00025 //   MA 02110-1301, USA.
00026 //
00027 //
00028 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00029 //
00030 
00031 /*! \file eztemplatetreecache.php
00032 */
00033 
00034 /*!
00035   \class eZTemplateTreeCache eztemplatetreecache.php
00036   \brief Cache handling for template tree nodes.
00037 
00038 */
00039 
00040 require_once( 'lib/ezutils/classes/ezdebug.php' );
00041 
00042 class eZTemplateTreeCache
00043 {
00044     const CODE_DATE = 1044440833;
00045 
00046     /*!
00047      \static
00048      \return the cache table which has cache keys and cache data.
00049     */
00050     static function &cacheTable()
00051     {
00052         $templateCache =& $GLOBALS['eZTemplateTreeCacheTable'];
00053         if ( !is_array( $templateCache ) )
00054             $templateCache = array();
00055         return $templateCache;
00056     }
00057 
00058     /*!
00059      \private
00060      \static
00061      \return a new key from \a $key which has some additional info.
00062     */
00063     static 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      \static
00077      \return the cache node tree which is stored with the cache key \a $key.
00078              Returns \c null if no cache data was found.
00079     */
00080     static 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      Sets the template tree node \a $root to be cached with the cache key $root.
00098      \note Trying to overwrite and existing cache key will give a warning and fail.
00099     */
00100     static 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      \static
00129      \return true if template tree node caching is enabled.
00130      \note To change this setting edit settings/site.ini and locate the group TemplateSettings and the entry NodeTreeCaching.
00131     */
00132     static 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      \static
00152      \return the cache directory for tree node cache files.
00153     */
00154     static 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      Creates the name for the tree cache file and returns it.
00168      The name conists of the md5 of the key and charset with the original filename appended.
00169     */
00170     static 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      \static
00185      \return true if the cache with the key \a $key can be restored.
00186              A cache file is found restorable when it exists and has a timestamp
00187              higher or equal to \a $timestamp.
00188     */
00189     static 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      \static
00210      Loads the cache with the key \a $key from a file and sets the result in the cache table.
00211      \return true if the cache was successfully restored.
00212     */
00213     static 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'] != eZTemplateTreeCache::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      \static
00243      Stores the data of the cache with the key \a $key to a file.
00244      \return false if the cache does not exist.
00245     */
00246     static 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', eZTemplateTreeCache::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 ?>