Change from global to parameter.
[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 * @file
6 * @ingroup MaintenanceLanguage
7 * @defgroup MaintenanceLanguage MaintenanceLanguage
8 */
9
10 require_once( dirname( __FILE__ ) . '/../commandLine.inc' );
11 require_once( 'languages.inc' );
12 require_once( 'writeMessagesArray.inc' );
13
14 /**
15 * Rewrite a messages array.
16 *
17 * @param $code The language code.
18 * @param $write Write to the messages file?
19 * @param $listUnknown List the unknown messages?
20 * @param $removeUnknown Remove the unknown messages?
21 * @param $removeDupes Remove the duplicated messages?
22 * @param $dupeMsgSource The source file intended to remove from the array.
23 */
24 function rebuildLanguage( $languages, $code, $write, $listUnknown, $removeUnknown, $removeDupes, $dupeMsgSource ) {
25 $messages = $languages->getMessages( $code );
26 $messages = $messages['all'];
27 if ( $removeDupes ) {
28 $messages = removeDupes( $messages, $dupeMsgSource );
29 }
30 MessageWriter::writeMessagesToFile( $messages, $code, $write, $listUnknown, $removeUnknown );
31 }
32
33 /**
34 * Remove duplicates from a message array.
35 *
36 * @param $oldMsgArray The input message array.
37 * @param $dupeMsgSource The source file path for duplicates.
38 * @return $newMsgArray The output message array, with duplicates removed.
39 */
40 function removeDupes( $oldMsgArray, $dupeMsgSource ) {
41 if ( file_exists( $dupeMsgSource ) ) {
42 include( $dupeMsgSource );
43 if ( !isset( $dupeMessages ) ) {
44 echo( "There are no duplicated messages in the source file provided." );
45 exit( 1 );
46 }
47 } else {
48 echo ( "The specified file $dupeMsgSource cannot be found." );
49 exit( 1 );
50 }
51 $newMsgArray = $oldMsgArray;
52 foreach ( $oldMsgArray as $key => $value ) {
53 if ( array_key_exists( $key, $dupeMessages ) ) {
54 unset( $newMsgArray[$key] );
55 }
56 }
57 return $newMsgArray;
58 }
59
60 # Show help
61 if ( isset( $options['help'] ) ) {
62 echo <<<TEXT
63 Run this script to rewrite the messages array in the files languages/messages/MessagesXX.php.
64 Parameters:
65 * lang: Language code (default: the installation default language). You can also specify "all" to check all the languages.
66 * help: Show this help.
67 Options:
68 * dry-run: Do not write the array to the file.
69 * no-unknown: Do not list the unknown messages.
70 * remove-unknown: Remove unknown messages.
71 * remove-duplicates: Remove duplicated messages based on a PHP source file.
72
73 TEXT;
74 exit( 1 );
75 }
76
77 # Get the language code
78 if ( isset( $options['lang'] ) ) {
79 $wgCode = $options['lang'];
80 } else {
81 $wgCode = $wgContLang->getCode();
82 }
83
84 # Get the duplicate message source
85 if ( isset( $options['remove-duplicates'] ) && ( strcmp( $options['remove-duplicates'], '' ) ) ) {
86 $wgDupeMessageSource = $options['remove-duplicates'];
87 } else {
88 $wgDupeMessageSource = '';
89 }
90
91 # Get the options
92 $wgWriteToFile = !isset( $options['dry-run'] );
93 $wgListUnknownMessages = !isset( $options['no-unknown'] );
94 $wgRemoveUnknownMessages = isset( $options['remove-unknown'] );
95 $wgRemoveDuplicateMessages = isset( $options['remove-duplicates'] );
96
97 # Get language objects
98 $languages = new languages();
99
100 # Write all the language
101 if ( $wgCode == 'all' ) {
102 foreach ( $languages->getLanguages() as $languageCode ) {
103 rebuildLanguage( $languages, $languageCode, $wgWriteToFile, $wgListUnknownMessages, $wgRemoveUnknownMessages, $wgRemoveDuplicateMessages, $wgDupeMessageSource );
104 }
105 } else {
106 rebuildLanguage( $languages, $wgCode, $wgWriteToFile, $wgListUnknownMessages, $wgRemoveUnknownMessages, $wgRemoveDuplicateMessages, $wgDupeMessageSource );
107 }