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 protected static $optionalComment =
29 'only translate this message to other languages if you have to change it';
30 protected static $ignoredComment = "do not translate or duplicate this message to other languages";
32 protected static $messageStructure;
33 protected static $blockComments;
34 protected static $ignoredMessages;
35 protected static $optionalMessages;
38 * Write a messages array as a PHP text and write it to the messages file.
40 * @param array $messages The messages array.
41 * @param string $code The language code.
42 * @param bool $write Write to the messages file?
43 * @param bool $listUnknown List the unknown messages?
44 * @param bool $removeUnknown Whether to remove unkown messages
45 * @param string $messagesFolder Path to a folder to store the MediaWiki messages.
46 * Defaults to the current install.
48 public static function writeMessagesToFile( $messages, $code, $write, $listUnknown,
49 $removeUnknown, $messagesFolder = false
51 # Rewrite the messages array
52 $messages = self::writeMessagesArray( $messages, $code == 'en', false, $removeUnknown );
53 $messagesText = $messages[0];
54 $sortedMessages = $messages[1];
57 if ( $messagesFolder ) {
58 $filename = Language::getFileName( "$messagesFolder/Messages", $code );
60 $filename = Language::getMessagesFileName( $code );
63 if ( file_exists( $filename ) ) {
64 $contents = file_get_contents( $filename );
72 if ( strpos( $contents, '$messages' ) !== false ) {
73 $contents = explode( '$messages', $contents );
74 if ( $messagesText == '$messages' . $contents[1] ) {
75 echo "Generated messages for language $code. Same as the current file.\n";
79 $new .= $messagesText;
80 file_put_contents( $filename, $new );
81 echo "Generated and wrote messages for language $code.\n";
83 echo "Generated messages for language $code.\n" .
84 "Please run the script again (without the parameter \"dry-run\") " .
85 "to write the array to the file.\n";
88 if ( $listUnknown && isset( $sortedMessages['unknown'] ) &&
89 !empty( $sortedMessages['unknown'] )
91 if ( $removeUnknown ) {
92 echo "\nThe following " . count( $sortedMessages['unknown'] ) .
93 " unknown messages have been removed:\n";
95 echo "\nThere are " . count( $sortedMessages['unknown'] ) .
96 " unknown messages, please check them:\n";
98 foreach ( $sortedMessages['unknown'] as $key => $value ) {
99 echo "* " . $key . "\n";
103 echo "Generated messages for language $code. There seem to be no messages array in the file.\n";
108 * Write a messages array as a PHP text.
110 * @param array $messages The messages array.
111 * @param bool $ignoredComments Show comments about ignored and optional
112 * messages? (For English.)
113 * @param string $prefix Base path for messages.inc and messageTypes.inc files
114 * or false for default path (this directory)
115 * @param bool $removeUnknown Whether to remove unkown messages
117 * @return array Array of the PHP text and the sorted messages array.
119 public static function writeMessagesArray( $messages, $ignoredComments = false,
120 $prefix = false, $removeUnknown = false
123 $dir = $prefix ? $prefix : __DIR__;
125 require $dir . '/messages.inc';
126 self::$messageStructure = $wgMessageStructure;
127 self::$blockComments = $wgBlockComments;
129 require $dir . '/messageTypes.inc';
130 self::$ignoredMessages = $wgIgnoredMessages;
131 self::$optionalMessages = $wgOptionalMessages;
133 # Sort messages to blocks
134 $sortedMessages['unknown'] = $messages;
135 foreach ( self::$messageStructure as $blockName => $block ) {
139 foreach ( $block as $key ) {
140 if ( array_key_exists( $key, $sortedMessages['unknown'] ) ) {
141 $sortedMessages[$blockName][$key] = $sortedMessages['unknown'][$key];
142 unset( $sortedMessages['unknown'][$key] );
147 # Write all the messages
148 $messagesText = "\$messages = array(
150 foreach ( $sortedMessages as $block => $messages ) {
151 # Skip if it's the block of unknown messages - handle that in the end of file
152 if ( $block == 'unknown' ) {
156 if ( $ignoredComments ) {
157 $ignored = self::$ignoredMessages;
158 $optional = self::$optionalMessages;
163 $comments = self::makeComments( array_keys( $messages ), $ignored, $optional );
166 $messagesText .= self::writeMessagesBlock( self::$blockComments[$block], $messages, $comments );
169 # Write the unknown messages, alphabetically sorted.
170 # Of course, we don't have any comments for them, because they are unknown.
171 if ( !$removeUnknown ) {
172 ksort( $sortedMessages['unknown'] );
173 $messagesText .= self::writeMessagesBlock( 'Unknown messages', $sortedMessages['unknown'] );
178 return array( $messagesText, $sortedMessages );
182 * Generates an array of comments for messages.
184 * @param array $messages Key of messages.
185 * @param array $ignored List of ingored message keys.
186 * @param array $optional List of optional message keys.
189 public static function makeComments( $messages, $ignored, $optional ) {
191 $commentArray = array();
194 foreach ( $messages as $key ) {
195 if ( in_array( $key, $ignored ) ) {
196 $commentArray[$key] = ' # ' . self::$ignoredComment;
197 } elseif ( in_array( $key, $optional ) ) {
198 $commentArray[$key] = ' # ' . self::$optionalComment;
202 return $commentArray;
206 * Write a block of messages to PHP.
208 * @param string $blockComment The comment of whole block.
209 * @param array $messages The block messages.
210 * @param array $messageComments Optional comments for messages in this block.
211 * @param string $prefix Prefix for every line, for indenting purposes.
213 * @return string The block, formatted in PHP.
215 public static function writeMessagesBlock( $blockComment, $messages,
216 $messageComments = array(), $prefix = '' ) {
220 # Skip the block if it includes no messages
221 if ( empty( $messages ) ) {
225 # Format the block comment (if exists); check for multiple lines comments
226 if ( !empty( $blockComment ) ) {
227 if ( strpos( $blockComment, "\n" ) === false ) {
228 $blockText .= "$prefix# $blockComment
231 $blockText .= "$prefix/*
239 $maxKeyLength = max( array_map( 'strlen', array_keys( $messages ) ) );
241 # Format the messages
242 foreach ( $messages as $key => $value ) {
244 $blockText .= "$prefix'$key'";
246 # Add the appropriate block whitespace
247 $blockText .= str_repeat( ' ', $maxKeyLength - strlen( $key ) );
250 $blockText .= ' => ';
252 # Check for the appropriate apostrophe and add the value
253 # Quote \ here, because it needs always escaping
254 $value = addcslashes( $value, '\\' );
260 if ( strpos( $value, $single ) === false ) {
261 # Nothing ugly, just use '
262 $blockText .= $single . $value . $single;
263 } elseif ( strpos( $value, $double ) === false &&
264 !preg_match( '/\$[a-zA-Z_\x7f-\xff]/', $value )
266 # No "-quotes, no variables that need quoting, use "
267 $blockText .= $double . $value . $double;
269 # Something needs quoting, pick the quote which causes less quoting
271 if ( substr_count( $value, $double ) + substr_count( $value, '$' ) >=
272 substr_count( $value, $single )
279 if ( $quote === $double ) {
284 $blockText .= $quote . addcslashes( $value, $quote . $extra ) . $quote;
290 # Add comments, if there is any
291 if ( array_key_exists( $key, $messageComments ) ) {
292 $blockText .= $messageComments[$key];
300 # Newline to end the block