Merge "Make DBAccessBase use DBConnRef, rename $wiki, and hide getLoadBalancer()"
[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 __DIR__ . '/Maintenance.php';
29 $maintClass = MergeMessageFileList::class;
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 function __construct() {
40 parent::__construct();
41 $this->addOption(
42 'list-file',
43 'A file containing a list of extension setup files, one per line.',
44 false,
45 true
46 );
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.'
51 );
52 }
53
54 public function execute() {
55 // phpcs:ignore MediaWiki.NamingConventions.ValidGlobalName.wgPrefix
56 global $mmfl;
57 global $wgExtensionEntryPointListFiles;
58
59 if ( !count( $wgExtensionEntryPointListFiles )
60 && !$this->hasOption( 'list-file' )
61 && !$this->hasOption( 'extensions-dir' )
62 ) {
63 $this->fatalError( "Either --list-file or --extensions-dir must be provided if " .
64 "\$wgExtensionEntryPointListFiles is not set" );
65 }
66
67 $mmfl = [ 'setupFiles' => [] ];
68
69 # Add setup files contained in file passed to --list-file
70 if ( $this->hasOption( 'list-file' ) ) {
71 $extensionPaths = $this->readFile( $this->getOption( 'list-file' ) );
72 $mmfl['setupFiles'] = array_merge( $mmfl['setupFiles'], $extensionPaths );
73 }
74
75 # Now find out files in a directory
76 if ( $this->hasOption( 'extensions-dir' ) ) {
77 $extdir = $this->getOption( 'extensions-dir' );
78 # Allow multiple directories to be passed with ":" as delimiter
79 $extdirs = explode( ':', $extdir );
80 foreach ( $extdirs as $extdir ) {
81 $entries = scandir( $extdir );
82 foreach ( $entries as $extname ) {
83 if ( $extname == '.' || $extname == '..' || !is_dir( "$extdir/$extname" ) ) {
84 continue;
85 }
86 $possibilities = [
87 "$extdir/$extname/extension.json",
88 "$extdir/$extname/skin.json",
89 "$extdir/$extname/$extname.php"
90 ];
91 $found = false;
92 foreach ( $possibilities as $extfile ) {
93 if ( file_exists( $extfile ) ) {
94 $mmfl['setupFiles'][] = $extfile;
95 $found = true;
96 break;
97 }
98 }
99
100 if ( !$found ) {
101 $this->error( "Extension {$extname} in {$extdir} lacks expected entry point: " .
102 "extension.json, skin.json, or {$extname}.php." );
103 }
104 }
105 }
106 }
107
108 # Add setup files defined via configuration
109 foreach ( $wgExtensionEntryPointListFiles as $points ) {
110 $extensionPaths = $this->readFile( $points );
111 $mmfl['setupFiles'] = array_merge( $mmfl['setupFiles'], $extensionPaths );
112 }
113
114 if ( $this->hasOption( 'output' ) ) {
115 $mmfl['output'] = $this->getOption( 'output' );
116 }
117 if ( $this->hasOption( 'quiet' ) ) {
118 $mmfl['quiet'] = true;
119 }
120 }
121
122 /**
123 * @param string $fileName
124 * @return array List of absolute extension paths
125 */
126 private function readFile( $fileName ) {
127 global $IP;
128
129 $files = [];
130 $fileLines = file( $fileName );
131 if ( $fileLines === false ) {
132 $this->error( "Unable to open list file $fileName." );
133
134 return $files;
135 }
136 # Strip comments, discard empty lines, and trim leading and trailing
137 # whitespace. Comments start with '#' and extend to the end of the line.
138 foreach ( $fileLines as $extension ) {
139 $extension = trim( preg_replace( '/#.*/', '', $extension ) );
140 if ( $extension !== '' ) {
141 # Paths may use the string $IP to be substituted by the actual value
142 $extension = str_replace( '$IP', $IP, $extension );
143 if ( file_exists( $extension ) ) {
144 $files[] = $extension;
145 } else {
146 $this->error( "Extension {$extension} doesn't exist" );
147 }
148 }
149 }
150
151 return $files;
152 }
153 }
154
155 require_once RUN_MAINTENANCE_IF_MAIN;
156
157 $queue = [];
158 '@phan-var string[][] $mmfl';
159 foreach ( $mmfl['setupFiles'] as $fileName ) {
160 if ( strval( $fileName ) === '' ) {
161 continue;
162 }
163 if ( empty( $mmfl['quiet'] ) ) {
164 fwrite( STDERR, "Loading data from $fileName\n" );
165 }
166 // Using extension.json or skin.json
167 if ( substr( $fileName, -strlen( '.json' ) ) === '.json' ) {
168 $queue[$fileName] = 1;
169 } else {
170 require_once $fileName;
171 }
172 }
173
174 if ( $queue ) {
175 $registry = new ExtensionRegistry();
176 $data = $registry->readFromQueue( $queue );
177 foreach ( [ 'wgExtensionMessagesFiles', 'wgMessagesDirs' ] as $var ) {
178 if ( isset( $data['globals'][$var] ) ) {
179 $GLOBALS[$var] = array_merge( $data['globals'][$var], $GLOBALS[$var] );
180 }
181 }
182 }
183
184 fwrite( STDERR, "\n" );
185 $s =
186 "<?php\n" .
187 "## This file is generated by mergeMessageFileList.php. Do not edit it directly.\n\n" .
188 "if ( defined( 'MW_NO_EXTENSION_MESSAGES' ) ) return;\n\n" .
189 '$wgExtensionMessagesFiles = ' . var_export( $wgExtensionMessagesFiles, true ) . ";\n\n" .
190 '$wgMessagesDirs = ' . var_export( $wgMessagesDirs, true ) . ";\n\n";
191
192 $dirs = [
193 $IP,
194 dirname( __DIR__ ),
195 realpath( $IP )
196 ];
197
198 foreach ( $dirs as $dir ) {
199 $s = preg_replace( "/'" . preg_quote( $dir, '/' ) . "([^']*)'/", '"$IP\1"', $s );
200 }
201
202 if ( isset( $mmfl['output'] ) ) {
203 file_put_contents( $mmfl['output'], $s );
204 } else {
205 echo $s;
206 }