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
28 define( 'MW_NO_EXTENSION_MESSAGES', 1 );
29 
30 require_once __DIR__ . '/Maintenance.php';
31 $maintClass = MergeMessageFileList::class;
32 $mmfl = false;
33 
41  public function __construct() {
42  parent::__construct();
43  $this->addOption(
44  'list-file',
45  'A file containing a list of extension setup files, one per line.',
46  false,
47  true
48  );
49  $this->addOption( 'extensions-dir', 'Path where extensions can be found.', false, true );
50  $this->addOption( 'output', 'Send output to this file (omit for stdout)', false, true );
51  $this->addDescription( 'Merge $wgExtensionMessagesFiles and $wgMessagesDirs from ' .
52  ' various extensions to produce a single file listing all message files and dirs.'
53  );
54  }
55 
56  public function execute() {
57  global $mmfl;
59 
60  if ( !count( $wgExtensionEntryPointListFiles )
61  && !$this->hasOption( 'list-file' )
62  && !$this->hasOption( 'extensions-dir' )
63  ) {
64  $this->fatalError( "Either --list-file or --extensions-dir must be provided if " .
65  "\$wgExtensionEntryPointListFiles is not set" );
66  }
67 
68  $mmfl = [ 'setupFiles' => [] ];
69 
70  # Add setup files contained in file passed to --list-file
71  if ( $this->hasOption( 'list-file' ) ) {
72  $extensionPaths = $this->readFile( $this->getOption( 'list-file' ) );
73  $mmfl['setupFiles'] = array_merge( $mmfl['setupFiles'], $extensionPaths );
74  }
75 
76  # Now find out files in a directory
77  if ( $this->hasOption( 'extensions-dir' ) ) {
78  $extdir = $this->getOption( 'extensions-dir' );
79  # Allow multiple directories to be passed with ":" as delimiter
80  $extdirs = explode( ':', $extdir );
81  foreach ( $extdirs as $extdir ) {
82  $entries = scandir( $extdir );
83  foreach ( $entries as $extname ) {
84  if ( $extname == '.' || $extname == '..' || !is_dir( "$extdir/$extname" ) ) {
85  continue;
86  }
87  $possibilities = [
88  "$extdir/$extname/extension.json",
89  "$extdir/$extname/skin.json",
90  "$extdir/$extname/$extname.php"
91  ];
92  $found = false;
93  foreach ( $possibilities as $extfile ) {
94  if ( file_exists( $extfile ) ) {
95  $mmfl['setupFiles'][] = $extfile;
96  $found = true;
97  break;
98  }
99  }
100 
101  if ( !$found ) {
102  $this->error( "Extension {$extname} in {$extdir} lacks expected entry point: " .
103  "extension.json, skin.json, or {$extname}.php." );
104  }
105  }
106  }
107  }
108 
109  # Add setup files defined via configuration
110  foreach ( $wgExtensionEntryPointListFiles as $points ) {
111  $extensionPaths = $this->readFile( $points );
112  $mmfl['setupFiles'] = array_merge( $mmfl['setupFiles'], $extensionPaths );
113  }
114 
115  if ( $this->hasOption( 'output' ) ) {
116  $mmfl['output'] = $this->getOption( 'output' );
117  }
118  if ( $this->hasOption( 'quiet' ) ) {
119  $mmfl['quiet'] = true;
120  }
121  }
122 
127  private function readFile( $fileName ) {
128  global $IP;
129 
130  $files = [];
131  $fileLines = file( $fileName );
132  if ( $fileLines === false ) {
133  $this->error( "Unable to open list file $fileName." );
134 
135  return $files;
136  }
137  # Strip comments, discard empty lines, and trim leading and trailing
138  # whitespace. Comments start with '#' and extend to the end of the line.
139  foreach ( $fileLines as $extension ) {
140  $extension = trim( preg_replace( '/#.*/', '', $extension ) );
141  if ( $extension !== '' ) {
142  # Paths may use the string $IP to be substituted by the actual value
143  $extension = str_replace( '$IP', $IP, $extension );
144  if ( file_exists( $extension ) ) {
145  $files[] = $extension;
146  } else {
147  $this->error( "Extension {$extension} doesn't exist" );
148  }
149  }
150  }
151 
152  return $files;
153  }
154 }
155 
156 require_once RUN_MAINTENANCE_IF_MAIN;
157 
158 $queue = [];
159 '@phan-var string[][] $mmfl';
160 foreach ( $mmfl['setupFiles'] as $fileName ) {
161  if ( strval( $fileName ) === '' ) {
162  continue;
163  }
164  if ( empty( $mmfl['quiet'] ) ) {
165  fwrite( STDERR, "Loading data from $fileName\n" );
166  }
167  // Using extension.json or skin.json
168  if ( substr( $fileName, -strlen( '.json' ) ) === '.json' ) {
169  $queue[$fileName] = 1;
170  } else {
171  require_once $fileName;
172  }
173 }
174 
175 if ( $queue ) {
176  $registry = new ExtensionRegistry();
177  $data = $registry->readFromQueue( $queue );
178  foreach ( [ 'wgExtensionMessagesFiles', 'wgMessagesDirs' ] as $var ) {
179  if ( isset( $data['globals'][$var] ) ) {
180  $GLOBALS[$var] = array_merge( $data['globals'][$var], $GLOBALS[$var] );
181  }
182  }
183 }
184 
185 fwrite( STDERR, "\n" );
186 $s =
187  "<?php\n" .
188  "## This file is generated by mergeMessageFileList.php. Do not edit it directly.\n\n" .
189  "if ( defined( 'MW_NO_EXTENSION_MESSAGES' ) ) return;\n\n" .
190  '$wgExtensionMessagesFiles = ' . var_export( $wgExtensionMessagesFiles, true ) . ";\n\n" .
191  '$wgMessagesDirs = ' . var_export( $wgMessagesDirs, true ) . ";\n\n";
192 
193 $dirs = [
194  $IP,
195  dirname( __DIR__ ),
196  realpath( $IP )
197 ];
198 
199 foreach ( $dirs as $dir ) {
200  $s = preg_replace( "/'" . preg_quote( $dir, '/' ) . "([^']*)'/", '"$IP\1"', $s );
201 }
202 
203 if ( isset( $mmfl['output'] ) ) {
204  file_put_contents( $mmfl['output'], $s );
205 } else {
206  echo $s;
207 }
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:44
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:486
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:327
ExtensionRegistry
ExtensionRegistry class.
Definition: ExtensionRegistry.php:17
$wgExtensionMessagesFiles
$wgExtensionMessagesFiles
Extension messages files.
Definition: DefaultSettings.php:7738
$wgMessagesDirs
$wgMessagesDirs
Extension messages directories.
Definition: DefaultSettings.php:7765
$maintClass
$maintClass
Definition: mergeMessageFileList.php:31
$s
$s
Definition: mergeMessageFileList.php:186
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:55
MergeMessageFileList
Maintenance script that merges $wgExtensionMessagesFiles from various extensions to produce a single ...
Definition: mergeMessageFileList.php:40
MergeMessageFileList\readFile
readFile( $fileName)
Definition: mergeMessageFileList.php:127
$wgExtensionEntryPointListFiles
$wgExtensionEntryPointListFiles
Array of files with list(s) of extension entry points to be used in maintenance/mergeMessageFileList....
Definition: DefaultSettings.php:7772
$mmfl
$mmfl
Definition: mergeMessageFileList.php:32
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:245
$queue
$queue
Definition: mergeMessageFileList.php:158
$dirs
$dirs
Definition: mergeMessageFileList.php:193
MergeMessageFileList\__construct
__construct()
Default constructor.
Definition: mergeMessageFileList.php:41
MergeMessageFileList\execute
execute()
Do the actual work.
Definition: mergeMessageFileList.php:56
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:281
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:462
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:266
$IP
$IP
Definition: WebStart.php:49