MediaWiki  REL1_20
writeMessagesArray.inc
Go to the documentation of this file.
00001 <?php
00027 class MessageWriter {
00028         static $optionalComment = 'only translate this message to other languages if you have to change it';
00029         static $ignoredComment = "do not translate or duplicate this message to other languages";
00030 
00031         static $messageStructure;
00032         static $blockComments;
00033         static $ignoredMessages;
00034         static $optionalMessages;
00035 
00046         public static function writeMessagesToFile( $messages, $code, $write, $listUnknown, $removeUnknown, $messagesFolder = false ) {
00047                 # Rewrite the messages array
00048                 $messages = self::writeMessagesArray( $messages, $code == 'en', false, $removeUnknown );
00049                 $messagesText = $messages[0];
00050                 $sortedMessages = $messages[1];
00051 
00052                 # Write to the file
00053                 if ( $messagesFolder )
00054                         $filename = Language::getFileName( "$messagesFolder/Messages", $code );
00055                 else
00056                         $filename = Language::getMessagesFileName( $code );
00057 
00058                 if ( file_exists( $filename ) )
00059                         $contents = file_get_contents( $filename );
00060                 else
00061                         $contents = '<?php
00062 $messages = array(
00063 );
00064 ';
00065 
00066                 if( strpos( $contents, '$messages' ) !== false ) {
00067                         $contents = explode( '$messages', $contents );
00068                         if( $messagesText == '$messages' . $contents[1] ) {
00069                                 echo "Generated messages for language $code. Same as the current file.\n";
00070                         } else {
00071                                 if( $write ) {
00072                                         $new = $contents[0];
00073                                         $new .= $messagesText;
00074                                         file_put_contents( $filename, $new );
00075                                         echo "Generated and wrote messages for language $code.\n";
00076                                 } else {
00077                                         echo "Generated messages for language $code. Please run the script again (without the parameter \"dry-run\") to write the array to the file.\n";
00078                                 }
00079                         }
00080                         if( $listUnknown && isset( $sortedMessages['unknown'] ) && !empty( $sortedMessages['unknown'] ) ) {
00081                                 if ( $removeUnknown )
00082                                         echo "\nThe following " . count( $sortedMessages['unknown'] ) . " unknown messages have been removed:\n";
00083                                 else
00084                                         echo "\nThere are " . count( $sortedMessages['unknown'] ) . " unknown messages, please check them:\n";
00085                                 foreach( $sortedMessages['unknown'] as $key => $value ) {
00086                                         echo "* " . $key . "\n";
00087                                 }
00088                         }
00089                 } else {
00090                         echo "Generated messages for language $code. There seem to be no messages array in the file.\n";
00091                 }
00092         }
00093 
00106         public static function writeMessagesArray( $messages, $ignoredComments = false, $prefix = false, $removeUnknown = false ) {
00107                 # Load messages
00108                 $dir = $prefix ? $prefix : __DIR__;
00109 
00110                 require( $dir . '/messages.inc' );
00111                 self::$messageStructure = $wgMessageStructure;
00112                 self::$blockComments = $wgBlockComments;
00113 
00114                 require( $dir . '/messageTypes.inc' );
00115                 self::$ignoredMessages = $wgIgnoredMessages;
00116                 self::$optionalMessages = $wgOptionalMessages;
00117 
00118                 # Sort messages to blocks
00119                 $sortedMessages['unknown'] = $messages;
00120                 foreach( self::$messageStructure as $blockName => $block ) {
00124                         foreach( $block as $key ) {
00125                                 if( array_key_exists( $key, $sortedMessages['unknown'] ) ) {
00126                                         $sortedMessages[$blockName][$key] = $sortedMessages['unknown'][$key];
00127                                         unset( $sortedMessages['unknown'][$key] );
00128                                 }
00129                         }
00130                 }
00131 
00132                 # Write all the messages
00133                 $messagesText = "\$messages = array(
00134 ";
00135                 foreach( $sortedMessages as $block => $messages ) {
00136                         # Skip if it's the block of unknown messages - handle that in the end of file
00137                         if( $block == 'unknown' ) {
00138                                 continue;
00139                         }
00140 
00141                         if( $ignoredComments ) {
00142                                 $ignored = self::$ignoredMessages;
00143                                 $optional = self::$optionalMessages;
00144                         } else {
00145                                 $ignored = array();
00146                                 $optional = array();
00147                         }
00148                         $comments = self::makeComments( array_keys( $messages ), $ignored, $optional );
00149 
00150                         # Write the block
00151                         $messagesText .= self::writeMessagesBlock( self::$blockComments[$block], $messages, $comments );
00152                 }
00153 
00154                 # Write the unknown messages, alphabetically sorted.
00155                 # Of course, we don't have any comments for them, because they are unknown.
00156                 if ( !$removeUnknown ) {
00157                         ksort( $sortedMessages['unknown'] );
00158                         $messagesText .= self::writeMessagesBlock( 'Unknown messages', $sortedMessages['unknown'] );
00159                 }
00160                 $messagesText .= ");
00161 ";
00162                 return array( $messagesText, $sortedMessages );
00163         }
00164 
00173         public static function makeComments( $messages, $ignored, $optional ) {
00174                 # Comment collector
00175                 $commentArray = array();
00176 
00177                 # List of keys only
00178                 foreach( $messages as $key ) {
00179                         if( in_array( $key, $ignored ) ) {
00180                                 $commentArray[$key] = ' # ' . self::$ignoredComment;
00181                         } elseif( in_array( $key, $optional ) ) {
00182                                 $commentArray[$key] = ' # ' . self::$optionalComment;
00183                         }
00184                 }
00185 
00186                 return $commentArray;
00187         }
00188 
00199         public static function writeMessagesBlock( $blockComment, $messages,
00200                 $messageComments = array(), $prefix = '' ) {
00201 
00202                 $blockText = '';
00203 
00204                 # Skip the block if it includes no messages
00205                 if( empty( $messages ) ) {
00206                         return '';
00207                 }
00208 
00209                 # Format the block comment (if exists); check for multiple lines comments
00210                 if( !empty( $blockComment ) ) {
00211                         if( strpos( $blockComment, "\n" ) === false ) {
00212                                 $blockText .= "$prefix# $blockComment
00213 ";
00214                         } else {
00215                                 $blockText .= "$prefix/*
00216 $blockComment
00217 */
00218 ";
00219                         }
00220                 }
00221 
00222                 # Get max key length
00223                 $maxKeyLength = max( array_map( 'strlen', array_keys( $messages ) ) );
00224 
00225                 # Format the messages
00226                 foreach( $messages as $key => $value ) {
00227                         # Add the key name
00228                         $blockText .= "$prefix'$key'";
00229 
00230                         # Add the appropriate block whitespace
00231                         $blockText .= str_repeat( ' ', $maxKeyLength - strlen( $key ) );
00232 
00233                         # Refer to the value
00234                         $blockText .= ' => ';
00235 
00236                         # Check for the appropriate apostrophe and add the value
00237                         # Quote \ here, because it needs always escaping
00238                         $value = addcslashes( $value, '\\' );
00239 
00240                         # For readability
00241                         $single = "'";
00242                         $double = '"';
00243 
00244                         if( strpos( $value, $single ) === false ) {
00245                                 # Nothing ugly, just use '
00246                                 $blockText .= $single.$value.$single;
00247                         } elseif( strpos( $value, $double ) === false && !preg_match('/\$[a-zA-Z_\x7f-\xff]/', $value) ) {
00248                                 # No "-quotes, no variables that need quoting, use "
00249                                 $blockText .= $double.$value.$double;
00250                         } else {
00251                                 # Something needs quoting, pick the quote which causes less quoting
00252                                 $quote = substr_count( $value, $double ) + substr_count( $value, '$' ) >= substr_count( $value, $single ) ? $single : $double;
00253                                 if( $quote === $double ) {
00254                                         $extra = '$';
00255                                 } else {
00256                                         $extra = '';
00257                                 }
00258                                 $blockText .= $quote . addcslashes( $value, $quote . $extra ) . $quote;
00259                         }
00260 
00261                         # Comma
00262                         $blockText .= ',';
00263 
00264                         # Add comments, if there is any
00265                         if( array_key_exists( $key, $messageComments ) ) {
00266                                 $blockText .= $messageComments[$key];
00267                         }
00268 
00269                         # Newline
00270                         $blockText .= "
00271 ";
00272                 }
00273 
00274                 # Newline to end the block
00275                 $blockText .= "
00276 ";
00277 
00278                 return $blockText;
00279         }
00280 }