MediaWiki REL1_37
LCStoreStaticArray.php
Go to the documentation of this file.
1<?php
24
28class LCStoreStaticArray implements LCStore {
30 private $currentLang = null;
31
33 private $data = [];
34
36 private $fname = null;
37
39 private $directory;
40
41 public function __construct( $conf = [] ) {
42 $this->directory = $conf['directory'];
43 }
44
45 public function startWrite( $code ) {
46 if ( !file_exists( $this->directory ) && !wfMkdirParents( $this->directory, null, __METHOD__ ) ) {
47 throw new MWException( "Unable to create the localisation store " .
48 "directory \"{$this->directory}\"" );
49 }
50
51 $this->currentLang = $code;
52 $this->fname = $this->directory . '/' . $code . '.l10n.php';
53 $this->data[$code] = [];
54 if ( file_exists( $this->fname ) ) {
55 $this->data[$code] = require $this->fname;
56 }
57 }
58
59 public function set( $key, $value ) {
60 $this->data[$this->currentLang][$key] = self::encode( $value );
61 }
62
69 private static function isValueArray( array $arr ) {
70 foreach ( $arr as $key => $value ) {
71 if ( is_scalar( $value )
72 || $value === null
73 || ( is_array( $value ) && self::isValueArray( $value ) )
74 ) {
75 continue;
76 }
77 return false;
78 }
79 return true;
80 }
81
89 public static function encode( $value ) {
90 if ( is_array( $value ) && self::isValueArray( $value ) ) {
91 // Type: scalar [v]alue.
92 // Optimization: Write large arrays as one value to avoid recursive decoding cost.
93 return [ 'v', $value ];
94 }
95 if ( is_array( $value ) || is_object( $value ) ) {
96 // Type: [s]seralized.
97 // Optimization: Avoid recursive decoding cost. Write arrays with an objects
98 // as one serialised value.
99 return [ 's', serialize( $value ) ];
100 }
101 if ( is_scalar( $value ) || $value === null ) {
102 // Optimization: Reduce file size by not wrapping scalar values.
103 return $value;
104 }
105
106 throw new RuntimeException( 'Cannot encode ' . var_export( $value, true ) );
107 }
108
116 public static function decode( $encoded ) {
117 if ( !is_array( $encoded ) ) {
118 // Unwrapped scalar value
119 return $encoded;
120 }
121
122 list( $type, $data ) = $encoded;
123
124 switch ( $type ) {
125 case 'v':
126 // Value array (1.35+) or unwrapped scalar value (1.32 and earlier)
127 return $data;
128 case 's':
129 return unserialize( $data );
130 case 'a':
131 // Support: MediaWiki 1.34 and earlier (older file format)
132 return array_map( [ __CLASS__, 'decode' ], $data );
133 default:
134 throw new RuntimeException(
135 'Unable to decode ' . var_export( $encoded, true ) );
136 }
137 }
138
139 public function finishWrite() {
140 $writer = new StaticArrayWriter();
141 $out = $writer->create(
142 $this->data[$this->currentLang],
143 'Generated by LCStoreStaticArray.php -- do not edit!'
144 );
145 file_put_contents( $this->fname, $out );
146 // Release the data to manage the memory in rebuildLocalisationCache
147 unset( $this->data[$this->currentLang] );
148 $this->currentLang = null;
149 $this->fname = null;
150 }
151
152 public function get( $code, $key ) {
153 if ( !array_key_exists( $code, $this->data ) ) {
154 $fname = $this->directory . '/' . $code . '.l10n.php';
155 if ( !file_exists( $fname ) ) {
156 return null;
157 }
158 $this->data[$code] = require $fname;
159 }
160 $data = $this->data[$code];
161 if ( array_key_exists( $key, $data ) ) {
162 return self::decode( $data[$key] );
163 }
164 return null;
165 }
166}
serialize()
unserialize( $serialized)
wfMkdirParents( $dir, $mode=null, $caller=null)
Make directory, and make all parent directories if they don't exist.
finishWrite()
Finish a write transaction.
static encode( $value)
Encodes a value into an array format.
string $fname
File name.
static decode( $encoded)
Decode something that was encoded with encode.
array $data
Localisation data.
startWrite( $code)
Start a write transaction.
string $directory
Directory for cache files.
string null $currentLang
Current language code.
static isValueArray(array $arr)
Determine whether this array contains only scalar values.
MediaWiki exception.
Format a static PHP array to be written to a file.
Interface for the persistence layer of LocalisationCache.
Definition LCStore.php:38