Merge "Minor CSS cleanup for Vector and Monobook skins"
[mediawiki.git] / maintenance / rebuildLocalisationCache.php
blobdb77564b36171af4bb25b11bf7258336e4d55352
1 <?php
3 /**
4 * Rebuild the localisation cache. Useful if you disabled automatic updates
5 * using $wgLocalisationCacheConf['manualRecache'] = true;
7 * Usage:
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
28 * @file
29 * @ingroup Maintenance
32 require_once( __DIR__ . '/Maintenance.php' );
34 /**
35 * Maintenance script to rebuild the localisation cache.
37 * @ingroup Maintenance
39 class RebuildLocalisationCache extends Maintenance {
40 public function __construct() {
41 parent::__construct();
42 $this->mDescription = "Rebuild the localisation cache";
43 $this->addOption( 'force', 'Rebuild all files, even ones not out of date' );
44 $this->addOption( 'threads', 'Fork more than one thread', false, true );
45 $this->addOption( 'outdir', 'Override the output directory (normally $wgCacheDirectory)',
46 false, true );
49 public function memoryLimit() {
50 if ( $this->hasOption( 'memory-limit' ) ) {
51 return parent::memoryLimit();
53 return '1000M';
56 public function finalSetup() {
57 # This script needs to be run to build the inital l10n cache. But if
58 # $wgLanguageCode is not 'en', it won't be able to run because there is
59 # no l10n cache. Break the cycle by forcing $wgLanguageCode = 'en'.
60 global $wgLanguageCode;
61 $wgLanguageCode = 'en';
62 return parent::finalSetup();
65 public function execute() {
66 global $wgLocalisationCacheConf;
68 $force = $this->hasOption( 'force' );
69 $threads = $this->getOption( 'threads', 1 );
70 if ( $threads < 1 || $threads != intval( $threads ) ) {
71 $this->output( "Invalid thread count specified; running single-threaded.\n" );
72 $threads = 1;
74 if ( $threads > 1 && wfIsWindows() ) {
75 $this->output( "Threaded rebuild is not supported on Windows; running single-threaded.\n" );
76 $threads = 1;
78 if ( $threads > 1 && !function_exists( 'pcntl_fork' ) ) {
79 $this->output( "PHP pcntl extension is not present; running single-threaded.\n" );
80 $threads = 1;
83 $conf = $wgLocalisationCacheConf;
84 $conf['manualRecache'] = false; // Allow fallbacks to create CDB files
85 if ( $force ) {
86 $conf['forceRecache'] = true;
88 if ( $this->hasOption( 'outdir' ) ) {
89 $conf['storeDirectory'] = $this->getOption( 'outdir' );
91 $lc = new LocalisationCache_BulkLoad( $conf );
93 $codes = array_keys( Language::fetchLanguageNames( null, 'mwfile' ) );
94 sort( $codes );
96 // Initialise and split into chunks
97 $numRebuilt = 0;
98 $total = count( $codes );
99 $chunks = array_chunk( $codes, ceil( count( $codes ) / $threads ) );
100 $pids = array();
101 foreach ( $chunks as $codes ) {
102 // Do not fork for only one thread
103 $pid = ( $threads > 1 ) ? pcntl_fork() : -1;
105 if ( $pid === 0 ) {
106 // Child, reseed because there is no bug in PHP:
107 // http://bugs.php.net/bug.php?id=42465
108 mt_srand( getmypid() );
109 $numRebuilt = $this->doRebuild( $codes, $lc, $force );
110 // Abuse the exit value for the count of rebuild languages
111 exit( $numRebuilt );
112 } elseif ( $pid === -1 ) {
113 // Fork failed or one thread, do it serialized
114 $numRebuilt += $this->doRebuild( $codes, $lc, $force );
115 } else {
116 // Main thread
117 $pids[] = $pid;
120 // Wait for all children
121 foreach ( $pids as $pid ) {
122 $status = 0;
123 pcntl_waitpid( $pid, $status );
124 // Fetch the count from the return value
125 $numRebuilt += pcntl_wexitstatus( $status );
128 $this->output( "$numRebuilt languages rebuilt out of $total\n" );
129 if ( $numRebuilt === 0 ) {
130 $this->output( "Use --force to rebuild the caches which are still fresh.\n" );
135 * Helper function to rebuild list of languages codes. Prints the code
136 * for each language which is rebuilt.
137 * @param $codes array List of language codes to rebuild.
138 * @param $lc LocalisationCache Instance of LocalisationCache_BulkLoad (?)
139 * @param $force bool Rebuild up-to-date languages
140 * @return int Number of rebuilt languages
142 private function doRebuild( $codes, $lc, $force ) {
143 $numRebuilt = 0;
144 foreach ( $codes as $code ) {
145 if ( $force || $lc->isExpired( $code ) ) {
146 $this->output( "Rebuilding $code...\n" );
147 $lc->recache( $code );
148 $numRebuilt++;
151 return $numRebuilt;
155 * Sets whether a run of this maintenance script has the force parameter set
157 * @param bool $forced
159 public function setForce( $forced = true ) {
160 $this->mOptions['force'] = $forced;
164 $maintClass = "RebuildLocalisationCache";
165 require_once( RUN_MAINTENANCE_IF_MAIN );