MediaWiki REL1_34
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
70 public static function encode( $value ) {
71 if ( is_array( $value ) ) {
72 // [a]rray
73 return [ 'a', array_map( 'LCStoreStaticArray::encode', $value ) ];
74 }
75 if ( is_object( $value ) ) {
76 // [s]erialized
77 return [ 's', serialize( $value ) ];
78 }
79 if ( is_scalar( $value ) || $value === null ) {
80 // Scalar value, written directly without array
81 return $value;
82 }
83
84 throw new RuntimeException( 'Cannot encode ' . var_export( $value, true ) );
85 }
86
94 public static function decode( $encoded ) {
95 if ( !is_array( $encoded ) ) {
96 // Scalar values are written directly without array
97 return $encoded;
98 }
99
100 list( $type, $data ) = $encoded;
101
102 switch ( $type ) {
103 case 'a':
104 return array_map( 'LCStoreStaticArray::decode', $data );
105 case 's':
106 return unserialize( $data );
107 case 'v':
108 // Support: MediaWiki 1.32 and earlier
109 // Backward compatibility with older file format
110 return $data;
111 default:
112 throw new RuntimeException(
113 'Unable to decode ' . var_export( $encoded, true ) );
114 }
115 }
116
117 public function finishWrite() {
118 $writer = new StaticArrayWriter();
119 $out = $writer->create(
120 $this->data[$this->currentLang],
121 'Generated by LCStoreStaticArray.php -- do not edit!'
122 );
123 file_put_contents( $this->fname, $out );
124 // Release the data to manage the memory in rebuildLocalisationCache
125 unset( $this->data[$this->currentLang] );
126 $this->currentLang = null;
127 $this->fname = null;
128 }
129
130 public function get( $code, $key ) {
131 if ( !array_key_exists( $code, $this->data ) ) {
132 $fname = $this->directory . '/' . $code . '.l10n.php';
133 if ( !file_exists( $fname ) ) {
134 return null;
135 }
136 $this->data[$code] = require $fname;
137 }
138 $data = $this->data[$code];
139 if ( array_key_exists( $key, $data ) ) {
140 return self::decode( $data[$key] );
141 }
142 return null;
143 }
144}
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.
MediaWiki exception.
Format a static PHP array to be written to a file.
Interface for the persistence layer of LocalisationCache.
Definition LCStore.php:38