31 private $currentLang =
null;
37 private $fname =
null;
43 $this->directory = $conf[
'directory'];
47 if ( !is_dir( $this->directory ) && !
wfMkdirParents( $this->directory,
null, __METHOD__ ) ) {
48 throw new MWException(
"Unable to create the localisation store " .
49 "directory \"{$this->directory}\"" );
52 $this->currentLang = $code;
53 $this->fname = $this->directory .
'/' . $code .
'.l10n.php';
54 $this->data[$code] = [];
55 if ( is_file( $this->fname ) ) {
56 $this->data[$code] = require $this->fname;
60 public function set( $key, $value ) {
61 $this->data[$this->currentLang][$key] =
self::encode( $value );
70 private static function isValueArray( array $arr ) {
71 foreach ( $arr as $value ) {
72 if ( is_scalar( $value )
74 || ( is_array( $value ) && self::isValueArray( $value ) )
90 public static function encode( $value ) {
91 if ( is_array( $value ) && self::isValueArray( $value ) ) {
94 return [
'v', $value ];
96 if ( is_array( $value ) || is_object( $value ) ) {
100 return [
's', serialize( $value ) ];
102 if ( is_scalar( $value ) || $value ===
null ) {
107 throw new RuntimeException(
'Cannot encode ' . var_export( $value,
true ) );
117 public static function decode( $encoded ) {
118 if ( !is_array( $encoded ) ) {
123 [
$type, $data ] = $encoded;
130 return unserialize( $data );
133 return array_map( [ __CLASS__,
'decode' ], $data );
135 throw new RuntimeException(
136 'Unable to decode ' . var_export( $encoded,
true ) );
142 $out = $writer->create(
143 $this->data[$this->currentLang],
144 'Generated by LCStoreStaticArray.php -- do not edit!'
148 $tmpFileName =
"{$this->fname}.tmp." . getmypid() .
'.' . mt_rand();
149 file_put_contents( $tmpFileName, $out );
150 rename( $tmpFileName, $this->fname );
152 unset( $this->data[$this->currentLang] );
153 $this->currentLang =
null;
157 public function get( $code, $key ) {
158 if ( !array_key_exists( $code, $this->data ) ) {
159 $fname = $this->directory .
'/' . $code .
'.l10n.php';
160 if ( !is_file( $fname ) ) {
163 $this->data[$code] = require $fname;
165 $data = $this->data[$code];
166 if ( array_key_exists( $key, $data ) ) {
wfMkdirParents( $dir, $mode=null, $caller=null)
Make directory, and make all parent directories if they don't exist.
Localisation cache storage based on PHP files and static arrays.
finishWrite()
Finish a write transaction.
static encode( $value)
Encodes a value into an array format.
static decode( $encoded)
Decode something that was encoded with encode.
startWrite( $code)
Start a write transaction.
Interface for the persistence layer of LocalisationCache.