* Removing redundant watchlist edit/clear links
[mediawiki.git] / maintenance / language / writeMessagesArray.inc
blobf9340bef7d176acf232fb27ae21b563d30bd84e3
1 <?php
2 /**
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
6  *
7  * @addtogroup Maintenance
8  */
10 require_once( 'messages.inc' );
11 require_once( 'messageTypes.inc' );
13 /**
14  * Write a messages array as a PHP text and write it to the messages file.
15  *
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?
20  */
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];
27         # Write to the file
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";
34                 } else {
35                         if ( $write ) {
36                                 $new = $contents[0];
37                                 $new .= $messagesText;
38                                 file_put_contents( $filename, $new );
39                                 echo "Generated and wrote messages for language $code.\n";
40                         } else {
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";
42                         }
43                 }
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";
48                         }
49                 }
50         } else {
51                 echo "Generated messages for language $code. There seems to be no messages array in the file.\n";
52         }
55 /**
56  * Write a messages array as a PHP text.
57  *
58  * @param $messages The messages array.
59  * @param $ignoredComments Show comments about ignored and optional messages? (For English.)
60  *
61  * @return Array of the PHP text and the sorted messages array.
62  */
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] );
73                         }
74                 }
75         }
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' ) {
82                         continue;
83                 }
85                 # Write the block
86                 $messagesText .= writeMessagesBlock( $block, $wgBlockComments[$block], $messages, $ignoredComments );
87         }
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 );
95 /**
96  * Write a block of messages to PHP.
97  *
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.
104  */
105 function writeMessagesBlock( $name, $comment, $messages, $ignoredComments ) {
106         global $wgMessageComments, $wgMessagseWithDollarSigns;
107         global $wgIgnoredMessages, $wgOptionalMessages;
108         $blockText = '';
110         # Skip the block if it includes no messages
111         if ( empty( $messages ) ) {
112                 return '';
113         }
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";
119                 } else {
120                         $blockText .= "/*\n$comment\n*/\n";
121                 }
122         }
124         # Get max key length
125         $maxKeyLength = max( array_map( 'strlen', array_keys( $messages ) ) );
127         # Format the messages
128         foreach( $messages as $key => $value ) {
129                 # Add the key name
130                 $blockText .= "'$key'";
132                 # Add the appropriate block whitespace
133                 $blockText .= str_repeat( ' ', $maxKeyLength - strlen( $key ) );
135                 # Refer to the value
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\"";
143                 } else {
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;
149                 }
151                 # Comma
152                 $blockText .= ',';
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 ) {
161                                         $blockText .= '; ';
162                                 }
163                         } elseif ( $showIgnoredOrOptionalComment ) {
164                                 $blockText .= ' # ';
165                         }
166                         if ( in_array( $key, $wgIgnoredMessages ) ) {
167                                 $blockText .= $ignoredComment;
168                         } elseif ( in_array( $key, $wgOptionalMessages ) ) {
169                                 $blockText .= $optionalComment;
170                         }
171                 } elseif ( array_key_exists( $key, $wgMessageComments ) ) {
172                         $blockText .= ' # ' . $wgMessageComments[$key];
173                 }
175                 # Newline
176                 $blockText .= "\n";
177         }
179         # Newline to end the block
180         $blockText .= "\n";
182         return $blockText;