let us force thumb rendering even if previous file exist
[mediawiki.git] / maintenance / language / writeMessagesArray.inc
blob524c2ba6fbd7385c5196e0d62aa7fcc53492cdc2
1 <?php
2 /**
3  * Write a messages array as a PHP text.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  * http://www.gnu.org/copyleft/gpl.html
19  *
20  * @file
21  * @ingroup MaintenanceLanguage
22  */
24 /**
25  * @ingroup MaintenanceLanguage
26  */
27 class MessageWriter {
28         static $optionalComment = 'only translate this message to other languages if you have to change it';
29         static $ignoredComment = "do not translate or duplicate this message to other languages";
31         static $messageStructure;
32         static $blockComments;
33         static $ignoredMessages;
34         static $optionalMessages;
36         /**
37          * Write a messages array as a PHP text and write it to the messages file.
38          *
39          * @param $messages Array: the messages array.
40          * @param $code String: the language code.
41          * @param $write Boolean: write to the messages file?
42          * @param $listUnknown Boolean: list the unknown messages?
43          * @param $removeUnknown Boolean: whether to remove unkown messages
44          */
45         public static function writeMessagesToFile( $messages, $code, $write, $listUnknown, $removeUnknown ) {
46                 # Rewrite the messages array
47                 $messages = self::writeMessagesArray( $messages, $code == 'en', false, $removeUnknown );
48                 $messagesText = $messages[0];
49                 $sortedMessages = $messages[1];
51                 # Write to the file
52                 $filename = Language::getMessagesFileName( $code );
53                 $contents = file_get_contents( $filename );
54                 if( strpos( $contents, '$messages' ) !== false ) {
55                         $contents = explode( '$messages', $contents );
56                         if( $messagesText == '$messages' . $contents[1] ) {
57                                 echo "Generated messages for language $code. Same as the current file.\n";
58                         } else {
59                                 if( $write ) {
60                                         $new = $contents[0];
61                                         $new .= $messagesText;
62                                         file_put_contents( $filename, $new );
63                                         echo "Generated and wrote messages for language $code.\n";
64                                 } else {
65                                         echo "Generated messages for language $code. Please run the script again (without the parameter \"dry-run\") to write the array to the file.\n";
66                                 }
67                         }
68                         if( $listUnknown && isset( $sortedMessages['unknown'] ) && !empty( $sortedMessages['unknown'] ) ) {
69                                 if ( $removeUnknown )
70                                         echo "\nThe following " . count( $sortedMessages['unknown'] ) . " unknown messages have been removed:\n";
71                                 else
72                                         echo "\nThere are " . count( $sortedMessages['unknown'] ) . " unknown messages, please check them:\n";
73                                 foreach( $sortedMessages['unknown'] as $key => $value ) {
74                                         echo "* " . $key . "\n";
75                                 }
76                         }
77                 } else {
78                         echo "Generated messages for language $code. There seem to be no messages array in the file.\n";
79                 }
80         }
82         /**
83          * Write a messages array as a PHP text.
84          *
85          * @param $messages Array: the messages array.
86          * @param $ignoredComments Boolean: show comments about ignored and optional
87          *                         messages? (For English.)
88          * @param $prefix String: base path for messages.inc and messageTypes.inc files
89          *                or false for default path (this directory)
90          * @param $removeUnknown Boolean: whether to remove unkown messages
91          *
92          * @return Array of the PHP text and the sorted messages array.
93          */
94         public static function writeMessagesArray( $messages, $ignoredComments = false, $prefix = false, $removeUnknown = false ) {
95                 # Load messages
96                 $dir = $prefix ? $prefix : dirname( __FILE__ );
98                 require( $dir . '/messages.inc' );
99                 self::$messageStructure = $wgMessageStructure;
100                 self::$blockComments = $wgBlockComments;
102                 require( $dir . '/messageTypes.inc' );
103                 self::$ignoredMessages = $wgIgnoredMessages;
104                 self::$optionalMessages = $wgOptionalMessages;
106                 # Sort messages to blocks
107                 $sortedMessages['unknown'] = $messages;
108                 foreach( self::$messageStructure as $blockName => $block ) {
109                         /**
110                          * @var $block array
111                          */
112                         foreach( $block as $key ) {
113                                 if( array_key_exists( $key, $sortedMessages['unknown'] ) ) {
114                                         $sortedMessages[$blockName][$key] = $sortedMessages['unknown'][$key];
115                                         unset( $sortedMessages['unknown'][$key] );
116                                 }
117                         }
118                 }
120                 # Write all the messages
121                 $messagesText = "\$messages = array(
123                 foreach( $sortedMessages as $block => $messages ) {
124                         # Skip if it's the block of unknown messages - handle that in the end of file
125                         if( $block == 'unknown' ) {
126                                 continue;
127                         }
129                         if( $ignoredComments ) {
130                                 $ignored = self::$ignoredMessages;
131                                 $optional = self::$optionalMessages;
132                         } else {
133                                 $ignored = array();
134                                 $optional = array();
135                         }
136                         $comments = self::makeComments( array_keys( $messages ), $ignored, $optional );
138                         # Write the block
139                         $messagesText .= self::writeMessagesBlock( self::$blockComments[$block], $messages, $comments );
140                 }
142                 # Write the unknown messages, alphabetically sorted.
143                 # Of course, we don't have any comments for them, because they are unknown.
144                 if ( !$removeUnknown ) {
145                         ksort( $sortedMessages['unknown'] );
146                         $messagesText .= self::writeMessagesBlock( 'Unknown messages', $sortedMessages['unknown'] );
147                 }
148                 $messagesText .= ");
150                 return array( $messagesText, $sortedMessages );
151         }
153         /**
154          * Generates an array of comments for messages.
155          *
156          * @param $messages Array: key of messages.
157          * @param $ignored Array: list of ingored message keys.
158          * @param $optional Array: list of optional message keys.
159          */
160         public static function makeComments( $messages, $ignored, $optional ) {
161                 # Comment collector
162                 $commentArray = array();
164                 # List of keys only
165                 foreach( $messages as $key ) {
166                         if( in_array( $key, $ignored ) ) {
167                                 $commentArray[$key] = ' # ' . self::$ignoredComment;
168                         } elseif( in_array( $key, $optional ) ) {
169                                 $commentArray[$key] = ' # ' . self::$optionalComment;
170                         }
171                 }
173                 return $commentArray;
174         }
176         /**
177          * Write a block of messages to PHP.
178          *
179          * @param $blockComment String: the comment of whole block.
180          * @param $messages Array: the block messages.
181          * @param $messageComments Array: optional comments for messages in this block.
182          * @param $prefix String: prefix for every line, for indenting purposes.
183          *
184          * @return The block, formatted in PHP.
185          */
186         public static function writeMessagesBlock( $blockComment, $messages,
187                 $messageComments = array(), $prefix = '' ) {
189                 $blockText = '';
191                 # Skip the block if it includes no messages
192                 if( empty( $messages ) ) {
193                         return '';
194                 }
196                 # Format the block comment (if exists); check for multiple lines comments
197                 if( !empty( $blockComment ) ) {
198                         if( strpos( $blockComment, "\n" ) === false ) {
199                                 $blockText .= "$prefix# $blockComment
201                         } else {
202                                 $blockText .= "$prefix/*
203 $blockComment
206                         }
207                 }
209                 # Get max key length
210                 $maxKeyLength = max( array_map( 'strlen', array_keys( $messages ) ) );
212                 # Format the messages
213                 foreach( $messages as $key => $value ) {
214                         # Add the key name
215                         $blockText .= "$prefix'$key'";
217                         # Add the appropriate block whitespace
218                         $blockText .= str_repeat( ' ', $maxKeyLength - strlen( $key ) );
220                         # Refer to the value
221                         $blockText .= ' => ';
223                         # Check for the appropriate apostrophe and add the value
224                         # Quote \ here, because it needs always escaping
225                         $value = addcslashes( $value, '\\' );
227                         # For readability
228                         $single = "'";
229                         $double = '"';
231                         if( strpos( $value, $single ) === false ) {
232                                 # Nothing ugly, just use '
233                                 $blockText .= $single.$value.$single;
234                         } elseif( strpos( $value, $double ) === false && !preg_match('/\$[a-zA-Z_\x7f-\xff]/', $value) ) {
235                                 # No "-quotes, no variables that need quoting, use "
236                                 $blockText .= $double.$value.$double;
237                         } else {
238                                 # Something needs quoting, pick the quote which causes less quoting
239                                 $quote = substr_count( $value, $double ) + substr_count( $value, '$' ) >= substr_count( $value, $single ) ? $single : $double;
240                                 if( $quote === $double ) {
241                                         $extra = '$';
242                                 } else {
243                                         $extra = '';
244                                 }
245                                 $blockText .= $quote . addcslashes( $value, $quote . $extra ) . $quote;
246                         }
248                         # Comma
249                         $blockText .= ',';
251                         # Add comments, if there is any
252                         if( array_key_exists( $key, $messageComments ) ) {
253                                 $blockText .= $messageComments[$key];
254                         }
256                         # Newline
257                         $blockText .= "
259                 }
261                 # Newline to end the block
262                 $blockText .= "
265                 return $blockText;
266         }