41 parent::__construct();
44 'A file containing a list of extension setup files, one per line.',
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.'
57 $extensionEntryPointListFiles = $config->get( MainConfigNames::ExtensionEntryPointListFiles );
59 if ( !count( $extensionEntryPointListFiles )
63 $this->
fatalError(
"Either --list-file or --extensions-dir must be provided if " .
64 "\$wgExtensionEntryPointListFiles is not set" );
69 # Add setup files contained in file passed to --list-file
71 $extensionPaths = $this->readFile( $this->
getOption(
'list-file' ) );
72 $setupFiles = array_merge( $setupFiles, $extensionPaths );
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" ) ) {
87 "$extdir/$extname/extension.json",
88 "$extdir/$extname/skin.json",
89 "$extdir/$extname/$extname.php"
92 foreach ( $possibilities as $extfile ) {
93 if ( file_exists( $extfile ) ) {
94 $setupFiles[] = $extfile;
101 $this->
error(
"Extension {$extname} in {$extdir} lacks expected entry point: " .
102 "extension.json, skin.json, or {$extname}.php." );
108 # Add setup files defined via configuration
109 foreach ( $extensionEntryPointListFiles as $points ) {
110 $extensionPaths = $this->readFile( $points );
111 $setupFiles = array_merge( $setupFiles, $extensionPaths );
114 $this->generateMessageFileList( $setupFiles );
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 );
138 private function readFile( $fileName ) {
139 $IP = MW_INSTALL_PATH;
142 $fileLines = file( $fileName );
143 if ( $fileLines ===
false ) {
144 $this->
error(
"Unable to open list file $fileName." );
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;
158 $this->
error(
"Extension {$extension} doesn't exist" );
166 private function generateMessageFileList( array $setupFiles ) {
167 $IP = MW_INSTALL_PATH;
169 $outputFile = $this->
getOption(
'output' );
174 foreach ( $setupFiles as $fileName ) {
175 if ( strval( $fileName ) ===
'' ) {
179 fwrite( STDERR,
"Loading data from $fileName\n" );
182 if ( str_ends_with( $fileName,
'.json' ) ) {
183 $queue[$fileName] = 1;
185 require_once $fileName;
191 'wgExtensionMessagesFiles' => $config->get( MainConfigNames::ExtensionMessagesFiles ),
192 'wgMessagesDirs' => $config->get( MainConfigNames::MessagesDirs ),
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] );
206 fwrite( STDERR,
"\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";
221 foreach ( $dirs as $dir ) {
222 $s = preg_replace(
"/'" . preg_quote( $dir,
'/' ) .
"([^']*)'/",
'"$IP\1"', $s );
225 if ( $outputFile !==
null ) {
226 $res = file_put_contents( $outputFile, $s );
227 if (
$res ===
false ) {
228 fwrite( STDERR,
"Failed to write to $outputFile\n" );
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.