Use Doxygen @addtogroup instead of phpdoc @package && @subpackage
[lhc/web/wiklou.git] / maintenance / language / writeMessagesArray.inc
1 <?php
2 /**
3 * Write a messages array as a PHP text.
4 *
5 * @addtogroup Maintenance
6 */
7
8 require_once( 'messages.inc' );
9 require_once( 'messageTypes.inc' );
10
11 /**
12 * Write a messages array as a PHP text.
13 *
14 * @param $messages The messages array.
15 * @param $ignoredComments Show comments about ignored and optional messages? (For English.)
16 *
17 * @return The PHP text.
18 */
19 function writeMessagesArray( $messages, $ignoredComments = false ) {
20 global $wgMessageStrucutre, $wgBlockComments;
21
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] );
29 }
30 }
31 }
32
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' ) {
38 continue;
39 }
40
41 # Write the block
42 $messagesText .= writeMessagesBlock( $block, $wgBlockComments[$block], $messages, $ignoredComments );
43 }
44 ksort( $sortedMessages['unknown'] );
45 $messagesText .= writeMessagesBlock( 'unknown', 'Unknown messages', $sortedMessages['unknown'], $ignoredComments ); # Write the unknown messages, alphabetically sorted
46 $messagesText .= ");\n";
47
48 return $messagesText;
49 }
50
51 /**
52 * Write a block of messages to PHP.
53 *
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.)
58 *
59 * @return The block, formatted in PHP.
60 */
61 function writeMessagesBlock( $name, $comment, $messages, $ignoredComments ) {
62 global $wgMessageComments, $wgMessagseWithDollarSigns;
63 global $wgIgnoredMessages, $wgOptionalMessages;
64 $blockText = '';
65
66 # Skip the block if it includes no messages
67 if ( empty( $messages ) ) {
68 return '';
69 }
70
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";
75 } else {
76 $blockText .= "/*\n$comment\n*/\n";
77 }
78 }
79
80 # Get max key length
81 $maxKeyLength = max( array_map( 'strlen', array_keys( $messages ) ) );
82
83 # Format the messages
84 foreach( $messages as $key => $value ) {
85 # Add the key name
86 $blockText .= "'$key'";
87
88 # Add the appropriate block whitespace
89 $blockText .= str_repeat( ' ', $maxKeyLength - strlen( $key ) );
90
91 # Refer to the value
92 $blockText .= ' => ';
93
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\"";
99 } else {
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;
105 }
106
107 # Comma
108 $blockText .= ',';
109
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 ) {
117 $blockText .= '; ';
118 }
119 } elseif ( $showIgnoredOrOptionalComment ) {
120 $blockText .= ' # ';
121 }
122 if ( in_array( $key, $wgIgnoredMessages ) ) {
123 $blockText .= $ignoredComment;
124 } elseif ( in_array( $key, $wgOptionalMessages ) ) {
125 $blockText .= $optionalComment;
126 }
127 } elseif ( array_key_exists( $key, $wgMessageComments ) ) {
128 $blockText .= ' # ' . $wgMessageComments[$key];
129 }
130
131 # Newline
132 $blockText .= "\n";
133 }
134
135 # Newline to end the block
136 $blockText .= "\n";
137
138 return $blockText;
139 }
140
141 ?>