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