MediaWiki  1.32.5
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';
30 $mmfl = false;
31 
39  function __construct() {
40  parent::__construct();
41  $this->addOption(
42  'list-file',
43  'A file containing a list of extension setup files, one per line.',
44  false,
45  true
46  );
47  $this->addOption( 'extensions-dir', 'Path where extensions can be found.', false, true );
48  $this->addOption( 'output', 'Send output to this file (omit for stdout)', false, true );
49  $this->addDescription( 'Merge $wgExtensionMessagesFiles and $wgMessagesDirs from ' .
50  ' various extensions to produce a single file listing all message files and dirs.'
51  );
52  }
53 
54  public function execute() {
55  // phpcs:ignore MediaWiki.NamingConventions.ValidGlobalName.wgPrefix
56  global $mmfl;
58 
60  && !$this->hasOption( 'list-file' )
61  && !$this->hasOption( 'extensions-dir' )
62  ) {
63  $this->fatalError( "Either --list-file or --extensions-dir must be provided if " .
64  "\$wgExtensionEntryPointListFiles is not set" );
65  }
66 
67  $mmfl = [ 'setupFiles' => [] ];
68 
69  # Add setup files contained in file passed to --list-file
70  if ( $this->hasOption( 'list-file' ) ) {
71  $extensionPaths = $this->readFile( $this->getOption( 'list-file' ) );
72  $mmfl['setupFiles'] = array_merge( $mmfl['setupFiles'], $extensionPaths );
73  }
74 
75  # Now find out files in a directory
76  if ( $this->hasOption( 'extensions-dir' ) ) {
77  $extdir = $this->getOption( 'extensions-dir' );
78  # Allow multiple directories to be passed with ":" as delimiter
79  $extdirs = explode( ':', $extdir );
80  $entries = [];
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->hasError = true;
134  $this->error( "Unable to open list file $fileName." );
135 
136  return $files;
137  }
138  # Strip comments, discard empty lines, and trim leading and trailing
139  # whitespace. Comments start with '#' and extend to the end of the line.
140  foreach ( $fileLines as $extension ) {
141  $extension = trim( preg_replace( '/#.*/', '', $extension ) );
142  if ( $extension !== '' ) {
143  # Paths may use the string $IP to be substituted by the actual value
144  $extension = str_replace( '$IP', $IP, $extension );
145  if ( file_exists( $extension ) ) {
146  $files[] = $extension;
147  } else {
148  $this->hasError = true;
149  $this->error( "Extension {$extension} doesn't exist" );
150  }
151  }
152  }
153 
154  return $files;
155  }
156 }
157 
158 require_once RUN_MAINTENANCE_IF_MAIN;
159 
160 $queue = [];
161 foreach ( $mmfl['setupFiles'] as $fileName ) {
162  if ( strval( $fileName ) === '' ) {
163  continue;
164  }
165  if ( empty( $mmfl['quiet'] ) ) {
166  fwrite( STDERR, "Loading data from $fileName\n" );
167  }
168  // Using extension.json or skin.json
169  if ( substr( $fileName, -strlen( '.json' ) ) === '.json' ) {
170  $queue[$fileName] = 1;
171  } else {
172  require_once $fileName;
173  }
174 }
175 
176 if ( $queue ) {
177  $registry = new ExtensionRegistry();
178  $data = $registry->readFromQueue( $queue );
179  foreach ( [ 'wgExtensionMessagesFiles', 'wgMessagesDirs' ] as $var ) {
180  if ( isset( $data['globals'][$var] ) ) {
181  $GLOBALS[$var] = array_merge( $data['globals'][$var], $GLOBALS[$var] );
182  }
183  }
184 }
185 
186 fwrite( STDERR, "\n" );
187 $s =
188  "<" . "?php\n" .
189  "## This file is generated by mergeMessageFileList.php. Do not edit it directly.\n\n" .
190  "if ( defined( 'MW_NO_EXTENSION_MESSAGES' ) ) return;\n\n" .
191  '$wgExtensionMessagesFiles = ' . var_export( $wgExtensionMessagesFiles, true ) . ";\n\n" .
192  '$wgMessagesDirs = ' . var_export( $wgMessagesDirs, true ) . ";\n\n";
193 
194 $dirs = [
195  $IP,
196  dirname( __DIR__ ),
197  realpath( $IP )
198 ];
199 
200 foreach ( $dirs as $dir ) {
201  $s = preg_replace( "/'" . preg_quote( $dir, '/' ) . "([^']*)'/", '"$IP\1"', $s );
202 }
203 
204 if ( isset( $mmfl['output'] ) ) {
205  file_put_contents( $mmfl['output'], $s );
206 } else {
207  echo $s;
208 }
file
Using a hook running we can avoid having all this option specific stuff in our mainline code Using the function 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:91
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:465
captcha-old.count
count
Definition: captcha-old.py:249
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:317
ExtensionRegistry
ExtensionRegistry class.
Definition: ExtensionRegistry.php:14
$maintClass
$maintClass
Definition: mergeMessageFileList.php:29
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
$s
$s
Definition: mergeMessageFileList.php:187
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
MergeMessageFileList
Maintenance script that merges $wgExtensionMessagesFiles from various extensions to produce a single ...
Definition: mergeMessageFileList.php:38
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:7365
php
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
$mmfl
$mmfl
Definition: mergeMessageFileList.php:30
$points
$points
Definition: profileinfo.php:415
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:236
$IP
$IP
Definition: update.php:3
$queue
$queue
Definition: mergeMessageFileList.php:160
$dirs
$dirs
Definition: mergeMessageFileList.php:194
$wgMessagesDirs
$wgMessagesDirs['ReplaceText']
Definition: ReplaceText.php:50
MergeMessageFileList\__construct
__construct()
Default constructor.
Definition: mergeMessageFileList.php:39
MergeMessageFileList\execute
execute()
Do the actual work.
Definition: mergeMessageFileList.php:54
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:271
$wgExtensionMessagesFiles
$wgExtensionMessagesFiles['ExtensionNameMagic']
Definition: magicword.txt:43
as
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
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:442
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:257
$GLOBALS
$GLOBALS['IP']
Definition: ComposerHookHandler.php:6