From 1cb8f5bff84bff539f4af99750a065c79304e7c0 Mon Sep 17 00:00:00 2001 From: Seb35 Date: Mon, 24 Sep 2018 00:52:56 +0200 Subject: [PATCH] localisation: Make PHP cache files slimmer The only difference is the scalar values are no longer encoded as an array, but just the value itself. As a result, PHP localisation cache files are about 2/3rds of their previous size and become slightly smaller than CDB files. Bug: T99740 Change-Id: Iaa5e32830dc1bb710b9e0f1a681afe91e521ece9 --- .../cache/localisation/LCStoreStaticArray.php | 35 +++++++++++-------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/includes/cache/localisation/LCStoreStaticArray.php b/includes/cache/localisation/LCStoreStaticArray.php index c5a2512f50..75c8465abf 100644 --- a/includes/cache/localisation/LCStoreStaticArray.php +++ b/includes/cache/localisation/LCStoreStaticArray.php @@ -68,21 +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( 'LCStoreStaticArray::encode', $value ) ]; + if ( is_scalar( $value ) || $value === null ) { + // Scalar value, written directly without array + return $value; } throw new RuntimeException( 'Cannot encode ' . var_export( $value, true ) ); @@ -91,21 +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 's': - return unserialize( $data ); case 'a': return array_map( 'LCStoreStaticArray::decode', $data ); + case 's': + return unserialize( $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 ) ); -- 2.20.1