MediaWiki REL1_40
mergeMessageFileList.php
Go to the documentation of this file.
1<?php
25# Start from scratch
28
29define( 'MW_NO_EXTENSION_MESSAGES', 1 );
30
31require_once __DIR__ . '/Maintenance.php';
32
40 public function __construct() {
41 parent::__construct();
42 $this->addOption(
43 'list-file',
44 'A file containing a list of extension setup files, one per line.',
45 false,
46 true
47 );
48 $this->addOption( 'extensions-dir', 'Path where extensions can be found.', false, true );
49 $this->addOption( 'output', 'Send output to this file (omit for stdout)', false, true );
50 $this->addDescription( 'Merge $wgExtensionMessagesFiles and $wgMessagesDirs from ' .
51 ' various extensions to produce a single file listing all message files and dirs.'
52 );
53 }
54
55 public function execute() {
56 $config = $this->getConfig();
57 $extensionEntryPointListFiles = $config->get( MainConfigNames::ExtensionEntryPointListFiles );
58
59 if ( !count( $extensionEntryPointListFiles )
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 $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 $setupFiles = array_merge( $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 foreach ( $extdirs as $extdir ) {
81 $entries = scandir( $extdir );
82 foreach ( $entries as $extname ) {
83 if ( $extname == '.' || $extname == '..' || !is_dir( "$extdir/$extname" ) ) {
84 continue;
85 }
86 $possibilities = [
87 "$extdir/$extname/extension.json",
88 "$extdir/$extname/skin.json",
89 "$extdir/$extname/$extname.php"
90 ];
91 $found = false;
92 foreach ( $possibilities as $extfile ) {
93 if ( file_exists( $extfile ) ) {
94 $setupFiles[] = $extfile;
95 $found = true;
96 break;
97 }
98 }
99
100 if ( !$found ) {
101 $this->error( "Extension {$extname} in {$extdir} lacks expected entry point: " .
102 "extension.json, skin.json, or {$extname}.php." );
103 }
104 }
105 }
106 }
107
108 # Add setup files defined via configuration
109 foreach ( $extensionEntryPointListFiles as $points ) {
110 $extensionPaths = $this->readFile( $points );
111 $setupFiles = array_merge( $setupFiles, $extensionPaths );
112 }
113
114 $this->generateMessageFileList( $setupFiles );
115 }
116
117 public function finalSetup( SettingsBuilder $settingsBuilder = null ) {
118 # This script commonly needs to be run before the l10n cache. But if
119 # LanguageCode is not 'en', it won't be able to run because there is
120 # no l10n cache. Break the cycle by forcing the LanguageCode setting to 'en'.
121 $settingsBuilder->putConfigValue( MainConfigNames::LanguageCode, 'en' );
122 parent::finalSetup( $settingsBuilder );
123 }
124
130 public function getDbType() {
132 }
133
138 private function readFile( $fileName ) {
139 $IP = MW_INSTALL_PATH;
140
141 $files = [];
142 $fileLines = file( $fileName );
143 if ( $fileLines === false ) {
144 $this->error( "Unable to open list file $fileName." );
145
146 return $files;
147 }
148 # Strip comments, discard empty lines, and trim leading and trailing
149 # whitespace. Comments start with '#' and extend to the end of the line.
150 foreach ( $fileLines as $extension ) {
151 $extension = trim( preg_replace( '/#.*/', '', $extension ) );
152 if ( $extension !== '' ) {
153 # Paths may use the string $IP to be substituted by the actual value
154 $extension = str_replace( '$IP', $IP, $extension );
155 if ( file_exists( $extension ) ) {
156 $files[] = $extension;
157 } else {
158 $this->error( "Extension {$extension} doesn't exist" );
159 }
160 }
161 }
162
163 return $files;
164 }
165
166 private function generateMessageFileList( array $setupFiles ) {
167 $IP = MW_INSTALL_PATH;
168
169 $outputFile = $this->getOption( 'output' );
170 $quiet = $this->hasOption( 'quiet' );
171
172 $queue = [];
173
174 foreach ( $setupFiles as $fileName ) {
175 if ( strval( $fileName ) === '' ) {
176 continue;
177 }
178 if ( !$quiet ) {
179 fwrite( STDERR, "Loading data from $fileName\n" );
180 }
181 // Using extension.json or skin.json
182 if ( str_ends_with( $fileName, '.json' ) ) {
183 $queue[$fileName] = 1;
184 } else {
185 require_once $fileName;
186 }
187 }
188
189 $config = $this->getConfig();
190 $vars = [
191 'wgExtensionMessagesFiles' => $config->get( MainConfigNames::ExtensionMessagesFiles ),
192 'wgMessagesDirs' => $config->get( MainConfigNames::MessagesDirs ),
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 $vars[$var] = array_merge( $data['globals'][$var], $vars[$var] );
201 }
202 }
203 }
204
205 if ( !$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( $vars['wgExtensionMessagesFiles'], true ) . ";\n\n" .
213 '$wgMessagesDirs = ' . var_export( $vars['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 ( $outputFile !== null ) {
226 $res = file_put_contents( $outputFile, $s );
227 if ( $res === false ) {
228 fwrite( STDERR, "Failed to write to $outputFile\n" );
229 exit( 1 );
230 }
231 } else {
232 echo $s;
233 }
234 }
235}
236
237$maintClass = MergeMessageFileList::class;
238require_once RUN_MAINTENANCE_IF_MAIN;
if(!defined( 'MEDIAWIKI')) if(ini_get('mbstring.func_overload')) if(!defined( 'MW_ENTRY_POINT')) global $IP
Environment checks.
Definition Setup.php:93
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...
error( $err, $die=0)
Throw an error to the user.
const DB_NONE
Constants for DB access type.
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.
Builder class for constructing a Config object from a set of sources during bootstrap.
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.