]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - maintenance/language/writeMessagesArray.inc
MediaWiki 1.11.0
[autoinstallsdev/mediawiki.git] / maintenance / language / writeMessagesArray.inc
1 <?php
2 /**
3  * Write a messages array as a PHP text.
4  *
5  * @addtogroup Maintenance
6  */
7
8 require_once( 'messages.inc' );
9 require_once( 'messageTypes.inc' );
10
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];
24
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 == '$messages' . $contents[1] ) {
31                         echo "Generated messages for language $code. Same as the current file.\n";
32                 } else {
33                         if ( $write ) {
34                                 $new = $contents[0];
35                                 $new .= $messagesText;
36                                 file_put_contents( $filename, $new );
37                                 echo "Generated and wrote messages for language $code.\n";
38                         } else {
39                                 echo "Generated messages for language $code. Please run the script again (without the parameter \"dry-run\") to write the array to the file.\n";
40                         }
41                 }
42                 if ( $listUnknown && isset( $sortedMessages['unknown'] ) && !empty( $sortedMessages['unknown'] ) ) {
43                         echo "\nThere are " . count( $sortedMessages['unknown'] ) . " unknown messages, please check them:\n";
44                         foreach ( $sortedMessages['unknown'] as $key => $value ) {
45                                 echo "* " . $key . "\n";
46                         }
47                 }
48         } else {
49                 echo "Generated messages for language $code. There seems to be no messages array in the file.\n";
50         }
51 }
52
53 /**
54  * Write a messages array as a PHP text.
55  *
56  * @param $messages The messages array.
57  * @param $ignoredComments Show comments about ignored and optional messages? (For English.)
58  *
59  * @return Array of the PHP text and the sorted messages array.
60  */
61 function writeMessagesArray( $messages, $ignoredComments = false ) {
62         global $wgMessageStructure, $wgBlockComments;
63
64         # Sort messages to blocks
65         $sortedMessages['unknown'] = $messages;
66         foreach ( $wgMessageStructure as $blockName => $block ) {
67                 foreach ( $block as $key ) {
68                         if ( array_key_exists( $key, $sortedMessages['unknown'] ) ) {
69                                 $sortedMessages[$blockName][$key] = $sortedMessages['unknown'][$key];
70                                 unset( $sortedMessages['unknown'][$key] );
71                         }
72                 }
73         }
74
75         # Write all the messages
76         $messagesText = "\$messages = array(
77 ";
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                 }
83
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 .= ");
90 ";
91
92         return array( $messagesText, $sortedMessages );
93 }
94
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.)
102  *
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 = '';
109
110         # Skip the block if it includes no messages
111         if ( empty( $messages ) ) {
112                 return '';
113         }
114
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
119 ";
120                 } else {
121                         $blockText .= "/*
122 $comment
123 */
124 ";
125                 }
126         }
127
128         # Get max key length
129         $maxKeyLength = max( array_map( 'strlen', array_keys( $messages ) ) );
130
131         # Format the messages
132         foreach( $messages as $key => $value ) {
133                 # Add the key name
134                 $blockText .= "'$key'";
135
136                 # Add the appropriate block whitespace
137                 $blockText .= str_repeat( ' ', $maxKeyLength - strlen( $key ) );
138
139                 # Refer to the value
140                 $blockText .= ' => ';
141
142                 # Check for the appropriate apostrophe and add the value
143                 if ( strpos( $value, "'" ) === false ) {
144                         $blockText .= "'$value'";
145                 } elseif ( strpos( $value, '"' ) === false && !in_array( $key, $wgMessagseWithDollarSigns ) ) {
146                         $blockText .= "\"$value\"";
147                 } else {
148                         # Pick the less numerous one to escape
149                         $quote = substr_count( $value, '"' ) + substr_count( $value, '$' ) >= substr_count( $value, "'" ) ? "'" : '"';
150                         if ('"' == $quote) { $extra = '$'; }
151                         else { $extra = ''; }
152                         $blockText .= $quote . addcslashes( $value, $quote.'\\'.$extra ) . $quote;
153                 }
154
155                 # Comma
156                 $blockText .= ',';
157
158                 $ignoredComment = "don't translate or duplicate this message to other languages";
159                 $optionalComment = "only translate this message to other languages if you have to change it";
160                 $showIgnoredOrOptionalComment = in_array( $key, $wgIgnoredMessages ) || in_array( $key, $wgOptionalMessages );
161                 if ( $ignoredComments ) {
162                         if ( array_key_exists( $key, $wgMessageComments ) ) {
163                                 $blockText .= ' # ' . $wgMessageComments[$key];
164                                 if ( $showIgnoredOrOptionalComment ) {
165                                         $blockText .= '; ';
166                                 }
167                         } elseif ( $showIgnoredOrOptionalComment ) {
168                                 $blockText .= ' # ';
169                         }
170                         if ( in_array( $key, $wgIgnoredMessages ) ) {
171                                 $blockText .= $ignoredComment;
172                         } elseif ( in_array( $key, $wgOptionalMessages ) ) {
173                                 $blockText .= $optionalComment;
174                         }
175                 } elseif ( array_key_exists( $key, $wgMessageComments ) ) {
176                         $blockText .= ' # ' . $wgMessageComments[$key];
177                 }
178
179                 # Newline
180                 $blockText .= "
181 ";
182         }
183
184         # Newline to end the block
185         $blockText .= "
186 ";
187
188         return $blockText;
189 }
190
191 ?>