MediaWiki  1.23.14
writeMessagesArray.inc
Go to the documentation of this file.
1 <?php
28  protected static $optionalComment =
29  'only translate this message to other languages if you have to change it';
30  protected static $ignoredComment = "do not translate or duplicate this message to other languages";
31 
32  protected static $messageStructure;
33  protected static $blockComments;
34  protected static $ignoredMessages;
35  protected static $optionalMessages;
36 
48  public static function writeMessagesToFile( $messages, $code, $write, $listUnknown,
49  $removeUnknown, $messagesFolder = false
50  ) {
51  # Rewrite the messages array
52  $messages = self::writeMessagesArray( $messages, $code == 'en', false, $removeUnknown );
53  $messagesText = $messages[0];
54  $sortedMessages = $messages[1];
55 
56  # Write to the file
57  if ( $messagesFolder ) {
58  $filename = Language::getFileName( "$messagesFolder/Messages", $code );
59  } else {
60  $filename = Language::getMessagesFileName( $code );
61  }
62 
63  if ( file_exists( $filename ) ) {
64  $contents = file_get_contents( $filename );
65  } else {
66  $contents = '<?php
67 $messages = array(
68 );
69 ';
70  }
71 
72  if ( strpos( $contents, '$messages' ) !== false ) {
73  $contents = explode( '$messages', $contents );
74  if ( $messagesText == '$messages' . $contents[1] ) {
75  echo "Generated messages for language $code. Same as the current file.\n";
76  } else {
77  if ( $write ) {
78  $new = $contents[0];
79  $new .= $messagesText;
80  file_put_contents( $filename, $new );
81  echo "Generated and wrote messages for language $code.\n";
82  } else {
83  echo "Generated messages for language $code.\n" .
84  "Please run the script again (without the parameter \"dry-run\") " .
85  "to write the array to the file.\n";
86  }
87  }
88  if ( $listUnknown && isset( $sortedMessages['unknown'] ) &&
89  !empty( $sortedMessages['unknown'] )
90  ) {
91  if ( $removeUnknown ) {
92  echo "\nThe following " . count( $sortedMessages['unknown'] ) .
93  " unknown messages have been removed:\n";
94  } else {
95  echo "\nThere are " . count( $sortedMessages['unknown'] ) .
96  " unknown messages, please check them:\n";
97  }
98  foreach ( $sortedMessages['unknown'] as $key => $value ) {
99  echo "* " . $key . "\n";
100  }
101  }
102  } else {
103  echo "Generated messages for language $code. There seem to be no messages array in the file.\n";
104  }
105  }
106 
119  public static function writeMessagesArray( $messages, $ignoredComments = false,
120  $prefix = false, $removeUnknown = false
121  ) {
122  # Load messages
123  $dir = $prefix ? $prefix : __DIR__;
124 
125  require $dir . '/messages.inc';
126  self::$messageStructure = $wgMessageStructure;
127  self::$blockComments = $wgBlockComments;
128 
129  require $dir . '/messageTypes.inc';
130  self::$ignoredMessages = $wgIgnoredMessages;
131  self::$optionalMessages = $wgOptionalMessages;
132 
133  # Sort messages to blocks
134  $sortedMessages['unknown'] = $messages;
135  foreach ( self::$messageStructure as $blockName => $block ) {
139  foreach ( $block as $key ) {
140  if ( array_key_exists( $key, $sortedMessages['unknown'] ) ) {
141  $sortedMessages[$blockName][$key] = $sortedMessages['unknown'][$key];
142  unset( $sortedMessages['unknown'][$key] );
143  }
144  }
145  }
146 
147  # Write all the messages
148  $messagesText = "\$messages = array(
149 ";
150  foreach ( $sortedMessages as $block => $messages ) {
151  # Skip if it's the block of unknown messages - handle that in the end of file
152  if ( $block == 'unknown' ) {
153  continue;
154  }
155 
156  if ( $ignoredComments ) {
157  $ignored = self::$ignoredMessages;
158  $optional = self::$optionalMessages;
159  } else {
160  $ignored = array();
161  $optional = array();
162  }
163  $comments = self::makeComments( array_keys( $messages ), $ignored, $optional );
164 
165  # Write the block
166  $messagesText .= self::writeMessagesBlock( self::$blockComments[$block], $messages, $comments );
167  }
168 
169  # Write the unknown messages, alphabetically sorted.
170  # Of course, we don't have any comments for them, because they are unknown.
171  if ( !$removeUnknown ) {
172  ksort( $sortedMessages['unknown'] );
173  $messagesText .= self::writeMessagesBlock( 'Unknown messages', $sortedMessages['unknown'] );
174  }
175  $messagesText .= ");
176 ";
177 
178  return array( $messagesText, $sortedMessages );
179  }
180 
189  public static function makeComments( $messages, $ignored, $optional ) {
190  # Comment collector
191  $commentArray = array();
192 
193  # List of keys only
194  foreach ( $messages as $key ) {
195  if ( in_array( $key, $ignored ) ) {
196  $commentArray[$key] = ' # ' . self::$ignoredComment;
197  } elseif ( in_array( $key, $optional ) ) {
198  $commentArray[$key] = ' # ' . self::$optionalComment;
199  }
200  }
201 
202  return $commentArray;
203  }
204 
215  public static function writeMessagesBlock( $blockComment, $messages,
216  $messageComments = array(), $prefix = '' ) {
217 
218  $blockText = '';
219 
220  # Skip the block if it includes no messages
221  if ( empty( $messages ) ) {
222  return '';
223  }
224 
225  # Format the block comment (if exists); check for multiple lines comments
226  if ( !empty( $blockComment ) ) {
227  if ( strpos( $blockComment, "\n" ) === false ) {
228  $blockText .= "$prefix# $blockComment
229 ";
230  } else {
231  $blockText .= "$prefix/*
232 $blockComment
233 */
234 ";
235  }
236  }
237 
238  # Get max key length
239  $maxKeyLength = max( array_map( 'strlen', array_keys( $messages ) ) );
240 
241  # Format the messages
242  foreach ( $messages as $key => $value ) {
243  # Add the key name
244  $blockText .= "$prefix'$key'";
245 
246  # Add the appropriate block whitespace
247  $blockText .= str_repeat( ' ', $maxKeyLength - strlen( $key ) );
248 
249  # Refer to the value
250  $blockText .= ' => ';
251 
252  # Check for the appropriate apostrophe and add the value
253  # Quote \ here, because it needs always escaping
254  $value = addcslashes( $value, '\\' );
255 
256  # For readability
257  $single = "'";
258  $double = '"';
259 
260  if ( strpos( $value, $single ) === false ) {
261  # Nothing ugly, just use '
262  $blockText .= $single . $value . $single;
263  } elseif ( strpos( $value, $double ) === false &&
264  !preg_match( '/\$[a-zA-Z_\x7f-\xff]/', $value )
265  ) {
266  # No "-quotes, no variables that need quoting, use "
267  $blockText .= $double . $value . $double;
268  } else {
269  # Something needs quoting, pick the quote which causes less quoting
270 
271  if ( substr_count( $value, $double ) + substr_count( $value, '$' ) >=
272  substr_count( $value, $single )
273  ) {
274  $quote = $single;
275  } else {
276  $quote = $double;
277  }
278 
279  if ( $quote === $double ) {
280  $extra = '$';
281  } else {
282  $extra = '';
283  }
284  $blockText .= $quote . addcslashes( $value, $quote . $extra ) . $quote;
285  }
286 
287  # Comma
288  $blockText .= ',';
289 
290  # Add comments, if there is any
291  if ( array_key_exists( $key, $messageComments ) ) {
292  $blockText .= $messageComments[$key];
293  }
294 
295  # Newline
296  $blockText .= "
297 ";
298  }
299 
300  # Newline to end the block
301  $blockText .= "
302 ";
303 
304  return $blockText;
305  }
306 }
MessageWriter\$ignoredMessages
static $ignoredMessages
Definition: writeMessagesArray.inc:34
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
MessageWriter\writeMessagesToFile
static writeMessagesToFile( $messages, $code, $write, $listUnknown, $removeUnknown, $messagesFolder=false)
Write a messages array as a PHP text and write it to the messages file.
Definition: writeMessagesArray.inc:48
Language\getFileName
static getFileName( $prefix='Language', $code, $suffix='.php')
Get the name of a file for a certain language code.
Definition: Language.php:4061
Language\getMessagesFileName
static getMessagesFileName( $code)
Definition: Language.php:4090
$messages
$messages
Definition: LogTests.i18n.php:8
MessageWriter\$optionalMessages
static $optionalMessages
Definition: writeMessagesArray.inc:35
$messagesFolder
$messagesFolder
Definition: rebuildLanguage.php:126
MessageWriter\$blockComments
static $blockComments
Definition: writeMessagesArray.inc:33
MessageWriter\$messageStructure
static $messageStructure
Definition: writeMessagesArray.inc:32
$wgMessageStructure
$wgMessageStructure
The structure of the messages, divided to blocks.
Definition: messages.inc:25
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
MessageWriter\makeComments
static makeComments( $messages, $ignored, $optional)
Generates an array of comments for messages.
Definition: writeMessagesArray.inc:189
$value
$value
Definition: styleTest.css.php:45
MessageWriter\$ignoredComment
static $ignoredComment
Definition: writeMessagesArray.inc:30
MessageWriter
Definition: writeMessagesArray.inc:27
$wgBlockComments
$wgBlockComments
Comments for each block.
Definition: messages.inc:4050
MessageWriter\writeMessagesBlock
static writeMessagesBlock( $blockComment, $messages, $messageComments=array(), $prefix='')
Write a block of messages to PHP.
Definition: writeMessagesArray.inc:215
$dir
if(count( $args)==0) $dir
Definition: importImages.php:49
$wgIgnoredMessages
$wgIgnoredMessages
Ignored messages, which should exist only in the English messages file.
Definition: messageTypes.inc:25
MessageWriter\writeMessagesArray
static writeMessagesArray( $messages, $ignoredComments=false, $prefix=false, $removeUnknown=false)
Write a messages array as a PHP text.
Definition: writeMessagesArray.inc:119
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
$wgOptionalMessages
$wgOptionalMessages
Optional messages, which may be translated only if changed in the target language.
Definition: messageTypes.inc:279
MessageWriter\$optionalComment
static $optionalComment
Definition: writeMessagesArray.inc:28