Fixed some doxygen warnings
[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 $ignoredMessages;
19 static $optionalMessages;
20
21 /**
22 * Write a messages array as a PHP text and write it to the messages file.
23 *
24 * @param $messages The messages array.
25 * @param $code The language code.
26 * @param $write Write to the messages file?
27 * @param $listUnknown List the unknown messages?
28 */
29 public static function writeMessagesToFile( $messages, $code, $write, $listUnknown, $removeUnknown ) {
30 # Rewrite the messages array
31 $messages = self::writeMessagesArray( $messages, $code == 'en', false, $removeUnknown );
32 $messagesText = $messages[0];
33 $sortedMessages = $messages[1];
34
35 # Write to the file
36 $filename = Language::getMessagesFileName( $code );
37 $contents = file_get_contents( $filename );
38 if( strpos( $contents, '$messages' ) !== false ) {
39 $contents = explode( '$messages', $contents );
40 if( $messagesText == '$messages' . $contents[1] ) {
41 echo "Generated messages for language $code. Same as the current file.\n";
42 } else {
43 if( $write ) {
44 $new = $contents[0];
45 $new .= $messagesText;
46 file_put_contents( $filename, $new );
47 echo "Generated and wrote messages for language $code.\n";
48 } else {
49 echo "Generated messages for language $code. Please run the script again (without the parameter \"dry-run\") to write the array to the file.\n";
50 }
51 }
52 if( $listUnknown && isset( $sortedMessages['unknown'] ) && !empty( $sortedMessages['unknown'] ) ) {
53 if ( $removeUnknown )
54 echo "\nThe following " . count( $sortedMessages['unknown'] ) . " unknown messages have been removed:\n";
55 else
56 echo "\nThere are " . count( $sortedMessages['unknown'] ) . " unknown messages, please check them:\n";
57 foreach( $sortedMessages['unknown'] as $key => $value ) {
58 echo "* " . $key . "\n";
59 }
60 }
61 } else {
62 echo "Generated messages for language $code. There seem to be no messages array in the file.\n";
63 }
64 }
65
66 /**
67 * Write a messages array as a PHP text.
68 *
69 * @param $messages The messages array.
70 * @param $ignoredComments Show comments about ignored and optional messages? (For English.)
71 *
72 * @return Array of the PHP text and the sorted messages array.
73 */
74 public static function writeMessagesArray( $messages, $ignoredComments = false, $prefix = false, $removeUnknown = false ) {
75 # Load messages
76 $dir = $prefix ? $prefix : dirname( __FILE__ );
77
78 require( $dir . '/messages.inc' );
79 self::$messageStructure = $wgMessageStructure;
80 self::$blockComments = $wgBlockComments;
81
82 require( $dir . '/messageTypes.inc' );
83 self::$ignoredMessages = $wgIgnoredMessages;
84 self::$optionalMessages = $wgOptionalMessages;
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 ), $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 they are unknown.
121 if ( !$removeUnknown ) {
122 ksort( $sortedMessages['unknown'] );
123 $messagesText .= self::writeMessagesBlock( 'Unknown messages', $sortedMessages['unknown'] );
124 }
125 $messagesText .= ");
126 ";
127 return array( $messagesText, $sortedMessages );
128 }
129
130 /**
131 * Generates an array of comments for messages.
132 *
133 * @param $messages Key of messages.
134 * @param $ignored List of ingored message keys.
135 * @param $optional List of optional message keys.
136 */
137 public static function makeComments( $messages, $ignored, $optional ) {
138 # Comment collector
139 $commentArray = array();
140
141 # List of keys only
142 foreach( $messages as $key ) {
143 if( in_array( $key, $ignored ) ) {
144 $commentArray[$key] = ' # ' . self::$ignoredComment;
145 } elseif( in_array( $key, $optional ) ) {
146 $commentArray[$key] = ' # ' . self::$optionalComment;
147 }
148 }
149
150 return $commentArray;
151 }
152
153 /**
154 * Write a block of messages to PHP.
155 *
156 * @param $blockComment The comment of whole block.
157 * @param $messages The block messages.
158 * @param $messageComments Optional comments for messages in this block.
159 * @param $prefix Prefix for every line, for indenting purposes.
160 *
161 * @return The block, formatted in PHP.
162 */
163 public static function writeMessagesBlock( $blockComment, $messages,
164 $messageComments = array(), $prefix = '' ) {
165
166 $blockText = '';
167
168 # Skip the block if it includes no messages
169 if( empty( $messages ) ) {
170 return '';
171 }
172
173 # Format the block comment (if exists); check for multiple lines comments
174 if( !empty( $blockComment ) ) {
175 if( strpos( $blockComment, "\n" ) === false ) {
176 $blockText .= "$prefix# $blockComment
177 ";
178 } else {
179 $blockText .= "$prefix/*
180 $blockComment
181 */
182 ";
183 }
184 }
185
186 # Get max key length
187 $maxKeyLength = max( array_map( 'strlen', array_keys( $messages ) ) );
188
189 # Format the messages
190 foreach( $messages as $key => $value ) {
191 # Add the key name
192 $blockText .= "$prefix'$key'";
193
194 # Add the appropriate block whitespace
195 $blockText .= str_repeat( ' ', $maxKeyLength - strlen( $key ) );
196
197 # Refer to the value
198 $blockText .= ' => ';
199
200 # Check for the appropriate apostrophe and add the value
201 # Quote \ here, because it needs always escaping
202 $value = addcslashes( $value, '\\' );
203
204 # For readability
205 $single = "'";
206 $double = '"';
207
208 if( strpos( $value, $single ) === false ) {
209 # Nothing ugly, just use '
210 $blockText .= $single.$value.$single;
211 } elseif( strpos( $value, $double ) === false && !preg_match('/\$[a-zA-Z_\x7f-\xff]/', $value) ) {
212 # No "-quotes, no variables that need quoting, use "
213 $blockText .= $double.$value.$double;
214 } else {
215 # Something needs quoting, pick the quote which causes less quoting
216 $quote = substr_count( $value, $double ) + substr_count( $value, '$' ) >= substr_count( $value, $single ) ? $single : $double;
217 if( $quote === $double ) {
218 $extra = '$';
219 } else {
220 $extra = '';
221 }
222 $blockText .= $quote . addcslashes( $value, $quote . $extra ) . $quote;
223 }
224
225 # Comma
226 $blockText .= ',';
227
228 # Add comments, if there is any
229 if( array_key_exists( $key, $messageComments ) ) {
230 $blockText .= $messageComments[$key];
231 }
232
233 # Newline
234 $blockText .= "
235 ";
236 }
237
238 # Newline to end the block
239 $blockText .= "
240 ";
241
242 return $blockText;
243 }
244 }