/lib/ezi18n/classes/eztranslationcache.php

https://bitbucket.org/ericsagnes/ezpublish-multisite · PHP · 213 lines · 133 code · 21 blank · 59 comment · 16 complexity · e22f801ea2a8c77b0ac7f447db7ee43a MD5 · raw file

  1. <?php
  2. /**
  3. * File containing the eZTranslationCache class.
  4. *
  5. * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
  6. * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
  7. * @version 2012.8
  8. * @package lib
  9. */
  10. /*!
  11. \class eZTranslationCache eztranslationcache.php
  12. \brief Cache handling for translations.
  13. */
  14. class eZTranslationCache
  15. {
  16. const CODE_DATE = 1058863428;
  17. /*!
  18. \static
  19. \return the cache table which has cache keys and cache data.
  20. */
  21. static function cacheTable()
  22. {
  23. if ( !isset( $GLOBALS['eZTranslationCacheTable'] ) )
  24. {
  25. $GLOBALS['eZTranslationCacheTable'] = array();
  26. }
  27. return $GLOBALS['eZTranslationCacheTable'];
  28. }
  29. /*!
  30. \static
  31. \return the cache translation context which is stored with the cache key \a $contextName.
  32. Returns \c null if no cache data was found.
  33. */
  34. static function contextCache( $contextName )
  35. {
  36. $translationCache = eZTranslationCache::cacheTable();
  37. if ( isset( $translationCache[$contextName] ) )
  38. {
  39. return $translationCache[$contextName]['root'];
  40. }
  41. return null;
  42. }
  43. /*!
  44. \static
  45. Sets the translation context \a $context to be cached with the cache key $contextName.
  46. \note Trying to overwrite and existing cache key will give a warning and fail.
  47. */
  48. static function setContextCache( $contextName, $context )
  49. {
  50. if ( $context === null )
  51. {
  52. return;
  53. }
  54. if ( isset( $GLOBALS['eZTranslationCacheTable'][$contextName] ) )
  55. {
  56. eZDebug::writeWarning( "Translation cache for context '$contextName' already exists", __METHOD__ );
  57. }
  58. else
  59. {
  60. $GLOBALS['eZTranslationCacheTable'][$contextName] = array();
  61. }
  62. $GLOBALS['eZTranslationCacheTable'][$contextName]['root'] = $context;
  63. $GLOBALS['eZTranslationCacheTable'][$contextName]['info'] = array( 'context' => $contextName );
  64. }
  65. /*!
  66. \static
  67. \return the cache directory for translation cache files.
  68. */
  69. static function cacheDirectory()
  70. {
  71. $cacheDirectory =& $GLOBALS['eZTranslationCacheDirectory'];
  72. if ( !isset( $cacheDirectory ) )
  73. {
  74. $ini = eZINI::instance();
  75. $locale = $ini->variable( 'RegionalSettings', 'Locale' );
  76. $rootCacheDirectory = eZTranslationCache::rootCacheDirectory();
  77. $cacheDirectory = eZDir::path( array( $rootCacheDirectory, $locale ) );
  78. }
  79. return $cacheDirectory;
  80. }
  81. /*!
  82. \static
  83. */
  84. static function rootCacheDirectory()
  85. {
  86. $internalCharset = eZTextCodec::internalCharset();
  87. $ini = eZINI::instance();
  88. $translationRepository = $ini->variable( 'RegionalSettings', 'TranslationRepository' );
  89. $translationExtensions = $ini->variable( 'RegionalSettings', 'TranslationExtensions' );
  90. $uniqueParts = array( $internalCharset, $translationRepository, implode( ';', $translationExtensions ) );
  91. $sharedTsCacheDir = $ini->hasVariable( 'RegionalSettings', 'SharedTranslationCacheDir' ) ?
  92. trim( $ini->variable( 'RegionalSettings', 'SharedTranslationCacheDir' ) ) :
  93. '';
  94. if ( $sharedTsCacheDir !== '')
  95. {
  96. $rootCacheDirectory = eZDir::path( array( $sharedTsCacheDir, md5( implode( '-', $uniqueParts ) ) ) );
  97. }
  98. else
  99. {
  100. $rootCacheDirectory = eZDir::path( array( eZSys::cacheDirectory(), 'translation', md5( implode( '-', $uniqueParts ) ) ) );
  101. }
  102. return $rootCacheDirectory;
  103. }
  104. /*!
  105. \static
  106. \return true if the cache with the key \a $key can be restored.
  107. A cache file is found restorable when it exists and has a timestamp
  108. higher or equal to \a $timestamp.
  109. */
  110. static function canRestoreCache( $key, $timestamp )
  111. {
  112. $translationCache = eZTranslationCache::cacheTable();
  113. if ( isset( $translationCache[$key] ) )
  114. {
  115. return false;
  116. }
  117. // $internalCharset = eZTextCodec::internalCharset();
  118. // $cacheFileKey = "$key-$internalCharset";
  119. $cacheFileKey = $key;
  120. $cacheFileName = md5( $cacheFileKey ) . '.php';
  121. $php = new eZPHPCreator( eZTranslationCache::cacheDirectory(), $cacheFileName );
  122. return $php->canRestore( $timestamp );
  123. }
  124. /*!
  125. \static
  126. Loads the cache with the key \a $key from a file and sets the result in the cache table.
  127. \return true if the cache was successfully restored.
  128. */
  129. static function restoreCache( $key )
  130. {
  131. $translationCache = eZTranslationCache::cacheTable();
  132. if ( isset( $translationCache[$key] ) )
  133. {
  134. eZDebug::writeWarning( "Translation cache for key '$key' already exist, cannot restore cache", __METHOD__ );
  135. return false;
  136. }
  137. // $internalCharset = eZTextCodec::internalCharset();
  138. // $cacheFileKey = "$key-$internalCharset";
  139. $cacheFileKey = $key;
  140. $cacheFileName = md5( $cacheFileKey ) . '.php';
  141. $php = new eZPHPCreator( eZTranslationCache::cacheDirectory(), $cacheFileName );
  142. $variables = $php->restore( array( 'info' => 'TranslationInfo',
  143. 'root' => 'TranslationRoot',
  144. 'cache-date' => 'eZTranslationCacheCodeDate' ) );
  145. if ( !isset($variables['cache-date']) || $variables['cache-date'] != self::CODE_DATE )
  146. return false;
  147. eZTranslationCache::setContextCache( $key, $variables['root'] );
  148. return true;
  149. }
  150. /*!
  151. \static
  152. Stores the data of the cache with the key \a $key to a file.
  153. \return false if the cache does not exist.
  154. */
  155. static function storeCache( $key )
  156. {
  157. $translationCache = eZTranslationCache::cacheTable();
  158. if ( !isset( $translationCache[$key] ) )
  159. {
  160. eZDebug::writeWarning( "Translation cache for key '$key' does not exist, cannot store cache", __METHOD__ );
  161. return;
  162. }
  163. $internalCharset = eZTextCodec::internalCharset();
  164. // $cacheFileKey = "$key-$internalCharset";
  165. $cacheFileKey = $key;
  166. $cacheFileName = md5( $cacheFileKey ) . '.php';
  167. $cache =& $translationCache[$key];
  168. if ( !file_exists( eZTranslationCache::cacheDirectory() ) )
  169. {
  170. eZDir::mkdir( eZTranslationCache::cacheDirectory(), false, true );
  171. }
  172. $php = new eZPHPCreator( eZTranslationCache::cacheDirectory(), $cacheFileName );
  173. $php->addRawVariable( 'eZTranslationCacheCodeDate', self::CODE_DATE );
  174. $php->addSpace();
  175. $php->addRawVariable( 'CacheInfo', array( 'charset' => $internalCharset ) );
  176. $php->addRawVariable( 'TranslationInfo', $cache['info'] );
  177. $php->addSpace();
  178. $php->addRawVariable( 'TranslationRoot', $cache['root'] );
  179. $php->store();
  180. }
  181. /*!
  182. \static
  183. Reset values strored in $GLOABLS variable
  184. */
  185. static function resetGlobals()
  186. {
  187. unset( $GLOBALS['eZTranslationCacheDirectory'] );
  188. unset( $GLOBALS['eZTranslationCacheTable'] );
  189. }
  190. }
  191. ?>