phpcs: More require/include is not a function
[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 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 if ( $this->hasOption( 'quiet' ) ) {
86 $mmfl['quiet'] = true;
87 }
88 }
89 }
90
91 require_once RUN_MAINTENANCE_IF_MAIN;
92
93 foreach ( $mmfl['setupFiles'] as $fileName ) {
94 if ( strval( $fileName ) === '' ) {
95 continue;
96 }
97 $fileName = str_replace( '$IP', $IP, $fileName );
98 if ( empty( $mmfl['quiet'] ) ) {
99 fwrite( STDERR, "Loading data from $fileName\n" );
100 }
101 if ( !( include_once $fileName ) ) {
102 fwrite( STDERR, "Unable to read $fileName\n" );
103 exit( 1 );
104 }
105 }
106 fwrite( STDERR, "\n" );
107 $s =
108 "<" . "?php\n" .
109 "## This file is generated by mergeMessageFileList.php. Do not edit it directly.\n\n" .
110 "if ( defined( 'MW_NO_EXTENSION_MESSAGES' ) ) return;\n\n" .
111 '$wgExtensionMessagesFiles = ' . var_export( $wgExtensionMessagesFiles, true ) . ";\n\n";
112
113 $dirs = array(
114 $IP,
115 dirname( __DIR__ ),
116 realpath( $IP )
117 );
118
119 foreach ( $dirs as $dir ) {
120 $s = preg_replace(
121 "/'" . preg_quote( $dir, '/' ) . "([^']*)'/",
122 '"$IP\1"',
123 $s );
124 }
125
126 if ( isset( $mmfl['output'] ) ) {
127 file_put_contents( $mmfl['output'], $s );
128 } else {
129 echo $s;
130 }