Revert eval.php changes from r54653, r54312, r54225 (maintenance-work branch merge...
[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]
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
49 $conf = $wgLocalisationCacheConf;
50 $conf['manualRecache'] = false; // Allow fallbacks to create CDB files
51 if ( $force ) {
52 $conf['forceRecache'] = true;
53 }
54 $lc = new LocalisationCache_BulkLoad( $conf );
55
56 $codes = array_keys( Language::getLanguageNames( true ) );
57 sort( $codes );
58
59 // Initialise and split into chunks
60 $numRebuilt = 0;
61 $total = count($codes);
62 $chunks = array_chunk( $codes, ceil(count($codes)/$threads) );
63 $pids = array();
64 foreach ( $chunks as $codes ) {
65 // Do not fork for only one thread
66 $pid = ( $threads > 1 ) ? pcntl_fork() : -1;
67
68 if ( $pid === 0 ) {
69 // Child, reseed because there is no bug in PHP:
70 // http://bugs.php.net/bug.php?id=42465
71 mt_srand(getmypid());
72 $this->doRebuild( $codes, $numRebuilt, $lc, $force );
73 exit();
74 } elseif ($pid === -1) {
75 // Fork failed or one thread, do it serialized
76 $this->doRebuild( $codes, $numRebuilt, $lc, $force );
77 } else {
78 // Main thread
79 $pids[] = $pid;
80 }
81 }
82 // Wait for all children
83 foreach ( $pids as $pid ) pcntl_waitpid($pid, $status);
84
85 $this->output( "$numRebuilt languages rebuilt out of $total\n" );
86 if ( $numRebuilt == 0 ) {
87 $this->output( "Use --force to rebuild the caches which are still fresh.\n" );
88 }
89 }
90
91 /**
92 * Rebuild language cache
93 * @todo Document
94 */
95 private function doRebuild( $codes, &$numRebuilt, $lc, $force ) {
96 foreach ( $codes as $code ) {
97 if ( $force || $lc->isExpired( $code ) ) {
98 $this->output( "Rebuilding $code...\n" );
99 $lc->recache( $code );
100 $numRebuilt++;
101 }
102 }
103 }
104 }
105
106 $maintClass = "RebuildLocalisationCache";
107 require_once( DO_MAINTENANCE );