eZ Publish  [4.0]
eztemplatefileresource.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZTemplateFileResource class
00004 //
00005 // Created on: <01-Mar-2002 13:49:18 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 /*!
00032  \class eZTemplateFileResource eztemplatefileresource.php
00033  \brief Handles filesystem retrieval of templates.
00034 
00035  Templates are loaded from the disk and returned to the template system.
00036  The name of the resource is "file:".
00037 */
00038 
00039 //include_once( "lib/ezi18n/classes/eztextcodec.php" );
00040 //include_once( "lib/eztemplate/classes/eztemplatetreecache.php" );
00041 //include_once( "lib/eztemplate/classes/eztemplatecompiler.php" );
00042 
00043 class eZTemplateFileResource
00044 {
00045     /*!
00046      Initializes with a default resource name "file".
00047      Also sets whether the resource servers static data files, this is needed
00048      for the cache system.
00049     */
00050     function eZTemplateFileResource( $name = "file", $servesStaticData = true )
00051     {
00052         $this->Name = $name;
00053         $this->ServesStaticData = $servesStaticData;
00054         $this->TemplateCache = array();
00055     }
00056 
00057     /*!
00058      Returns the name of the resource.
00059     */
00060     function resourceName()
00061     {
00062         return $this->Name;
00063     }
00064 
00065     /*
00066      \return true if this resource handler servers static data,
00067      this means that the data can be cached by the template system.
00068     */
00069     function servesStaticData()
00070     {
00071         return $this->ServesStaticData;
00072     }
00073 
00074     /*!
00075     */
00076     function templateNodeTransformation( $functionName, &$node,
00077                                          $tpl, &$resourceData, $parameters, $namespaceValue )
00078     {
00079         if ( $this->Name != 'file' )
00080             return false;
00081         $file = $resourceData['template-name'];
00082         if ( !file_exists( $file ) )
00083             return false;
00084         $newNodes = array();
00085         $newNodes[] = eZTemplateNodeTool::createResourceAcquisitionNode( $resourceData['resource'],
00086                                                                          $file, $file,
00087                                                                          eZTemplate::RESOURCE_FETCH, false,
00088                                                                          $node[4],
00089                                                                          array(),
00090                                                                          $namespaceValue );
00091         return $newNodes;
00092     }
00093 
00094     /*!
00095      Generates a unique key string from the input data and returns it.
00096      The key will be used for storing cached data and retrieving cache files.
00097      When implementing file resource handlers this key must be reimplemented if
00098      the current code does not generate correct keys. However most file based
00099      resource handlers can simple reuse this class.
00100 
00101      Default implementation returns an md5 of the \a $keyData.
00102     */
00103     function cacheKey( $keyData, $res, $templatePath, &$extraParameters )
00104     {
00105         $key = md5( $keyData );
00106         return $key;
00107     }
00108 
00109     /*!
00110      \return the cached node tree for the selected template.
00111     */
00112     function hasCachedProcessTree( $keyData, $uri, $res, $templatePath, &$extraParameters, $timestamp )
00113     {
00114         return false;
00115         $key = $this->cacheKey( $keyData, $res, $templatePath, $extraParameters );
00116         if ( eZTemplateTreeCache::canRestoreCache( $key, $timestamp, $templatePath ) )
00117             eZTemplateTreeCache::restoreCache( $key, $templatePath );
00118         return eZTemplateTreeCache::cachedTree( $key, $uri, $res, $templatePath, $extraParameters );
00119     }
00120 
00121     /*!
00122      Sets the cached node tree for the selected template to \a $root.
00123     */
00124     function compileTemplate( $tpl, $keyData, $uri, $res, $templatePath, &$extraParameters, &$resourceData )
00125     {
00126         $key = $this->cacheKey( $keyData, $res, $templatePath, $extraParameters );
00127         return eZTemplateCompiler::compileTemplate( $tpl, $key, $resourceData );
00128     }
00129 
00130     /*!
00131      Sets the cached node tree for the selected template to \a $root.
00132     */
00133     function executeCompiledTemplate( $tpl, &$textElements,
00134                                       $keyData, $uri, $resourceData, $templatePath,
00135                                       &$extraParameters, $timestamp,
00136                                       $rootNamespace, $currentNamespace )
00137     {
00138         $key = $this->cacheKey( $keyData, $resourceData, $templatePath, $extraParameters );
00139         return eZTemplateCompiler::executeCompilation( $tpl, $textElements, $key, $resourceData,
00140                                                        $rootNamespace, $currentNamespace );
00141     }
00142 
00143     /*!
00144      \return \c true if a compiled template exists for the current request.
00145     */
00146     function hasCompiledTemplate( $keyData, $uri, &$resourceData, $templatePath, &$extraParameters, $timestamp )
00147     {
00148         $key = $this->cacheKey( $keyData, $resourceData, $templatePath, $extraParameters );
00149         return eZTemplateCompiler::hasCompiledTemplate( $key, $timestamp, $resourceData );
00150     }
00151 
00152     /*!
00153      \return \c true if a compiled template can be generated for this request.
00154     */
00155     function canCompileTemplate( $tpl, &$resourceData, &$extraParameters )
00156     {
00157         return eZTemplateCompiler::isCompilationEnabled();
00158     }
00159 
00160     /*!
00161      \return the cached node tree for the selected template.
00162     */
00163     function cachedTemplateTree( $keyData, $uri, $res, $templatePath, &$extraParameters, $timestamp )
00164     {
00165         $key = $this->cacheKey( $keyData, $res, $templatePath, $extraParameters );
00166         if ( eZTemplateTreeCache::canRestoreCache( $key, $timestamp, $templatePath ) )
00167             eZTemplateTreeCache::restoreCache( $key, $templatePath );
00168         return eZTemplateTreeCache::cachedTree( $key, $uri, $res, $templatePath, $extraParameters );
00169     }
00170 
00171     /*!
00172      Sets the cached node tree for the selected template to \a $root.
00173     */
00174     function setCachedTemplateTree( $keyData, $uri, $res, $templatePath, &$extraParameters, &$root )
00175     {
00176         $key = $this->cacheKey( $keyData, $res, $templatePath, $extraParameters );
00177         eZTemplateTreeCache::setCachedTree( $key, $uri, $res, $templatePath, $extraParameters, $root );
00178         eZTemplateTreeCache::storeCache( $key, $templatePath );
00179     }
00180 
00181     /*!
00182      Loads the template file if it exists, also sets the modification timestamp.
00183      Returns true if the file exists.
00184     */
00185     function handleResource( $tpl, &$resourceData, $method, &$extraParameters )
00186     {
00187         return $this->handleResourceData( $tpl, $this, $resourceData, $method, $extraParameters );
00188     }
00189 
00190     /*!
00191      \static
00192      Reusable function for handling file based loading.
00193      Call this with the resource handler object in \a $handler.
00194      It will load the template file and handle any charsets conversion if necessary.
00195      It will also handle tree node caching if one is found.
00196     */
00197     function handleResourceData( $tpl, $handler, &$resourceData, $method, &$extraParameters )
00198     {
00199         // &$templateRoot, &$text, &$tstamp, $uri, $resourceName, &$path, &$keyData
00200         $templateRoot =& $resourceData['root-node'];
00201         $text =& $resourceData['text'];
00202         $tstamp =& $resourceData['time-stamp'];
00203         $uri =& $resourceData['uri'];
00204         $resourceName =& $resourceData['resource'];
00205         $path =& $resourceData['template-filename'];
00206         $keyData =& $resourceData['key-data'];
00207         $localeData =& $resourceData['locales'];
00208 
00209         if ( !file_exists( $path ) )
00210             return false;
00211         $tstamp = filemtime( $path );
00212         $result = false;
00213         $canCache = true;
00214         $templateRoot = null;
00215         if ( !$handler->servesStaticData() )
00216             $canCache = false;
00217         if ( !$tpl->isCachingAllowed() )
00218             $canCache = false;
00219         $keyData = 'file:' . $path;
00220         if ( $method == eZTemplate::RESOURCE_FETCH )
00221         {
00222             if ( $canCache )
00223             {
00224                 if ( $handler->hasCompiledTemplate( $keyData, $uri, $resourceData, $path, $extraParameters, $tstamp ) )
00225                 {
00226                     $resourceData['compiled-template'] = true;
00227                     return true;
00228                 }
00229             }
00230             if ( $canCache )
00231                 $templateRoot = $handler->cachedTemplateTree( $keyData, $uri, $resourceName, $path, $extraParameters, $tstamp );
00232 
00233             if ( $templateRoot !== null )
00234                 return true;
00235 
00236             if ( is_readable( $path ) )
00237             {
00238                 $text = file_get_contents( $path );
00239                 $text = preg_replace( "/\n|\r\n|\r/", "\n", $text );
00240                 $tplINI = $tpl->ini();
00241                 $charset = $tplINI->variable( 'CharsetSettings', 'DefaultTemplateCharset' );
00242                 $locales = array();
00243                 $pos = strpos( $text, "\n" );
00244                 if ( $pos !== false )
00245                 {
00246                     $line = substr( $text, 0, $pos );
00247                     if ( preg_match( "/^\{\*\?template(.+)\?\*\}/", $line, $tpl_arr ) )
00248                     {
00249                         $args = explode( " ", trim( $tpl_arr[1] ) );
00250                         foreach ( $args as $arg )
00251                         {
00252                             $vars = explode( '=', trim( $arg ) );
00253                             switch ( $vars[0] ) {
00254                                 case 'charset': {
00255                                     $val = $vars[1];
00256                                     if ( $val[0] == '"' and
00257                                          strlen( $val ) > 0 and
00258                                          $val[strlen($val)-1] == '"' )
00259                                     {
00260                                         $val = substr( $val, 1, strlen($val) - 2 );
00261                                     }
00262                                     $charset = $val;
00263                                 } break;
00264                                 case 'locale': {
00265                                     $val = $vars[1];
00266                                     if ( $val[0] == '"' and
00267                                          strlen( $val ) > 0 and
00268                                          $val[strlen($val)-1] == '"' )
00269                                     {
00270                                         $val = substr( $val, 1, strlen($val) - 2 );
00271                                     }
00272                                     $locales = explode( ',', $val );
00273                                 } break;
00274                             }
00275                         }
00276                     }
00277                 }
00278 
00279                 /* Setting locale to allow standard PHP functions to handle
00280                  * strtoupper/lower() */
00281                 $defaultLocale = trim( $tplINI->variable( 'CharsetSettings', 'DefaultTemplateLocale' ) );
00282                 if ( $defaultLocale != '' )
00283                 {
00284                     $locales = array_merge( $locales, explode( ',', $defaultLocale ) );
00285                 }
00286                 $localeData = $locales;
00287                 if ( $locales && count( $locales ) )
00288                 {
00289                     setlocale( LC_CTYPE, $locales );
00290                 }
00291 
00292                 if ( eZTemplate::isDebugEnabled() )
00293                     eZDebug::writeNotice( "$path, $charset" );
00294                 $codec = eZTextCodec::instance( $charset, false, false );
00295                 if ( $codec )
00296                 {
00297                     eZDebug::accumulatorStart( 'template_resource_conversion', 'template_total', 'String conversion in template resource' );
00298                     $text = $codec->convertString( $text );
00299                     eZDebug::accumulatorStop( 'template_resource_conversion', 'template_total', 'String conversion in template resource' );
00300                 }
00301                 $result = true;
00302             }
00303         }
00304         else if ( $method == eZTemplate::RESOURCE_QUERY )
00305             $result = true;
00306         return $result;
00307     }
00308 
00309     /// \privatesection
00310     /// The name of the resource
00311     public $Name;
00312     /// True if the data served from this resource is static, ie it can be cached properly
00313     public $ServesStaticData;
00314     /// The cache for templates
00315     public $TemplateCache;
00316 }
00317 
00318 ?>