MediaWiki  1.28.0
mergeMessageFileList.php
Go to the documentation of this file.
1 <?php
25 # Start from scratch
26 define( 'MW_NO_EXTENSION_MESSAGES', 1 );
27 
28 require_once __DIR__ . '/Maintenance.php';
29 $maintClass = 'MergeMessageFileList';
30 $mmfl = false;
31 
42  protected $hasError;
43 
44  function __construct() {
45  parent::__construct();
46  $this->addOption(
47  'list-file',
48  'A file containing a list of extension setup files, one per line.',
49  false,
50  true
51  );
52  $this->addOption( 'extensions-dir', 'Path where extensions can be found.', false, true );
53  $this->addOption( 'output', 'Send output to this file (omit for stdout)', false, true );
54  $this->addDescription( 'Merge $wgExtensionMessagesFiles and $wgMessagesDirs from ' .
55  ' various extensions to produce a single file listing all message files and dirs.'
56  );
57  }
58 
59  public function execute() {
60  // @codingStandardsIgnoreStart Ignore error: Global variable "$mmfl" is lacking 'wg' prefix
61  global $mmfl;
62  // @codingStandardsIgnoreEnd
63  global $wgExtensionEntryPointListFiles;
64 
65  if ( !count( $wgExtensionEntryPointListFiles )
66  && !$this->hasOption( 'list-file' )
67  && !$this->hasOption( 'extensions-dir' )
68  ) {
69  $this->error( "Either --list-file or --extensions-dir must be provided if " .
70  "\$wgExtensionEntryPointListFiles is not set", 1 );
71  }
72 
73  $mmfl = [ 'setupFiles' => [] ];
74 
75  # Add setup files contained in file passed to --list-file
76  if ( $this->hasOption( 'list-file' ) ) {
77  $extensionPaths = $this->readFile( $this->getOption( 'list-file' ) );
78  $mmfl['setupFiles'] = array_merge( $mmfl['setupFiles'], $extensionPaths );
79  }
80 
81  # Now find out files in a directory
82  if ( $this->hasOption( 'extensions-dir' ) ) {
83  $extdir = $this->getOption( 'extensions-dir' );
84  # Allow multiple directories to be passed with ":" as delimiter
85  $extdirs = explode( ':', $extdir );
86  $entries = [];
87  foreach ( $extdirs as $extdir ) {
88  $entries = array_merge( $entries, scandir( $extdir ) );
89  }
90  foreach ( $entries as $extname ) {
91  if ( $extname == '.' || $extname == '..' || !is_dir( "$extdir/$extname" ) ) {
92  continue;
93  }
94  $possibilities = [
95  "$extdir/$extname/extension.json",
96  "$extdir/$extname/skin.json",
97  "$extdir/$extname/$extname.php"
98  ];
99  $found = false;
100  foreach ( $possibilities as $extfile ) {
101  if ( file_exists( $extfile ) ) {
102  $mmfl['setupFiles'][] = $extfile;
103  $found = true;
104  break;
105  }
106  }
107 
108  if ( !$found ) {
109  $this->hasError = true;
110  $this->error( "Extension {$extname} in {$extdir} lacks expected entry point: " .
111  "extension.json, skin.json, or {$extname}.php." );
112  }
113  }
114  }
115 
116  # Add setup files defined via configuration
117  foreach ( $wgExtensionEntryPointListFiles as $points ) {
118  $extensionPaths = $this->readFile( $points );
119  $mmfl['setupFiles'] = array_merge( $mmfl['setupFiles'], $extensionPaths );
120  }
121 
122  if ( $this->hasError ) {
123  $this->error( "Some files are missing (see above). Giving up.", 1 );
124  }
125 
126  if ( $this->hasOption( 'output' ) ) {
127  $mmfl['output'] = $this->getOption( 'output' );
128  }
129  if ( $this->hasOption( 'quiet' ) ) {
130  $mmfl['quiet'] = true;
131  }
132  }
133 
138  private function readFile( $fileName ) {
139  global $IP;
140 
141  $files = [];
142  $fileLines = file( $fileName );
143  if ( $fileLines === false ) {
144  $this->hasError = true;
145  $this->error( "Unable to open list file $fileName." );
146 
147  return $files;
148  }
149  # Strip comments, discard empty lines, and trim leading and trailing
150  # whitespace. Comments start with '#' and extend to the end of the line.
151  foreach ( $fileLines as $extension ) {
152  $extension = trim( preg_replace( '/#.*/', '', $extension ) );
153  if ( $extension !== '' ) {
154  # Paths may use the string $IP to be substituted by the actual value
155  $extension = str_replace( '$IP', $IP, $extension );
156  if ( file_exists( $extension ) ) {
157  $files[] = $extension;
158  } else {
159  $this->hasError = true;
160  $this->error( "Extension {$extension} doesn't exist" );
161  }
162  }
163  }
164 
165  return $files;
166  }
167 }
168 
169 require_once RUN_MAINTENANCE_IF_MAIN;
170 
171 $queue = [];
172 foreach ( $mmfl['setupFiles'] as $fileName ) {
173  if ( strval( $fileName ) === '' ) {
174  continue;
175  }
176  if ( empty( $mmfl['quiet'] ) ) {
177  fwrite( STDERR, "Loading data from $fileName\n" );
178  }
179  // Using extension.json or skin.json
180  if ( substr( $fileName, -strlen( '.json' ) ) === '.json' ) {
181  $queue[$fileName] = 1;
182  } else {
183  require_once $fileName;
184  }
185 }
186 
187 if ( $queue ) {
188  $registry = new ExtensionRegistry();
189  $data = $registry->readFromQueue( $queue );
190  foreach ( [ 'wgExtensionMessagesFiles', 'wgMessagesDirs' ] as $var ) {
191  if ( isset( $data['globals'][$var] ) ) {
192  $GLOBALS[$var] = array_merge( $data['globals'][$var], $GLOBALS[$var] );
193  }
194  }
195 }
196 
197 fwrite( STDERR, "\n" );
198 $s =
199  "<" . "?php\n" .
200  "## This file is generated by mergeMessageFileList.php. Do not edit it directly.\n\n" .
201  "if ( defined( 'MW_NO_EXTENSION_MESSAGES' ) ) return;\n\n" .
202  '$wgExtensionMessagesFiles = ' . var_export( $wgExtensionMessagesFiles, true ) . ";\n\n" .
203  '$wgMessagesDirs = ' . var_export( $wgMessagesDirs, true ) . ";\n\n";
204 
205 $dirs = [
206  $IP,
207  dirname( __DIR__ ),
208  realpath( $IP )
209 ];
210 
211 foreach ( $dirs as $dir ) {
212  $s = preg_replace( "/'" . preg_quote( $dir, '/' ) . "([^']*)'/", '"$IP\1"', $s );
213 }
214 
215 if ( isset( $mmfl['output'] ) ) {
216  file_put_contents( $mmfl['output'], $s );
217 } else {
218  echo $s;
219 }
$IP
Definition: WebStart.php:58
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
$files
hasOption($name)
Checks to see if a particular param exists.
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
addOption($name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
We ve cleaned up the code here by removing clumps of infrequently used code and moving them off somewhere else It s much easier for someone working with this code to see what s _really_ going and make changes or fix bugs In we can take all the code that deals with the little used title reversing we can concentrate it all in an extension file
Definition: hooks.txt:93
$GLOBALS['IP']
$points
addDescription($text)
Set the description text.
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
getOption($name, $default=null)
Get an option, or return the default.
ExtensionRegistry class.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
error($err, $die=0)
Throw an error to the user.
Maintenance script that merges $wgExtensionMessagesFiles from various extensions to produce a single ...
$wgExtensionMessagesFiles['ExtensionNameMagic']
Definition: magicword.txt:43