Exponential Basic 2.4.0.1 Exponential Basic CMS — PHP Class Reference

eztranslationcache.php
Go to the documentation of this file.
1 <?php
18 class eZTranslationCache
19 {
20 const CODE_DATE = 1058863428;
21
26 static function cacheTable()
27 {
28 if ( !isset( $GLOBALS['eZTranslationCacheTable'] ) )
29 {
30 $GLOBALS['eZTranslationCacheTable'] = array();
31 }
32 return $GLOBALS['eZTranslationCacheTable'];
33 }
34
40 static function contextCache( $contextName )
41 {
42 $translationCache = eZTranslationCache::cacheTable();
43 if ( isset( $translationCache[$contextName] ) )
44 {
45 return $translationCache[$contextName]['root'];
46 }
47 return null;
48 }
49
55 static function setContextCache( $contextName, $context )
56 {
57 if ( $context === null )
58 {
59 return;
60 }
61 if ( isset( $GLOBALS['eZTranslationCacheTable'][$contextName] ) )
62 {
63 eZDebug::writeWarning( "Translation cache for context '$contextName' already exists", __METHOD__ );
64 }
65 else
66 {
67 $GLOBALS['eZTranslationCacheTable'][$contextName] = array();
68 }
69 $GLOBALS['eZTranslationCacheTable'][$contextName]['root'] = $context;
70 $GLOBALS['eZTranslationCacheTable'][$contextName]['info'] = array( 'context' => $contextName );
71 }
72
77 static function cacheDirectory()
78 {
79 $cacheDirectory =& $GLOBALS['eZTranslationCacheDirectory'];
80 if ( !isset( $cacheDirectory ) )
81 {
82 $ini = eZINI::instance();
83 $locale = $ini->variable( 'RegionalSettings', 'Locale' );
84
85 $rootCacheDirectory = eZTranslationCache::rootCacheDirectory();
86 $cacheDirectory = eZDir::path( array( $rootCacheDirectory, $locale ) );
87
88 }
89 return $cacheDirectory;
90 }
91
95 static function rootCacheDirectory()
96 {
97 $internalCharset = eZTextCodec::internalCharset();
98
99 $ini = eZINI::instance();
100 $translationRepository = $ini->variable( 'RegionalSettings', 'TranslationRepository' );
101 $translationExtensions = $ini->variable( 'RegionalSettings', 'TranslationExtensions' );
102
103 $uniqueParts = array( $internalCharset, $translationRepository, implode( ';', $translationExtensions ) );
104
105 $sharedTsCacheDir = $ini->hasVariable( 'RegionalSettings', 'SharedTranslationCacheDir' ) ?
106 trim( $ini->variable( 'RegionalSettings', 'SharedTranslationCacheDir' ) ) :
107 '';
108 if ( $sharedTsCacheDir !== '')
109 {
110 $rootCacheDirectory = eZDir::path( array( $sharedTsCacheDir, md5( implode( '-', $uniqueParts ) ) ) );
111 }
112 else
113 {
114 $rootCacheDirectory = eZDir::path( array( eZSys::cacheDirectory(), 'translation', md5( implode( '-', $uniqueParts ) ) ) );
115 }
116 return $rootCacheDirectory;
117 }
118
125 static function canRestoreCache( $key, $timestamp )
126 {
127 $translationCache = eZTranslationCache::cacheTable();
128 if ( isset( $translationCache[$key] ) )
129 {
130 return false;
131 }
132 // $internalCharset = eZTextCodec::internalCharset();
133 // $cacheFileKey = "$key-$internalCharset";
134 $cacheFileKey = $key;
135 $cacheFileName = md5( $cacheFileKey ) . '.php';
136
137 $php = new eZPHPCreator( eZTranslationCache::cacheDirectory(), $cacheFileName );
138 return $php->canRestore( $timestamp );
139 }
140
146 static function restoreCache( $key )
147 {
148 $translationCache = eZTranslationCache::cacheTable();
149 if ( isset( $translationCache[$key] ) )
150 {
151 eZDebug::writeWarning( "Translation cache for key '$key' already exist, cannot restore cache", __METHOD__ );
152 return false;
153 }
154 // $internalCharset = eZTextCodec::internalCharset();
155 // $cacheFileKey = "$key-$internalCharset";
156 $cacheFileKey = $key;
157 $cacheFileName = md5( $cacheFileKey ) . '.php';
158
159 $php = new eZPHPCreator( eZTranslationCache::cacheDirectory(), $cacheFileName );
160 $variables = $php->restore( array( 'info' => 'TranslationInfo',
161 'root' => 'TranslationRoot',
162 'cache-date' => 'eZTranslationCacheCodeDate' ) );
163 if ( !isset($variables['cache-date']) || $variables['cache-date'] != self::CODE_DATE )
164 return false;
165 eZTranslationCache::setContextCache( $key, $variables['root'] );
166 return true;
167 }
168
174 static function storeCache( $key )
175 {
176 $translationCache = eZTranslationCache::cacheTable();
177 if ( !isset( $translationCache[$key] ) )
178 {
179 eZDebug::writeWarning( "Translation cache for key '$key' does not exist, cannot store cache", __METHOD__ );
180 return;
181 }
182 $internalCharset = eZTextCodec::internalCharset();
183 // $cacheFileKey = "$key-$internalCharset";
184 $cacheFileKey = $key;
185 $cacheFileName = md5( $cacheFileKey ) . '.php';
186
187 $cache =& $translationCache[$key];
188
189 if ( !file_exists( eZTranslationCache::cacheDirectory() ) )
190 {
191 eZDir::mkdir( eZTranslationCache::cacheDirectory(), false, true );
192 }
193 $php = new eZPHPCreator( eZTranslationCache::cacheDirectory(), $cacheFileName );
194 $php->addRawVariable( 'eZTranslationCacheCodeDate', self::CODE_DATE );
195 $php->addSpace();
196 $php->addRawVariable( 'CacheInfo', array( 'charset' => $internalCharset ) );
197 $php->addRawVariable( 'TranslationInfo', $cache['info'] );
198 $php->addSpace();
199 $php->addRawVariable( 'TranslationRoot', $cache['root'] );
200 $php->store( true );
201 }
202
207 static function resetGlobals()
208 {
209 unset( $GLOBALS['eZTranslationCacheDirectory'] );
210 unset( $GLOBALS['eZTranslationCacheTable'] );
211 }
212 }
213
214 ?>
$locale
$locale
Definition: admin/adlist.php:127
eZDebug\writeWarning
static writeWarning( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:503
eZDir\mkdir
static mkdir( $dir, $perm=false, $recursive=false)
Definition: ezdir.php:74
eZDir\path
static path( $names, $includeEndSeparator=false, $type=self::SEPARATOR_UNIX)
Definition: ezdir.php:238
eZPHPCreator
eZPHPCreator provides a simple interface for creating and executing PHP code.
eZSys\cacheDirectory
static cacheDirectory()
Definition: ezsys.php:528
eZTextCodec\internalCharset
static internalCharset()
Definition: ezi18n/classes/eztextcodec.php:547
eZTranslationCache
Cache handling for translations.
Definition: eztranslationcache.php:19
eZTranslationCache\contextCache
static contextCache( $contextName)
Definition: eztranslationcache.php:40
eZTranslationCache\cacheDirectory
static cacheDirectory()
Definition: eztranslationcache.php:77
eZTranslationCache\rootCacheDirectory
static rootCacheDirectory()
Definition: eztranslationcache.php:95
eZTranslationCache\CODE_DATE
const CODE_DATE
Definition: eztranslationcache.php:20
eZTranslationCache\storeCache
static storeCache( $key)
Definition: eztranslationcache.php:174
eZTranslationCache\setContextCache
static setContextCache( $contextName, $context)
Definition: eztranslationcache.php:55
eZTranslationCache\resetGlobals
static resetGlobals()
Definition: eztranslationcache.php:207
eZTranslationCache\canRestoreCache
static canRestoreCache( $key, $timestamp)
Definition: eztranslationcache.php:125
eZTranslationCache\cacheTable
static cacheTable()
Definition: eztranslationcache.php:26
eZTranslationCache\restoreCache
static restoreCache( $key)
Definition: eztranslationcache.php:146
$ini
$ini
Definition: classes/admin/linklist.php:64
file_exists
static file_exists( $inifilename)
Definition: eZPBINIFile.php:195
$timestamp
$timestamp
Definition: ezstats/admin/cron.php:35
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.