36require_once __DIR__ .
'/Maintenance.php';
45 parent::__construct();
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)',
51 $this->
addOption(
'lang',
'Only rebuild these languages, comma separated.',
55 'Override the LC store class (normally $wgLocalisationCacheConf[\'storeClass\'])',
62 # This script needs to be run to build the inital l10n cache. But if
63 # $wgLanguageCode is not 'en', it won't be able to run because there is
64 # no l10n cache. Break the cycle by forcing $wgLanguageCode = 'en'.
74 $threads = $this->
getOption(
'threads', 1 );
75 if ( $threads < 1 || $threads != intval( $threads ) ) {
76 $this->
output(
"Invalid thread count specified; running single-threaded.\n" );
80 $this->
output(
"Threaded rebuild is not supported on Windows; running single-threaded.\n" );
83 if ( $threads > 1 && !function_exists(
'pcntl_fork' ) ) {
84 $this->
output(
"PHP pcntl extension is not present; running single-threaded.\n" );
90 $conf[
'manualRecache'] =
false;
91 $conf[
'forceRecache'] = $force || !empty( $conf[
'forceRecache'] );
93 $conf[
'storeDirectory'] = $this->
getOption(
'outdir' );
96 if ( $this->
hasOption(
'store-class' ) ) {
97 $conf[
'storeClass'] = $this->
getOption(
'store-class' );
100 $services = MediaWikiServices::getInstance();
103 LocalisationCache::CONSTRUCTOR_OPTIONS,
105 $services->getMainConfig()
108 LoggerFactory::getInstance(
'localisation' ),
109 [ function () use ( $services ) {
112 $services->getLanguageNameUtils(),
113 $services->getHookContainer()
116 $allCodes = array_keys( $services
117 ->getLanguageNameUtils()
118 ->getLanguageNames(
null,
'mwfile' ) );
120 # Validate requested languages
121 $codes = array_intersect( $allCodes,
122 explode(
',', $this->
getOption(
'lang' ) ) );
123 # Bailed out if nothing is left
124 if ( count( $codes ) == 0 ) {
125 $this->
fatalError(
'None of the languages specified exists.' );
128 # By default get all languages
135 $total = count( $codes );
136 $chunks = array_chunk( $codes, ceil( count( $codes ) / $threads ) );
139 foreach ( $chunks as $codes ) {
141 $pid = ( $threads > 1 ) ? pcntl_fork() : -1;
146 mt_srand( getmypid() );
150 } elseif ( $pid === -1 ) {
152 $numRebuilt += $this->
doRebuild( $codes, $lc, $force );
159 foreach ( $pids as $pid ) {
161 pcntl_waitpid( $pid, $status );
162 if ( pcntl_wexitstatus( $status ) ) {
164 $parentStatus = pcntl_wexitstatus( $status );
169 $this->
output(
"$numRebuilt languages rebuilt out of $total\n" );
170 if ( $numRebuilt === 0 ) {
171 $this->
output(
"Use --force to rebuild the caches which are still fresh.\n" );
174 if ( $parentStatus ) {
175 exit( $parentStatus );
189 foreach ( $codes as $code ) {
190 if ( $force || $lc->isExpired( $code ) ) {
191 $this->
output(
"Rebuilding $code...\n" );
192 $lc->recache( $code );
206 $this->mOptions[
'force'] = $forced;
$wgLanguageCode
Site language code.
$wgCacheDirectory
Directory for caching data in the local filesystem.
$wgLocalisationCacheConf
Localisation cache configuration.
wfIsWindows()
Check if the operating system is Windows.
const RUN_MAINTENANCE_IF_MAIN
A localisation cache optimised for loading large amounts of data for many languages.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
static clearGlobalCacheEntry(WANObjectCache $cache)
Invalidate cache keys for all known modules.
Maintenance script to rebuild the localisation cache.
execute()
Do the actual work.
__construct()
Default constructor.
finalSetup()
Handle some last-minute setup here.
setForce( $forced=true)
Sets whether a run of this maintenance script has the force parameter set.
doRebuild( $codes, $lc, $force)
Helper function to rebuild list of languages codes.