Merge "Typo fix"
[mediawiki.git] / maintenance / mergeMessageFileList.php
blobb36a319dea275c5a7ee18e1616b7eaca3b362471
1 <?php
2 /**
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
21 * @file
22 * @ingroup Maintenance
25 # Start from scratch
26 define( 'MW_NO_EXTENSION_MESSAGES', 1 );
28 require_once __DIR__ . '/Maintenance.php';
29 $maintClass = 'MergeMessageFileList';
30 $mmfl = false;
32 /**
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 {
40 function __construct() {
41 parent::__construct();
42 $this->addOption( 'list-file', 'A file containing a list of extension setup files, one per line.', true, true );
43 $this->addOption( 'extensions-dir', 'Path where extensions can be found.', false, true );
44 $this->addOption( 'output', 'Send output to this file (omit for stdout)', false, true );
45 $this->mDescription = 'Merge $wgExtensionMessagesFiles from various extensions to produce a ' .
46 'single array containing all message files.';
49 public function execute() {
50 global $mmfl;
52 # Add setup files contained in file passed to --list-file
53 $lines = file( $this->getOption( 'list-file' ) );
54 if ( $lines === false ) {
55 $this->error( 'Unable to open list file.' );
57 $mmfl = array( 'setupFiles' => array() );
59 # Strip comments, discard empty lines, and trim leading and trailing
60 # whitespace. Comments start with '#' and extend to the end of the line.
61 foreach( $lines as $line ) {
62 $line = trim( preg_replace( '/#.*/', '', $line ) );
63 if ( $line !== '' ) {
64 $mmfl['setupFiles'][] = $line;
68 # Now find out files in a directory
69 $hasError = false;
70 if ( $this->hasOption( 'extensions-dir' ) ) {
71 $extdir = $this->getOption( 'extensions-dir' );
72 $entries = scandir( $extdir );
73 foreach ( $entries as $extname ) {
74 if ( $extname == '.' || $extname == '..' || !is_dir( "$extdir/$extname" ) ) {
75 continue;
77 $extfile = "{$extdir}/{$extname}/{$extname}.php";
78 if ( file_exists( $extfile ) ) {
79 $mmfl['setupFiles'][] = $extfile;
80 } else {
81 $hasError = true;
82 $this->error( "Extension {$extname} in {$extdir} lacks expected {$extname}.php" );
87 if ( $hasError ) {
88 $this->error( "Some files are missing (see above). Giving up.", 1 );
91 if ( $this->hasOption( 'output' ) ) {
92 $mmfl['output'] = $this->getOption( 'output' );
94 if ( $this->hasOption( 'quiet' ) ) {
95 $mmfl['quiet'] = true;
100 require_once RUN_MAINTENANCE_IF_MAIN;
102 foreach ( $mmfl['setupFiles'] as $fileName ) {
103 if ( strval( $fileName ) === '' ) {
104 continue;
106 $fileName = str_replace( '$IP', $IP, $fileName );
107 if ( empty( $mmfl['quiet'] ) ) {
108 fwrite( STDERR, "Loading data from $fileName\n" );
110 if ( !( include_once $fileName ) ) {
111 fwrite( STDERR, "Unable to read $fileName\n" );
112 exit( 1 );
115 fwrite( STDERR, "\n" );
116 $s =
117 "<" . "?php\n" .
118 "## This file is generated by mergeMessageFileList.php. Do not edit it directly.\n\n" .
119 "if ( defined( 'MW_NO_EXTENSION_MESSAGES' ) ) return;\n\n" .
120 '$wgExtensionMessagesFiles = ' . var_export( $wgExtensionMessagesFiles, true ) . ";\n\n";
122 $dirs = array(
123 $IP,
124 dirname( __DIR__ ),
125 realpath( $IP )
128 foreach ( $dirs as $dir ) {
129 $s = preg_replace(
130 "/'" . preg_quote( $dir, '/' ) . "([^']*)'/",
131 '"$IP\1"',
132 $s );
135 if ( isset( $mmfl['output'] ) ) {
136 file_put_contents( $mmfl['output'], $s );
137 } else {
138 echo $s;