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