Merge "Add templateOverridesBySection to multi LBFactory"
[lhc/web/wiklou.git] / maintenance / rebuildLocalisationCache.php
1 <?php
2
3 /**
4 * Rebuild the localisation cache. Useful if you disabled automatic updates
5 * using $wgLocalisationCacheConf['manualRecache'] = true;
6 *
7 * Usage:
8 * php rebuildLocalisationCache.php [--force] [--threads=N]
9 *
10 * Use --force to rebuild all files, even the ones that are not out of date.
11 * Use --threads=N to fork more threads.
12 *
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.
17 *
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.
22 *
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
27 *
28 * @file
29 * @ingroup Maintenance
30 */
31
32 use MediaWiki\MediaWikiServices;
33
34 require_once __DIR__ . '/Maintenance.php';
35
36 /**
37 * Maintenance script to rebuild the localisation cache.
38 *
39 * @ingroup Maintenance
40 */
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)',
48 false, true );
49 $this->addOption( 'lang', 'Only rebuild these languages, comma separated.',
50 false, true );
51 }
52
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';
59 parent::finalSetup();
60 }
61
62 public function execute() {
63 global $wgLocalisationCacheConf;
64
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" );
69 $threads = 1;
70 }
71 if ( $threads > 1 && wfIsWindows() ) {
72 $this->output( "Threaded rebuild is not supported on Windows; running single-threaded.\n" );
73 $threads = 1;
74 }
75 if ( $threads > 1 && !function_exists( 'pcntl_fork' ) ) {
76 $this->output( "PHP pcntl extension is not present; running single-threaded.\n" );
77 $threads = 1;
78 }
79
80 $conf = $wgLocalisationCacheConf;
81 $conf['manualRecache'] = false; // Allow fallbacks to create CDB files
82 if ( $force ) {
83 $conf['forceRecache'] = true;
84 }
85 if ( $this->hasOption( 'outdir' ) ) {
86 $conf['storeDirectory'] = $this->getOption( 'outdir' );
87 }
88 $lc = new LocalisationCacheBulkLoad( $conf );
89
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 );
98 }
99 } else {
100 # By default get all languages
101 $codes = $allCodes;
102 }
103 sort( $codes );
104
105 // Initialise and split into chunks
106 $numRebuilt = 0;
107 $total = count( $codes );
108 $chunks = array_chunk( $codes, ceil( count( $codes ) / $threads ) );
109 $pids = [];
110 $parentStatus = 0;
111 foreach ( $chunks as $codes ) {
112 // Do not fork for only one thread
113 $pid = ( $threads > 1 ) ? pcntl_fork() : -1;
114
115 if ( $pid === 0 ) {
116 // Reset services, so we don't re-use connections.
117 MediaWikiServices::resetChildProcessServices();
118
119 $this->doRebuild( $codes, $lc, $force );
120 exit( 0 );
121 } elseif ( $pid === -1 ) {
122 // Fork failed or one thread, do it serialized
123 $numRebuilt += $this->doRebuild( $codes, $lc, $force );
124 } else {
125 // Main thread
126 $pids[] = $pid;
127 }
128 }
129 // Wait for all children
130 foreach ( $pids as $pid ) {
131 $status = 0;
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 );
136 }
137 }
138
139 if ( !$pids ) {
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" );
143 }
144 }
145 if ( $parentStatus ) {
146 exit( $parentStatus );
147 }
148 }
149
150 /**
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
157 */
158 private function doRebuild( $codes, $lc, $force ) {
159 $numRebuilt = 0;
160 foreach ( $codes as $code ) {
161 if ( $force || $lc->isExpired( $code ) ) {
162 $this->output( "Rebuilding $code...\n" );
163 $lc->recache( $code );
164 $numRebuilt++;
165 }
166 }
167
168 return $numRebuilt;
169 }
170
171 /**
172 * Sets whether a run of this maintenance script has the force parameter set
173 *
174 * @param bool $forced
175 */
176 public function setForce( $forced = true ) {
177 $this->mOptions['force'] = $forced;
178 }
179 }
180
181 $maintClass = "RebuildLocalisationCache";
182 require_once RUN_MAINTENANCE_IF_MAIN;