44 parent::__construct();
47 'A file containing a list of extension setup files, one per line.',
51 $this->addOption(
'extensions-dir',
'Path where extensions can be found.',
false,
true );
52 $this->addOption(
'output',
'Send output to this file (omit for stdout)',
false,
true );
53 $this->addDescription(
'Merge $wgExtensionMessagesFiles and $wgMessagesDirs from ' .
54 ' various extensions to produce a single file listing all message files and dirs.'
59 $config = $this->getConfig();
60 $extensionEntryPointListFiles = $config->get( MainConfigNames::ExtensionEntryPointListFiles );
62 if ( !count( $extensionEntryPointListFiles )
63 && !$this->hasOption(
'list-file' )
64 && !$this->hasOption(
'extensions-dir' )
66 $this->fatalError(
"Either --list-file or --extensions-dir must be provided if " .
67 "\$wgExtensionEntryPointListFiles is not set" );
72 # Add setup files contained in file passed to --list-file
73 if ( $this->hasOption(
'list-file' ) ) {
74 $extensionPaths = $this->readFile( $this->getOption(
'list-file' ) );
75 $setupFiles = array_merge( $setupFiles, $extensionPaths );
78 # Now find out files in a directory
79 if ( $this->hasOption(
'extensions-dir' ) ) {
80 $extdir = $this->getOption(
'extensions-dir' );
81 # Allow multiple directories to be passed with ":" as delimiter
82 $extdirs = explode(
':', $extdir );
83 foreach ( $extdirs as $extdir ) {
84 $entries = scandir( $extdir );
85 foreach ( $entries as $extname ) {
86 if ( $extname ==
'.' || $extname ==
'..' || !is_dir(
"$extdir/$extname" ) ) {
90 "$extdir/$extname/extension.json",
91 "$extdir/$extname/skin.json",
94 foreach ( $possibilities as $extfile ) {
95 if ( file_exists( $extfile ) ) {
96 $setupFiles[] = $extfile;
103 $this->error(
"Extension {$extname} in {$extdir} lacks expected entry point: " .
104 "extension.json or skin.json " .
105 "(PHP entry points are no longer supported by this script)." );
111 # Add setup files defined via configuration
112 foreach ( $extensionEntryPointListFiles as $points ) {
113 $extensionPaths = $this->readFile( $points );
114 $setupFiles = array_merge( $setupFiles, $extensionPaths );
117 $this->generateMessageFileList( $setupFiles );
121 # This script commonly needs to be run before the l10n cache. But if
122 # LanguageCode is not 'en', it won't be able to run because there is
123 # no l10n cache. Break the cycle by forcing the LanguageCode setting to 'en'.
124 $settingsBuilder->
putConfigValue( MainConfigNames::LanguageCode,
'en' );
125 parent::finalSetup( $settingsBuilder );
134 return Maintenance::DB_NONE;
141 private function readFile( $fileName ) {
142 $IP = MW_INSTALL_PATH;
145 $fileLines = file( $fileName );
146 if ( $fileLines ===
false ) {
147 $this->error(
"Unable to open list file $fileName." );
151 # Strip comments, discard empty lines, and trim leading and trailing
152 # whitespace. Comments start with '#' and extend to the end of the line.
153 foreach ( $fileLines as $extension ) {
154 $extension = trim( preg_replace(
'/#.*/',
'', $extension ) );
155 if ( $extension !==
'' ) {
156 # Paths may use the string $IP to be substituted by the actual value
157 $extension = str_replace(
'$IP',
$IP, $extension );
158 if ( !str_ends_with( $extension,
'.json' ) ) {
159 $this->error(
"Extension {$extension} does not end with .json " .
160 "(PHP entry points are no longer supported by this script)" );
161 } elseif ( file_exists( $extension ) ) {
162 $files[] = $extension;
164 $this->error(
"Extension {$extension} doesn't exist" );
172 private function generateMessageFileList( array $setupFiles ) {
173 $IP = MW_INSTALL_PATH;
175 $outputFile = $this->getOption(
'output' );
176 $quiet = $this->hasOption(
'quiet' );
180 foreach ( $setupFiles as $fileName ) {
181 if ( strval( $fileName ) ===
'' ) {
185 fwrite( STDERR,
"Loading data from $fileName\n" );
187 $queue[$fileName] = 1;
190 $config = $this->getConfig();
192 'wgExtensionMessagesFiles' => $config->get( MainConfigNames::ExtensionMessagesFiles ),
193 'wgMessagesDirs' => $config->get( MainConfigNames::MessagesDirs ),
198 $data = $registry->readFromQueue( $queue );
199 foreach ( [
'wgExtensionMessagesFiles',
'wgMessagesDirs' ] as $var ) {
200 if ( isset( $data[
'globals'][$var] ) ) {
201 $vars[$var] = array_merge( $data[
'globals'][$var], $vars[$var] );
207 fwrite( STDERR,
"\n" );
211 "## This file is generated by mergeMessageFileList.php. Do not edit it directly.\n\n" .
212 "if ( defined( 'MW_NO_EXTENSION_MESSAGES' ) ) return;\n\n" .
213 '$wgExtensionMessagesFiles = ' . var_export( $vars[
'wgExtensionMessagesFiles'],
true ) .
";\n\n" .
214 '$wgMessagesDirs = ' . var_export( $vars[
'wgMessagesDirs'],
true ) .
";\n\n";
222 foreach ( $dirs as $dir ) {
223 $s = preg_replace(
"/'" . preg_quote( $dir,
'/' ) .
"([^']*)'/",
'"$IP\1"', $s );
226 if ( $outputFile !==
null ) {
227 $res = file_put_contents( $outputFile, $s );
228 if ( $res ===
false ) {
229 fwrite( STDERR,
"Failed to write to $outputFile\n" );