MediaWiki  master
mergeMessageFileList.php
Go to the documentation of this file.
1 <?php
25 // NO_AUTOLOAD -- file-scope define() used to modify behaviour
26 
27 # Start from scratch
30 
31 define( 'MW_NO_EXTENSION_MESSAGES', 1 );
32 
33 require_once __DIR__ . '/Maintenance.php';
34 $maintClass = MergeMessageFileList::class;
35 $mmfl = false;
36 
44  public 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  global $mmfl;
62 
63  if ( !count( $wgExtensionEntryPointListFiles )
64  && !$this->hasOption( 'list-file' )
65  && !$this->hasOption( 'extensions-dir' )
66  ) {
67  $this->fatalError( "Either --list-file or --extensions-dir must be provided if " .
68  "\$wgExtensionEntryPointListFiles is not set" );
69  }
70 
71  $mmfl = [ 'setupFiles' => [] ];
72 
73  # Add setup files contained in file passed to --list-file
74  if ( $this->hasOption( 'list-file' ) ) {
75  $extensionPaths = $this->readFile( $this->getOption( 'list-file' ) );
76  $mmfl['setupFiles'] = array_merge( $mmfl['setupFiles'], $extensionPaths );
77  }
78 
79  # Now find out files in a directory
80  if ( $this->hasOption( 'extensions-dir' ) ) {
81  $extdir = $this->getOption( 'extensions-dir' );
82  # Allow multiple directories to be passed with ":" as delimiter
83  $extdirs = explode( ':', $extdir );
84  foreach ( $extdirs as $extdir ) {
85  $entries = scandir( $extdir );
86  foreach ( $entries as $extname ) {
87  if ( $extname == '.' || $extname == '..' || !is_dir( "$extdir/$extname" ) ) {
88  continue;
89  }
90  $possibilities = [
91  "$extdir/$extname/extension.json",
92  "$extdir/$extname/skin.json",
93  "$extdir/$extname/$extname.php"
94  ];
95  $found = false;
96  foreach ( $possibilities as $extfile ) {
97  if ( file_exists( $extfile ) ) {
98  $mmfl['setupFiles'][] = $extfile;
99  $found = true;
100  break;
101  }
102  }
103 
104  if ( !$found ) {
105  $this->error( "Extension {$extname} in {$extdir} lacks expected entry point: " .
106  "extension.json, skin.json, or {$extname}.php." );
107  }
108  }
109  }
110  }
111 
112  # Add setup files defined via configuration
113  foreach ( $wgExtensionEntryPointListFiles as $points ) {
114  $extensionPaths = $this->readFile( $points );
115  $mmfl['setupFiles'] = array_merge( $mmfl['setupFiles'], $extensionPaths );
116  }
117 
118  if ( $this->hasOption( 'output' ) ) {
119  $mmfl['output'] = $this->getOption( 'output' );
120  }
121  if ( $this->hasOption( 'quiet' ) ) {
122  $mmfl['quiet'] = true;
123  }
124  }
125 
126  public function finalSetup( SettingsBuilder $settingsBuilder = null ) {
127  # This script commonly needs to be run before the l10n cache. But if
128  # LanguageCode is not 'en', it won't be able to run because there is
129  # no l10n cache. Break the cycle by forcing the LanguageCode setting to 'en'.
130  $settingsBuilder->putConfigValue( MainConfigNames::LanguageCode, 'en' );
131  parent::finalSetup( $settingsBuilder );
132  }
133 
139  public function getDbType() {
140  return Maintenance::DB_NONE;
141  }
142 
147  private function readFile( $fileName ) {
148  global $IP;
149 
150  $files = [];
151  $fileLines = file( $fileName );
152  if ( $fileLines === false ) {
153  $this->error( "Unable to open list file $fileName." );
154 
155  return $files;
156  }
157  # Strip comments, discard empty lines, and trim leading and trailing
158  # whitespace. Comments start with '#' and extend to the end of the line.
159  foreach ( $fileLines as $extension ) {
160  $extension = trim( preg_replace( '/#.*/', '', $extension ) );
161  if ( $extension !== '' ) {
162  # Paths may use the string $IP to be substituted by the actual value
163  $extension = str_replace( '$IP', $IP, $extension );
164  if ( file_exists( $extension ) ) {
165  $files[] = $extension;
166  } else {
167  $this->error( "Extension {$extension} doesn't exist" );
168  }
169  }
170  }
171 
172  return $files;
173  }
174 }
175 
176 require_once RUN_MAINTENANCE_IF_MAIN;
177 
178 $queue = [];
179 '@phan-var string[][] $mmfl';
180 foreach ( $mmfl['setupFiles'] as $fileName ) {
181  if ( strval( $fileName ) === '' ) {
182  continue;
183  }
184  if ( empty( $mmfl['quiet'] ) ) {
185  fwrite( STDERR, "Loading data from $fileName\n" );
186  }
187  // Using extension.json or skin.json
188  if ( str_ends_with( $fileName, '.json' ) ) {
189  $queue[$fileName] = 1;
190  } else {
191  require_once $fileName;
192  }
193 }
194 
195 if ( $queue ) {
196  $registry = new ExtensionRegistry();
197  $data = $registry->readFromQueue( $queue );
198  foreach ( [ 'wgExtensionMessagesFiles', 'wgMessagesDirs' ] as $var ) {
199  if ( isset( $data['globals'][$var] ) ) {
200  $GLOBALS[$var] = array_merge( $data['globals'][$var], $GLOBALS[$var] );
201  }
202  }
203 }
204 
205 if ( empty( $mmfl['quiet'] ) ) {
206  fwrite( STDERR, "\n" );
207 }
208 $s =
209  "<?php\n" .
210  "## This file is generated by mergeMessageFileList.php. Do not edit it directly.\n\n" .
211  "if ( defined( 'MW_NO_EXTENSION_MESSAGES' ) ) return;\n\n" .
212  '$wgExtensionMessagesFiles = ' . var_export( $wgExtensionMessagesFiles, true ) . ";\n\n" .
213  '$wgMessagesDirs = ' . var_export( $wgMessagesDirs, true ) . ";\n\n";
214 
215 $dirs = [
216  $IP,
217  dirname( __DIR__ ),
218  realpath( $IP )
219 ];
220 
221 foreach ( $dirs as $dir ) {
222  $s = preg_replace( "/'" . preg_quote( $dir, '/' ) . "([^']*)'/", '"$IP\1"', $s );
223 }
224 
225 if ( isset( $mmfl['output'] ) ) {
226  $outputFile = $mmfl['output'];
227  $res = file_put_contents( $outputFile, $s );
228  if ( $res === false ) {
229  fwrite( STDERR, "Failed to write to $outputFile\n" );
230  exit( 1 );
231  }
232 } else {
233  echo $s;
234 }
if(!defined( 'MEDIAWIKI')) if(ini_get( 'mbstring.func_overload')) if(!defined( 'MW_ENTRY_POINT')) global $IP
Environment checks.
Definition: Setup.php:91
The Registry loads JSON files, and uses a Processor to extract information from them.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:66
error( $err, $die=0)
Throw an error to the user.
const DB_NONE
Constants for DB access type.
Definition: Maintenance.php:71
hasOption( $name)
Checks to see if a particular option was set.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
A class containing constants representing the names of configuration variables.
Utility for loading settings files.
Maintenance script that merges $wgExtensionMessagesFiles from various extensions to produce a single ...
getDbType()
Database access is not needed.
finalSetup(SettingsBuilder $settingsBuilder=null)
Handle some last-minute setup here.
execute()
Do the actual work.
__construct()
Default constructor.
$wgExtensionEntryPointListFiles
Config variable stub for the ExtensionEntryPointListFiles setting, for use by phpdoc and IDEs.
$wgMessagesDirs
Config variable stub for the MessagesDirs setting, for use by phpdoc and IDEs.
$wgExtensionMessagesFiles
Config variable stub for the ExtensionMessagesFiles setting, for use by phpdoc and IDEs.
foreach( $mmfl['setupFiles'] as $fileName) if( $queue) if(empty( $mmfl['quiet'])) $s