eZ Publish  [4.0]
ezurlwildcard.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZURLWildcard class
00004 //
00005 // Created on: <08-Nov-2007 16:44:56 dl>
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 ezurlwildcard.php
00032 */
00033 
00034 /*!
00035   \class eZURLWildcard ezurlwildcard.php
00036   \brief Handles URL alias wildcards in eZ Publish
00037 
00038   \private
00039 */
00040 
00041 //include_once( "kernel/classes/ezpersistentobject.php" );
00042 //include_once( "kernel/classes/ezurlaliasml.php" );
00043 
00044 class eZURLWildcard extends eZPersistentObject
00045 {
00046     const EZURLWILDCARD_REGEXP_ARRAY_CALLBACK = 'eZURLWilcardCachedReqexpArray';
00047     const EZURLWILDCARD_TRANSLATE_CALLBACK = 'eZURLWildcardTranslateWithCache';
00048     const EZURLWILDCARD_CACHED_TRANSLATE = 'eZURLWilcardCachedTranslate';
00049 
00050     const EZURLWILDCARD_WILDCARDS_PER_CACHE_FILE = 100;
00051 
00052     const EZ_URLWILDCARD_TYPE_NONE = 0;
00053     const EZ_URLWILDCARD_TYPE_FORWARD = 1;
00054     const EZ_URLWILDCARD_TYPE_DIRECT = 2;
00055 
00056     const EZ_URLWILDCARD_CACHE_SIGNATURE = 'urlalias-wildcard';
00057 
00058     /*!
00059      Initializes a new URL alias.
00060     */
00061     function eZURLWildcard( $row )
00062     {
00063         $this->eZPersistentObject( $row );
00064     }
00065 
00066     /*!
00067      \reimp
00068     */
00069     static function definition()
00070     {
00071         return array( "fields" => array( "id" => array( 'name' => 'ID',
00072                                                         'datatype' => 'integer',
00073                                                         'default' => 0,
00074                                                         'required' => true ),
00075                                          "source_url" => array( 'name' => "SourceURL",
00076                                                                 'datatype' => 'string',
00077                                                                 'default' => '',
00078                                                                 'required' => true ),
00079                                          "destination_url" => array( 'name' => "DestinationURL",
00080                                                                 'datatype' => 'string',
00081                                                                 'default' => '',
00082                                                                 'required' => true ),
00083                                          "type" => array( 'name' => "Type",
00084                                                           'datatype' => 'integer',
00085                                                           'default' => '0',
00086                                                           'required' => true ) ),
00087                       "keys" => array( "id" ),
00088                       'function_attributes' => array(),
00089                       "increment_key" => "id",
00090                       "class_name" => "eZURLWildcard",
00091                       "name" => "ezurlwildcard" );
00092     }
00093 
00094     /*!
00095      \return the url alias object as an associative array with all the attribute values.
00096     */
00097     function asArray()
00098     {
00099         return array( 'id' => $this->attribute( 'id' ),
00100                       'source_url' => $this->attribute( 'source_url' ),
00101                       'destination_url' => $this->attribute( 'destination_url' ),
00102                       'type' => $this->attribute( 'type' ) );
00103     }
00104 
00105     /*!
00106      \note Transaction unsafe. If you call several transaction unsafe methods you must enclose
00107      the calls within a db transaction; thus within db->begin and db->commit.
00108     */
00109     function store( $fieldFilters = null )
00110     {
00111         eZPersistentObject::store( $fieldFilters );
00112     }
00113 
00114     /*!
00115      \static
00116       Removes all wildcards that matches the base URL \a $baseURL.
00117     */
00118     static function cleanup( $baseURL )
00119     {
00120         $db = eZDB::instance();
00121         $baseURLText = $db->escapeString( $baseURL . "/*" );
00122         $sql = "DELETE FROM ezurlwildcard
00123                 WHERE source_url = '$baseURLText'";
00124         $db->query( $sql );
00125     }
00126 
00127     /*!
00128      \static
00129       Removes all wildcards.
00130      */
00131     static function removeAll()
00132     {
00133         eZPersistentObject::removeObject( eZURLWildcard::definition() );
00134     }
00135 
00136     /*!
00137      \static
00138       Removes wildcards by IDs specified in \a $idList
00139      */
00140     static function removeByIDs( $idList )
00141     {
00142         if ( !is_array( $idList ) )
00143             return;
00144 
00145         while ( count( $idList ) > 0 )
00146         {
00147             // remove by portion of 100 rows.
00148             $ids = array_splice( $idList, 0, 100 );
00149 
00150             $conditions = array( 'id' => array( $ids ) );
00151 
00152             eZPersistentObject::removeObject( eZURLWildcard::definition(),
00153                                               $conditions );
00154         }
00155     }
00156 
00157     /*!
00158      \static
00159       Fetches the URL wildcard by ID.
00160     */
00161     static function fetch( $id, $asObject = true )
00162     {
00163         return eZPersistentObject::fetchObject( eZURLWildcard::definition(),
00164                                                 null,
00165                                                 array( "id" => $id ),
00166                                                 $asObject );
00167     }
00168 
00169     /*!
00170      \static
00171       Fetches wildcard by source url \a $url
00172      */
00173     static function fetchBySourceURL( $url, $asObject = true )
00174     {
00175         return eZPersistentObject::fetchObject( eZURLWildcard::definition(),
00176                                                 null,
00177                                                 array( "source_url" => $url ),
00178                                                 $asObject );
00179     }
00180 
00181     /*!
00182      \static
00183       Fetches URL wildcards by offset \a $offset and limit \a $limit.
00184       By default fetches all wildcards.
00185     */
00186     static function fetchList( $offset = false, $limit = false, $asObject = true )
00187     {
00188         return eZPersistentObject::fetchObjectList( eZURLWildcard::definition(),
00189                                                     null,
00190                                                     null,
00191                                                     null,
00192                                                     array( 'offset' => $offset, 'length' => $limit ),
00193                                                     $asObject );
00194     }
00195 
00196     /*!
00197      \static
00198      \return number of wildcards in db
00199      */
00200     static function fetchListCount()
00201     {
00202         $rows = eZPersistentObject::fetchObjectList( eZURLWildcard::definition(),
00203                                                      array(),
00204                                                      null,
00205                                                      false,
00206                                                      null,
00207                                                      false, false,
00208                                                      array( array( 'operation' => 'count( * )',
00209                                                                    'name' => 'count' ) ) );
00210         return $rows[0]['count'];
00211     }
00212 
00213     /*!
00214      \static
00215      \return array with information on the wildcard cache.
00216 
00217      The array containst the following keys
00218      - dir - The directory for the cache
00219      - file - The base filename for the caches
00220      - path - The entire path (including filename) for the cache
00221      - keys - Array with key values which is used to uniquely identify the cache
00222     */
00223     static function cacheInfo()
00224     {
00225         $cacheDir = eZSys::cacheDirectory();
00226         $ini = eZINI::instance();
00227         $keys = array( 'implementation' => $ini->variable( 'DatabaseSettings', 'DatabaseImplementation' ),
00228                        'server' => $ini->variable( 'DatabaseSettings', 'Server' ),
00229                        'database' => $ini->variable( 'DatabaseSettings', 'Database' ) );
00230         $wildcardKey = md5( implode( "\n", $keys ) );
00231         $wildcardCacheDir = "$cacheDir/wildcard";
00232         $wildcardCacheFile = "wildcard_$wildcardKey";
00233         $wildcardCachePath = "$wildcardCacheDir/$wildcardCacheFile";
00234         return array( 'dir' => $wildcardCacheDir,
00235                       'file' => $wildcardCacheFile,
00236                       'path' => $wildcardCachePath,
00237                       'keys' => $keys );
00238     }
00239 
00240     /*!
00241      \static
00242      Sets the various cache information to the parameters.
00243      \sa cacheInfo
00244     */
00245     static function cacheInfoDirectories( &$wildcardCacheDir, &$wildcardCacheFile, &$wildcardCachePath, &$wildcardKeys )
00246     {
00247         $info = eZURLWildcard::cacheInfo();
00248         $wildcardCacheDir = $info['dir'];
00249         $wildcardCacheFile = $info['file'];
00250         $wildcardCachePath = $info['path'];
00251         $wildcardKeys = $info['keys'];
00252     }
00253 
00254     /*!
00255      \static
00256      \return true if the wildcard cache is expired.
00257     */
00258     static function isCacheExpired( $timestamp )
00259     {
00260         $expired = false;
00261 
00262         include_once( 'lib/ezutils/classes/ezexpiryhandler.php' );
00263         $handler = eZExpiryHandler::instance();
00264 
00265         if ( $handler->hasTimestamp( eZURLWildcard::EZ_URLWILDCARD_CACHE_SIGNATURE ) )
00266         {
00267             $expiryTime = $handler->timestamp( eZURLWildcard::EZ_URLWILDCARD_CACHE_SIGNATURE );
00268             if ( $expiryTime >= $timestamp )
00269                 $expired = true;
00270         }
00271 
00272         return $expired;
00273     }
00274 
00275     /*!
00276      \static
00277      Expires the wildcard cache. This causes the wildcard cache to be
00278      regenerated on the next page load.
00279     */
00280     static function expireCache()
00281     {
00282         include_once( 'lib/ezutils/classes/ezexpiryhandler.php' );
00283         $handler = eZExpiryHandler::instance();
00284         $handler->setTimestamp( eZURLWildcard::EZ_URLWILDCARD_CACHE_SIGNATURE, time() );
00285         $handler->store();
00286     }
00287 
00288     /*!
00289      \static
00290      Transforms the URI if there exists an alias for it.
00291      \return \c true is if successful, \c false otherwise
00292      \return The string with new url is returned if the translation was found, but the resource has moved.
00293     */
00294     static function translate( &$uri )
00295     {
00296         $result = false;
00297 
00298         // get uri string
00299         $uriString = ( $uri instanceof eZURI ) ? $uri->elements() : $uri;
00300         $uriString = eZURLAliasML::cleanURL( $uriString );
00301 
00302         eZDebugSetting::writeDebug( 'kernel-urltranslator', "input uriString: '$uriString'", 'eZURLWildcard::translate' );
00303 
00304         // setup helper callbacks
00305         $regexpArrayCallback = false;
00306         $translateCallback = false;
00307         if ( !eZURLWildcard::setupMatchCallbacks( $regexpArrayCallback, $translateCallback ) )
00308         {
00309             eZDebugSetting::writeDebug( 'kernel-urltranslator', "no match callbacks", 'eZURLWildcard::translate' );
00310             return false;
00311         }
00312 
00313         // fetch wildcards(actually regexps)
00314         $wildcards = $regexpArrayCallback();
00315 
00316         $ini = eZINI::instance();
00317         $iteration = $ini->variable( 'URLTranslator', 'MaximumWildcardIterations' );
00318 
00319         eZDebugSetting::writeDebug( 'kernel-urltranslator', "MaximumWildcardIterations: '$iteration'", 'eZURLWildcard::translate' );
00320 
00321         // translate
00322         $urlTranslated = false;
00323         while ( !$urlTranslated && $iteration >= 0 )
00324         {
00325             foreach ( $wildcards as $wildcardNum => $wildcard )
00326             {
00327                 if ( preg_match( $wildcard, $uriString, $matches ) )
00328                 {
00329                     eZDebugSetting::writeDebug( 'kernel-urltranslator', "matched with: '$wildcard'", 'eZURLWildcard::translate' );
00330 
00331                     // get new $uriString from wildcard
00332                     $translateCallback( $wildcardNum, $uriString, $wildcardInfo, $matches );
00333 
00334                     eZDebugSetting::writeDebug( 'kernel-urltranslator', "new uri string: '$uriString'", 'eZURLWildcard::translate' );
00335 
00336                     // optimization: don't try further translation if wildcard type is 'forward'
00337                     if ( $wildcardInfo['type'] == eZURLWildcard::EZ_URLWILDCARD_TYPE_FORWARD )
00338                     {
00339                         $urlTranslated = true;
00340                         break;
00341                     }
00342 
00343                     // try to tranlsate
00344                     if ( $urlTranslated = eZURLAliasML::translate( $uriString ) )
00345                     {
00346                         // success
00347                         eZDebugSetting::writeDebug( 'kernel-urltranslator', "uri is translated to '$uriString' with result '$urlTranslated'", 'eZURLWildcard::translate' );
00348                         break;
00349                     }
00350 
00351                     eZDebugSetting::writeDebug( 'kernel-urltranslator', "uri is not translated, trying another wildcard", 'eZURLWildcard::translate' );
00352 
00353                     // translation failed. Try to match new $uriString with another wildcard.
00354                     --$iteration;
00355                     continue 2;
00356                 }
00357             }
00358 
00359             // we here if non of the wildcards is matched
00360             break;
00361         }
00362 
00363         // check translation result
00364         // NOTE: 'eZURLAliasML::translate'(see above) can return 'true', 'false' or new url(in case of 'error/301').
00365         //       $urlTranslated can also be 'false' if no wildcard is matched.
00366         if ( $urlTranslated )
00367         {
00368             // check wildcard type and set appropriate $result and $uriString
00369             $wildcardType = $wildcardInfo['type'];
00370 
00371             eZDebugSetting::writeDebug( 'kernel-urltranslator', "wildcard type: $wildcardType", 'eZURLWildcard::translate' );
00372 
00373             switch ( $wildcardType )
00374             {
00375                 case eZURLWildcard::EZ_URLWILDCARD_TYPE_FORWARD:
00376                     {
00377                         // do redirect => set $result to untranslated uri
00378                         $result = $uriString;
00379                         $uriString = 'error/301';
00380                     }
00381                     break;
00382 
00383                 default:
00384                     eZDebug::writeError( 'Invalid wildcard type.', 'eZURLWildcard::translate()' );
00385                     // no break, using 'EZ_URLALIAS_WILDCARD_TYPE_DIRECT' as fallback
00386                 case eZURLWildcard::EZ_URLWILDCARD_TYPE_DIRECT:
00387                     $result = $urlTranslated;
00388                     // $uriString already has correct value
00389                     break;
00390             }
00391         }
00392         else
00393         {
00394             // we are here if:
00395             // - input url is not matched with any wildcard;
00396             // - url is matched with wildcard and:
00397             //   - points to module
00398             //   - invalide url
00399             eZDebugSetting::writeDebug( 'kernel-urltranslator', "wildcard is not translated", 'eZURLWildcard::translate' );
00400             $result = false;
00401         }
00402 
00403         // set value back to $uri
00404         if ( $uri instanceof eZURI )
00405         {
00406             $uri->setURIString( $uriString, false );
00407         }
00408         else
00409         {
00410             $uri = $uriString;
00411         }
00412 
00413         eZDebugSetting::writeDebug( 'kernel-urltranslator', "finished with url '$uriString' and result '$result'", 'eZURLWildcard::translate' );
00414 
00415         return $result;
00416     }
00417 
00418     /*!
00419      \static
00420      \private
00421       Assign function names to input variables:
00422         $regexpArrayCallback  - function to get an array of regexps;
00423         $translateCallback - function to get data for urlalias;
00424      */
00425     static private function setupMatchCallbacks( &$regexpArrayCallback, &$translateCallback )
00426     {
00427         if ( !eZURLWildcard::createCacheIfExpired() )
00428         {
00429             // mandatory cache is required in <= 3.9.x.
00430             // note: the appropriate $regexpArrayCallback and $translateCallback can be implemented
00431             //       to support translation without cache.
00432             eZDebugSetting::writeDebug( 'kernel-urltranslator', "setting up callback for non cache mode", 'eZURLWildcard::setupMatchCallbacks' );
00433             return false;
00434         }
00435         else
00436         {
00437             eZDebugSetting::writeDebug( 'kernel-urltranslator', "setting up callback for cache mode", 'eZURLWildcard::setupMatchCallbacks' );
00438 
00439             eZURLWildcard::loadCacheIndex();
00440 
00441             if ( function_exists( eZURLWildcard::EZURLWILDCARD_REGEXP_ARRAY_CALLBACK ) )
00442             {
00443                 $regexpArrayCallback = eZURLWildcard::EZURLWILDCARD_REGEXP_ARRAY_CALLBACK;
00444                 $translateCallback = eZURLWildcard::EZURLWILDCARD_TRANSLATE_CALLBACK;
00445             }
00446             else
00447             {
00448                 eZDebug::writeError( 'Broken wildcard cache index file.', 'eZURLWildcard::setupMatchCallbacks()' );
00449                 return false;
00450             }
00451         }
00452 
00453         return true;
00454     }
00455 
00456     /*!
00457      \static
00458     */
00459     static function createCacheIfExpired()
00460     {
00461         $cacheFile = eZURLWildcard::cacheIndexFile();
00462 
00463         $isExpired = true;
00464         if ( $cacheFile->exists() )
00465         {
00466             $timestamp = $cacheFile->mtime();
00467             $isExpired = eZURLWildcard::isCacheExpired( $timestamp );
00468         }
00469 
00470         if ( $isExpired )
00471         {
00472             eZURLWildcard::createCache();
00473         }
00474 
00475         return true;
00476     }
00477 
00478     /*!
00479      \static
00480      The wildcard caches are splitted between several files:
00481         'wildcard_<md5>_index.php' - contains regexps for wildcards,
00482         'wildcard_<md5>_0.php',
00483         'wildcard_<md5>_1.php',
00484         ...
00485         'wildcard_<md5>_N.php'     - contains cached wildcards.
00486                                      Each file has info about eZURLWildcard::EZURLWILDCARD_WILDCARDS_PER_CACHE_FILE wildcards.
00487      */
00488     static function createCache()
00489     {
00490         eZURLWildcard::cacheInfoDirectories( $wildcardCacheDir, $wildcardCacheFile, $wildcardCachePath, $wildcardKeys );
00491         if ( !file_exists( $wildcardCacheDir ) )
00492         {
00493             eZDir::mkdir( $wildcardCacheDir, false, true );
00494         }
00495 
00496 
00497         include_once( 'lib/ezutils/classes/ezphpcreator.php' );
00498         $phpCacheIndex = new eZPHPCreator( $wildcardCacheDir, $wildcardCacheFile . "_index.php", '', array( 'clustering' => 'wildcard-cache-index' ) );
00499 
00500         foreach ( $wildcardKeys as $wildcardKey => $wildcardKeyValue )
00501         {
00502             $phpCacheIndex->addComment( "$wildcardKey = $wildcardKeyValue" );
00503         }
00504         $phpCacheIndex->addSpace();
00505 
00506         $phpCodeIndex = "function " . eZURLWildcard::EZURLWILDCARD_REGEXP_ARRAY_CALLBACK . "()\n{\n";
00507 
00508         $phpCodeIndex .= "    ";
00509         $phpCodeIndex .= "\$wildcards = array(\n";
00510 
00511         $limit = eZURLWildcard::EZURLWILDCARD_WILDCARDS_PER_CACHE_FILE;
00512         $offset = 0;
00513         $cacheFilesCount = 0;
00514         $wildcardNum = 0;
00515         while( 1 )
00516         {
00517             $wildcards = eZURLWildcard::fetchList( $offset, $limit, false );
00518             if ( count( $wildcards ) === 0 )
00519             {
00520                 break;
00521             }
00522 
00523             $phpCache = new eZPHPCreator( $wildcardCacheDir, $wildcardCacheFile . "_$cacheFilesCount.php", '', array( 'clustering' => 'wildcard-cache-' . $cacheFilesCount ) );
00524 
00525             foreach ( $wildcardKeys as $wildcardKey => $wildcardKeyValue )
00526             {
00527                 $phpCache->addComment( "$wildcardKey = $wildcardKeyValue" );
00528             }
00529             $phpCache->addSpace();
00530 
00531             $phpCode = "function " . eZURLWildcard::EZURLWILDCARD_CACHED_TRANSLATE . "( \$wildcardNum, &\$uri, &\$wildcardInfo, \$matches )\n{\n";
00532 
00533             $phpCode .= "    switch ( \$wildcardNum )\n";
00534             $phpCode .= "    {\n";
00535 
00536             foreach ( $wildcards as $wildcard )
00537             {
00538                 $phpCodeIndex .= '        ';
00539                 if ( $wildcardNum > 0 )
00540                 {
00541                     $phpCodeIndex .= ", ";
00542                 }
00543                 $phpCodeIndex .= eZURLWildcard::matchRegexpCode( $wildcard ) . "\n";
00544 
00545 
00546                 $phpCode .= "        case $wildcardNum:\n";
00547                 $phpCode .= "            {\n";
00548                 $phpCode .= eZURLWildcard::matchReplaceCode( $wildcard, '                ' );
00549                 $phpCode .= "            } break;\n";
00550 
00551                 ++$wildcardNum;
00552             }
00553 
00554             $phpCode .= "\n    };\n";
00555             $phpCode .= "}\n";
00556 
00557             $phpCache->addCodePiece( $phpCode );
00558             $phpCache->store( true );
00559 
00560             $offset += $limit;
00561             ++$cacheFilesCount;
00562         }
00563 
00564         $phpCodeIndex .= " );\n";
00565         $phpCodeIndex .= "    return \$wildcards;\n";
00566         $phpCodeIndex .= "}\n";
00567 
00568         $phpCacheIndex->addCodePiece( $phpCodeIndex );
00569         $phpCacheIndex->store( true );
00570     }
00571 
00572     /*!
00573      \static
00574      \private
00575      Creates a 'regexp' portion of php-code for cache-index.
00576      */
00577     static private function matchRegexpCode( $wildcard )
00578     {
00579         $phpCode = "";
00580 
00581         $matchWilcard = $wildcard['source_url'];
00582         $matchWilcardList = explode( "*", $matchWilcard );
00583         $regexpList = array();
00584         foreach ( $matchWilcardList as $matchWilcardItem )
00585         {
00586             $regexpList[] = preg_quote( $matchWilcardItem, '#' );
00587         }
00588         $matchRegexp = implode( '(.*)', $regexpList );
00589 
00590         $phpCode = "\"#^$matchRegexp#\"";
00591 
00592         return $phpCode;
00593     }
00594 
00595     /*!
00596      \static
00597      \private
00598      Creates a 'replace' and wildcard-info portions of php-code for cache.
00599      */
00600     static private function matchReplaceCode( $wildcard, $indent = '' )
00601     {
00602         $phpCode = "";
00603 
00604         $replaceWildcard = $wildcard['destination_url'];
00605         $replaceWildcardList = preg_split( "#{([0-9]+)}#", $replaceWildcard, false, PREG_SPLIT_DELIM_CAPTURE );
00606         $regexpList = array();
00607         $counter = 0;
00608         $replaceCode = "\$uri = ";
00609         foreach ( $replaceWildcardList as $replaceWildcardItem )
00610         {
00611             if ( $counter > 0 )
00612                 $replaceCode .= " . ";
00613             if ( ( $counter % 2 ) == 0 )
00614             {
00615                 if ( $replaceWildcardItem == "" )
00616                 {
00617                     $replaceCode .= "\"\"";
00618                 }
00619                 else
00620                 {
00621                     $replaceCode .= "\"$replaceWildcardItem\"";
00622                 }
00623             }
00624             else
00625             {
00626                 $replaceCode .= "\$matches[$replaceWildcardItem]";
00627             }
00628             ++$counter;
00629         }
00630         $replaceRegexp = implode( '', $regexpList );
00631 
00632         $phpCode .= $indent . "$replaceCode;\n";
00633 
00634         $wildcardCode = $indent . "\$wildcardInfo = array( ";
00635 
00636         $counter = 0;
00637         foreach ( $wildcard as $key => $value )
00638         {
00639             if ( $counter == 0 )
00640             {
00641                 $wildcardCode .= "'$key' => '$value'";
00642             }
00643             else
00644             {
00645                 $wildcardCode .= ",\n" . $indent . "                       '$key' => '$value'";
00646             }
00647 
00648             ++$counter;
00649         }
00650 
00651         $wildcardCode .= " );\n";
00652 
00653         $phpCode .= $wildcardCode;
00654 
00655         return $phpCode;
00656     }
00657 
00658     /*!
00659      \static
00660      \return instance of 'eZClusterFileHandler' for cache-index file
00661     */
00662     static function cacheIndexFile()
00663     {
00664         $info = eZURLWildcard::cacheInfo();
00665 
00666         require_once( 'kernel/classes/ezclusterfilehandler.php' );
00667         $cacheFile = eZClusterFileHandler::instance( $info['path'] . "_index.php" );
00668 
00669         return $cacheFile;
00670     }
00671 
00672     /*!
00673      \static
00674      Loads cache-index.
00675      */
00676     static function loadCacheIndex()
00677     {
00678         $info = eZURLWildcard::cacheInfo();
00679 
00680         require_once( 'kernel/classes/ezclusterfilehandler.php' );
00681         $cacheFile = eZClusterFileHandler::instance( $info['path'] . "_index.php" );
00682 
00683         if ( !$cacheFile->exists() )
00684             return false;
00685 
00686         $fetchedFilePath = $cacheFile->fetchUnique();
00687         include_once( $fetchedFilePath );
00688         $cacheFile->fileDeleteLocal( $fetchedFilePath );
00689 
00690         return true;
00691     }
00692 }
00693 
00694 /*!
00695  \global
00696  The callback loads appropriate cache file for wildcard \a $wildcardNum,
00697  extracts wildcard info and 'replace' url from cache.
00698  \note The wildcard number(not a wildcard id) is used here. Reason: to find correct cache-file.
00699        If it's needed to fetch wildcard from db, use eZURLWildcard::fetchList with offset = $wildcardNum and
00700        $limit = 1.
00701  */
00702 function eZURLWildcardTranslateWithCache( $wildcardNum, &$uri, &$wildcardInfo, $matches )
00703 {
00704     eZDebugSetting::writeDebug( 'kernel-urltranslator', "eZURLWildcardTranslateWithCache:: wildcardNum = $wildcardNum, uri = $uri", 'eZURLWildcardTranslateWithCache' );
00705 
00706     $cacheFileNum = (int) ( $wildcardNum / eZURLWildcard::EZURLWILDCARD_WILDCARDS_PER_CACHE_FILE );
00707 
00708     eZDebugSetting::writeDebug( 'kernel-urltranslator', "eZURLWildcardTranslateWithCache:: cacheFileNum = $cacheFileNum", 'eZURLWildcardTranslateWithCache' );
00709 
00710     $info = eZURLWildcard::cacheInfo();
00711     $cacheFileName = $info['path'] . "_$cacheFileNum" . ".php";
00712 
00713     require_once( 'kernel/classes/ezclusterfilehandler.php' );
00714     $cacheFile = eZClusterFileHandler::instance( $cacheFileName );
00715 
00716     if ( !$cacheFile->exists() )
00717     {
00718         eZDebugSetting::writeDebug( 'kernel-urltranslator', "eZURLWildcardTranslateWithCache:: no cache file '$cacheFileName'", 'eZURLWildcardTranslateWithCache' );
00719         return false;
00720     }
00721 
00722     $fetchedFilePath = $cacheFile->fetchUnique();
00723     include_once( $fetchedFilePath );
00724     $cacheFile->fileDeleteLocal( $fetchedFilePath );
00725 
00726     if ( !function_exists( eZURLWildcard::EZURLWILDCARD_CACHED_TRANSLATE ) )
00727     {
00728         eZDebugSetting::writeDebug( 'kernel-urltranslator', "eZURLWildcardTranslateWithCache:: no function in cache file", 'eZURLWildcardTranslateWithCache' );
00729         return false;
00730     }
00731 
00732     $function = eZURLWildcard::EZURLWILDCARD_CACHED_TRANSLATE;
00733     $wildcards = $function( $wildcardNum, $uri, $wildcardInfo, $matches );
00734 
00735     eZDebugSetting::writeDebug( 'kernel-urltranslator', "eZURLWildcardTranslateWithCache:: found wildcard: " . var_export( $wildcardInfo, true ), 'eZURLWildcardTranslateWithCache' );
00736 
00737     return true;
00738 }
00739 
00740 ?>