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