getBools doesn't exist in Translate anymore
[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 require_once( __DIR__ . '/Maintenance.php' );
33
34 /**
35 * Maintenance script to rebuild the localisation cache.
36 *
37 * @ingroup Maintenance
38 */
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 );
47 }
48
49 public function memoryLimit() {
50 if ( $this->hasOption( 'memory-limit' ) ) {
51 return parent::memoryLimit();
52 }
53 return '1000M';
54 }
55
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();
63 }
64
65 public function execute() {
66 global $wgLocalisationCacheConf;
67
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;
73 }
74 if ( $threads > 1 && wfIsWindows() ) {
75 $this->output( "Threaded rebuild is not supported on Windows; running single-threaded.\n" );
76 $threads = 1;
77 }
78 if ( $threads > 1 && !function_exists( 'pcntl_fork' ) ) {
79 $this->output( "PHP pcntl extension is not present; running single-threaded.\n" );
80 $threads = 1;
81 }
82
83 $conf = $wgLocalisationCacheConf;
84 $conf['manualRecache'] = false; // Allow fallbacks to create CDB files
85 if ( $force ) {
86 $conf['forceRecache'] = true;
87 }
88 if ( $this->hasOption( 'outdir' ) ) {
89 $conf['storeDirectory'] = $this->getOption( 'outdir' );
90 }
91 $lc = new LocalisationCache_BulkLoad( $conf );
92
93 $codes = array_keys( Language::fetchLanguageNames( null, 'mwfile' ) );
94 sort( $codes );
95
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;
104
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;
118 }
119 }
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 );
126 }
127
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" );
131 }
132 }
133
134 /**
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
141 */
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++;
149 }
150 }
151 return $numRebuilt;
152 }
153
154 /**
155 * Sets whether a run of this maintenance script has the force parameter set
156 *
157 * @param bool $forced
158 */
159 public function setForce( $forced = true ) {
160 $this->mOptions['force'] = $forced;
161 }
162 }
163
164 $maintClass = "RebuildLocalisationCache";
165 require_once( RUN_MAINTENANCE_IF_MAIN );