Services: Convert LocalisationCache's static to a const now HHVM is gone
[mediawiki.git] / maintenance / rebuildLocalisationCache.php
blob07c55698df436b654488506c71c5752d6d037289
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 use MediaWiki\Config\ServiceOptions;
33 use MediaWiki\Logger\LoggerFactory;
34 use MediaWiki\MediaWikiServices;
36 require_once __DIR__ . '/Maintenance.php';
38 /**
39 * Maintenance script to rebuild the localisation cache.
41 * @ingroup Maintenance
43 class RebuildLocalisationCache extends Maintenance {
44 public function __construct() {
45 parent::__construct();
46 $this->addDescription( 'Rebuild the localisation cache' );
47 $this->addOption( 'force', 'Rebuild all files, even ones not out of date' );
48 $this->addOption( 'threads', 'Fork more than one thread', false, true );
49 $this->addOption( 'outdir', 'Override the output directory (normally $wgCacheDirectory)',
50 false, true );
51 $this->addOption( 'lang', 'Only rebuild these languages, comma separated.',
52 false, true );
55 public function finalSetup() {
56 # This script needs to be run to build the inital l10n cache. But if
57 # $wgLanguageCode is not 'en', it won't be able to run because there is
58 # no l10n cache. Break the cycle by forcing $wgLanguageCode = 'en'.
59 global $wgLanguageCode;
60 $wgLanguageCode = 'en';
61 parent::finalSetup();
64 public function execute() {
65 global $wgLocalisationCacheConf, $wgCacheDirectory;
67 $force = $this->hasOption( 'force' );
68 $threads = $this->getOption( 'threads', 1 );
69 if ( $threads < 1 || $threads != intval( $threads ) ) {
70 $this->output( "Invalid thread count specified; running single-threaded.\n" );
71 $threads = 1;
73 if ( $threads > 1 && wfIsWindows() ) {
74 $this->output( "Threaded rebuild is not supported on Windows; running single-threaded.\n" );
75 $threads = 1;
77 if ( $threads > 1 && !function_exists( 'pcntl_fork' ) ) {
78 $this->output( "PHP pcntl extension is not present; running single-threaded.\n" );
79 $threads = 1;
82 $conf = $wgLocalisationCacheConf;
83 $conf['manualRecache'] = false; // Allow fallbacks to create CDB files
84 $conf['forceRecache'] = $force || !empty( $conf['forceRecache'] );
85 if ( $this->hasOption( 'outdir' ) ) {
86 $conf['storeDirectory'] = $this->getOption( 'outdir' );
88 // XXX Copy-pasted from ServiceWiring.php. Do we need a factory for this one caller?
89 $lc = new LocalisationCacheBulkLoad(
90 new ServiceOptions(
91 LocalisationCache::CONSTRUCTOR_OPTIONS,
92 $conf,
93 MediaWikiServices::getInstance()->getMainConfig()
95 LocalisationCache::getStoreFromConf( $conf, $wgCacheDirectory ),
96 LoggerFactory::getInstance( 'localisation' ),
97 [ function () {
98 MediaWikiServices::getInstance()->getResourceLoader()
99 ->getMessageBlobStore()->clear();
103 $allCodes = array_keys( Language::fetchLanguageNames( null, 'mwfile' ) );
104 if ( $this->hasOption( 'lang' ) ) {
105 # Validate requested languages
106 $codes = array_intersect( $allCodes,
107 explode( ',', $this->getOption( 'lang' ) ) );
108 # Bailed out if nothing is left
109 if ( count( $codes ) == 0 ) {
110 $this->fatalError( 'None of the languages specified exists.' );
112 } else {
113 # By default get all languages
114 $codes = $allCodes;
116 sort( $codes );
118 // Initialise and split into chunks
119 $numRebuilt = 0;
120 $total = count( $codes );
121 $chunks = array_chunk( $codes, ceil( count( $codes ) / $threads ) );
122 $pids = [];
123 $parentStatus = 0;
124 foreach ( $chunks as $codes ) {
125 // Do not fork for only one thread
126 $pid = ( $threads > 1 ) ? pcntl_fork() : -1;
128 if ( $pid === 0 ) {
129 // Child, reseed because there is no bug in PHP:
130 // https://bugs.php.net/bug.php?id=42465
131 mt_srand( getmypid() );
133 $this->doRebuild( $codes, $lc, $force );
134 exit( 0 );
135 } elseif ( $pid === -1 ) {
136 // Fork failed or one thread, do it serialized
137 $numRebuilt += $this->doRebuild( $codes, $lc, $force );
138 } else {
139 // Main thread
140 $pids[] = $pid;
143 // Wait for all children
144 foreach ( $pids as $pid ) {
145 $status = 0;
146 pcntl_waitpid( $pid, $status );
147 if ( pcntl_wexitstatus( $status ) ) {
148 // Pass a fatal error code through to the caller
149 $parentStatus = pcntl_wexitstatus( $status );
153 if ( !$pids ) {
154 $this->output( "$numRebuilt languages rebuilt out of $total\n" );
155 if ( $numRebuilt === 0 ) {
156 $this->output( "Use --force to rebuild the caches which are still fresh.\n" );
159 if ( $parentStatus ) {
160 exit( $parentStatus );
165 * Helper function to rebuild list of languages codes. Prints the code
166 * for each language which is rebuilt.
167 * @param array $codes List of language codes to rebuild.
168 * @param LocalisationCache $lc Instance of LocalisationCacheBulkLoad (?)
169 * @param bool $force Rebuild up-to-date languages
170 * @return int Number of rebuilt languages
172 private function doRebuild( $codes, $lc, $force ) {
173 $numRebuilt = 0;
174 foreach ( $codes as $code ) {
175 if ( $force || $lc->isExpired( $code ) ) {
176 $this->output( "Rebuilding $code...\n" );
177 $lc->recache( $code );
178 $numRebuilt++;
182 return $numRebuilt;
186 * Sets whether a run of this maintenance script has the force parameter set
188 * @param bool $forced
190 public function setForce( $forced = true ) {
191 $this->mOptions['force'] = $forced;
195 $maintClass = RebuildLocalisationCache::class;
196 require_once RUN_MAINTENANCE_IF_MAIN;