MediaWiki REL1_29
mergeMessageFileList.php
Go to the documentation of this file.
1<?php
25# Start from scratch
26define( 'MW_NO_EXTENSION_MESSAGES', 1 );
27
28require_once __DIR__ . '/Maintenance.php';
29$maintClass = 'MergeMessageFileList';
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 // @codingStandardsIgnoreStart Ignore error: Global variable "$mmfl" is lacking 'wg' prefix
57 // @codingStandardsIgnoreEnd
59
61 && !$this->hasOption( 'list-file' )
62 && !$this->hasOption( 'extensions-dir' )
63 ) {
64 $this->error( "Either --list-file or --extensions-dir must be provided if " .
65 "\$wgExtensionEntryPointListFiles is not set", 1 );
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 $entries = [];
82 foreach ( $extdirs as $extdir ) {
83 $entries = array_merge( $entries, scandir( $extdir ) );
84 }
85 foreach ( $entries as $extname ) {
86 if ( $extname == '.' || $extname == '..' || !is_dir( "$extdir/$extname" ) ) {
87 continue;
88 }
89 $possibilities = [
90 "$extdir/$extname/extension.json",
91 "$extdir/$extname/skin.json",
92 "$extdir/$extname/$extname.php"
93 ];
94 $found = false;
95 foreach ( $possibilities as $extfile ) {
96 if ( file_exists( $extfile ) ) {
97 $mmfl['setupFiles'][] = $extfile;
98 $found = true;
99 break;
100 }
101 }
102
103 if ( !$found ) {
104 $this->error( "Extension {$extname} in {$extdir} lacks expected entry point: " .
105 "extension.json, skin.json, or {$extname}.php." );
106 }
107 }
108 }
109
110 # Add setup files defined via configuration
112 $extensionPaths = $this->readFile( $points );
113 $mmfl['setupFiles'] = array_merge( $mmfl['setupFiles'], $extensionPaths );
114 }
115
116 if ( $this->hasOption( 'output' ) ) {
117 $mmfl['output'] = $this->getOption( 'output' );
118 }
119 if ( $this->hasOption( 'quiet' ) ) {
120 $mmfl['quiet'] = true;
121 }
122 }
123
128 private function readFile( $fileName ) {
129 global $IP;
130
131 $files = [];
132 $fileLines = file( $fileName );
133 if ( $fileLines === false ) {
134 $this->hasError = true;
135 $this->error( "Unable to open list file $fileName." );
136
137 return $files;
138 }
139 # Strip comments, discard empty lines, and trim leading and trailing
140 # whitespace. Comments start with '#' and extend to the end of the line.
141 foreach ( $fileLines as $extension ) {
142 $extension = trim( preg_replace( '/#.*/', '', $extension ) );
143 if ( $extension !== '' ) {
144 # Paths may use the string $IP to be substituted by the actual value
145 $extension = str_replace( '$IP', $IP, $extension );
146 if ( file_exists( $extension ) ) {
147 $files[] = $extension;
148 } else {
149 $this->hasError = true;
150 $this->error( "Extension {$extension} doesn't exist" );
151 }
152 }
153 }
154
155 return $files;
156 }
157}
158
159require_once RUN_MAINTENANCE_IF_MAIN;
160
162foreach ( $mmfl['setupFiles'] as $fileName ) {
163 if ( strval( $fileName ) === '' ) {
164 continue;
165 }
166 if ( empty( $mmfl['quiet'] ) ) {
167 fwrite( STDERR, "Loading data from $fileName\n" );
168 }
169 // Using extension.json or skin.json
170 if ( substr( $fileName, -strlen( '.json' ) ) === '.json' ) {
171 $queue[$fileName] = 1;
172 } else {
173 require_once $fileName;
174 }
175}
176
177if ( $queue ) {
178 $registry = new ExtensionRegistry();
179 $data = $registry->readFromQueue( $queue );
180 foreach ( [ 'wgExtensionMessagesFiles', 'wgMessagesDirs' ] as $var ) {
181 if ( isset( $data['globals'][$var] ) ) {
182 $GLOBALS[$var] = array_merge( $data['globals'][$var], $GLOBALS[$var] );
183 }
184 }
185}
186
187fwrite( STDERR, "\n" );
189 "<" . "?php\n" .
190 "## This file is generated by mergeMessageFileList.php. Do not edit it directly.\n\n" .
191 "if ( defined( 'MW_NO_EXTENSION_MESSAGES' ) ) return;\n\n" .
192 '$wgExtensionMessagesFiles = ' . var_export( $wgExtensionMessagesFiles, true ) . ";\n\n" .
193 '$wgMessagesDirs = ' . var_export( $wgMessagesDirs, true ) . ";\n\n";
194
196 $IP,
197 dirname( __DIR__ ),
198 realpath( $IP )
199];
200
201foreach ( $dirs as $dir ) {
202 $s = preg_replace( "/'" . preg_quote( $dir, '/' ) . "([^']*)'/", '"$IP\1"', $s );
203}
204
205if ( isset( $mmfl['output'] ) ) {
206 file_put_contents( $mmfl['output'], $s );
207} else {
208 echo $s;
209}
$GLOBALS['IP']
$wgExtensionEntryPointListFiles
Array of files with list(s) of extension entry points to be used in maintenance/mergeMessageFileList....
$wgMessagesDirs
Extension messages directories.
$IP
Definition WebStart.php:58
ExtensionRegistry class.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
error( $err, $die=0)
Throw an error to the user.
hasOption( $name)
Checks to see if a particular param exists.
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.
Maintenance script that merges $wgExtensionMessagesFiles from various extensions to produce a single ...
execute()
Do the actual work.
__construct()
Default constructor.
when a variable name is used in a it is silently declared as a new local masking the global
Definition design.txt:95
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
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:108
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:37
$wgExtensionMessagesFiles['ExtensionNameMagic']
Definition magicword.txt:43
require_once RUN_MAINTENANCE_IF_MAIN
$points