phpcs: More require/include is not a function
[lhc/web/wiklou.git] / maintenance / language / writeMessagesArray.inc
1 <?php
2 /**
3 * Write a messages array as a PHP text.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup MaintenanceLanguage
22 */
23
24 /**
25 * @ingroup MaintenanceLanguage
26 */
27 class MessageWriter {
28 static $optionalComment = 'only translate this message to other languages if you have to change it';
29 static $ignoredComment = "do not translate or duplicate this message to other languages";
30
31 static $messageStructure;
32 static $blockComments;
33 static $ignoredMessages;
34 static $optionalMessages;
35
36 /**
37 * Write a messages array as a PHP text and write it to the messages file.
38 *
39 * @param $messages Array: the messages array.
40 * @param $code String: the language code.
41 * @param $write Boolean: write to the messages file?
42 * @param $listUnknown Boolean: list the unknown messages?
43 * @param $removeUnknown Boolean: whether to remove unkown messages
44 * @param $messagesFolder String: path to a folder to store the MediaWiki messages. Defaults to the current install.
45 */
46 public static function writeMessagesToFile( $messages, $code, $write, $listUnknown, $removeUnknown, $messagesFolder = false ) {
47 # Rewrite the messages array
48 $messages = self::writeMessagesArray( $messages, $code == 'en', false, $removeUnknown );
49 $messagesText = $messages[0];
50 $sortedMessages = $messages[1];
51
52 # Write to the file
53 if ( $messagesFolder ) {
54 $filename = Language::getFileName( "$messagesFolder/Messages", $code );
55 } else {
56 $filename = Language::getMessagesFileName( $code );
57 }
58
59 if ( file_exists( $filename ) ) {
60 $contents = file_get_contents( $filename );
61 } else {
62 $contents = '<?php
63 $messages = array(
64 );
65 ';
66 }
67
68 if ( strpos( $contents, '$messages' ) !== false ) {
69 $contents = explode( '$messages', $contents );
70 if ( $messagesText == '$messages' . $contents[1] ) {
71 echo "Generated messages for language $code. Same as the current file.\n";
72 } else {
73 if ( $write ) {
74 $new = $contents[0];
75 $new .= $messagesText;
76 file_put_contents( $filename, $new );
77 echo "Generated and wrote messages for language $code.\n";
78 } else {
79 echo "Generated messages for language $code. Please run the script again (without the parameter \"dry-run\") to write the array to the file.\n";
80 }
81 }
82 if ( $listUnknown && isset( $sortedMessages['unknown'] ) && !empty( $sortedMessages['unknown'] ) ) {
83 if ( $removeUnknown ) {
84 echo "\nThe following " . count( $sortedMessages['unknown'] ) . " unknown messages have been removed:\n";
85 } else {
86 echo "\nThere are " . count( $sortedMessages['unknown'] ) . " unknown messages, please check them:\n";
87 }
88 foreach ( $sortedMessages['unknown'] as $key => $value ) {
89 echo "* " . $key . "\n";
90 }
91 }
92 } else {
93 echo "Generated messages for language $code. There seem to be no messages array in the file.\n";
94 }
95 }
96
97 /**
98 * Write a messages array as a PHP text.
99 *
100 * @param $messages Array: the messages array.
101 * @param $ignoredComments Boolean: show comments about ignored and optional
102 * messages? (For English.)
103 * @param $prefix String: base path for messages.inc and messageTypes.inc files
104 * or false for default path (this directory)
105 * @param $removeUnknown Boolean: whether to remove unkown messages
106 *
107 * @return Array of the PHP text and the sorted messages array.
108 */
109 public static function writeMessagesArray( $messages, $ignoredComments = false, $prefix = false, $removeUnknown = false ) {
110 # Load messages
111 $dir = $prefix ? $prefix : __DIR__;
112
113 require $dir . '/messages.inc';
114 self::$messageStructure = $wgMessageStructure;
115 self::$blockComments = $wgBlockComments;
116
117 require $dir . '/messageTypes.inc';
118 self::$ignoredMessages = $wgIgnoredMessages;
119 self::$optionalMessages = $wgOptionalMessages;
120
121 # Sort messages to blocks
122 $sortedMessages['unknown'] = $messages;
123 foreach ( self::$messageStructure as $blockName => $block ) {
124 /**
125 * @var $block array
126 */
127 foreach ( $block as $key ) {
128 if ( array_key_exists( $key, $sortedMessages['unknown'] ) ) {
129 $sortedMessages[$blockName][$key] = $sortedMessages['unknown'][$key];
130 unset( $sortedMessages['unknown'][$key] );
131 }
132 }
133 }
134
135 # Write all the messages
136 $messagesText = "\$messages = array(
137 ";
138 foreach ( $sortedMessages as $block => $messages ) {
139 # Skip if it's the block of unknown messages - handle that in the end of file
140 if ( $block == 'unknown' ) {
141 continue;
142 }
143
144 if ( $ignoredComments ) {
145 $ignored = self::$ignoredMessages;
146 $optional = self::$optionalMessages;
147 } else {
148 $ignored = array();
149 $optional = array();
150 }
151 $comments = self::makeComments( array_keys( $messages ), $ignored, $optional );
152
153 # Write the block
154 $messagesText .= self::writeMessagesBlock( self::$blockComments[$block], $messages, $comments );
155 }
156
157 # Write the unknown messages, alphabetically sorted.
158 # Of course, we don't have any comments for them, because they are unknown.
159 if ( !$removeUnknown ) {
160 ksort( $sortedMessages['unknown'] );
161 $messagesText .= self::writeMessagesBlock( 'Unknown messages', $sortedMessages['unknown'] );
162 }
163 $messagesText .= ");
164 ";
165 return array( $messagesText, $sortedMessages );
166 }
167
168 /**
169 * Generates an array of comments for messages.
170 *
171 * @param $messages Array: key of messages.
172 * @param $ignored Array: list of ingored message keys.
173 * @param $optional Array: list of optional message keys.
174 * @return array
175 */
176 public static function makeComments( $messages, $ignored, $optional ) {
177 # Comment collector
178 $commentArray = array();
179
180 # List of keys only
181 foreach ( $messages as $key ) {
182 if ( in_array( $key, $ignored ) ) {
183 $commentArray[$key] = ' # ' . self::$ignoredComment;
184 } elseif ( in_array( $key, $optional ) ) {
185 $commentArray[$key] = ' # ' . self::$optionalComment;
186 }
187 }
188
189 return $commentArray;
190 }
191
192 /**
193 * Write a block of messages to PHP.
194 *
195 * @param $blockComment String: the comment of whole block.
196 * @param $messages Array: the block messages.
197 * @param $messageComments Array: optional comments for messages in this block.
198 * @param $prefix String: prefix for every line, for indenting purposes.
199 *
200 * @return string The block, formatted in PHP.
201 */
202 public static function writeMessagesBlock( $blockComment, $messages,
203 $messageComments = array(), $prefix = '' ) {
204
205 $blockText = '';
206
207 # Skip the block if it includes no messages
208 if ( empty( $messages ) ) {
209 return '';
210 }
211
212 # Format the block comment (if exists); check for multiple lines comments
213 if ( !empty( $blockComment ) ) {
214 if ( strpos( $blockComment, "\n" ) === false ) {
215 $blockText .= "$prefix# $blockComment
216 ";
217 } else {
218 $blockText .= "$prefix/*
219 $blockComment
220 */
221 ";
222 }
223 }
224
225 # Get max key length
226 $maxKeyLength = max( array_map( 'strlen', array_keys( $messages ) ) );
227
228 # Format the messages
229 foreach ( $messages as $key => $value ) {
230 # Add the key name
231 $blockText .= "$prefix'$key'";
232
233 # Add the appropriate block whitespace
234 $blockText .= str_repeat( ' ', $maxKeyLength - strlen( $key ) );
235
236 # Refer to the value
237 $blockText .= ' => ';
238
239 # Check for the appropriate apostrophe and add the value
240 # Quote \ here, because it needs always escaping
241 $value = addcslashes( $value, '\\' );
242
243 # For readability
244 $single = "'";
245 $double = '"';
246
247 if ( strpos( $value, $single ) === false ) {
248 # Nothing ugly, just use '
249 $blockText .= $single . $value . $single;
250 } elseif ( strpos( $value, $double ) === false && !preg_match( '/\$[a-zA-Z_\x7f-\xff]/', $value ) ) {
251 # No "-quotes, no variables that need quoting, use "
252 $blockText .= $double . $value . $double;
253 } else {
254 # Something needs quoting, pick the quote which causes less quoting
255 $quote = substr_count( $value, $double ) + substr_count( $value, '$' ) >= substr_count( $value, $single ) ? $single : $double;
256 if ( $quote === $double ) {
257 $extra = '$';
258 } else {
259 $extra = '';
260 }
261 $blockText .= $quote . addcslashes( $value, $quote . $extra ) . $quote;
262 }
263
264 # Comma
265 $blockText .= ',';
266
267 # Add comments, if there is any
268 if ( array_key_exists( $key, $messageComments ) ) {
269 $blockText .= $messageComments[$key];
270 }
271
272 # Newline
273 $blockText .= "
274 ";
275 }
276
277 # Newline to end the block
278 $blockText .= "
279 ";
280
281 return $blockText;
282 }
283 }