MediaWiki master
mergeMessageFileList.php
Go to the documentation of this file.
1<?php
11# Start from scratch
16
17// @codeCoverageIgnoreStart
18define( 'MW_NO_EXTENSION_MESSAGES', 1 );
19
20require_once __DIR__ . '/Maintenance.php';
21// @codeCoverageIgnoreEnd
22
30 public function __construct() {
31 parent::__construct();
32 $this->addOption(
33 'list-file',
34 'A file containing a list of extension setup files, one per line.',
35 false,
36 true
37 );
38 $this->addOption( 'extensions-dir', 'Path where extensions can be found.', false, true );
39 $this->addOption( 'output', 'Send output to this file (omit for stdout)', false, true );
40 $this->addDescription( 'Merge $wgExtensionMessagesFiles and $wgMessagesDirs from ' .
41 ' various extensions to produce a single file listing all message files and dirs.'
42 );
43 }
44
45 public function execute() {
46 $config = $this->getConfig();
47 $extensionEntryPointListFiles = $config->get( MainConfigNames::ExtensionEntryPointListFiles );
48
49 if ( !count( $extensionEntryPointListFiles )
50 && !$this->hasOption( 'list-file' )
51 && !$this->hasOption( 'extensions-dir' )
52 ) {
53 $this->fatalError( "Either --list-file or --extensions-dir must be provided if " .
54 "\$wgExtensionEntryPointListFiles is not set" );
55 }
56
57 $setupFiles = [];
58
59 # Add setup files contained in file passed to --list-file
60 if ( $this->hasOption( 'list-file' ) ) {
61 $extensionPaths = $this->readFile( $this->getOption( 'list-file' ) );
62 $setupFiles = array_merge( $setupFiles, $extensionPaths );
63 }
64
65 # Now find out files in a directory
66 if ( $this->hasOption( 'extensions-dir' ) ) {
67 $extdir = $this->getOption( 'extensions-dir' );
68 # Allow multiple directories to be passed with ":" as delimiter
69 $extdirs = explode( ':', $extdir );
70 foreach ( $extdirs as $extdir ) {
71 $entries = scandir( $extdir );
72 foreach ( $entries as $extname ) {
73 if ( $extname == '.' || $extname == '..' || !is_dir( "$extdir/$extname" ) ) {
74 continue;
75 }
76 $possibilities = [
77 "$extdir/$extname/extension.json",
78 "$extdir/$extname/skin.json",
79 ];
80 $found = false;
81 foreach ( $possibilities as $extfile ) {
82 if ( file_exists( $extfile ) ) {
83 $setupFiles[] = $extfile;
84 $found = true;
85 break;
86 }
87 }
88
89 if ( !$found ) {
90 $this->error( "Extension {$extname} in {$extdir} lacks expected entry point: " .
91 "extension.json or skin.json " .
92 "(PHP entry points are no longer supported by this script)." );
93 }
94 }
95 }
96 }
97
98 # Add setup files defined via configuration
99 foreach ( $extensionEntryPointListFiles as $points ) {
100 $extensionPaths = $this->readFile( $points );
101 $setupFiles = array_merge( $setupFiles, $extensionPaths );
102 }
103
104 $this->generateMessageFileList( $setupFiles );
105 }
106
107 public function finalSetup( SettingsBuilder $settingsBuilder ) {
108 # This script commonly needs to be run before the l10n cache. But if
109 # LanguageCode is not 'en', it won't be able to run because there is
110 # no l10n cache. Break the cycle by forcing the LanguageCode setting to 'en'.
111 $settingsBuilder->putConfigValue( MainConfigNames::LanguageCode, 'en' );
112 parent::finalSetup( $settingsBuilder );
113 }
114
120 public function getDbType() {
121 return Maintenance::DB_NONE;
122 }
123
128 private function readFile( $fileName ) {
129 $IP = MW_INSTALL_PATH;
130
131 $files = [];
132 $fileLines = file( $fileName );
133 if ( $fileLines === false ) {
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 ( !str_ends_with( $extension, '.json' ) ) {
146 $this->error( "Extension {$extension} does not end with .json " .
147 "(PHP entry points are no longer supported by this script)" );
148 } elseif ( file_exists( $extension ) ) {
149 $files[] = $extension;
150 } else {
151 $this->error( "Extension {$extension} doesn't exist" );
152 }
153 }
154 }
155
156 return $files;
157 }
158
159 private function generateMessageFileList( array $setupFiles ) {
160 $IP = MW_INSTALL_PATH;
161
162 $outputFile = $this->getOption( 'output' );
163 $quiet = $this->hasOption( 'quiet' );
164
165 $queue = [];
166
167 foreach ( $setupFiles as $fileName ) {
168 if ( strval( $fileName ) === '' ) {
169 continue;
170 }
171 if ( !$quiet ) {
172 fwrite( STDERR, "Loading data from $fileName\n" );
173 }
174 $queue[$fileName] = 1;
175 }
176
177 $config = $this->getConfig();
178 $vars = [
179 'wgExtensionMessagesFiles' => $config->get( MainConfigNames::ExtensionMessagesFiles ),
180 'wgMessagesDirs' => $config->get( MainConfigNames::MessagesDirs ),
181 ];
182
183 if ( $queue ) {
184 $registry = new ExtensionRegistry();
185 $data = $registry->readFromQueue( $queue );
186 foreach ( [ 'wgExtensionMessagesFiles', 'wgMessagesDirs' ] as $var ) {
187 if ( isset( $data['globals'][$var] ) ) {
188 $vars[$var] = array_merge( $data['globals'][$var], $vars[$var] );
189 }
190 }
191 }
192
193 if ( !$quiet ) {
194 fwrite( STDERR, "\n" );
195 }
196 $s =
197 "<?php\n" .
198 "## This file is generated by mergeMessageFileList.php. Do not edit it directly.\n\n" .
199 "if ( defined( 'MW_NO_EXTENSION_MESSAGES' ) ) return;\n\n" .
200 '$wgExtensionMessagesFiles = ' . var_export( $vars['wgExtensionMessagesFiles'], true ) . ";\n\n" .
201 '$wgMessagesDirs = ' . var_export( $vars['wgMessagesDirs'], true ) . ";\n\n";
202
203 $dirs = [
204 $IP,
205 dirname( __DIR__ ),
206 realpath( $IP )
207 ];
208
209 foreach ( $dirs as $dir ) {
210 $s = preg_replace( "/'" . preg_quote( $dir, '/' ) . "([^']*)'/", '"$IP\1"', $s );
211 }
212
213 if ( $outputFile !== null ) {
214 $res = file_put_contents( $outputFile, $s );
215 if ( $res === false ) {
216 fwrite( STDERR, "Failed to write to $outputFile\n" );
217 exit( 1 );
218 }
219 } else {
220 echo $s;
221 }
222 }
223}
224
225// @codeCoverageIgnoreStart
226$maintClass = MergeMessageFileList::class;
227require_once RUN_MAINTENANCE_IF_MAIN;
228// @codeCoverageIgnoreEnd
if(!defined('MEDIAWIKI')) if(!defined( 'MW_ENTRY_POINT')) global $IP
Environment checks.
Definition Setup.php:90
A class containing constants representing the names of configuration variables.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
hasOption( $name)
Checks to see if a particular option was set.
getOption( $name, $default=null)
Get an option, or return the default.
error( $err, $die=0)
Throw an error to the user.
addDescription( $text)
Set the description text.
Load JSON files, and uses a Processor to extract information.
Builder class for constructing a Config object from a set of sources during bootstrap.
putConfigValue(string $key, $value)
Puts a value into a config variable.
Maintenance script that merges $wgExtensionMessagesFiles from various extensions to produce a single ...
getDbType()
Database access is not needed.
execute()
Do the actual work.
finalSetup(SettingsBuilder $settingsBuilder)
Handle some last-minute setup here.
__construct()
Default constructor.