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