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