Index: trunk/phase3/maintenance/language/rebuildLanguage.php |
— | — | @@ -18,14 +18,46 @@ |
19 | 19 | * @param $write Write to the messages file? |
20 | 20 | * @param $listUnknown List the unknown messages? |
21 | 21 | * @param $removeUnKnown Remove the unknown messages? |
| 22 | + * @param $removeDupes Remove the duplicated messages? |
| 23 | + * @param $dupeMsgSource The source file intended to remove from the array. |
22 | 24 | */ |
23 | | -function rebuildLanguage( $code, $write, $listUnknown, $removeUnknown ) { |
| 25 | +function rebuildLanguage( $code, $write, $listUnknown, $removeUnknown, $removeDupes, $dupeMsgSource ) { |
24 | 26 | global $wgLanguages; |
25 | 27 | $messages = $wgLanguages->getMessages( $code ); |
26 | 28 | $messages = $messages['all']; |
| 29 | + if ($removeDupes) { |
| 30 | + $messages = removeDupes( $messages, $dupeMsgSource ); |
| 31 | + } |
27 | 32 | MessageWriter::writeMessagesToFile( $messages, $code, $write, $listUnknown, $removeUnknown ); |
28 | 33 | } |
29 | 34 | |
| 35 | +/** |
| 36 | + * Remove duplicates from a message array. |
| 37 | + * |
| 38 | + * @param $oldMsgArray The input message array. |
| 39 | + * @param $dupeMsgSource The source file path for duplicates. |
| 40 | + * @return $newMsgArray The output message array, with duplicates removed. |
| 41 | + */ |
| 42 | +function removeDupes( $oldMsgArray, $dupeMsgSource ) { |
| 43 | + if (file_exists($dupeMsgSource)) { |
| 44 | + include($dupeMsgSource); |
| 45 | + if (!isset($dupeMessages)) { |
| 46 | + echo("There are no duplicated messages in the source file provided."); |
| 47 | + exit(1); |
| 48 | + } |
| 49 | + } else { |
| 50 | + echo ("The specified file $dupeMsgSource cannot be found."); |
| 51 | + exit(1); |
| 52 | + } |
| 53 | + $newMsgArray = $oldMsgArray; |
| 54 | + foreach ($oldMsgArray as $key => $value) { |
| 55 | + if ( array_key_exists( $key, $dupeMessages ) ) { |
| 56 | + unset($newMsgArray[$key]); |
| 57 | + } |
| 58 | + } |
| 59 | + return $newMsgArray; |
| 60 | +} |
| 61 | + |
30 | 62 | # Show help |
31 | 63 | if ( isset( $options['help'] ) ) { |
32 | 64 | echo <<<END |
— | — | @@ -37,6 +69,7 @@ |
38 | 70 | * dry-run: Do not write the array to the file. |
39 | 71 | * no-unknown: Do not list the unknown messages. |
40 | 72 | * remove-unknown: Remove unknown messages. |
| 73 | + * remove-duplicates: Remove duplicated messages based on a PHP source file. |
41 | 74 | |
42 | 75 | END; |
43 | 76 | exit(1); |
— | — | @@ -49,10 +82,18 @@ |
50 | 83 | $wgCode = $wgContLang->getCode(); |
51 | 84 | } |
52 | 85 | |
| 86 | +# Get the duplicate message source |
| 87 | +if ( isset( $options['remove-duplicates'] ) && ( strcmp( $options['remove-duplicates'], '' ) ) ) { |
| 88 | + $wgDupeMessageSource = $options['remove-duplicates']; |
| 89 | +} else { |
| 90 | + $wgDupeMessageSource = ''; |
| 91 | +} |
| 92 | + |
53 | 93 | # Get the options |
54 | 94 | $wgWriteToFile = !isset( $options['dry-run'] ); |
55 | 95 | $wgListUnknownMessages = !isset( $options['no-unknown'] ); |
56 | 96 | $wgRemoveUnknownMessages = isset( $options['remove-unknown'] ); |
| 97 | +$wgRemoveDuplicateMessages = isset( $options['remove-duplicates'] ); |
57 | 98 | |
58 | 99 | # Get language objects |
59 | 100 | $wgLanguages = new languages(); |
— | — | @@ -60,8 +101,8 @@ |
61 | 102 | # Write all the language |
62 | 103 | if ( $wgCode == 'all' ) { |
63 | 104 | foreach ( $wgLanguages->getLanguages() as $language ) { |
64 | | - rebuildLanguage( $language, $wgWriteToFile, $wgListUnknownMessages, $wgRemoveUnknownMessages ); |
| 105 | + rebuildLanguage( $language, $wgWriteToFile, $wgListUnknownMessages, $wgRemoveUnknownMessages, $wgRemoveDuplicateMessages, $wgDupeMessageSource ); |
65 | 106 | } |
66 | 107 | } else { |
67 | | - rebuildLanguage( $wgCode, $wgWriteToFile, $wgListUnknownMessages, $wgRemoveUnknownMessages ); |
| 108 | + rebuildLanguage( $wgCode, $wgWriteToFile, $wgListUnknownMessages, $wgRemoveUnknownMessages, $wgRemoveDuplicateMessages, $wgDupeMessageSource ); |
68 | 109 | } |