3 * Write a messages array as a PHP text.
4 * TODO: make it possible to use Windows endlines
5 * TODO: remove PHP ending tags recently removed from MediaWiki files
7 * @addtogroup Maintenance
10 require_once( 'messages.inc' );
11 require_once( 'messageTypes.inc' );
14 * Write a messages array as a PHP text and write it to the messages file.
16 * @param $messages The messages array.
17 * @param $code The language code.
18 * @param $write Write to the messages file?
19 * @param $listUnknown List the unknown messages?
21 function writeMessagesToFile( $messages, $code, $write, $listUnknown ) {
22 # Rewrite the messages array
23 $messages = writeMessagesArray( $messages, $code == 'en' );
24 $messagesText = $messages[0];
25 $sortedMessages = $messages[1];
28 $filename = Language::getMessagesFileName( $code );
29 $contents = file_get_contents( $filename );
30 if ( strpos( $contents, '$messages' ) !== false ) {
31 $contents = explode( '$messages', $contents );
32 if ( $messagesText == '$messages' . $contents[1] ) {
33 echo "Generated messages for language $code. Same as the current file.\n";
37 $new .= $messagesText;
38 file_put_contents( $filename, $new );
39 echo "Generated and wrote messages for language $code.\n";
41 echo "Generated messages for language $code. Please run the script again (without the parameter \"dry-run\") to write the array to the file.\n";
44 if ( $listUnknown && isset( $sortedMessages['unknown'] ) && !empty( $sortedMessages['unknown'] ) ) {
45 echo "\nThere are " . count( $sortedMessages['unknown'] ) . " unknown messages, please check them:\n";
46 foreach ( $sortedMessages['unknown'] as $key => $value ) {
47 echo "* " . $key . "\n";
51 echo "Generated messages for language $code. There seems to be no messages array in the file.\n";
56 * Write a messages array as a PHP text.
58 * @param $messages The messages array.
59 * @param $ignoredComments Show comments about ignored and optional messages? (For English.)
61 * @return Array of the PHP text and the sorted messages array.
63 function writeMessagesArray( $messages, $ignoredComments = false ) {
64 global $wgMessageStructure, $wgBlockComments;
66 # Sort messages to blocks
67 $sortedMessages['unknown'] = $messages;
68 foreach ( $wgMessageStructure as $blockName => $block ) {
69 foreach ( $block as $key ) {
70 if ( array_key_exists( $key, $sortedMessages['unknown'] ) ) {
71 $sortedMessages[$blockName][$key] = $sortedMessages['unknown'][$key];
72 unset( $sortedMessages['unknown'][$key] );
77 # Write all the messages
78 $messagesText = "\$messages = array(\n";
79 foreach( $sortedMessages as $block => $messages ) {
80 # Skip if it's the block of unknown messages - handle that in the end of file
81 if ( $block == 'unknown' ) {
86 $messagesText .= writeMessagesBlock( $block, $wgBlockComments[$block], $messages, $ignoredComments );
88 ksort( $sortedMessages['unknown'] );
89 $messagesText .= writeMessagesBlock( 'unknown', 'Unknown messages', $sortedMessages['unknown'], $ignoredComments ); # Write the unknown messages, alphabetically sorted
90 $messagesText .= ");\n";
92 return array( $messagesText, $sortedMessages );
96 * Write a block of messages to PHP.
98 * @param $name The block name.
99 * @param $comment The block comment.
100 * @param $messages The block messages.
101 * @param $ignoredComments Show comments about ignored and optional messages? (For English.)
103 * @return The block, formatted in PHP.
105 function writeMessagesBlock( $name, $comment, $messages, $ignoredComments ) {
106 global $wgMessageComments, $wgMessagseWithDollarSigns;
107 global $wgIgnoredMessages, $wgOptionalMessages;
110 # Skip the block if it includes no messages
111 if ( empty( $messages ) ) {
115 # Format the block comment (if exists); check for multiple lines comments
116 if ( !empty( $comment ) ) {
117 if ( strpos( $comment, "\n" ) === false ) {
118 $blockText .= "# $comment\n";
120 $blockText .= "/*\n$comment\n*/\n";
125 $maxKeyLength = max( array_map( 'strlen', array_keys( $messages ) ) );
127 # Format the messages
128 foreach( $messages as $key => $value ) {
130 $blockText .= "'$key'";
132 # Add the appropriate block whitespace
133 $blockText .= str_repeat( ' ', $maxKeyLength - strlen( $key ) );
136 $blockText .= ' => ';
138 # Check for the appropriate apostrophe and add the value
139 if ( strpos( $value, "'" ) === false ) {
140 $blockText .= "'$value'";
141 } elseif ( strpos( $value, '"' ) === false && !in_array( $key, $wgMessagseWithDollarSigns ) ) {
142 $blockText .= "\"$value\"";
144 # Pick the less numerous one to escape
145 $quote = substr_count( $value, '"' ) + substr_count( $value, '$' ) >= substr_count( $value, "'" ) ? "'" : '"';
146 if ('"' == $quote) { $extra = '$'; }
147 else { $extra = ''; }
148 $blockText .= $quote . addcslashes( $value, $quote.'\\'.$extra ) . $quote;
154 $ignoredComment = "don't translate or duplicate this message to other languages";
155 $optionalComment = "only translate this message to other languages if you have to change it";
156 $showIgnoredOrOptionalComment = in_array( $key, $wgIgnoredMessages ) || in_array( $key, $wgOptionalMessages );
157 if ( $ignoredComments ) {
158 if ( array_key_exists( $key, $wgMessageComments ) ) {
159 $blockText .= ' # ' . $wgMessageComments[$key];
160 if ( $showIgnoredOrOptionalComment ) {
163 } elseif ( $showIgnoredOrOptionalComment ) {
166 if ( in_array( $key, $wgIgnoredMessages ) ) {
167 $blockText .= $ignoredComment;
168 } elseif ( in_array( $key, $wgOptionalMessages ) ) {
169 $blockText .= $optionalComment;
171 } elseif ( array_key_exists( $key, $wgMessageComments ) ) {
172 $blockText .= ' # ' . $wgMessageComments[$key];
179 # Newline to end the block