]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - maintenance/language/writeMessagesArray.inc
MediaWiki 1.14.0
[autoinstallsdev/mediawiki.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, $removeUnknown ) {
31                 # Rewrite the messages array
32                 $messages = self::writeMessagesArray( $messages, $code == 'en', false, $removeUnknown );
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                                 if ( $removeUnknown )
55                                         echo "\nThe following " . count( $sortedMessages['unknown'] ) . " unknown messages have been removed:\n";
56                                 else
57                                         echo "\nThere are " . count( $sortedMessages['unknown'] ) . " unknown messages, please check them:\n";
58                                 foreach( $sortedMessages['unknown'] as $key => $value ) {
59                                         echo "* " . $key . "\n";
60                                 }
61                         }
62                 } else {
63                         echo "Generated messages for language $code. There seem to be no messages array in the file.\n";
64                 }
65         }
66
67         /**
68          * Write a messages array as a PHP text.
69          *
70          * @param $messages The messages array.
71          * @param $ignoredComments Show comments about ignored and optional messages? (For English.)
72          *
73          * @return Array of the PHP text and the sorted messages array.
74          */
75         public static function writeMessagesArray( $messages, $ignoredComments = false, $prefix = false, $removeUnknown = false ) {
76                 # Load messages
77                 $dir = $prefix ? $prefix : dirname( __FILE__ );
78
79                 require( $dir . '/messages.inc' );
80                 self::$messageStructure = $wgMessageStructure;
81                 self::$blockComments = $wgBlockComments;
82                 self::$messageComments = $wgMessageComments;
83
84                 require( $dir . '/messageTypes.inc' );
85                 self::$ignoredMessages = $wgIgnoredMessages;
86                 self::$optionalMessages = $wgOptionalMessages;
87
88
89                 # Sort messages to blocks
90                 $sortedMessages['unknown'] = $messages;
91                 foreach( self::$messageStructure as $blockName => $block ) {
92                         foreach( $block as $key ) {
93                                 if( array_key_exists( $key, $sortedMessages['unknown'] ) ) {
94                                         $sortedMessages[$blockName][$key] = $sortedMessages['unknown'][$key];
95                                         unset( $sortedMessages['unknown'][$key] );
96                                 }
97                         }
98                 }
99
100                 # Write all the messages
101                 $messagesText = "\$messages = array(
102 ";
103                 foreach( $sortedMessages as $block => $messages ) {
104                         # Skip if it's the block of unknown messages - handle that in the end of file
105                         if( $block == 'unknown' ) {
106                                 continue;
107                         }
108
109                         if( $ignoredComments ) {
110                                 $ignored = self::$ignoredMessages;
111                                 $optional = self::$optionalMessages;
112                         } else {
113                                 $ignored = array();
114                                 $optional = array();
115                         }
116                         $comments = self::makeComments( array_keys($messages), self::$messageComments, $ignored, $optional );
117
118                         # Write the block
119                         $messagesText .= self::writeMessagesBlock( self::$blockComments[$block], $messages, $comments );
120                 }
121
122                 # Write the unknown messages, alphabetically sorted.
123                 # Of course, we don't have any comments for them, because they are unknown.
124                 if ( !$removeUnknown ) {
125                         ksort( $sortedMessages['unknown'] );
126                         $messagesText .= self::writeMessagesBlock( 'Unknown messages', $sortedMessages['unknown'] );
127                 }
128                 $messagesText .= ");
129 ";
130                 return array( $messagesText, $sortedMessages );
131         }
132
133         /**
134          * Generates an array of comments for messages.
135          *
136          * @param $messages Key of messages.
137          * @param $comments Comments for messages, indexed by key.
138          * @param $ignored List of ingored message keys.
139          * @param $optional List of optional message keys.
140          */
141         public static function makeComments( $messages, $comments, $ignored, $optional ) {
142                 # Comment collector
143                 $commentArray = array();
144
145                 # List of keys only
146                 foreach( $messages as $key ) {
147                         $commentsForKey = array();
148
149                         # Add descriptive comment for this message if there is one
150                         if( array_key_exists( $key, $comments ) ) {
151                                 $commentsForKey[] = $comments[$key];
152                         }
153
154                         # For translator information only
155                         if( in_array( $key, $ignored ) ) {
156                                 $commentsForKey[] = self::$ignoredComment;
157                         } elseif( in_array( $key, $optional ) ) {
158                                 $commentsForKey[] = self::$optionalComment;
159                         }
160
161                         # Format one or more comments nicely and store in array
162                         if( count( $commentsForKey ) ) {
163                                 $commentArray[$key] = ' # ' . implode( '; ', $commentsForKey );
164                         }
165                 }
166
167                 return $commentArray;
168         }
169
170         /**
171          * Write a block of messages to PHP.
172          *
173          * @param $blockComment The comment of whole block.
174          * @param $messages The block messages.
175          * @param $messageComments Optional comments for messages in this block.
176          * @param $prefix Prefix for every line, for indenting purposes.
177          *
178          * @return The block, formatted in PHP.
179          */
180         public static function writeMessagesBlock( $blockComment, $messages,
181                 $messageComments = array(), $prefix = '' ) {
182
183                 $blockText = '';
184
185                 # Skip the block if it includes no messages
186                 if( empty( $messages ) ) {
187                         return '';
188                 }
189
190                 # Format the block comment (if exists); check for multiple lines comments
191                 if( !empty( $blockComment ) ) {
192                         if( strpos( $blockComment, "\n" ) === false ) {
193                                 $blockText .= "$prefix# $blockComment
194 ";
195                         } else {
196                                 $blockText .= "$prefix/*
197 $blockComment
198 */
199 ";
200                         }
201                 }
202
203                 # Get max key length
204                 $maxKeyLength = max( array_map( 'strlen', array_keys( $messages ) ) );
205
206                 # Format the messages
207                 foreach( $messages as $key => $value ) {
208                         # Add the key name
209                         $blockText .= "$prefix'$key'";
210
211                         # Add the appropriate block whitespace
212                         $blockText .= str_repeat( ' ', $maxKeyLength - strlen( $key ) );
213
214                         # Refer to the value
215                         $blockText .= ' => ';
216
217                         # Check for the appropriate apostrophe and add the value
218                         # Quote \ here, because it needs always escaping
219                         $value = addcslashes( $value, '\\' );
220
221                         # For readability
222                         $single = "'";
223                         $double = '"';
224
225                         if( strpos( $value, $single ) === false ) {
226                                 # Nothing ugly, just use '
227                                 $blockText .= $single.$value.$single;
228                         } elseif( strpos( $value, $double ) === false && !preg_match('/\$[a-zA-Z_\x7f-\xff]/', $value) ) {
229                                 # No "-quotes, no variables that need quoting, use "
230                                 $blockText .= $double.$value.$double;
231                         } else {
232                                 # Something needs quoting, pick the quote which causes less quoting
233                                 $quote = substr_count( $value, $double ) + substr_count( $value, '$' ) >= substr_count( $value, $single ) ? $single : $double;
234                                 if( $quote === $double ) {
235                                         $extra = '$';
236                                 } else {
237                                         $extra = '';
238                                 }
239                                 $blockText .= $quote . addcslashes( $value, $quote . $extra ) . $quote;
240                         }
241
242                         # Comma
243                         $blockText .= ',';
244
245                         # Add comments, if there is any
246                         if( array_key_exists( $key, $messageComments ) ) {
247                                 $blockText .= $messageComments[$key];
248                         }
249
250                         # Newline
251                         $blockText .= "
252 ";
253                 }
254
255                 # Newline to end the block
256                 $blockText .= "
257 ";
258
259                 return $blockText;
260         }
261 }