3 * Merge $wgExtensionMessagesFiles from various extensions to produce a
4 * single array containing all message files.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
22 * @ingroup Maintenance
26 define( 'MW_NO_EXTENSION_MESSAGES', 1 );
28 require_once __DIR__
. '/Maintenance.php';
29 $maintClass = 'MergeMessageFileList';
33 * Maintenance script that merges $wgExtensionMessagesFiles from various
34 * extensions to produce a single array containing all message files.
36 * @ingroup Maintenance
38 class MergeMessageFileList
extends Maintenance
{
39 function __construct() {
40 parent
::__construct();
43 'A file containing a list of extension setup files, one per line.',
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.'
54 public function execute() {
55 // @codingStandardsIgnoreStart Ignore error: Global variable "$mmfl" is lacking 'wg' prefix
57 // @codingStandardsIgnoreEnd
58 global $wgExtensionEntryPointListFiles;
60 if ( !count( $wgExtensionEntryPointListFiles )
61 && !$this->hasOption( 'list-file' )
62 && !$this->hasOption( 'extensions-dir' )
64 $this->error( "Either --list-file or --extensions-dir must be provided if " .
65 "\$wgExtensionEntryPointListFiles is not set", 1 );
68 $mmfl = [ 'setupFiles' => [] ];
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 );
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 );
82 foreach ( $extdirs as $extdir ) {
83 $entries = array_merge( $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",
92 "$extdir/$extname/$extname.php"
95 foreach ( $possibilities as $extfile ) {
96 if ( file_exists( $extfile ) ) {
97 $mmfl['setupFiles'][] = $extfile;
104 $this->error( "Extension {$extname} in {$extdir} lacks expected entry point: " .
105 "extension.json, skin.json, or {$extname}.php." );
110 # Add setup files defined via configuration
111 foreach ( $wgExtensionEntryPointListFiles as $points ) {
112 $extensionPaths = $this->readFile( $points );
113 $mmfl['setupFiles'] = array_merge( $mmfl['setupFiles'], $extensionPaths );
116 if ( $this->hasOption( 'output' ) ) {
117 $mmfl['output'] = $this->getOption( 'output' );
119 if ( $this->hasOption( 'quiet' ) ) {
120 $mmfl['quiet'] = true;
125 * @param string $fileName
126 * @return array List of absolute extension paths
128 private function readFile( $fileName ) {
132 $fileLines = file( $fileName );
133 if ( $fileLines === false ) {
134 $this->hasError
= true;
135 $this->error( "Unable to open list file $fileName." );
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;
149 $this->hasError
= true;
150 $this->error( "Extension {$extension} doesn't exist" );
159 require_once RUN_MAINTENANCE_IF_MAIN
;
162 foreach ( $mmfl['setupFiles'] as $fileName ) {
163 if ( strval( $fileName ) === '' ) {
166 if ( empty( $mmfl['quiet'] ) ) {
167 fwrite( STDERR
, "Loading data from $fileName\n" );
169 // Using extension.json or skin.json
170 if ( substr( $fileName, -strlen( '.json' ) ) === '.json' ) {
171 $queue[$fileName] = 1;
173 require_once $fileName;
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] );
187 fwrite( STDERR
, "\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";
201 foreach ( $dirs as $dir ) {
202 $s = preg_replace( "/'" . preg_quote( $dir, '/' ) . "([^']*)'/", '"$IP\1"', $s );
205 if ( isset( $mmfl['output'] ) ) {
206 file_put_contents( $mmfl['output'], $s );