Merge "Moved JobQueueDB::recycleAndDeleteStaleJobs() function below overriden ones."
[lhc/web/wiklou.git] / maintenance / language / writeMessagesArray.inc
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 */
23
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";
30
31 static $messageStructure;
32 static $blockComments;
33 static $ignoredMessages;
34 static $optionalMessages;
35
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 * @param $messagesFolder String: path to a folder to store the MediaWiki messages. Defaults to the current install.
45 */
46 public static function writeMessagesToFile( $messages, $code, $write, $listUnknown, $removeUnknown, $messagesFolder = false ) {
47 # Rewrite the messages array
48 $messages = self::writeMessagesArray( $messages, $code == 'en', false, $removeUnknown );
49 $messagesText = $messages[0];
50 $sortedMessages = $messages[1];
51
52 # Write to the file
53 if ( $messagesFolder )
54 $filename = Language::getFileName( "$messagesFolder/Messages", $code );
55 else
56 $filename = Language::getMessagesFileName( $code );
57
58 if ( file_exists( $filename ) )
59 $contents = file_get_contents( $filename );
60 else
61 $contents = '<?php
62 $messages = array(
63 );
64 ';
65
66 if( strpos( $contents, '$messages' ) !== false ) {
67 $contents = explode( '$messages', $contents );
68 if( $messagesText == '$messages' . $contents[1] ) {
69 echo "Generated messages for language $code. Same as the current file.\n";
70 } else {
71 if( $write ) {
72 $new = $contents[0];
73 $new .= $messagesText;
74 file_put_contents( $filename, $new );
75 echo "Generated and wrote messages for language $code.\n";
76 } else {
77 echo "Generated messages for language $code. Please run the script again (without the parameter \"dry-run\") to write the array to the file.\n";
78 }
79 }
80 if( $listUnknown && isset( $sortedMessages['unknown'] ) && !empty( $sortedMessages['unknown'] ) ) {
81 if ( $removeUnknown )
82 echo "\nThe following " . count( $sortedMessages['unknown'] ) . " unknown messages have been removed:\n";
83 else
84 echo "\nThere are " . count( $sortedMessages['unknown'] ) . " unknown messages, please check them:\n";
85 foreach( $sortedMessages['unknown'] as $key => $value ) {
86 echo "* " . $key . "\n";
87 }
88 }
89 } else {
90 echo "Generated messages for language $code. There seem to be no messages array in the file.\n";
91 }
92 }
93
94 /**
95 * Write a messages array as a PHP text.
96 *
97 * @param $messages Array: the messages array.
98 * @param $ignoredComments Boolean: show comments about ignored and optional
99 * messages? (For English.)
100 * @param $prefix String: base path for messages.inc and messageTypes.inc files
101 * or false for default path (this directory)
102 * @param $removeUnknown Boolean: whether to remove unkown messages
103 *
104 * @return Array of the PHP text and the sorted messages array.
105 */
106 public static function writeMessagesArray( $messages, $ignoredComments = false, $prefix = false, $removeUnknown = false ) {
107 # Load messages
108 $dir = $prefix ? $prefix : __DIR__;
109
110 require( $dir . '/messages.inc' );
111 self::$messageStructure = $wgMessageStructure;
112 self::$blockComments = $wgBlockComments;
113
114 require( $dir . '/messageTypes.inc' );
115 self::$ignoredMessages = $wgIgnoredMessages;
116 self::$optionalMessages = $wgOptionalMessages;
117
118 # Sort messages to blocks
119 $sortedMessages['unknown'] = $messages;
120 foreach( self::$messageStructure as $blockName => $block ) {
121 /**
122 * @var $block array
123 */
124 foreach( $block as $key ) {
125 if( array_key_exists( $key, $sortedMessages['unknown'] ) ) {
126 $sortedMessages[$blockName][$key] = $sortedMessages['unknown'][$key];
127 unset( $sortedMessages['unknown'][$key] );
128 }
129 }
130 }
131
132 # Write all the messages
133 $messagesText = "\$messages = array(
134 ";
135 foreach( $sortedMessages as $block => $messages ) {
136 # Skip if it's the block of unknown messages - handle that in the end of file
137 if( $block == 'unknown' ) {
138 continue;
139 }
140
141 if( $ignoredComments ) {
142 $ignored = self::$ignoredMessages;
143 $optional = self::$optionalMessages;
144 } else {
145 $ignored = array();
146 $optional = array();
147 }
148 $comments = self::makeComments( array_keys( $messages ), $ignored, $optional );
149
150 # Write the block
151 $messagesText .= self::writeMessagesBlock( self::$blockComments[$block], $messages, $comments );
152 }
153
154 # Write the unknown messages, alphabetically sorted.
155 # Of course, we don't have any comments for them, because they are unknown.
156 if ( !$removeUnknown ) {
157 ksort( $sortedMessages['unknown'] );
158 $messagesText .= self::writeMessagesBlock( 'Unknown messages', $sortedMessages['unknown'] );
159 }
160 $messagesText .= ");
161 ";
162 return array( $messagesText, $sortedMessages );
163 }
164
165 /**
166 * Generates an array of comments for messages.
167 *
168 * @param $messages Array: key of messages.
169 * @param $ignored Array: list of ingored message keys.
170 * @param $optional Array: list of optional message keys.
171 * @return array
172 */
173 public static function makeComments( $messages, $ignored, $optional ) {
174 # Comment collector
175 $commentArray = array();
176
177 # List of keys only
178 foreach( $messages as $key ) {
179 if( in_array( $key, $ignored ) ) {
180 $commentArray[$key] = ' # ' . self::$ignoredComment;
181 } elseif( in_array( $key, $optional ) ) {
182 $commentArray[$key] = ' # ' . self::$optionalComment;
183 }
184 }
185
186 return $commentArray;
187 }
188
189 /**
190 * Write a block of messages to PHP.
191 *
192 * @param $blockComment String: the comment of whole block.
193 * @param $messages Array: the block messages.
194 * @param $messageComments Array: optional comments for messages in this block.
195 * @param $prefix String: prefix for every line, for indenting purposes.
196 *
197 * @return string The block, formatted in PHP.
198 */
199 public static function writeMessagesBlock( $blockComment, $messages,
200 $messageComments = array(), $prefix = '' ) {
201
202 $blockText = '';
203
204 # Skip the block if it includes no messages
205 if( empty( $messages ) ) {
206 return '';
207 }
208
209 # Format the block comment (if exists); check for multiple lines comments
210 if( !empty( $blockComment ) ) {
211 if( strpos( $blockComment, "\n" ) === false ) {
212 $blockText .= "$prefix# $blockComment
213 ";
214 } else {
215 $blockText .= "$prefix/*
216 $blockComment
217 */
218 ";
219 }
220 }
221
222 # Get max key length
223 $maxKeyLength = max( array_map( 'strlen', array_keys( $messages ) ) );
224
225 # Format the messages
226 foreach( $messages as $key => $value ) {
227 # Add the key name
228 $blockText .= "$prefix'$key'";
229
230 # Add the appropriate block whitespace
231 $blockText .= str_repeat( ' ', $maxKeyLength - strlen( $key ) );
232
233 # Refer to the value
234 $blockText .= ' => ';
235
236 # Check for the appropriate apostrophe and add the value
237 # Quote \ here, because it needs always escaping
238 $value = addcslashes( $value, '\\' );
239
240 # For readability
241 $single = "'";
242 $double = '"';
243
244 if( strpos( $value, $single ) === false ) {
245 # Nothing ugly, just use '
246 $blockText .= $single . $value . $single;
247 } elseif( strpos( $value, $double ) === false && !preg_match('/\$[a-zA-Z_\x7f-\xff]/', $value) ) {
248 # No "-quotes, no variables that need quoting, use "
249 $blockText .= $double . $value . $double;
250 } else {
251 # Something needs quoting, pick the quote which causes less quoting
252 $quote = substr_count( $value, $double ) + substr_count( $value, '$' ) >= substr_count( $value, $single ) ? $single : $double;
253 if( $quote === $double ) {
254 $extra = '$';
255 } else {
256 $extra = '';
257 }
258 $blockText .= $quote . addcslashes( $value, $quote . $extra ) . $quote;
259 }
260
261 # Comma
262 $blockText .= ',';
263
264 # Add comments, if there is any
265 if( array_key_exists( $key, $messageComments ) ) {
266 $blockText .= $messageComments[$key];
267 }
268
269 # Newline
270 $blockText .= "
271 ";
272 }
273
274 # Newline to end the block
275 $blockText .= "
276 ";
277
278 return $blockText;
279 }
280 }