| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
use Wikimedia\StaticArrayWriter; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
class LCStoreStaticArray implements LCStore { |
| 30 |
|
| 31 |
private $currentLang = null; |
| 32 |
|
| 33 |
|
| 34 |
private $data = []; |
| 35 |
|
| 36 |
|
| 37 |
private $fname = null; |
| 38 |
|
| 39 |
|
| 40 |
private $directory; |
| 41 |
|
| 42 |
public function __construct( $conf = [] ) { |
| 43 |
$this->directory = $conf['directory']; |
| 44 |
} |
| 45 |
|
| 46 |
public function startWrite( $code ) { |
| 47 |
if ( !is_dir( $this->directory ) && !wfMkdirParents( $this->directory, null, __METHOD__ ) ) { |
| 48 |
throw new RuntimeException( "Unable to create the localisation store " . |
| 49 |
"directory \"{$this->directory}\"" ); |
| 50 |
} |
| 51 |
|
| 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; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
public function set( $key, $value ) { |
| 61 |
$this->data[$this->currentLang][$key] = self::encode( $value ); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
private static function isValueArray( array $arr ) { |
| 71 |
foreach ( $arr as $value ) { |
| 72 |
if ( is_scalar( $value ) |
| 73 |
|| $value === null |
| 74 |
|| ( is_array( $value ) && self::isValueArray( $value ) ) |
| 75 |
) { |
| 76 |
continue; |
| 77 |
} |
| 78 |
return false; |
| 79 |
} |
| 80 |
return true; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public static function encode( $value ) { |
| 91 |
if ( is_array( $value ) && self::isValueArray( $value ) ) { |
| 92 |
|
| 93 |
|
| 94 |
return [ 'v', $value ]; |
| 95 |
} |
| 96 |
if ( is_array( $value ) || is_object( $value ) ) { |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
return [ 's', serialize( $value ) ]; |
| 101 |
} |
| 102 |
if ( is_scalar( $value ) || $value === null ) { |
| 103 |
|
| 104 |
return $value; |
| 105 |
} |
| 106 |
|
| 107 |
throw new RuntimeException( 'Cannot encode ' . var_export( $value, true ) ); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
public static function decode( $encoded ) { |
| 118 |
if ( !is_array( $encoded ) ) { |
| 119 |
|
| 120 |
return $encoded; |
| 121 |
} |
| 122 |
|
| 123 |
[ $type, $data ] = $encoded; |
| 124 |
|
| 125 |
switch ( $type ) { |
| 126 |
case 'v': |
| 127 |
|
| 128 |
return $data; |
| 129 |
case 's': |
| 130 |
return unserialize( $data ); |
| 131 |
case 'a': |
| 132 |
|
| 133 |
return array_map( [ __CLASS__, 'decode' ], $data ); |
| 134 |
default: |
| 135 |
throw new RuntimeException( |
| 136 |
'Unable to decode ' . var_export( $encoded, true ) ); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
public function finishWrite() { |
| 141 |
$writer = new StaticArrayWriter(); |
| 142 |
$out = $writer->create( |
| 143 |
$this->data[$this->currentLang], |
| 144 |
'Generated by LCStoreStaticArray.php -- do not edit!' |
| 145 |
); |
| 146 |
|
| 147 |
|
| 148 |
$tmpFileName = "{$this->fname}.tmp." . getmypid() . '.' . mt_rand(); |
| 149 |
file_put_contents( $tmpFileName, $out ); |
| 150 |
rename( $tmpFileName, $this->fname ); |
| 151 |
|
| 152 |
unset( $this->data[$this->currentLang] ); |
| 153 |
$this->currentLang = null; |
| 154 |
$this->fname = null; |
| 155 |
} |
| 156 |
|
| 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 ) ) { |
| 161 |
return null; |
| 162 |
} |
| 163 |
$this->data[$code] = require $fname; |
| 164 |
} |
| 165 |
$data = $this->data[$code]; |
| 166 |
if ( array_key_exists( $key, $data ) ) { |
| 167 |
return self::decode( $data[$key] ); |
| 168 |
} |
| 169 |
return null; |
| 170 |
} |
| 171 |
} |