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
{
44 function __construct() {
45 parent
::__construct();
48 'A file containing a list of extension setup files, one per line.',
52 $this->addOption( 'extensions-dir', 'Path where extensions can be found.', false, true );
53 $this->addOption( 'output', 'Send output to this file (omit for stdout)', false, true );
54 $this->mDescription
= 'Merge $wgExtensionMessagesFiles and $wgMessagesDirs from ' .
55 ' various extensions to produce a single file listing all message files and dirs.';
58 public function execute() {
59 // @codingStandardsIgnoreStart Ignore error: Global variable "$mmfl" is lacking 'wg' prefix
61 // @codingStandardsIgnoreEnd
62 global $wgExtensionEntryPointListFiles;
64 if ( !count( $wgExtensionEntryPointListFiles )
65 && !$this->hasOption( 'list-file' )
66 && !$this->hasOption( 'extensions-dir' )
68 $this->error( "Either --list-file or --extensions-dir must be provided if " .
69 "\$wgExtensionEntryPointListFiles is not set", 1 );
72 $mmfl = array( 'setupFiles' => array() );
74 # Add setup files contained in file passed to --list-file
75 if ( $this->hasOption( 'list-file' ) ) {
76 $extensionPaths = $this->readFile( $this->getOption( 'list-file' ) );
77 $mmfl['setupFiles'] = array_merge( $mmfl['setupFiles'], $extensionPaths );
80 # Now find out files in a directory
81 if ( $this->hasOption( 'extensions-dir' ) ) {
82 $extdir = $this->getOption( 'extensions-dir' );
83 $entries = scandir( $extdir );
84 foreach ( $entries as $extname ) {
85 if ( $extname == '.' ||
$extname == '..' ||
!is_dir( "$extdir/$extname" ) ) {
88 $possibilities = array(
89 "$extdir/$extname/extension.json",
90 "$extdir/$extname/skin.json",
91 "$extdir/$extname/$extname.php"
94 foreach ( $possibilities as $extfile ) {
95 if ( file_exists( $extfile ) ) {
96 $mmfl['setupFiles'][] = $extfile;
103 $this->hasError
= true;
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->hasError
) {
117 $this->error( "Some files are missing (see above). Giving up.", 1 );
120 if ( $this->hasOption( 'output' ) ) {
121 $mmfl['output'] = $this->getOption( 'output' );
123 if ( $this->hasOption( 'quiet' ) ) {
124 $mmfl['quiet'] = true;
129 * @param string $fileName
130 * @return array List of absolute extension paths
132 private function readFile( $fileName ) {
136 $fileLines = file( $fileName );
137 if ( $fileLines === false ) {
138 $this->hasError
= true;
139 $this->error( "Unable to open list file $fileName." );
143 # Strip comments, discard empty lines, and trim leading and trailing
144 # whitespace. Comments start with '#' and extend to the end of the line.
145 foreach ( $fileLines as $extension ) {
146 $extension = trim( preg_replace( '/#.*/', '', $extension ) );
147 if ( $extension !== '' ) {
148 # Paths may use the string $IP to be substituted by the actual value
149 $extension = str_replace( '$IP', $IP, $extension );
150 if ( file_exists( $extension ) ) {
151 $files[] = $extension;
153 $this->hasError
= true;
154 $this->error( "Extension {$extension} doesn't exist" );
163 require_once RUN_MAINTENANCE_IF_MAIN
;
166 foreach ( $mmfl['setupFiles'] as $fileName ) {
167 if ( strval( $fileName ) === '' ) {
170 if ( empty( $mmfl['quiet'] ) ) {
171 fwrite( STDERR
, "Loading data from $fileName\n" );
173 // Using extension.json or skin.json
174 if ( substr( $fileName, -strlen( '.json' ) ) === '.json' ) {
175 $queue[$fileName] = 1;
177 require_once $fileName;
182 $registry = new ExtensionRegistry();
183 $data = $registry->readFromQueue( $queue );
184 foreach ( array( 'wgExtensionMessagesFiles', 'wgMessagesDirs' ) as $var ) {
185 if ( isset( $data['globals'][$var] ) ) {
186 $GLOBALS[$var] = array_merge( $data['globals'][$var], $GLOBALS[$var] );
191 fwrite( STDERR
, "\n" );
194 "## This file is generated by mergeMessageFileList.php. Do not edit it directly.\n\n" .
195 "if ( defined( 'MW_NO_EXTENSION_MESSAGES' ) ) return;\n\n" .
196 '$wgExtensionMessagesFiles = ' . var_export( $wgExtensionMessagesFiles, true ) . ";\n\n" .
197 '$wgMessagesDirs = ' . var_export( $wgMessagesDirs, true ) . ";\n\n";
205 foreach ( $dirs as $dir ) {
206 $s = preg_replace( "/'" . preg_quote( $dir, '/' ) . "([^']*)'/", '"$IP\1"', $s );
209 if ( isset( $mmfl['output'] ) ) {
210 file_put_contents( $mmfl['output'], $s );