Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

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