Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
LocalisationCacheBulkLoad
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 6
132
0.00% covered (danger)
0.00%
0 / 1
 readPHPFile
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
20
 getItem
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getSubitem
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 recache
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 unload
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 trimCache
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
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 */
28class 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     */
34    private $fileCache = [];
35
36    /**
37     * Most recently used languages. Uses the linked-list aspect of PHP hashtables
38     * to keep the most recently used language codes at the end of the array, and
39     * the language codes that are ready to be deleted at the beginning.
40     */
41    private $mruLangs = [];
42
43    /**
44     * Maximum number of languages that may be loaded into $this->data
45     */
46    private $maxLoadedLangs = 10;
47
48    /**
49     * @param string $fileName
50     * @param string $fileType
51     * @return array|mixed
52     */
53    protected function readPHPFile( $fileName, $fileType ) {
54        $serialize = $fileType === 'core';
55        if ( !isset( $this->fileCache[$fileName][$fileType] ) ) {
56            $data = parent::readPHPFile( $fileName, $fileType );
57
58            if ( $serialize ) {
59                $encData = serialize( $data );
60            } else {
61                $encData = $data;
62            }
63
64            $this->fileCache[$fileName][$fileType] = $encData;
65
66            return $data;
67        } elseif ( $serialize ) {
68            return unserialize( $this->fileCache[$fileName][$fileType] );
69        } else {
70            return $this->fileCache[$fileName][$fileType];
71        }
72    }
73
74    /**
75     * @param string $code
76     * @param string $key
77     * @return mixed
78     */
79    public function getItem( $code, $key ) {
80        unset( $this->mruLangs[$code] );
81        $this->mruLangs[$code] = true;
82
83        return parent::getItem( $code, $key );
84    }
85
86    /**
87     * @param string $code
88     * @param string $key
89     * @param string $subkey
90     * @return mixed
91     */
92    public function getSubitem( $code, $key, $subkey ) {
93        unset( $this->mruLangs[$code] );
94        $this->mruLangs[$code] = true;
95
96        return parent::getSubitem( $code, $key, $subkey );
97    }
98
99    /**
100     * @param string $code
101     */
102    public function recache( $code ) {
103        parent::recache( $code );
104        unset( $this->mruLangs[$code] );
105        $this->mruLangs[$code] = true;
106        $this->trimCache();
107    }
108
109    /**
110     * @param string $code
111     */
112    public function unload( $code ) {
113        unset( $this->mruLangs[$code] );
114        parent::unload( $code );
115    }
116
117    /**
118     * Unload cached languages until there are less than $this->maxLoadedLangs
119     */
120    protected function trimCache() {
121        while ( count( $this->data ) > $this->maxLoadedLangs && count( $this->mruLangs ) ) {
122            $code = array_key_first( $this->mruLangs );
123            wfDebug( __METHOD__ . ": unloading $code" );
124            $this->unload( $code );
125        }
126    }
127
128}