rebuildLocalisationCache.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /**
  3. * Rebuild the localisation cache. Useful if you disabled automatic updates
  4. * using $wgLocalisationCacheConf['manualRecache'] = true;
  5. *
  6. * Usage:
  7. * php rebuildLocalisationCache.php [--force] [--threads=N]
  8. *
  9. * Use --force to rebuild all files, even the ones that are not out of date.
  10. * Use --threads=N to fork more threads.
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License along
  23. * with this program; if not, write to the Free Software Foundation, Inc.,
  24. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  25. * http://www.gnu.org/copyleft/gpl.html
  26. *
  27. * @file
  28. * @ingroup Maintenance
  29. */
  30. require_once __DIR__ . '/Maintenance.php';
  31. /**
  32. * Maintenance script to rebuild the localisation cache.
  33. *
  34. * @ingroup Maintenance
  35. */
  36. class RebuildLocalisationCache extends Maintenance {
  37. public function __construct() {
  38. parent::__construct();
  39. $this->addDescription( 'Rebuild the localisation cache' );
  40. $this->addOption( 'force', 'Rebuild all files, even ones not out of date' );
  41. $this->addOption( 'threads', 'Fork more than one thread', false, true );
  42. $this->addOption( 'outdir', 'Override the output directory (normally $wgCacheDirectory)',
  43. false, true );
  44. $this->addOption( 'lang', 'Only rebuild these languages, comma separated.',
  45. false, true );
  46. }
  47. public function finalSetup() {
  48. # This script needs to be run to build the inital l10n cache. But if
  49. # $wgLanguageCode is not 'en', it won't be able to run because there is
  50. # no l10n cache. Break the cycle by forcing $wgLanguageCode = 'en'.
  51. global $wgLanguageCode;
  52. $wgLanguageCode = 'en';
  53. parent::finalSetup();
  54. }
  55. public function execute() {
  56. global $wgLocalisationCacheConf;
  57. $force = $this->hasOption( 'force' );
  58. $threads = $this->getOption( 'threads', 1 );
  59. if ( $threads < 1 || $threads != intval( $threads ) ) {
  60. $this->output( "Invalid thread count specified; running single-threaded.\n" );
  61. $threads = 1;
  62. }
  63. if ( $threads > 1 && wfIsWindows() ) {
  64. $this->output( "Threaded rebuild is not supported on Windows; running single-threaded.\n" );
  65. $threads = 1;
  66. }
  67. if ( $threads > 1 && !function_exists( 'pcntl_fork' ) ) {
  68. $this->output( "PHP pcntl extension is not present; running single-threaded.\n" );
  69. $threads = 1;
  70. }
  71. $conf = $wgLocalisationCacheConf;
  72. $conf['manualRecache'] = false; // Allow fallbacks to create CDB files
  73. if ( $force ) {
  74. $conf['forceRecache'] = true;
  75. }
  76. if ( $this->hasOption( 'outdir' ) ) {
  77. $conf['storeDirectory'] = $this->getOption( 'outdir' );
  78. }
  79. $lc = new LocalisationCacheBulkLoad( $conf );
  80. $allCodes = array_keys( Language::fetchLanguageNames( null, 'mwfile' ) );
  81. if ( $this->hasOption( 'lang' ) ) {
  82. # Validate requested languages
  83. $codes = array_intersect( $allCodes,
  84. explode( ',', $this->getOption( 'lang' ) ) );
  85. # Bailed out if nothing is left
  86. if ( count( $codes ) == 0 ) {
  87. $this->error( 'None of the languages specified exists.', 1 );
  88. }
  89. } else {
  90. # By default get all languages
  91. $codes = $allCodes;
  92. }
  93. sort( $codes );
  94. // Initialise and split into chunks
  95. $numRebuilt = 0;
  96. $total = count( $codes );
  97. $chunks = array_chunk( $codes, ceil( count( $codes ) / $threads ) );
  98. $pids = [];
  99. $parentStatus = 0;
  100. foreach ( $chunks as $codes ) {
  101. // Do not fork for only one thread
  102. $pid = ( $threads > 1 ) ? pcntl_fork() : -1;
  103. if ( $pid === 0 ) {
  104. // Child, reseed because there is no bug in PHP:
  105. // http://bugs.php.net/bug.php?id=42465
  106. mt_srand( getmypid() );
  107. $this->doRebuild( $codes, $lc, $force );
  108. exit( 0 );
  109. } elseif ( $pid === -1 ) {
  110. // Fork failed or one thread, do it serialized
  111. $numRebuilt += $this->doRebuild( $codes, $lc, $force );
  112. } else {
  113. // Main thread
  114. $pids[] = $pid;
  115. }
  116. }
  117. // Wait for all children
  118. foreach ( $pids as $pid ) {
  119. $status = 0;
  120. pcntl_waitpid( $pid, $status );
  121. if ( pcntl_wexitstatus( $status ) ) {
  122. // Pass a fatal error code through to the caller
  123. $parentStatus = pcntl_wexitstatus( $status );
  124. }
  125. }
  126. if ( !$pids ) {
  127. $this->output( "$numRebuilt languages rebuilt out of $total\n" );
  128. if ( $numRebuilt === 0 ) {
  129. $this->output( "Use --force to rebuild the caches which are still fresh.\n" );
  130. }
  131. }
  132. if ( $parentStatus ) {
  133. exit( $parentStatus );
  134. }
  135. }
  136. /**
  137. * Helper function to rebuild list of languages codes. Prints the code
  138. * for each language which is rebuilt.
  139. * @param array $codes List of language codes to rebuild.
  140. * @param LocalisationCache $lc Instance of LocalisationCacheBulkLoad (?)
  141. * @param bool $force Rebuild up-to-date languages
  142. * @return int Number of rebuilt languages
  143. */
  144. private function doRebuild( $codes, $lc, $force ) {
  145. $numRebuilt = 0;
  146. foreach ( $codes as $code ) {
  147. if ( $force || $lc->isExpired( $code ) ) {
  148. $this->output( "Rebuilding $code...\n" );
  149. $lc->recache( $code );
  150. $numRebuilt++;
  151. }
  152. }
  153. return $numRebuilt;
  154. }
  155. /**
  156. * Sets whether a run of this maintenance script has the force parameter set
  157. *
  158. * @param bool $forced
  159. */
  160. public function setForce( $forced = true ) {
  161. $this->mOptions['force'] = $forced;
  162. }
  163. }
  164. $maintClass = "RebuildLocalisationCache";
  165. require_once RUN_MAINTENANCE_IF_MAIN;