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