Fix for r36587:
[lhc/web/wiklou.git] / maintenance / language / writeMessagesArray.inc
1 <?php
2 /**
3 * Write a messages array as a PHP text.
4 *
5 * @file
6 * @ingroup MaintenanceLanguage
7 */
8
9 /**
10 * @ingroup MaintenanceLanguage
11 */
12 class MessageWriter {
13 static $optionalComment = 'only translate this message to other languages if you have to change it';
14 static $ignoredComment = "do not translate or duplicate this message to other languages";
15
16 static $messageStructure;
17 static $blockComments;
18 static $messageComments;
19 static $ignoredMessages;
20 static $optionalMessages;
21
22 /**
23 * Write a messages array as a PHP text and write it to the messages file.
24 *
25 * @param $messages The messages array.
26 * @param $code The language code.
27 * @param $write Write to the messages file?
28 * @param $listUnknown List the unknown messages?
29 */
30 public static function writeMessagesToFile( $messages, $code, $write, $listUnknown ) {
31 # Rewrite the messages array
32 $messages = self::writeMessagesArray( $messages, $code == 'en' );
33 $messagesText = $messages[0];
34 $sortedMessages = $messages[1];
35
36 # Write to the file
37 $filename = Language::getMessagesFileName( $code );
38 $contents = file_get_contents( $filename );
39 if( strpos( $contents, '$messages' ) !== false ) {
40 $contents = explode( '$messages', $contents );
41 if( $messagesText == '$messages' . $contents[1] ) {
42 echo "Generated messages for language $code. Same as the current file.\n";
43 } else {
44 if( $write ) {
45 $new = $contents[0];
46 $new .= $messagesText;
47 file_put_contents( $filename, $new );
48 echo "Generated and wrote messages for language $code.\n";
49 } else {
50 echo "Generated messages for language $code. Please run the script again (without the parameter \"dry-run\") to write the array to the file.\n";
51 }
52 }
53 if( $listUnknown && isset( $sortedMessages['unknown'] ) && !empty( $sortedMessages['unknown'] ) ) {
54 echo "\nThere are " . count( $sortedMessages['unknown'] ) . " unknown messages, please check them:\n";
55 foreach( $sortedMessages['unknown'] as $key => $value ) {
56 echo "* " . $key . "\n";
57 }
58 }
59 } else {
60 echo "Generated messages for language $code. There seem to be no messages array in the file.\n";
61 }
62 }
63
64 /**
65 * Write a messages array as a PHP text.
66 *
67 * @param $messages The messages array.
68 * @param $ignoredComments Show comments about ignored and optional messages? (For English.)
69 *
70 * @return Array of the PHP text and the sorted messages array.
71 */
72 public static function writeMessagesArray( $messages, $ignoredComments = false, $prefix = false ) {
73 # Load messages
74 $dir = $prefix ? $prefix : dirname( __FILE__ );
75
76 require( $dir . '/messages.inc' );
77 self::$messageStructure = $wgMessageStructure;
78 self::$blockComments = $wgBlockComments;
79 self::$messageComments = $wgMessageComments;
80
81 require( $dir . '/messageTypes.inc' );
82 self::$ignoredMessages = $wgIgnoredMessages;
83 self::$optionalMessages = $wgOptionalMessages;
84
85
86 # Sort messages to blocks
87 $sortedMessages['unknown'] = $messages;
88 foreach( self::$messageStructure as $blockName => $block ) {
89 foreach( $block as $key ) {
90 if( array_key_exists( $key, $sortedMessages['unknown'] ) ) {
91 $sortedMessages[$blockName][$key] = $sortedMessages['unknown'][$key];
92 unset( $sortedMessages['unknown'][$key] );
93 }
94 }
95 }
96
97 # Write all the messages
98 $messagesText = "\$messages = array(
99 ";
100 foreach( $sortedMessages as $block => $messages ) {
101 # Skip if it's the block of unknown messages - handle that in the end of file
102 if( $block == 'unknown' ) {
103 continue;
104 }
105
106 if( $ignoredComments ) {
107 $ignored = self::$ignoredMessages;
108 $optional = self::$optionalMessages;
109 } else {
110 $ignored = array();
111 $optional = array();
112 }
113 $comments = self::makeComments( array_keys($messages), self::$messageComments, $ignored, $optional );
114
115 # Write the block
116 $messagesText .= self::writeMessagesBlock( self::$blockComments[$block], $messages, $comments );
117 }
118
119 # Write the unknown messages, alphabetically sorted.
120 # Of course, we don't have any comments for them, because the are unknown.
121 ksort( $sortedMessages['unknown'] );
122 $messagesText .= self::writeMessagesBlock( 'Unknown messages', $sortedMessages['unknown'] );
123 $messagesText .= ");
124 ";
125
126 return array( $messagesText, $sortedMessages );
127 }
128
129 /**
130 * Generates an array of comments for messages.
131 *
132 * @param $messages Key of messages.
133 * @param $comments Comments for messages, indexed by key.
134 * @param $ignored List of ingored message keys.
135 * @param $optional List of optional message keys.
136 */
137 public static function makeComments( $messages, $comments, $ignored, $optional ) {
138 # Comment collector
139 $commentArray = array();
140
141 # List of keys only
142 foreach( $messages as $key ) {
143 $commentsForKey = array();
144
145 # Add descriptive comment for this message if there is one
146 if( array_key_exists( $key, $comments ) ) {
147 $commentsForKey[] = $comments[$key];
148 }
149
150 # For translator information only
151 if( in_array( $key, $ignored ) ) {
152 $commentsForKey[] = self::$ignoredComment;
153 } elseif( in_array( $key, $optional ) ) {
154 $commentsForKey[] = self::$optionalComment;
155 }
156
157 # Format one or more comments nicely and store in array
158 if( count( $commentsForKey ) ) {
159 $commentArray[$key] = ' # ' . implode( '; ', $commentsForKey );
160 }
161 }
162
163 return $commentArray;
164 }
165
166 /**
167 * Write a block of messages to PHP.
168 *
169 * @param $blockComment The comment of whole block.
170 * @param $messages The block messages.
171 * @param $messageComments Optional comments for messages in this block.
172 * @param $prefix Prefix for every line, for indenting purposes.
173 *
174 * @return The block, formatted in PHP.
175 */
176 public static function writeMessagesBlock( $blockComment, $messages,
177 $messageComments = array(), $prefix = '' ) {
178
179 $blockText = '';
180
181 # Skip the block if it includes no messages
182 if( empty( $messages ) ) {
183 return '';
184 }
185
186 # Format the block comment (if exists); check for multiple lines comments
187 if( !empty( $blockComment ) ) {
188 if( strpos( $blockComment, "\n" ) === false ) {
189 $blockText .= "$prefix# $blockComment
190 ";
191 } else {
192 $blockText .= "$prefix/*
193 $blockComment
194 */
195 ";
196 }
197 }
198
199 # Get max key length
200 $maxKeyLength = max( array_map( 'strlen', array_keys( $messages ) ) );
201
202 # Format the messages
203 foreach( $messages as $key => $value ) {
204 # Add the key name
205 $blockText .= "$prefix'$key'";
206
207 # Add the appropriate block whitespace
208 $blockText .= str_repeat( ' ', $maxKeyLength - strlen( $key ) );
209
210 # Refer to the value
211 $blockText .= ' => ';
212
213 # Check for the appropriate apostrophe and add the value
214 # Quote \ here, because it needs always escaping
215 $value = addcslashes( $value, '\\' );
216
217 # For readability
218 $single = "'";
219 $double = '"';
220
221 if( strpos( $value, $single ) === false ) {
222 # Nothing ugly, just use '
223 $blockText .= $single.$value.$single;
224 } elseif( strpos( $value, $double ) === false && !preg_match('/\$[a-zA-Z_\x7f-\xff]/', $value) ) {
225 # No "-quotes, no variables that need quoting, use "
226 $blockText .= $double.$value.$double;
227 } else {
228 # Something needs quoting, pick the quote which causes less quoting
229 $quote = substr_count( $value, $double ) + substr_count( $value, '$' ) >= substr_count( $value, $single ) ? $single : $double;
230 if( $quote === $double ) {
231 $extra = '$';
232 } else {
233 $extra = '';
234 }
235 $blockText .= $quote . addcslashes( $value, $quote . $extra ) . $quote;
236 }
237
238 # Comma
239 $blockText .= ',';
240
241 # Add comments, if there is any
242 if( array_key_exists( $key, $messageComments ) ) {
243 $blockText .= $messageComments[$key];
244 }
245
246 # Newline
247 $blockText .= "
248 ";
249 }
250
251 # Newline to end the block
252 $blockText .= "
253 ";
254
255 return $blockText;
256 }
257 }