* (bug 9993) Force $wgShowExceptionDetails on during installation
[mediawiki.git] / maintenance / language / writeMessagesArray.inc
blob01fc77621a6252cc69663b75ff773787496c1407
1 <?php
2 /**
3  * Write a messages array as a PHP text.
4  *
5  * @addtogroup Maintenance
6  */
8 require_once( 'messages.inc' );
9 require_once( 'messageTypes.inc' );
11 /**
12  * Write a messages array as a PHP text and write it to the messages file.
13  *
14  * @param $messages The messages array.
15  * @param $code The language code.
16  * @param $write Write to the messages file?
17  * @param $listUnknown List the unknown messages?
18  */
19 function writeMessagesToFile( $messages, $code, $write, $listUnknown ) {
20         # Rewrite the messages array
21         $messages = writeMessagesArray( $messages, $code == 'en' );
22         $messagesText = $messages[0];
23         $sortedMessages = $messages[1];
25         # Write to the file
26         $filename = Language::getMessagesFileName( $code );
27         $contents = file_get_contents( $filename );
28         if ( strpos( $contents, '$messages' ) !== false ) {
29                 $contents = explode( '$messages', $contents );
30                 if ( $messagesText . "\n?>\n" == '$messages' . $contents[1] ) {
31                         echo "Generated messages for language $code. Same to the current file.\n";
32                 } else {
33                         if ( $write ) {
34                                 $new = $contents[0];
35                                 $new .= $messagesText;
36                                 $new .= "\n?>\n";
37                                 file_put_contents( $filename, $new );
38                                 echo "Generated and wrote messages for language $code.\n";
39                         } else {
40                                 echo "Generated messages for language $code. Please run the script again (without the parameter \"dry-run\") to write the array to the file.\n";
41                         }
42                 }
43                 if ( $listUnknown && isset( $sortedMessages['unknown'] ) && !empty( $sortedMessages['unknown'] ) ) {
44                         echo "\nThere are " . count( $sortedMessages['unknown'] ) . " unknown messages, please check them:\n";
45                         foreach ( $sortedMessages['unknown'] as $key => $value ) {
46                                 echo "* " . $key . "\n";
47                         }
48                 }
49         } else {
50                 echo "Generated messages for language $code. There seems to be no messages array in the file.\n";
51         }
54 /**
55  * Write a messages array as a PHP text.
56  *
57  * @param $messages The messages array.
58  * @param $ignoredComments Show comments about ignored and optional messages? (For English.)
59  *
60  * @return Array of the PHP text and the sorted messages array.
61  */
62 function writeMessagesArray( $messages, $ignoredComments = false ) {
63         global $wgMessageStructure, $wgBlockComments;
65         # Sort messages to blocks
66         $sortedMessages['unknown'] = $messages;
67         foreach ( $wgMessageStructure as $blockName => $block ) {
68                 foreach ( $block as $key ) {
69                         if ( array_key_exists( $key, $sortedMessages['unknown'] ) ) {
70                                 $sortedMessages[$blockName][$key] = $sortedMessages['unknown'][$key];
71                                 unset( $sortedMessages['unknown'][$key] );
72                         }
73                 }
74         }
76         # Write all the messages
77         $messagesText = "\$messages = array(\n";
78         foreach( $sortedMessages as $block => $messages ) {
79                 # Skip if it's the block of unknown messages - handle that in the end of file
80                 if ( $block == 'unknown' ) {
81                         continue;
82                 }
84                 # Write the block
85                 $messagesText .= writeMessagesBlock( $block, $wgBlockComments[$block], $messages, $ignoredComments );
86         }
87         ksort( $sortedMessages['unknown'] );
88         $messagesText .= writeMessagesBlock( 'unknown', 'Unknown messages', $sortedMessages['unknown'], $ignoredComments ); # Write the unknown messages, alphabetically sorted
89         $messagesText .= ");\n";
91         return array( $messagesText, $sortedMessages );
94 /**
95  * Write a block of messages to PHP.
96  *
97  * @param $name The block name.
98  * @param $comment The block comment.
99  * @param $messages The block messages.
100  * @param $ignoredComments Show comments about ignored and optional messages? (For English.)
102  * @return The block, formatted in PHP.
103  */
104 function writeMessagesBlock( $name, $comment, $messages, $ignoredComments ) {
105         global $wgMessageComments, $wgMessagseWithDollarSigns;
106         global $wgIgnoredMessages, $wgOptionalMessages;
107         $blockText = '';
109         # Skip the block if it includes no messages
110         if ( empty( $messages ) ) {
111                 return '';
112         }
114         # Format the block comment (if exists); check for multiple lines comments
115         if ( !empty( $comment ) ) {
116                 if ( strpos( $comment, "\n" ) === false ) {
117                         $blockText .= "# $comment\n";
118                 } else {
119                         $blockText .= "/*\n$comment\n*/\n";
120                 }
121         }
123         # Get max key length
124         $maxKeyLength = max( array_map( 'strlen', array_keys( $messages ) ) );
126         # Format the messages
127         foreach( $messages as $key => $value ) {
128                 # Add the key name
129                 $blockText .= "'$key'";
131                 # Add the appropriate block whitespace
132                 $blockText .= str_repeat( ' ', $maxKeyLength - strlen( $key ) );
134                 # Refer to the value
135                 $blockText .= ' => ';
137                 # Check for the appropriate apostrophe and add the value
138                 if ( strpos( $value, "'" ) === false ) {
139                         $blockText .= "'$value'";
140                 } elseif ( strpos( $value, '"' ) === false && !in_array( $key, $wgMessagseWithDollarSigns ) ) {
141                         $blockText .= "\"$value\"";
142                 } else {
143                         # Pick the less numerous one to escape
144                         $quote = substr_count( $value, '"' ) + substr_count( $value, '$' ) >= substr_count( $value, "'" ) ? "'" : '"';
145                         if ('"' == $quote) { $extra = '$'; }
146                         else { $extra = ''; }
147                         $blockText .= $quote . addcslashes( $value, $quote.'\\'.$extra ) . $quote;
148                 }
150                 # Comma
151                 $blockText .= ',';
153                 $ignoredComment = "don't translate or duplicate this message to other languages";
154                 $optionalComment = "only translate this message to other languages if you have to change it";
155                 $showIgnoredOrOptionalComment = in_array( $key, $wgIgnoredMessages ) || in_array( $key, $wgOptionalMessages );
156                 if ( $ignoredComments ) {
157                         if ( array_key_exists( $key, $wgMessageComments ) ) {
158                                 $blockText .= ' # ' . $wgMessageComments[$key];
159                                 if ( $showIgnoredOrOptionalComment ) {
160                                         $blockText .= '; ';
161                                 }
162                         } elseif ( $showIgnoredOrOptionalComment ) {
163                                 $blockText .= ' # ';
164                         }
165                         if ( in_array( $key, $wgIgnoredMessages ) ) {
166                                 $blockText .= $ignoredComment;
167                         } elseif ( in_array( $key, $wgOptionalMessages ) ) {
168                                 $blockText .= $optionalComment;
169                         }
170                 } elseif ( array_key_exists( $key, $wgMessageComments ) ) {
171                         $blockText .= ' # ' . $wgMessageComments[$key];
172                 }
174                 # Newline
175                 $blockText .= "\n";
176         }
178         # Newline to end the block
179         $blockText .= "\n";
181         return $blockText;