AuthManager: Break AuthPlugin::addUser more explicitly
[mediawiki.git] / includes / cache / localisation / LocalisationCacheBulkLoad.php
blob30c7d37516dbce7f97054576f4f8d6a82cefda50
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.
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.
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
18 * @file
21 /**
22 * A localisation cache optimised for loading large amounts of data for many
23 * languages. Used by rebuildLocalisationCache.php.
25 class LocalisationCacheBulkLoad extends LocalisationCache {
27 /**
28 * A cache of the contents of data files.
29 * Core files are serialized to avoid using ~1GB of RAM during a recache.
31 private $fileCache = [];
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.
38 private $mruLangs = [];
40 /**
41 * Maximum number of languages that may be loaded into $this->data
43 private $maxLoadedLangs = 10;
45 /**
46 * @param string $fileName
47 * @param string $fileType
48 * @return array|mixed
50 protected function readPHPFile( $fileName, $fileType ) {
51 $serialize = $fileType === 'core';
52 if ( !isset( $this->fileCache[$fileName][$fileType] ) ) {
53 $data = parent::readPHPFile( $fileName, $fileType );
55 if ( $serialize ) {
56 $encData = serialize( $data );
57 } else {
58 $encData = $data;
61 $this->fileCache[$fileName][$fileType] = $encData;
63 return $data;
64 } elseif ( $serialize ) {
65 return unserialize( $this->fileCache[$fileName][$fileType] );
66 } else {
67 return $this->fileCache[$fileName][$fileType];
71 /**
72 * @param string $code
73 * @param string $key
74 * @return mixed
76 public function getItem( $code, $key ) {
77 unset( $this->mruLangs[$code] );
78 $this->mruLangs[$code] = true;
80 return parent::getItem( $code, $key );
83 /**
84 * @param string $code
85 * @param string $key
86 * @param string $subkey
87 * @return mixed
89 public function getSubitem( $code, $key, $subkey ) {
90 unset( $this->mruLangs[$code] );
91 $this->mruLangs[$code] = true;
93 return parent::getSubitem( $code, $key, $subkey );
96 /**
97 * @param string $code
99 public function recache( $code ) {
100 parent::recache( $code );
101 unset( $this->mruLangs[$code] );
102 $this->mruLangs[$code] = true;
103 $this->trimCache();
107 * @param string $code
109 public function unload( $code ) {
110 unset( $this->mruLangs[$code] );
111 parent::unload( $code );
115 * Unload cached languages until there are less than $this->maxLoadedLangs
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 );