Merge "Revert "merged master"" into Wikidata
[lhc/web/wiklou.git] / maintenance / mergeMessageFileList.php
1 <?php
2 /**
3 * Merge $wgExtensionMessagesFiles from various extensions to produce a
4 * single array containing all message files.
5 *
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.
10 *
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.
15 *
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
20 *
21 * @file
22 * @ingroup Maintenance
23 */
24
25 # Start from scratch
26 define( 'MW_NO_EXTENSION_MESSAGES', 1 );
27
28 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
29 $maintClass = 'MergeMessageFileList';
30 $mmfl = false;
31
32 /**
33 * Maintenance script that merges $wgExtensionMessagesFiles from various
34 * extensions to produce a single array containing all message files.
35 *
36 * @ingroup Maintenance
37 */
38 class MergeMessageFileList extends Maintenance {
39
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.';
47 }
48
49 public function execute() {
50 global $mmfl;
51
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.' );
56 }
57 $mmfl = array( 'setupFiles' => array_map( 'trim', $lines ) );
58
59 # Now find out files in a directory
60 $hasError = false;
61 if ( $this->hasOption( 'extensions-dir' ) ) {
62 $extdir = $this->getOption( 'extensions-dir' );
63 $entries = scandir( $extdir );
64 foreach( $entries as $extname ) {
65 if ( $extname == '.' || $extname == '..' || !is_dir( "$extdir/$extname" ) ) {
66 continue;
67 }
68 $extfile = "{$extdir}/{$extname}/{$extname}.php";
69 if ( file_exists( $extfile ) ) {
70 $mmfl['setupFiles'][] = $extfile;
71 } else {
72 $hasError = true;
73 $this->error( "Extension {$extname} in {$extdir} lacks expected {$extname}.php" );
74 }
75 }
76 }
77
78 if ( $hasError ) {
79 $this->error( "Some files are missing (see above). Giving up.", 1 );
80 }
81
82 if ( $this->hasOption( 'output' ) ) {
83 $mmfl['output'] = $this->getOption( 'output' );
84 }
85 }
86 }
87
88 require_once( RUN_MAINTENANCE_IF_MAIN );
89
90 foreach ( $mmfl['setupFiles'] as $fileName ) {
91 if ( strval( $fileName ) === '' ) {
92 continue;
93 }
94 $fileName = str_replace( '$IP', $IP, $fileName );
95 fwrite( STDERR, "Loading data from $fileName\n" );
96 include_once( $fileName );
97 }
98 fwrite( STDERR, "\n" );
99 $s =
100 "<" . "?php\n" .
101 "## This file is generated by mergeMessageFileList.php. Do not edit it directly.\n\n" .
102 "if ( defined( 'MW_NO_EXTENSION_MESSAGES' ) ) return;\n\n" .
103 '$wgExtensionMessagesFiles = ' . var_export( $wgExtensionMessagesFiles, true ) . ";\n\n";
104
105 $dirs = array(
106 $IP,
107 dirname( dirname( __FILE__ ) ),
108 realpath( $IP )
109 );
110
111 foreach ( $dirs as $dir ) {
112 $s = preg_replace(
113 "/'" . preg_quote( $dir, '/' ) . "([^']*)'/",
114 '"$IP\1"',
115 $s );
116 }
117
118 if ( isset( $mmfl['output'] ) ) {
119 file_put_contents( $mmfl['output'], $s );
120 } else {
121 echo $s;
122 }
123