Merge "Use local context to get messages and don't use implicit Message object to...
[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 * @ingroup Maintenance
29 */
30
31 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
32
33 class RebuildLocalisationCache extends Maintenance {
34 public function __construct() {
35 parent::__construct();
36 $this->mDescription = "Rebuild the localisation cache";
37 $this->addOption( 'force', 'Rebuild all files, even ones not out of date' );
38 $this->addOption( 'threads', 'Fork more than one thread', false, true );
39 $this->addOption( 'outdir', 'Override the output directory (normally $wgCacheDirectory)',
40 false, true );
41 }
42
43 public function memoryLimit() {
44 return '1000M';
45 }
46
47 public function execute() {
48 global $wgLocalisationCacheConf;
49
50 $force = $this->hasOption( 'force' );
51 $threads = $this->getOption( 'threads', 1 );
52 if ( $threads < 1 || $threads != intval( $threads ) ) {
53 $this->output( "Invalid thread count specified; running single-threaded.\n" );
54 $threads = 1;
55 }
56 if ( $threads > 1 && wfIsWindows() ) {
57 $this->output( "Threaded rebuild is not supported on Windows; running single-threaded.\n" );
58 $threads = 1;
59 }
60 if ( $threads > 1 && !function_exists( 'pcntl_fork' ) ) {
61 $this->output( "PHP pcntl extension is not present; running single-threaded.\n" );
62 $threads = 1;
63 }
64
65 $conf = $wgLocalisationCacheConf;
66 $conf['manualRecache'] = false; // Allow fallbacks to create CDB files
67 if ( $force ) {
68 $conf['forceRecache'] = true;
69 }
70 if ( $this->hasOption( 'outdir' ) ) {
71 $conf['storeDirectory'] = $this->getOption( 'outdir' );
72 }
73 $lc = new LocalisationCache_BulkLoad( $conf );
74
75 $codes = array_keys( Language::fetchLanguageNames( null, 'mwfile' ) );
76 sort( $codes );
77
78 // Initialise and split into chunks
79 $numRebuilt = 0;
80 $total = count( $codes );
81 $chunks = array_chunk( $codes, ceil( count( $codes ) / $threads ) );
82 $pids = array();
83 foreach ( $chunks as $codes ) {
84 // Do not fork for only one thread
85 $pid = ( $threads > 1 ) ? pcntl_fork() : -1;
86
87 if ( $pid === 0 ) {
88 // Child, reseed because there is no bug in PHP:
89 // http://bugs.php.net/bug.php?id=42465
90 mt_srand( getmypid() );
91 $numRebuilt = $this->doRebuild( $codes, $lc, $force );
92 // Abuse the exit value for the count of rebuild languages
93 exit( $numRebuilt );
94 } elseif ( $pid === -1 ) {
95 // Fork failed or one thread, do it serialized
96 $numRebuilt += $this->doRebuild( $codes, $lc, $force );
97 } else {
98 // Main thread
99 $pids[] = $pid;
100 }
101 }
102 // Wait for all children
103 foreach ( $pids as $pid ) {
104 $status = 0;
105 pcntl_waitpid( $pid, $status );
106 // Fetch the count from the return value
107 $numRebuilt += pcntl_wexitstatus( $status );
108 }
109
110 $this->output( "$numRebuilt languages rebuilt out of $total\n" );
111 if ( $numRebuilt === 0 ) {
112 $this->output( "Use --force to rebuild the caches which are still fresh.\n" );
113 }
114 }
115
116 /**
117 * Helper function to rebuild list of languages codes. Prints the code
118 * for each language which is rebuilt.
119 * @param $codes array List of language codes to rebuild.
120 * @param $lc LocalisationCache Instance of LocalisationCache_BulkLoad (?)
121 * @param $force bool Rebuild up-to-date languages
122 * @return int Number of rebuilt languages
123 */
124 private function doRebuild( $codes, $lc, $force ) {
125 $numRebuilt = 0;
126 foreach ( $codes as $code ) {
127 if ( $force || $lc->isExpired( $code ) ) {
128 $this->output( "Rebuilding $code...\n" );
129 $lc->recache( $code );
130 $numRebuilt++;
131 }
132 }
133 return $numRebuilt;
134 }
135
136 /**
137 * Sets whether a run of this maintenance script has the force parameter set
138 *
139 * @param bool $forced
140 */
141 public function setForce( $forced = true ) {
142 $this->mOptions['force'] = $forced;
143 }
144 }
145
146 $maintClass = "RebuildLocalisationCache";
147 require_once( RUN_MAINTENANCE_IF_MAIN );