Added --threads=N parameter
[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
14 require( dirname(__FILE__).'/commandLine.inc' );
15 ini_set( 'memory_limit', '200M' );
16
17 $force = isset( $options['force'] );
18 $threads = intval( isset( $options['threads'] ) ? $options['threads'] : 1 );
19
20 $conf = $wgLocalisationCacheConf;
21 $conf['manualRecache'] = false; // Allow fallbacks to create CDB files
22 if ( $force ) {
23 $conf['forceRecache'] = true;
24 }
25 $lc = new LocalisationCache_BulkLoad( $conf );
26
27 $codes = array_keys( Language::getLanguageNames( true ) );
28 sort( $codes );
29
30 // Initialise and split into chunks
31 $numRebuilt = 0;
32 $total = count($codes);
33 $chunks = array_chunk( $codes, ceil(count($codes)/$threads) );
34 $pids = array();
35
36 foreach ( $chunks as $codes ) {
37 // Do not fork for only one thread
38 $pid = ( $threads > 1 ) ? pcntl_fork() : -1;
39
40 if ( $pid === 0 ) {
41 // Child
42 doRebuild( $codes, $numRebuilt, $lc, $force );
43 exit();
44 } elseif ($pid === -1) {
45 // Fork failed or one thread, do it serialized
46 doRebuild( $codes, $numRebuilt, $lc, $force );
47 } else {
48 // Main thread
49 $pids[] = $pid;
50 }
51 }
52
53 // Wait for all children
54 foreach ( $pids as $pid ) pcntl_waitpid($pid, $status);
55
56 echo "$numRebuilt languages rebuilt out of $total.\n";
57 if ( $numRebuilt == 0 ) {
58 echo "Use --force to rebuild the caches which are still fresh.\n";
59 }
60
61 function doRebuild( $codes, &$numRebuilt, $lc, $force ) {
62 foreach ( $codes as $code ) {
63 if ( $force || $lc->isExpired( $code ) ) {
64 echo "Rebuilding $code...\n";
65 $lc->recache( $code );
66 $numRebuilt++;
67 }
68 }
69 }