mergeMessageFileList.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. /**
  3. * Merge $wgExtensionMessagesFiles from various extensions to produce a
  4. * single array containing all message files.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. * http://www.gnu.org/copyleft/gpl.html
  20. *
  21. * @file
  22. * @ingroup Maintenance
  23. */
  24. # Start from scratch
  25. define( 'MW_NO_EXTENSION_MESSAGES', 1 );
  26. require_once __DIR__ . '/Maintenance.php';
  27. $maintClass = MergeMessageFileList::class;
  28. $mmfl = false;
  29. /**
  30. * Maintenance script that merges $wgExtensionMessagesFiles from various
  31. * extensions to produce a single array containing all message files.
  32. *
  33. * @ingroup Maintenance
  34. */
  35. class MergeMessageFileList extends Maintenance {
  36. function __construct() {
  37. parent::__construct();
  38. $this->addOption(
  39. 'list-file',
  40. 'A file containing a list of extension setup files, one per line.',
  41. false,
  42. true
  43. );
  44. $this->addOption( 'extensions-dir', 'Path where extensions can be found.', false, true );
  45. $this->addOption( 'output', 'Send output to this file (omit for stdout)', false, true );
  46. $this->addDescription( 'Merge $wgExtensionMessagesFiles and $wgMessagesDirs from ' .
  47. ' various extensions to produce a single file listing all message files and dirs.'
  48. );
  49. }
  50. public function execute() {
  51. // phpcs:ignore MediaWiki.NamingConventions.ValidGlobalName.wgPrefix
  52. global $mmfl;
  53. global $wgExtensionEntryPointListFiles;
  54. if ( !count( $wgExtensionEntryPointListFiles )
  55. && !$this->hasOption( 'list-file' )
  56. && !$this->hasOption( 'extensions-dir' )
  57. ) {
  58. $this->fatalError( "Either --list-file or --extensions-dir must be provided if " .
  59. "\$wgExtensionEntryPointListFiles is not set" );
  60. }
  61. $mmfl = [ 'setupFiles' => [] ];
  62. # Add setup files contained in file passed to --list-file
  63. if ( $this->hasOption( 'list-file' ) ) {
  64. $extensionPaths = $this->readFile( $this->getOption( 'list-file' ) );
  65. $mmfl['setupFiles'] = array_merge( $mmfl['setupFiles'], $extensionPaths );
  66. }
  67. # Now find out files in a directory
  68. if ( $this->hasOption( 'extensions-dir' ) ) {
  69. $extdir = $this->getOption( 'extensions-dir' );
  70. # Allow multiple directories to be passed with ":" as delimiter
  71. $extdirs = explode( ':', $extdir );
  72. $entries = [];
  73. foreach ( $extdirs as $extdir ) {
  74. $entries = scandir( $extdir );
  75. foreach ( $entries as $extname ) {
  76. if ( $extname == '.' || $extname == '..' || !is_dir( "$extdir/$extname" ) ) {
  77. continue;
  78. }
  79. $possibilities = [
  80. "$extdir/$extname/extension.json",
  81. "$extdir/$extname/skin.json",
  82. "$extdir/$extname/$extname.php"
  83. ];
  84. $found = false;
  85. foreach ( $possibilities as $extfile ) {
  86. if ( file_exists( $extfile ) ) {
  87. $mmfl['setupFiles'][] = $extfile;
  88. $found = true;
  89. break;
  90. }
  91. }
  92. if ( !$found ) {
  93. $this->error( "Extension {$extname} in {$extdir} lacks expected entry point: " .
  94. "extension.json, skin.json, or {$extname}.php." );
  95. }
  96. }
  97. }
  98. }
  99. # Add setup files defined via configuration
  100. foreach ( $wgExtensionEntryPointListFiles as $points ) {
  101. $extensionPaths = $this->readFile( $points );
  102. $mmfl['setupFiles'] = array_merge( $mmfl['setupFiles'], $extensionPaths );
  103. }
  104. if ( $this->hasOption( 'output' ) ) {
  105. $mmfl['output'] = $this->getOption( 'output' );
  106. }
  107. if ( $this->hasOption( 'quiet' ) ) {
  108. $mmfl['quiet'] = true;
  109. }
  110. }
  111. /**
  112. * @param string $fileName
  113. * @return array List of absolute extension paths
  114. */
  115. private function readFile( $fileName ) {
  116. global $IP;
  117. $files = [];
  118. $fileLines = file( $fileName );
  119. if ( $fileLines === false ) {
  120. $this->hasError = true;
  121. $this->error( "Unable to open list file $fileName." );
  122. return $files;
  123. }
  124. # Strip comments, discard empty lines, and trim leading and trailing
  125. # whitespace. Comments start with '#' and extend to the end of the line.
  126. foreach ( $fileLines as $extension ) {
  127. $extension = trim( preg_replace( '/#.*/', '', $extension ) );
  128. if ( $extension !== '' ) {
  129. # Paths may use the string $IP to be substituted by the actual value
  130. $extension = str_replace( '$IP', $IP, $extension );
  131. if ( file_exists( $extension ) ) {
  132. $files[] = $extension;
  133. } else {
  134. $this->hasError = true;
  135. $this->error( "Extension {$extension} doesn't exist" );
  136. }
  137. }
  138. }
  139. return $files;
  140. }
  141. }
  142. require_once RUN_MAINTENANCE_IF_MAIN;
  143. $queue = [];
  144. foreach ( $mmfl['setupFiles'] as $fileName ) {
  145. if ( strval( $fileName ) === '' ) {
  146. continue;
  147. }
  148. if ( empty( $mmfl['quiet'] ) ) {
  149. fwrite( STDERR, "Loading data from $fileName\n" );
  150. }
  151. // Using extension.json or skin.json
  152. if ( substr( $fileName, -strlen( '.json' ) ) === '.json' ) {
  153. $queue[$fileName] = 1;
  154. } else {
  155. require_once $fileName;
  156. }
  157. }
  158. if ( $queue ) {
  159. $registry = new ExtensionRegistry();
  160. $data = $registry->readFromQueue( $queue );
  161. foreach ( [ 'wgExtensionMessagesFiles', 'wgMessagesDirs' ] as $var ) {
  162. if ( isset( $data['globals'][$var] ) ) {
  163. $GLOBALS[$var] = array_merge( $data['globals'][$var], $GLOBALS[$var] );
  164. }
  165. }
  166. }
  167. fwrite( STDERR, "\n" );
  168. $s =
  169. "<" . "?php\n" .
  170. "## This file is generated by mergeMessageFileList.php. Do not edit it directly.\n\n" .
  171. "if ( defined( 'MW_NO_EXTENSION_MESSAGES' ) ) return;\n\n" .
  172. '$wgExtensionMessagesFiles = ' . var_export( $wgExtensionMessagesFiles, true ) . ";\n\n" .
  173. '$wgMessagesDirs = ' . var_export( $wgMessagesDirs, true ) . ";\n\n";
  174. $dirs = [
  175. $IP,
  176. dirname( __DIR__ ),
  177. realpath( $IP )
  178. ];
  179. foreach ( $dirs as $dir ) {
  180. $s = preg_replace( "/'" . preg_quote( $dir, '/' ) . "([^']*)'/", '"$IP\1"', $s );
  181. }
  182. if ( isset( $mmfl['output'] ) ) {
  183. file_put_contents( $mmfl['output'], $s );
  184. } else {
  185. echo $s;
  186. }