X-Git-Url: http://git.cyclocoop.org/%7B%7B%20url_for%28%27admin_vote_del%27%2C%20idvote=vote.voteid%29%20%7D%7D?a=blobdiff_plain;f=includes%2Fcache%2Flocalisation%2FLCStoreStaticArray.php;h=75c8465abfaca524770bffd0d06e678db0a7e772;hb=1cb8f5bff84bff539f4af99750a065c79304e7c0;hp=38700b85b5a2c9baea23878093e0262e335e99fd;hpb=56d45558b102349f3480a46819669407aa3be2d6;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/cache/localisation/LCStoreStaticArray.php b/includes/cache/localisation/LCStoreStaticArray.php index 38700b85b5..75c8465abf 100644 --- a/includes/cache/localisation/LCStoreStaticArray.php +++ b/includes/cache/localisation/LCStoreStaticArray.php @@ -20,6 +20,8 @@ * @file */ +use Wikimedia\StaticArrayWriter; + /** * @since 1.26 */ @@ -39,11 +41,7 @@ class LCStoreStaticArray implements LCStore { public function __construct( $conf = [] ) { global $wgCacheDirectory; - if ( isset( $conf['directory'] ) ) { - $this->directory = $conf['directory']; - } else { - $this->directory = $wgCacheDirectory; - } + $this->directory = $conf['directory'] ?? $wgCacheDirectory; } public function startWrite( $code ) { @@ -70,23 +68,21 @@ class LCStoreStaticArray implements LCStore { * Encodes a value into an array format * * @param mixed $value - * @return array + * @return array|mixed * @throws RuntimeException */ public static function encode( $value ) { - if ( is_scalar( $value ) || $value === null ) { - // [V]alue - return [ 'v', $value ]; + if ( is_array( $value ) ) { + // [a]rray + return [ 'a', array_map( 'LCStoreStaticArray::encode', $value ) ]; } if ( is_object( $value ) ) { - // [S]erialized + // [s]erialized return [ 's', serialize( $value ) ]; } - if ( is_array( $value ) ) { - // [A]rray - return [ 'a', array_map( function ( $v ) { - return LCStoreStaticArray::encode( $v ); - }, $value ) ]; + if ( is_scalar( $value ) || $value === null ) { + // Scalar value, written directly without array + return $value; } throw new RuntimeException( 'Cannot encode ' . var_export( $value, true ) ); @@ -95,23 +91,28 @@ class LCStoreStaticArray implements LCStore { /** * Decode something that was encoded with encode * - * @param array $encoded + * @param mixed $encoded * @return array|mixed * @throws RuntimeException */ - public static function decode( array $encoded ) { + public static function decode( $encoded ) { + if ( !is_array( $encoded ) ) { + // Scalar values are written directly without array + return $encoded; + } + $type = $encoded[0]; $data = $encoded[1]; switch ( $type ) { - case 'v': - return $data; + case 'a': + return array_map( 'LCStoreStaticArray::decode', $data ); case 's': return unserialize( $data ); - case 'a': - return array_map( function ( $v ) { - return LCStoreStaticArray::decode( $v ); - }, $data ); + case 'v': + // Support: MediaWiki 1.32 and earlier + // Backward compatibility with older file format + return $data; default: throw new RuntimeException( 'Unable to decode ' . var_export( $encoded, true ) ); @@ -119,13 +120,12 @@ class LCStoreStaticArray implements LCStore { } public function finishWrite() { - file_put_contents( - $this->fname, - "data[$this->currentLang], true ) . ';' + $writer = new StaticArrayWriter(); + $out = $writer->create( + $this->data[$this->currentLang], + 'Generated by LCStoreStaticArray.php -- do not edit!' ); + file_put_contents( $this->fname, $out ); $this->currentLang = null; $this->fname = null; }