TODO list.
[lhc/web/wiklou.git] / maintenance / language / writeMessagesArray.inc
1 <?php
2 /**
3 * Write a messages array as a PHP text.
4 * TODO: make it possible to use Windows endlines
5 * TODO: remove PHP ending tags recently removed from MediaWiki files
6 *
7 * @addtogroup Maintenance
8 */
9
10 require_once( 'messages.inc' );
11 require_once( 'messageTypes.inc' );
12
13 /**
14 * Write a messages array as a PHP text and write it to the messages file.
15 *
16 * @param $messages The messages array.
17 * @param $code The language code.
18 * @param $write Write to the messages file?
19 * @param $listUnknown List the unknown messages?
20 */
21 function writeMessagesToFile( $messages, $code, $write, $listUnknown ) {
22 # Rewrite the messages array
23 $messages = writeMessagesArray( $messages, $code == 'en' );
24 $messagesText = $messages[0];
25 $sortedMessages = $messages[1];
26
27 # Write to the file
28 $filename = Language::getMessagesFileName( $code );
29 $contents = file_get_contents( $filename );
30 if ( strpos( $contents, '$messages' ) !== false ) {
31 $contents = explode( '$messages', $contents );
32 if ( $messagesText . "\n?>\n" == '$messages' . $contents[1] ) {
33 echo "Generated messages for language $code. Same to the current file.\n";
34 } else {
35 if ( $write ) {
36 $new = $contents[0];
37 $new .= $messagesText;
38 $new .= "\n?>\n";
39 file_put_contents( $filename, $new );
40 echo "Generated and wrote messages for language $code.\n";
41 } else {
42 echo "Generated messages for language $code. Please run the script again (without the parameter \"dry-run\") to write the array to the file.\n";
43 }
44 }
45 if ( $listUnknown && isset( $sortedMessages['unknown'] ) && !empty( $sortedMessages['unknown'] ) ) {
46 echo "\nThere are " . count( $sortedMessages['unknown'] ) . " unknown messages, please check them:\n";
47 foreach ( $sortedMessages['unknown'] as $key => $value ) {
48 echo "* " . $key . "\n";
49 }
50 }
51 } else {
52 echo "Generated messages for language $code. There seems to be no messages array in the file.\n";
53 }
54 }
55
56 /**
57 * Write a messages array as a PHP text.
58 *
59 * @param $messages The messages array.
60 * @param $ignoredComments Show comments about ignored and optional messages? (For English.)
61 *
62 * @return Array of the PHP text and the sorted messages array.
63 */
64 function writeMessagesArray( $messages, $ignoredComments = false ) {
65 global $wgMessageStructure, $wgBlockComments;
66
67 # Sort messages to blocks
68 $sortedMessages['unknown'] = $messages;
69 foreach ( $wgMessageStructure as $blockName => $block ) {
70 foreach ( $block as $key ) {
71 if ( array_key_exists( $key, $sortedMessages['unknown'] ) ) {
72 $sortedMessages[$blockName][$key] = $sortedMessages['unknown'][$key];
73 unset( $sortedMessages['unknown'][$key] );
74 }
75 }
76 }
77
78 # Write all the messages
79 $messagesText = "\$messages = array(\n";
80 foreach( $sortedMessages as $block => $messages ) {
81 # Skip if it's the block of unknown messages - handle that in the end of file
82 if ( $block == 'unknown' ) {
83 continue;
84 }
85
86 # Write the block
87 $messagesText .= writeMessagesBlock( $block, $wgBlockComments[$block], $messages, $ignoredComments );
88 }
89 ksort( $sortedMessages['unknown'] );
90 $messagesText .= writeMessagesBlock( 'unknown', 'Unknown messages', $sortedMessages['unknown'], $ignoredComments ); # Write the unknown messages, alphabetically sorted
91 $messagesText .= ");\n";
92
93 return array( $messagesText, $sortedMessages );
94 }
95
96 /**
97 * Write a block of messages to PHP.
98 *
99 * @param $name The block name.
100 * @param $comment The block comment.
101 * @param $messages The block messages.
102 * @param $ignoredComments Show comments about ignored and optional messages? (For English.)
103 *
104 * @return The block, formatted in PHP.
105 */
106 function writeMessagesBlock( $name, $comment, $messages, $ignoredComments ) {
107 global $wgMessageComments, $wgMessagseWithDollarSigns;
108 global $wgIgnoredMessages, $wgOptionalMessages;
109 $blockText = '';
110
111 # Skip the block if it includes no messages
112 if ( empty( $messages ) ) {
113 return '';
114 }
115
116 # Format the block comment (if exists); check for multiple lines comments
117 if ( !empty( $comment ) ) {
118 if ( strpos( $comment, "\n" ) === false ) {
119 $blockText .= "# $comment\n";
120 } else {
121 $blockText .= "/*\n$comment\n*/\n";
122 }
123 }
124
125 # Get max key length
126 $maxKeyLength = max( array_map( 'strlen', array_keys( $messages ) ) );
127
128 # Format the messages
129 foreach( $messages as $key => $value ) {
130 # Add the key name
131 $blockText .= "'$key'";
132
133 # Add the appropriate block whitespace
134 $blockText .= str_repeat( ' ', $maxKeyLength - strlen( $key ) );
135
136 # Refer to the value
137 $blockText .= ' => ';
138
139 # Check for the appropriate apostrophe and add the value
140 if ( strpos( $value, "'" ) === false ) {
141 $blockText .= "'$value'";
142 } elseif ( strpos( $value, '"' ) === false && !in_array( $key, $wgMessagseWithDollarSigns ) ) {
143 $blockText .= "\"$value\"";
144 } else {
145 # Pick the less numerous one to escape
146 $quote = substr_count( $value, '"' ) + substr_count( $value, '$' ) >= substr_count( $value, "'" ) ? "'" : '"';
147 if ('"' == $quote) { $extra = '$'; }
148 else { $extra = ''; }
149 $blockText .= $quote . addcslashes( $value, $quote.'\\'.$extra ) . $quote;
150 }
151
152 # Comma
153 $blockText .= ',';
154
155 $ignoredComment = "don't translate or duplicate this message to other languages";
156 $optionalComment = "only translate this message to other languages if you have to change it";
157 $showIgnoredOrOptionalComment = in_array( $key, $wgIgnoredMessages ) || in_array( $key, $wgOptionalMessages );
158 if ( $ignoredComments ) {
159 if ( array_key_exists( $key, $wgMessageComments ) ) {
160 $blockText .= ' # ' . $wgMessageComments[$key];
161 if ( $showIgnoredOrOptionalComment ) {
162 $blockText .= '; ';
163 }
164 } elseif ( $showIgnoredOrOptionalComment ) {
165 $blockText .= ' # ';
166 }
167 if ( in_array( $key, $wgIgnoredMessages ) ) {
168 $blockText .= $ignoredComment;
169 } elseif ( in_array( $key, $wgOptionalMessages ) ) {
170 $blockText .= $optionalComment;
171 }
172 } elseif ( array_key_exists( $key, $wgMessageComments ) ) {
173 $blockText .= ' # ' . $wgMessageComments[$key];
174 }
175
176 # Newline
177 $blockText .= "\n";
178 }
179
180 # Newline to end the block
181 $blockText .= "\n";
182
183 return $blockText;
184 }
185
186 ?>