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 . "\n?>\n" == '$messages' . $contents[1] ) {
33 echo "Generated messages for language $code. Same to the current file.\n";
37 $new .= $messagesText;
39 file_put_contents( $filename, $new );
40 echo "Generated and wrote messages for language $code.\n";
42 echo "Generated messages for language $code. Please run the script again (without the parameter \"dry-run\") to write the array to the file.\n";
45 if ( $listUnknown && isset( $sortedMessages['unknown'] ) && !empty( $sortedMessages['unknown'] ) ) {
46 echo "\nThere are " . count( $sortedMessages['unknown'] ) . " unknown messages, please check them:\n";
47 foreach ( $sortedMessages['unknown'] as $key => $value ) {
48 echo "* " . $key . "\n";
52 echo "Generated messages for language $code. There seems to be no messages array in the file.\n";
57 * Write a messages array as a PHP text.
59 * @param $messages The messages array.
60 * @param $ignoredComments Show comments about ignored and optional messages? (For English.)
62 * @return Array of the PHP text and the sorted messages array.
64 function writeMessagesArray( $messages, $ignoredComments = false ) {
65 global $wgMessageStructure, $wgBlockComments;
67 # Sort messages to blocks
68 $sortedMessages['unknown'] = $messages;
69 foreach ( $wgMessageStructure as $blockName => $block ) {
70 foreach ( $block as $key ) {
71 if ( array_key_exists( $key, $sortedMessages['unknown'] ) ) {
72 $sortedMessages[$blockName][$key] = $sortedMessages['unknown'][$key];
73 unset( $sortedMessages['unknown'][$key] );
78 # Write all the messages
79 $messagesText = "\$messages = array(\n";
80 foreach( $sortedMessages as $block => $messages ) {
81 # Skip if it's the block of unknown messages - handle that in the end of file
82 if ( $block == 'unknown' ) {
87 $messagesText .= writeMessagesBlock( $block, $wgBlockComments[$block], $messages, $ignoredComments );
89 ksort( $sortedMessages['unknown'] );
90 $messagesText .= writeMessagesBlock( 'unknown', 'Unknown messages', $sortedMessages['unknown'], $ignoredComments ); # Write the unknown messages, alphabetically sorted
91 $messagesText .= ");\n";
93 return array( $messagesText, $sortedMessages );
97 * Write a block of messages to PHP.
99 * @param $name The block name.
100 * @param $comment The block comment.
101 * @param $messages The block messages.
102 * @param $ignoredComments Show comments about ignored and optional messages? (For English.)
104 * @return The block, formatted in PHP.
106 function writeMessagesBlock( $name, $comment, $messages, $ignoredComments ) {
107 global $wgMessageComments, $wgMessagseWithDollarSigns;
108 global $wgIgnoredMessages, $wgOptionalMessages;
111 # Skip the block if it includes no messages
112 if ( empty( $messages ) ) {
116 # Format the block comment (if exists); check for multiple lines comments
117 if ( !empty( $comment ) ) {
118 if ( strpos( $comment, "\n" ) === false ) {
119 $blockText .= "# $comment\n";
121 $blockText .= "/*\n$comment\n*/\n";
126 $maxKeyLength = max( array_map( 'strlen', array_keys( $messages ) ) );
128 # Format the messages
129 foreach( $messages as $key => $value ) {
131 $blockText .= "'$key'";
133 # Add the appropriate block whitespace
134 $blockText .= str_repeat( ' ', $maxKeyLength - strlen( $key ) );
137 $blockText .= ' => ';
139 # Check for the appropriate apostrophe and add the value
140 if ( strpos( $value, "'" ) === false ) {
141 $blockText .= "'$value'";
142 } elseif ( strpos( $value, '"' ) === false && !in_array( $key, $wgMessagseWithDollarSigns ) ) {
143 $blockText .= "\"$value\"";
145 # Pick the less numerous one to escape
146 $quote = substr_count( $value, '"' ) + substr_count( $value, '$' ) >= substr_count( $value, "'" ) ? "'" : '"';
147 if ('"' == $quote) { $extra = '$'; }
148 else { $extra = ''; }
149 $blockText .= $quote . addcslashes( $value, $quote.'\\'.$extra ) . $quote;
155 $ignoredComment = "don't translate or duplicate this message to other languages";
156 $optionalComment = "only translate this message to other languages if you have to change it";
157 $showIgnoredOrOptionalComment = in_array( $key, $wgIgnoredMessages ) || in_array( $key, $wgOptionalMessages );
158 if ( $ignoredComments ) {
159 if ( array_key_exists( $key, $wgMessageComments ) ) {
160 $blockText .= ' # ' . $wgMessageComments[$key];
161 if ( $showIgnoredOrOptionalComment ) {
164 } elseif ( $showIgnoredOrOptionalComment ) {
167 if ( in_array( $key, $wgIgnoredMessages ) ) {
168 $blockText .= $ignoredComment;
169 } elseif ( in_array( $key, $wgOptionalMessages ) ) {
170 $blockText .= $optionalComment;
172 } elseif ( array_key_exists( $key, $wgMessageComments ) ) {
173 $blockText .= ' # ' . $wgMessageComments[$key];
180 # Newline to end the block