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\Config\ServiceOptions
;
33 use MediaWiki\Languages\LanguageNameUtils
;
34 use MediaWiki\Logger\LoggerFactory
;
35 use MediaWiki\MainConfigNames
;
36 use MediaWiki\Maintenance\Maintenance
;
37 use MediaWiki\ResourceLoader\MessageBlobStore
;
38 use MediaWiki\Settings\SettingsBuilder
;
40 // @codeCoverageIgnoreStart
41 require_once __DIR__
. '/Maintenance.php';
42 // @codeCoverageIgnoreEnd
45 * Maintenance script to rebuild the localisation cache.
47 * @ingroup Maintenance
49 class RebuildLocalisationCache
extends Maintenance
{
50 public function __construct() {
51 parent
::__construct();
52 $this->addDescription( 'Rebuild the localisation cache' );
53 $this->addOption( 'dry-run', 'Determine what languages need to be rebuilt without changing anything' );
54 $this->addOption( 'force', 'Rebuild all files, even ones not out of date' );
55 $this->addOption( 'threads', 'Fork more than one thread', false, true );
56 $this->addOption( 'outdir', 'Override the output directory (normally $wgCacheDirectory)',
58 $this->addOption( 'lang', 'Only rebuild these languages, comma separated.',
62 'Override the LC store class (normally $wgLocalisationCacheConf[\'storeClass\'])',
68 'EXPERIMENTAL: Disable the database backend. Setting this option will result in an error ' .
69 'if you have extensions or use site configuration that need the database. This is an ' .
70 'experimental feature to allow offline building of the localisation cache. Known limitations:' .
72 '* Incompatible with LCStoreDB, which always requires a database. ' . "\n" .
73 '* The message purge may require a database. See --skip-message-purge.'
75 // T237148: The Gadget extension (bundled with MediaWiki by default) requires a database`
76 // connection to register its modules for MessageBlobStore.
79 'Skip purging of MessageBlobStore. The purge operation may require a database, depending ' .
80 'on the configuration and extensions on this wiki. If skipping the purge now, you need to ' .
81 'run purgeMessageBlobStore.php shortly after deployment.'
85 "Don't print a message for each rebuilt language file. Use this instead of " .
86 "--quiet to get a brief summary of the operation."
90 public function finalSetup( SettingsBuilder
$settingsBuilder ) {
91 # This script needs to be run to build the initial l10n cache. But if
92 # LanguageCode is not 'en', it won't be able to run because there is
93 # no l10n cache. Break the cycle by forcing the LanguageCode setting to 'en'.
94 $settingsBuilder->putConfigValue( MainConfigNames
::LanguageCode
, 'en' );
95 parent
::finalSetup( $settingsBuilder );
98 public function execute() {
99 $force = $this->hasOption( 'force' );
100 $threads = $this->getOption( 'threads', 1 );
101 if ( $threads < 1 ||
$threads != intval( $threads ) ) {
102 $this->output( "Invalid thread count specified; running single-threaded.\n" );
105 if ( $threads > 1 && wfIsWindows() ) {
106 $this->output( "Threaded rebuild is not supported on Windows; running single-threaded.\n" );
109 if ( $threads > 1 && ( !extension_loaded( 'sockets' ) ||
!function_exists( 'pcntl_fork' ) ) ) {
110 $this->output( "Threaded rebuild requires ext-pcntl and ext-sockets; running single-threaded.\n" );
114 $conf = $this->getConfig()->get( MainConfigNames
::LocalisationCacheConf
);
115 // Allow fallbacks to create CDB files
116 $conf['manualRecache'] = false;
117 $conf['forceRecache'] = $force ||
!empty( $conf['forceRecache'] );
118 if ( $this->hasOption( 'outdir' ) ) {
119 $conf['storeDirectory'] = $this->getOption( 'outdir' );
122 if ( $this->hasOption( 'store-class' ) ) {
123 $conf['storeClass'] = $this->getOption( 'store-class' );
126 // XXX Copy-pasted from ServiceWiring.php. Do we need a factory for this one caller?
127 $services = $this->getServiceContainer();
128 $lc = new LocalisationCacheBulkLoad(
130 LocalisationCache
::CONSTRUCTOR_OPTIONS
,
132 $services->getMainConfig()
134 LocalisationCache
::getStoreFromConf( $conf, $this->getConfig()->get( MainConfigNames
::CacheDirectory
) ),
135 LoggerFactory
::getInstance( 'localisation' ),
136 $this->hasOption( 'skip-message-purge' ) ?
[] :
137 [ static function () use ( $services ) {
138 MessageBlobStore
::clearGlobalCacheEntry( $services->getMainWANObjectCache() );
140 $services->getLanguageNameUtils(),
141 $services->getHookContainer()
144 $allCodes = array_keys( $services
145 ->getLanguageNameUtils()
146 ->getLanguageNames( LanguageNameUtils
::AUTONYMS
, LanguageNameUtils
::SUPPORTED
) );
147 if ( $this->hasOption( 'lang' ) ) {
148 # Validate requested languages
149 $codes = array_intersect( $allCodes,
150 explode( ',', $this->getOption( 'lang' ) ) );
151 # Bailed out if nothing is left
152 if ( count( $codes ) == 0 ) {
153 $this->fatalError( 'None of the languages specified exists.' );
156 # By default get all languages
162 $total = count( $codes );
165 if ( $threads <= 1 ) {
166 // Single-threaded implementation
167 $numRebuilt +
= $this->doRebuild( $codes, $lc, $force );
169 // Multi-threaded implementation
170 $chunks = array_chunk( $codes, ceil( count( $codes ) / $threads ) );
171 // Map from PID to readable socket
174 foreach ( $chunks as $codes ) {
176 // Create a pair of sockets so that the child can communicate
177 // the number of rebuilt langs to the parent.
178 if ( !socket_create_pair( AF_UNIX
, SOCK_STREAM
, 0, $socketpair ) ) {
179 $this->fatalError( 'socket_create_pair failed' );
185 $this->fatalError( ' pcntl_fork failed' );
186 } elseif ( $pid === 0 ) {
187 // Child, reseed because there is no bug in PHP:
188 // https://bugs.php.net/bug.php?id=42465
189 mt_srand( getmypid() );
191 $numRebuilt = $this->doRebuild( $codes, $lc, $force );
192 // Report the number of rebuilt langs to the parent.
193 $msg = "$numRebuilt\n";
194 socket_write( $socketpair[1], $msg, strlen( $msg ) );
199 $sockets[$pid] = $socketpair[0];
203 // Wait for all children
204 foreach ( $sockets as $pid => $socket ) {
206 pcntl_waitpid( $pid, $status );
208 if ( pcntl_wifexited( $status ) ) {
209 $code = pcntl_wexitstatus( $status );
211 $this->output( "Pid $pid exited with status $code !!\n" );
213 // Good exit status from child. Read the number of rebuilt langs from it.
214 $res = socket_read( $socket, 512, PHP_NORMAL_READ
);
215 if ( $res === false ) {
216 $this->output( "socket_read failed in parent\n" );
218 $numRebuilt +
= intval( $res );
222 // Mush all child statuses into a single value in the parent.
223 $parentStatus |
= $code;
224 } elseif ( pcntl_wifsignaled( $status ) ) {
225 $signum = pcntl_wtermsig( $status );
226 $this->output( "Pid $pid terminated by signal $signum !!\n" );
232 $this->output( "$numRebuilt languages rebuilt out of $total\n" );
233 if ( $numRebuilt === 0 ) {
234 $this->output( "Use --force to rebuild the caches which are still fresh.\n" );
236 if ( $parentStatus ) {
237 $this->fatalError( 'Failed.', $parentStatus );
242 * Helper function to rebuild list of languages codes. Prints the code
243 * for each language which is rebuilt.
244 * @param string[] $codes List of language codes to rebuild.
245 * @param LocalisationCache $lc
246 * @param bool $force Rebuild up-to-date languages
247 * @return int Number of rebuilt languages
249 private function doRebuild( $codes, $lc, $force ) {
251 $operation = $this->hasOption( 'dry-run' ) ?
"Would rebuild" : "Rebuilding";
253 foreach ( $codes as $code ) {
254 if ( $force ||
$lc->isExpired( $code ) ) {
255 if ( !$this->hasOption( 'no-progress' ) ) {
256 $this->output( "$operation $code...\n" );
258 if ( !$this->hasOption( 'dry-run' ) ) {
259 $lc->recache( $code );
269 public function getDbType() {
270 if ( $this->hasOption( 'no-database' ) ) {
271 return Maintenance
::DB_NONE
;
274 return parent
::getDbType();
278 * Sets whether a run of this maintenance script has the force parameter set
280 * @param bool $forced
282 public function setForce( $forced = true ) {
283 $this->setOption( 'force', $forced );
287 // @codeCoverageIgnoreStart
288 $maintClass = RebuildLocalisationCache
::class;
289 require_once RUN_MAINTENANCE_IF_MAIN
;
290 // @codeCoverageIgnoreEnd