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