Merge "Added a separate error message for mkdir failures"
[lhc/web/wiklou.git] / includes / cache / localisation / LocalisationCacheBulkLoad.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 /**
22 * A localisation cache optimised for loading large amounts of data for many
23 * languages. Used by rebuildLocalisationCache.php.
24 */
25 class LocalisationCacheBulkLoad extends LocalisationCache {
26
27 /**
28 * A cache of the contents of data files.
29 * Core files are serialized to avoid using ~1GB of RAM during a recache.
30 */
31 private $fileCache = [];
32
33 /**
34 * Most recently used languages. Uses the linked-list aspect of PHP hashtables
35 * to keep the most recently used language codes at the end of the array, and
36 * the language codes that are ready to be deleted at the beginning.
37 */
38 private $mruLangs = [];
39
40 /**
41 * Maximum number of languages that may be loaded into $this->data
42 */
43 private $maxLoadedLangs = 10;
44
45 /**
46 * @param string $fileName
47 * @param string $fileType
48 * @return array|mixed
49 */
50 protected function readPHPFile( $fileName, $fileType ) {
51 $serialize = $fileType === 'core';
52 if ( !isset( $this->fileCache[$fileName][$fileType] ) ) {
53 $data = parent::readPHPFile( $fileName, $fileType );
54
55 if ( $serialize ) {
56 $encData = serialize( $data );
57 } else {
58 $encData = $data;
59 }
60
61 $this->fileCache[$fileName][$fileType] = $encData;
62
63 return $data;
64 } elseif ( $serialize ) {
65 return unserialize( $this->fileCache[$fileName][$fileType] );
66 } else {
67 return $this->fileCache[$fileName][$fileType];
68 }
69 }
70
71 /**
72 * @param string $code
73 * @param string $key
74 * @return mixed
75 */
76 public function getItem( $code, $key ) {
77 unset( $this->mruLangs[$code] );
78 $this->mruLangs[$code] = true;
79
80 return parent::getItem( $code, $key );
81 }
82
83 /**
84 * @param string $code
85 * @param string $key
86 * @param string $subkey
87 * @return mixed
88 */
89 public function getSubitem( $code, $key, $subkey ) {
90 unset( $this->mruLangs[$code] );
91 $this->mruLangs[$code] = true;
92
93 return parent::getSubitem( $code, $key, $subkey );
94 }
95
96 /**
97 * @param string $code
98 */
99 public function recache( $code ) {
100 parent::recache( $code );
101 unset( $this->mruLangs[$code] );
102 $this->mruLangs[$code] = true;
103 $this->trimCache();
104 }
105
106 /**
107 * @param string $code
108 */
109 public function unload( $code ) {
110 unset( $this->mruLangs[$code] );
111 parent::unload( $code );
112 }
113
114 /**
115 * Unload cached languages until there are less than $this->maxLoadedLangs
116 */
117 protected function trimCache() {
118 while ( count( $this->data ) > $this->maxLoadedLangs && count( $this->mruLangs ) ) {
119 reset( $this->mruLangs );
120 $code = key( $this->mruLangs );
121 wfDebug( __METHOD__ . ": unloading $code\n" );
122 $this->unload( $code );
123 }
124 }
125
126 }