[ Index ] |
|
Code source de eZ Publish 3.9.0 |
1 <?php 2 // 3 // $Id$ 4 // 5 // Definition of eZTranslationCache class 6 // 7 // Gunnstein Lye <gl@ez.no> 8 // Created on: <23-Jan-2003 10:19:26 gl> 9 // 10 // SOFTWARE NAME: eZ publish 11 // SOFTWARE RELEASE: 3.9.0 12 // BUILD VERSION: 17785 13 // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS 14 // SOFTWARE LICENSE: GNU General Public License v2.0 15 // NOTICE: > 16 // This program is free software; you can redistribute it and/or 17 // modify it under the terms of version 2.0 of the GNU General 18 // Public License as published by the Free Software Foundation. 19 // 20 // This program is distributed in the hope that it will be useful, 21 // but WITHOUT ANY WARRANTY; without even the implied warranty of 22 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 23 // GNU General Public License for more details. 24 // 25 // You should have received a copy of version 2.0 of the GNU General 26 // Public License along with this program; if not, write to the Free 27 // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 28 // MA 02110-1301, USA. 29 // 30 // 31 32 /*! \file eztranslationcache.php 33 */ 34 35 /*! 36 \class eZTranslationCache eztranslationcache.php 37 \brief Cache handling for translations. 38 39 */ 40 41 include_once ( 'lib/ezutils/classes/ezdebug.php' ); 42 43 define( 'EZ_TRANSLATION_CACHE_CODE_DATE', 1058863428 ); 44 45 class eZTranslationCache 46 { 47 /*! 48 \static 49 \return the cache table which has cache keys and cache data. 50 */ 51 function &cacheTable() 52 { 53 $translationCache =& $GLOBALS['eZTranslationCacheTable']; 54 if ( !is_array( $translationCache ) ) 55 $translationCache = array(); 56 return $translationCache; 57 } 58 59 /*! 60 \static 61 \return the cache translation context which is stored with the cache key \a $contextName. 62 Returns \c null if no cache data was found. 63 */ 64 function contextCache( $contextName ) 65 { 66 $translationCache =& eZTranslationCache::cacheTable(); 67 $context = null; 68 if ( isset( $translationCache[$contextName] ) ) 69 { 70 $context =& $translationCache[$contextName]['root']; 71 // eZDebug::writeDebug( "Cache hit for context '$contextName'", 72 // 'eZTranslationCache::contextCache' ); 73 } 74 // else 75 // eZDebug::writeDebug( "Cache miss for context '$contextName'", 76 // 'eZTranslationCache::contextCache' ); 77 return $context; 78 } 79 80 /*! 81 \static 82 Sets the translation context \a $context to be cached with the cache key $contextName. 83 \note Trying to overwrite and existing cache key will give a warning and fail. 84 */ 85 function setContextCache( $contextName, $context ) 86 { 87 if ( $context === null ) 88 return; 89 $translationCache =& eZTranslationCache::cacheTable(); 90 if ( isset( $translationCache[$contextName] ) ) 91 { 92 eZDebug::writeWarning( "Translation cache for context '$contextName' already exists", 93 'eZTranslationCache::setContextCache' ); 94 } 95 else 96 { 97 $translationCache[$contextName] = array(); 98 } 99 $translationCache[$contextName]['root'] =& $context; 100 $translationCache[$contextName]['info'] = array( 'context' => $contextName ); 101 } 102 103 /*! 104 \static 105 \return the cache directory for translation cache files. 106 */ 107 function cacheDirectory() 108 { 109 $cacheDirectory =& $GLOBALS['eZTranslationCacheDirectory']; 110 if ( !isset( $cacheDirectory ) ) 111 { 112 include_once ( 'lib/ezutils/classes/ezini.php' ); 113 include_once ( 'lib/ezfile/classes/ezdir.php' ); 114 include_once ( 'lib/ezutils/classes/ezsys.php' ); 115 116 $ini =& eZINI::instance(); 117 $locale = $ini->variable( 'RegionalSettings', 'Locale' ); 118 $internalCharset = eZTextCodec::internalCharset(); 119 $rootName = 'root-' . md5( $internalCharset ); 120 $cacheDirectory = eZDir::path( array( eZSys::cacheDirectory(), 'translation', $rootName, $locale ) ); 121 } 122 return $cacheDirectory; 123 } 124 125 /*! 126 \static 127 \return true if the cache with the key \a $key can be restored. 128 A cache file is found restorable when it exists and has a timestamp 129 higher or equal to \a $timestamp. 130 */ 131 function canRestoreCache( $key, $timestamp ) 132 { 133 $translationCache =& eZTranslationCache::cacheTable(); 134 if ( isset( $translationCache[$key] ) ) 135 { 136 return false; 137 } 138 // $internalCharset = eZTextCodec::internalCharset(); 139 // $cacheFileKey = "$key-$internalCharset"; 140 $cacheFileKey = $key; 141 $cacheFileName = md5( $cacheFileKey ) . '.php'; 142 143 include_once ( 'lib/ezutils/classes/ezphpcreator.php' ); 144 145 $php = new eZPHPCreator( eZTranslationCache::cacheDirectory(), $cacheFileName ); 146 return $php->canRestore( $timestamp ); 147 } 148 149 /*! 150 \static 151 Loads the cache with the key \a $key from a file and sets the result in the cache table. 152 \return true if the cache was successfully restored. 153 */ 154 function restoreCache( $key ) 155 { 156 $translationCache =& eZTranslationCache::cacheTable(); 157 if ( isset( $translationCache[$key] ) ) 158 { 159 eZDebug::writeWarning( "Translation cache for key '$key' already exist, cannot restore cache", 'eZTranslationCache::restoreCache' ); 160 return false; 161 } 162 // $internalCharset = eZTextCodec::internalCharset(); 163 // $cacheFileKey = "$key-$internalCharset"; 164 $cacheFileKey = $key; 165 $cacheFileName = md5( $cacheFileKey ) . '.php'; 166 167 include_once ( 'lib/ezutils/classes/ezphpcreator.php' ); 168 169 $php = new eZPHPCreator( eZTranslationCache::cacheDirectory(), $cacheFileName ); 170 $variables = $php->restore( array( 'info' => 'TranslationInfo', 171 'root' => 'TranslationRoot', 172 'cache-date' => 'eZTranslationCacheCodeDate' ) ); 173 if ( $variables['cache-date'] != EZ_TRANSLATION_CACHE_CODE_DATE ) 174 return false; 175 $cache =& $translationCache[$key]; 176 $cache['root'] =& $variables['root']; 177 $cache['info'] =& $variables['info']; 178 return true; 179 } 180 181 /*! 182 \static 183 Stores the data of the cache with the key \a $key to a file. 184 \return false if the cache does not exist. 185 */ 186 function storeCache( $key ) 187 { 188 $translationCache =& eZTranslationCache::cacheTable(); 189 if ( !isset( $translationCache[$key] ) ) 190 { 191 eZDebug::writeWarning( "Translation cache for key '$key' does not exist, cannot store cache", 'eZTranslationCache::storeCache' ); 192 return; 193 } 194 $internalCharset = eZTextCodec::internalCharset(); 195 // $cacheFileKey = "$key-$internalCharset"; 196 $cacheFileKey = $key; 197 $cacheFileName = md5( $cacheFileKey ) . '.php'; 198 199 $cache =& $translationCache[$key]; 200 201 include_once ( 'lib/ezutils/classes/ezphpcreator.php' ); 202 203 if ( file_exists( eZTranslationCache::cacheDirectory() ) ) 204 { 205 eZDir::mkdir( eZTranslationCache::cacheDirectory(), eZDir::directoryPermission(), true ); 206 } 207 $php = new eZPHPCreator( eZTranslationCache::cacheDirectory(), $cacheFileName ); 208 $php->addRawVariable( 'eZTranslationCacheCodeDate', EZ_TRANSLATION_CACHE_CODE_DATE ); 209 $php->addSpace(); 210 $php->addRawVariable( 'CacheInfo', array( 'charset' => $internalCharset ) ); 211 $php->addRawVariable( 'TranslationInfo', $cache['info'] ); 212 $php->addSpace(); 213 $php->addRawVariable( 'TranslationRoot', $cache['root'] ); 214 $php->store(); 215 } 216 } 217 218 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sat Feb 24 10:30:04 2007 | par Balluche grâce à PHPXref 0.7 |