3 * Write a messages array as a PHP text.
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.
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.
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
21 * @ingroup MaintenanceLanguage
25 * @ingroup MaintenanceLanguage
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";
31 static $messageStructure;
32 static $blockComments;
33 static $ignoredMessages;
34 static $optionalMessages;
37 * Write a messages array as a PHP text and write it to the messages file.
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.
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];
53 if ( $messagesFolder )
54 $filename = Language::getFileName( "$messagesFolder/Messages", $code );
56 $filename = Language::getMessagesFileName( $code );
58 if ( file_exists( $filename ) )
59 $contents = file_get_contents( $filename );
66 if( strpos( $contents, '$messages' ) !== false ) {
67 $contents = explode( '$messages', $contents );
68 if( $messagesText == '$messages' . $contents[1] ) {
69 echo "Generated messages for language $code. Same as the current file.\n";
73 $new .= $messagesText;
74 file_put_contents( $filename, $new );
75 echo "Generated and wrote messages for language $code.\n";
77 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 if( $listUnknown && isset( $sortedMessages['unknown'] ) && !empty( $sortedMessages['unknown'] ) ) {
82 echo "\nThe following " . count( $sortedMessages['unknown'] ) . " unknown messages have been removed:\n";
84 echo "\nThere are " . count( $sortedMessages['unknown'] ) . " unknown messages, please check them:\n";
85 foreach( $sortedMessages['unknown'] as $key => $value ) {
86 echo "* " . $key . "\n";
90 echo "Generated messages for language $code. There seem to be no messages array in the file.\n";
95 * Write a messages array as a PHP text.
97 * @param $messages Array: the messages array.
98 * @param $ignoredComments Boolean: show comments about ignored and optional
99 * messages? (For English.)
100 * @param $prefix String: base path for messages.inc and messageTypes.inc files
101 * or false for default path (this directory)
102 * @param $removeUnknown Boolean: whether to remove unkown messages
104 * @return Array of the PHP text and the sorted messages array.
106 public static function writeMessagesArray( $messages, $ignoredComments = false, $prefix = false, $removeUnknown = false ) {
108 $dir = $prefix ? $prefix : __DIR__;
110 require( $dir . '/messages.inc' );
111 self::$messageStructure = $wgMessageStructure;
112 self::$blockComments = $wgBlockComments;
114 require( $dir . '/messageTypes.inc' );
115 self::$ignoredMessages = $wgIgnoredMessages;
116 self::$optionalMessages = $wgOptionalMessages;
118 # Sort messages to blocks
119 $sortedMessages['unknown'] = $messages;
120 foreach( self::$messageStructure as $blockName => $block ) {
124 foreach( $block as $key ) {
125 if( array_key_exists( $key, $sortedMessages['unknown'] ) ) {
126 $sortedMessages[$blockName][$key] = $sortedMessages['unknown'][$key];
127 unset( $sortedMessages['unknown'][$key] );
132 # Write all the messages
133 $messagesText = "\$messages = array(
135 foreach( $sortedMessages as $block => $messages ) {
136 # Skip if it's the block of unknown messages - handle that in the end of file
137 if( $block == 'unknown' ) {
141 if( $ignoredComments ) {
142 $ignored = self::$ignoredMessages;
143 $optional = self::$optionalMessages;
148 $comments = self::makeComments( array_keys( $messages ), $ignored, $optional );
151 $messagesText .= self::writeMessagesBlock( self::$blockComments[$block], $messages, $comments );
154 # Write the unknown messages, alphabetically sorted.
155 # Of course, we don't have any comments for them, because they are unknown.
156 if ( !$removeUnknown ) {
157 ksort( $sortedMessages['unknown'] );
158 $messagesText .= self::writeMessagesBlock( 'Unknown messages', $sortedMessages['unknown'] );
162 return array( $messagesText, $sortedMessages );
166 * Generates an array of comments for messages.
168 * @param $messages Array: key of messages.
169 * @param $ignored Array: list of ingored message keys.
170 * @param $optional Array: list of optional message keys.
173 public static function makeComments( $messages, $ignored, $optional ) {
175 $commentArray = array();
178 foreach( $messages as $key ) {
179 if( in_array( $key, $ignored ) ) {
180 $commentArray[$key] = ' # ' . self::$ignoredComment;
181 } elseif( in_array( $key, $optional ) ) {
182 $commentArray[$key] = ' # ' . self::$optionalComment;
186 return $commentArray;
190 * Write a block of messages to PHP.
192 * @param $blockComment String: the comment of whole block.
193 * @param $messages Array: the block messages.
194 * @param $messageComments Array: optional comments for messages in this block.
195 * @param $prefix String: prefix for every line, for indenting purposes.
197 * @return string The block, formatted in PHP.
199 public static function writeMessagesBlock( $blockComment, $messages,
200 $messageComments = array(), $prefix = '' ) {
204 # Skip the block if it includes no messages
205 if( empty( $messages ) ) {
209 # Format the block comment (if exists); check for multiple lines comments
210 if( !empty( $blockComment ) ) {
211 if( strpos( $blockComment, "\n" ) === false ) {
212 $blockText .= "$prefix# $blockComment
215 $blockText .= "$prefix/*
223 $maxKeyLength = max( array_map( 'strlen', array_keys( $messages ) ) );
225 # Format the messages
226 foreach( $messages as $key => $value ) {
228 $blockText .= "$prefix'$key'";
230 # Add the appropriate block whitespace
231 $blockText .= str_repeat( ' ', $maxKeyLength - strlen( $key ) );
234 $blockText .= ' => ';
236 # Check for the appropriate apostrophe and add the value
237 # Quote \ here, because it needs always escaping
238 $value = addcslashes( $value, '\\' );
244 if( strpos( $value, $single ) === false ) {
245 # Nothing ugly, just use '
246 $blockText .= $single . $value . $single;
247 } elseif( strpos( $value, $double ) === false && !preg_match('/\$[a-zA-Z_\x7f-\xff]/', $value) ) {
248 # No "-quotes, no variables that need quoting, use "
249 $blockText .= $double . $value . $double;
251 # Something needs quoting, pick the quote which causes less quoting
252 $quote = substr_count( $value, $double ) + substr_count( $value, '$' ) >= substr_count( $value, $single ) ? $single : $double;
253 if( $quote === $double ) {
258 $blockText .= $quote . addcslashes( $value, $quote . $extra ) . $quote;
264 # Add comments, if there is any
265 if( array_key_exists( $key, $messageComments ) ) {
266 $blockText .= $messageComments[$key];
274 # Newline to end the block