3 * Write a messages array as a PHP text.
6 * @subpackage Maintenance
9 require_once( 'messages.inc' );
10 require_once( 'messageTypes.inc' );
13 * Write a messages array as a PHP text.
15 * @param $messages The messages array.
16 * @param $ignoredComments Show comments about ignored and optional messages? (For English.)
18 * @return The PHP text.
20 function writeMessagesArray( $messages, $ignoredComments = false ) {
21 global $wgMessageStrucutre, $wgBlockComments, $wgMessageComments;
23 # Sort messages to blocks
24 $sortedMessages['unknown'] = $messages;
25 foreach ( $wgMessageStrucutre as $blockName => $block ) {
26 foreach ( $block as $key ) {
27 if ( array_key_exists( $key, $sortedMessages['unknown'] ) ) {
28 $sortedMessages[$blockName][$key] = $sortedMessages['unknown'][$key];
29 unset( $sortedMessages['unknown'][$key] );
34 # Write all the messages
35 $messagesText = "\$messages = array(\n";
36 foreach( $sortedMessages as $block => $messages ) {
37 # Skip if it's the block of unknown messages - handle that in the end of file
38 if ( $block == 'unknown' ) {
43 $messagesText .= writeMessagesBlock( $block, $wgBlockComments[$block], $messages, $ignoredComments );
45 ksort( $sortedMessages['unknown'] );
46 $messagesText .= writeMessagesBlock( 'unknown', 'Unknown messages', $sortedMessages['unknown'], $ignoredComments ); # Write the unknown messages, alphabetically sorted
47 $messagesText .= ");\n";
53 * Write a block of messages to PHP.
55 * @param $name The block name.
56 * @param $comment The block comment.
57 * @param $messages The block messages.
58 * @param $ignoredComments Show comments about ignored and optional messages? (For English.)
60 * @return The block, formatted in PHP.
62 function writeMessagesBlock( $name, $comment, $messages, $ignoredComments ) {
63 global $wgMessageComments, $wgMessagseWithDollarSigns;
64 global $wgIgnoredMessages, $wgOptionalMessages;
67 # Skip the block if it includes no messages
68 if ( empty( $messages ) ) {
72 # Format the block comment (if exists); check for multiple lines comments
73 if ( !empty( $comment ) ) {
74 if ( strpos( $comment, "\n" ) === false ) {
75 $blockText .= "# $comment\n";
77 $blockText .= "/*\n$comment\n*/\n";
82 $maxKeyLength = max( array_map( 'strlen', array_keys( $messages ) ) );
85 foreach( $messages as $key => $value ) {
87 $blockText .= "'$key'";
89 # Add the appropriate block whitespace
90 $blockText .= str_repeat( ' ', $maxKeyLength - strlen( $key ) );
95 # Check for the appropriate apostrophe and add the value
96 if ( strpos( $value, "'" ) === false ) {
97 $blockText .= "'$value'";
98 } elseif ( strpos( $value, '"' ) === false && !in_array( $key, $wgMessagseWithDollarSigns ) ) {
99 $blockText .= "\"$value\"";
101 # Pick the less numerous one to escape
102 $quote = substr_count( $value, '"' ) >= substr_count( $value, "'" ) ? "'" : '"';
103 $blockText .= $quote . addcslashes( $value, $quote.'\\' ) . $quote;
109 $ignoredComment = "don't translate or duplicate this message to other languages";
110 $optionalComment = "only translate this message to other languages if you have to change it";
111 $showIgnoredOrOptionalComment = in_array( $key, $wgIgnoredMessages ) || in_array( $key, $wgOptionalMessages );
112 if ( $ignoredComments ) {
113 if ( array_key_exists( $key, $wgMessageComments ) ) {
114 $blockText .= ' # ' . $wgMessageComments[$key];
115 if ( $showIgnoredOrOptionalComment ) {
118 } elseif ( $showIgnoredOrOptionalComment ) {
121 if ( in_array( $key, $wgIgnoredMessages ) ) {
122 $blockText .= $ignoredComment;
123 } elseif ( in_array( $key, $wgOptionalMessages ) ) {
124 $blockText .= $optionalComment;
126 } elseif ( array_key_exists( $key, $wgMessageComments ) ) {
127 $blockText .= ' # ' . $wgMessageComments[$key];
134 # Newline to end the block