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