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