Debian Codegraph @

includes/language/LocalisationCacheBulkLoad.php

raw symbols SCIP
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 * LocalisationCache optimised for loading many languages at once.
23 *
24 * Used by maintenance/rebuildLocalisationCache.php.
25 *
26 * @ingroup Language
27 */
28 class LocalisationCacheBulkLoad extends LocalisationCache {
29
30 /**
31 * A cache for the contents of the data files.
32 * Core files are serialized to avoid using ~1GB of RAM during a re-cache.
33 * @var string[][]
34 */
35 private $fileCache = [];
36
37 /**
38 * Most recently used languages. Uses the linked-list aspect of PHP hashtables
39 * to keep the most recently used language codes at the end of the array, and
40 * the language codes that are ready to be deleted at the beginning.
41 * @var array<string,true>
42 */
43 private $mruLangs = [];
44
45 /**
46 * Maximum number of languages that may be loaded into $this->data
47 * @var int
48 */
49 private $maxLoadedLangs = 10;
50
51 /**
52 * @param string $fileName
53 * @param string $fileType
54 * @return array|mixed
55 */
56 protected function readPHPFile( $fileName, $fileType ) {
57 $serialize = $fileType === 'core';
58 if ( !isset( $this->fileCache[$fileName][$fileType] ) ) {
59 $data = parent::readPHPFile( $fileName, $fileType );
60
61 if ( $serialize ) {
62 $encData = serialize( $data );
63 } else {
64 $encData = $data;
65 }
66
67 $this->fileCache[$fileName][$fileType] = $encData;
68
69 return $data;
70 } elseif ( $serialize ) {
71 return unserialize( $this->fileCache[$fileName][$fileType] );
72 } else {
73 return $this->fileCache[$fileName][$fileType];
74 }
75 }
76
77 /**
78 * @param string $code
79 * @param string $key
80 * @return mixed
81 */
82 public function getItem( $code, $key ) {
83 unset( $this->mruLangs[$code] );
84 $this->mruLangs[$code] = true;
85
86 return parent::getItem( $code, $key );
87 }
88
89 /**
90 * @param string $code
91 * @param string $key
92 * @param string $subkey
93 * @return mixed
94 */
95 public function getSubitem( $code, $key, $subkey ) {
96 unset( $this->mruLangs[$code] );
97 $this->mruLangs[$code] = true;
98
99 return parent::getSubitem( $code, $key, $subkey );
100 }
101
102 /**
103 * @param string $code
104 */
105 public function recache( $code ) {
106 parent::recache( $code );
107 unset( $this->mruLangs[$code] );
108 $this->mruLangs[$code] = true;
109 $this->trimCache();
110 }
111
112 /**
113 * @param string $code
114 */
115 public function unload( $code ) {
116 unset( $this->mruLangs[$code] );
117 parent::unload( $code );
118 }
119
120 /**
121 * Unload cached languages until there are less than $this->maxLoadedLangs
122 */
123 protected function trimCache() {
124 while ( count( $this->data ) > $this->maxLoadedLangs && count( $this->mruLangs ) ) {
125 $code = array_key_first( $this->mruLangs );
126 wfDebug( __METHOD__ . ": unloading $code" );
127 $this->unload( $code );
128 }
129 }
130
131 }