3 * Write a messages array as a PHP text.
5 * @addtogroup Maintenance
8 require_once( 'messages.inc' );
9 require_once( 'messageTypes.inc' );
12 * Write a messages array as a PHP text.
14 * @param $messages The messages array.
15 * @param $ignoredComments Show comments about ignored and optional messages? (For English.)
17 * @return The PHP text.
19 function writeMessagesArray( $messages, $ignoredComments = false ) {
20 global $wgMessageStrucutre, $wgBlockComments;
22 # Sort messages to blocks
23 $sortedMessages['unknown'] = $messages;
24 foreach ( $wgMessageStrucutre as $blockName => $block ) {
25 foreach ( $block as $key ) {
26 if ( array_key_exists( $key, $sortedMessages['unknown'] ) ) {
27 $sortedMessages[$blockName][$key] = $sortedMessages['unknown'][$key];
28 unset( $sortedMessages['unknown'][$key] );
33 # Write all the messages
34 $messagesText = "\$messages = array(\n";
35 foreach( $sortedMessages as $block => $messages ) {
36 # Skip if it's the block of unknown messages - handle that in the end of file
37 if ( $block == 'unknown' ) {
42 $messagesText .= writeMessagesBlock( $block, $wgBlockComments[$block], $messages, $ignoredComments );
44 ksort( $sortedMessages['unknown'] );
45 $messagesText .= writeMessagesBlock( 'unknown', 'Unknown messages', $sortedMessages['unknown'], $ignoredComments ); # Write the unknown messages, alphabetically sorted
46 $messagesText .= ");\n";
52 * Write a block of messages to PHP.
54 * @param $name The block name.
55 * @param $comment The block comment.
56 * @param $messages The block messages.
57 * @param $ignoredComments Show comments about ignored and optional messages? (For English.)
59 * @return The block, formatted in PHP.
61 function writeMessagesBlock( $name, $comment, $messages, $ignoredComments ) {
62 global $wgMessageComments, $wgMessagseWithDollarSigns;
63 global $wgIgnoredMessages, $wgOptionalMessages;
66 # Skip the block if it includes no messages
67 if ( empty( $messages ) ) {
71 # Format the block comment (if exists); check for multiple lines comments
72 if ( !empty( $comment ) ) {
73 if ( strpos( $comment, "\n" ) === false ) {
74 $blockText .= "# $comment\n";
76 $blockText .= "/*\n$comment\n*/\n";
81 $maxKeyLength = max( array_map( 'strlen', array_keys( $messages ) ) );
84 foreach( $messages as $key => $value ) {
86 $blockText .= "'$key'";
88 # Add the appropriate block whitespace
89 $blockText .= str_repeat( ' ', $maxKeyLength - strlen( $key ) );
94 # Check for the appropriate apostrophe and add the value
95 if ( strpos( $value, "'" ) === false ) {
96 $blockText .= "'$value'";
97 } elseif ( strpos( $value, '"' ) === false && !in_array( $key, $wgMessagseWithDollarSigns ) ) {
98 $blockText .= "\"$value\"";
100 # Pick the less numerous one to escape
101 $quote = substr_count( $value, '"' ) + substr_count( $value, '$' ) >= substr_count( $value, "'" ) ? "'" : '"';
102 if ('"' == $quote) { $extra = '$'; }
103 else { $extra = ''; }
104 $blockText .= $quote . addcslashes( $value, $quote.'\\'.$extra ) . $quote;
110 $ignoredComment = "don't translate or duplicate this message to other languages";
111 $optionalComment = "only translate this message to other languages if you have to change it";
112 $showIgnoredOrOptionalComment = in_array( $key, $wgIgnoredMessages ) || in_array( $key, $wgOptionalMessages );
113 if ( $ignoredComments ) {
114 if ( array_key_exists( $key, $wgMessageComments ) ) {
115 $blockText .= ' # ' . $wgMessageComments[$key];
116 if ( $showIgnoredOrOptionalComment ) {
119 } elseif ( $showIgnoredOrOptionalComment ) {
122 if ( in_array( $key, $wgIgnoredMessages ) ) {
123 $blockText .= $ignoredComment;
124 } elseif ( in_array( $key, $wgOptionalMessages ) ) {
125 $blockText .= $optionalComment;
127 } elseif ( array_key_exists( $key, $wgMessageComments ) ) {
128 $blockText .= ' # ' . $wgMessageComments[$key];
135 # Newline to end the block