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