For the maintenance/ directory files:
[lhc/web/wiklou.git] / maintenance / language / rebuildLanguage.php
1 <?php
2 /**
3 * Rewrite the messages array in the files languages/messages/MessagesXX.php.
4 *
5 * @package MediaWiki
6 * @subpackage Maintenance
7 */
8
9 require_once( dirname(__FILE__).'/../commandLine.inc' );
10 require_once( 'languages.inc' );
11 require_once( 'writeMessagesArray.inc' );
12
13 /**
14 * Rewrite a messages array.
15 *
16 * @param $code The language code.
17 * @param $write Write to the messages file?
18 */
19 function rebuildLanguage( $code, $write ) {
20 global $wgLanguages;
21
22 # Get messages
23 $messages = $wgLanguages->getMessages( $code );
24 $messages = $messages['all'];
25
26 # Rewrite messages array
27 $messagesText = writeMessagesArray( $messages, $code == 'en' );
28
29 # Write to the file
30 if ( $write ) {
31 $filename = Language::getMessagesFileName( $code );
32 $contents = file_get_contents( $filename );
33 if ( strpos( $contents, '$messages' ) !== false ) {
34 $new = explode( '$messages', $contents );
35 $new = $new[0];
36 $new .= $messagesText;
37 $new .= "\n?>\n";
38 file_put_contents( $filename, $new );
39 echo "Generated and wrote messages in language $code.\n";
40 }
41 } else {
42 echo "Generated messages in language $code.\n";
43 }
44 }
45
46 # Show help
47 if ( isset( $options['help'] ) ) {
48 echo <<<END
49 Run this script to rewrite the messages array in the files languages/messages/MessagesXX.php.
50 Parameters:
51 * lang: Language code (default: the installation default language). You can also specify "all" to check all the languages.
52 * help: Show this help.
53 Options:
54 * dry-run: Don't write the array to the file.
55
56 END;
57 exit();
58 }
59
60 # Get the language code
61 if ( isset( $options['lang'] ) ) {
62 $wgCode = $options['lang'];
63 } else {
64 $wgCode = $wgContLang->getCode();
65 }
66
67 # Get the write options
68 $wgWriteToFile = !isset( $options['dry-run'] );
69
70 # Get language objects
71 $wgLanguages = new languages();
72
73 # Write all the language
74 if ( $wgCode == 'all' ) {
75 foreach ( $wgLanguages->getLanguages() as $language ) {
76 rebuildLanguage( $language, $wgWriteToFile );
77 }
78 } else {
79 rebuildLanguage( $wgCode, $wgWriteToFile );
80 }
81
82 ?>