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