4 * Rebuild the localisation cache. Useful if you disabled automatic updates
5 * using $wgLocalisationCacheConf['manualRecache'] = true;
8 * php rebuildLocalisationCache.php [--force] [--threads=N]
10 * Use --force to rebuild all files, even the ones that are not out of date.
11 * Use --threads=N to fork more threads.
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 * http://www.gnu.org/copyleft/gpl.html
29 * @ingroup Maintenance
32 use MediaWiki\MediaWikiServices
;
34 require_once __DIR__
. '/Maintenance.php';
37 * Maintenance script to rebuild the localisation cache.
39 * @ingroup Maintenance
41 class RebuildLocalisationCache
extends Maintenance
{
42 public function __construct() {
43 parent
::__construct();
44 $this->addDescription( 'Rebuild the localisation cache' );
45 $this->addOption( 'force', 'Rebuild all files, even ones not out of date' );
46 $this->addOption( 'threads', 'Fork more than one thread', false, true );
47 $this->addOption( 'outdir', 'Override the output directory (normally $wgCacheDirectory)',
49 $this->addOption( 'lang', 'Only rebuild these languages, comma separated.',
53 public function finalSetup() {
54 # This script needs to be run to build the inital l10n cache. But if
55 # $wgLanguageCode is not 'en', it won't be able to run because there is
56 # no l10n cache. Break the cycle by forcing $wgLanguageCode = 'en'.
57 global $wgLanguageCode;
58 $wgLanguageCode = 'en';
62 public function execute() {
63 global $wgLocalisationCacheConf;
65 $force = $this->hasOption( 'force' );
66 $threads = $this->getOption( 'threads', 1 );
67 if ( $threads < 1 ||
$threads != intval( $threads ) ) {
68 $this->output( "Invalid thread count specified; running single-threaded.\n" );
71 if ( $threads > 1 && wfIsWindows() ) {
72 $this->output( "Threaded rebuild is not supported on Windows; running single-threaded.\n" );
75 if ( $threads > 1 && !function_exists( 'pcntl_fork' ) ) {
76 $this->output( "PHP pcntl extension is not present; running single-threaded.\n" );
80 $conf = $wgLocalisationCacheConf;
81 $conf['manualRecache'] = false; // Allow fallbacks to create CDB files
83 $conf['forceRecache'] = true;
85 if ( $this->hasOption( 'outdir' ) ) {
86 $conf['storeDirectory'] = $this->getOption( 'outdir' );
88 $lc = new LocalisationCacheBulkLoad( $conf );
90 $allCodes = array_keys( Language
::fetchLanguageNames( null, 'mwfile' ) );
91 if ( $this->hasOption( 'lang' ) ) {
92 # Validate requested languages
93 $codes = array_intersect( $allCodes,
94 explode( ',', $this->getOption( 'lang' ) ) );
95 # Bailed out if nothing is left
96 if ( count( $codes ) == 0 ) {
97 $this->error( 'None of the languages specified exists.', 1 );
100 # By default get all languages
105 // Initialise and split into chunks
107 $total = count( $codes );
108 $chunks = array_chunk( $codes, ceil( count( $codes ) / $threads ) );
111 foreach ( $chunks as $codes ) {
112 // Do not fork for only one thread
113 $pid = ( $threads > 1 ) ?
pcntl_fork() : -1;
116 // Reset services, so we don't re-use connections.
117 MediaWikiServices
::resetChildProcessServices();
119 $this->doRebuild( $codes, $lc, $force );
121 } elseif ( $pid === -1 ) {
122 // Fork failed or one thread, do it serialized
123 $numRebuilt +
= $this->doRebuild( $codes, $lc, $force );
129 // Wait for all children
130 foreach ( $pids as $pid ) {
132 pcntl_waitpid( $pid, $status );
133 if ( pcntl_wexitstatus( $status ) ) {
134 // Pass a fatal error code through to the caller
135 $parentStatus = pcntl_wexitstatus( $status );
140 $this->output( "$numRebuilt languages rebuilt out of $total\n" );
141 if ( $numRebuilt === 0 ) {
142 $this->output( "Use --force to rebuild the caches which are still fresh.\n" );
145 if ( $parentStatus ) {
146 exit( $parentStatus );
151 * Helper function to rebuild list of languages codes. Prints the code
152 * for each language which is rebuilt.
153 * @param array $codes List of language codes to rebuild.
154 * @param LocalisationCache $lc Instance of LocalisationCacheBulkLoad (?)
155 * @param bool $force Rebuild up-to-date languages
156 * @return int Number of rebuilt languages
158 private function doRebuild( $codes, $lc, $force ) {
160 foreach ( $codes as $code ) {
161 if ( $force ||
$lc->isExpired( $code ) ) {
162 $this->output( "Rebuilding $code...\n" );
163 $lc->recache( $code );
172 * Sets whether a run of this maintenance script has the force parameter set
174 * @param bool $forced
176 public function setForce( $forced = true ) {
177 $this->mOptions
['force'] = $forced;
181 $maintClass = "RebuildLocalisationCache";
182 require_once RUN_MAINTENANCE_IF_MAIN
;