LCStoreStaticArray: create cache directory when it doesn't exist
[lhc/web/wiklou.git] / includes / cache / localisation / LCStoreStaticArray.php
1 <?php
2 /**
3 * Localisation cache storage based on PHP files and static arrays.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 /**
24 * @since 1.26
25 */
26 class LCStoreStaticArray implements LCStore {
27 /** @var string|null Current language code. */
28 private $currentLang = null;
29
30 /** @var array Localisation data. */
31 private $data = [];
32
33 /** @var string File name. */
34 private $fname = null;
35
36 /** @var string Directory for cache files. */
37 private $directory;
38
39 public function __construct( $conf = [] ) {
40 global $wgCacheDirectory;
41
42 if ( isset( $conf['directory'] ) ) {
43 $this->directory = $conf['directory'];
44 } else {
45 $this->directory = $wgCacheDirectory;
46 }
47 }
48
49 public function startWrite( $code ) {
50 if ( !file_exists( $this->directory ) ) {
51 if ( !wfMkdirParents( $this->directory, null, __METHOD__ ) ) {
52 throw new MWException( "Unable to create the localisation store " .
53 "directory \"{$this->directory}\"" );
54 }
55 }
56
57 $this->currentLang = $code;
58 $this->fname = $this->directory . '/' . $code . '.l10n.php';
59 $this->data[$code] = [];
60 if ( file_exists( $this->fname ) ) {
61 $this->data[$code] = require $this->fname;
62 }
63 }
64
65 public function set( $key, $value ) {
66 $this->data[$this->currentLang][$key] = self::encode( $value );
67 }
68
69 /**
70 * Encodes a value into an array format
71 *
72 * @param mixed $value
73 * @return array
74 * @throws RuntimeException
75 */
76 public static function encode( $value ) {
77 if ( is_scalar( $value ) || $value === null ) {
78 // [V]alue
79 return [ 'v', $value ];
80 }
81 if ( is_object( $value ) ) {
82 // [S]erialized
83 return [ 's', serialize( $value ) ];
84 }
85 if ( is_array( $value ) ) {
86 // [A]rray
87 return [ 'a', array_map( function ( $v ) {
88 return LCStoreStaticArray::encode( $v );
89 }, $value ) ];
90 }
91
92 throw new RuntimeException( 'Cannot encode ' . var_export( $value, true ) );
93 }
94
95 /**
96 * Decode something that was encoded with encode
97 *
98 * @param array $encoded
99 * @return array|mixed
100 * @throws RuntimeException
101 */
102 public static function decode( array $encoded ) {
103 $type = $encoded[0];
104 $data = $encoded[1];
105
106 switch ( $type ) {
107 case 'v':
108 return $data;
109 case 's':
110 return unserialize( $data );
111 case 'a':
112 return array_map( function ( $v ) {
113 return LCStoreStaticArray::decode( $v );
114 }, $data );
115 default:
116 throw new RuntimeException(
117 'Unable to decode ' . var_export( $encoded, true ) );
118 }
119 }
120
121 public function finishWrite() {
122 file_put_contents(
123 $this->fname,
124 "<?php\n" .
125 "// Generated by LCStoreStaticArray.php -- do not edit!\n" .
126 "return " .
127 var_export( $this->data[$this->currentLang], true ) . ';'
128 );
129 $this->currentLang = null;
130 $this->fname = null;
131 }
132
133 public function get( $code, $key ) {
134 if ( !array_key_exists( $code, $this->data ) ) {
135 $fname = $this->directory . '/' . $code . '.l10n.php';
136 if ( !file_exists( $fname ) ) {
137 return null;
138 }
139 $this->data[$code] = require $fname;
140 }
141 $data = $this->data[$code];
142 if ( array_key_exists( $key, $data ) ) {
143 return self::decode( $data[$key] );
144 }
145 return null;
146 }
147 }