Index: trunk/extensions/Translate/scripts/magic-export-2.php |
— | — | @@ -3,6 +3,7 @@ |
4 | 4 | * Script to export special page aliases and magic words of extensions. |
5 | 5 | * |
6 | 6 | * @author Robert Leverington <robert@rhl.me.uk> |
| 7 | + * |
7 | 8 | * @copyright Copyright © 2010 Robert Leverington |
8 | 9 | * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later |
9 | 10 | * @file |
— | — | @@ -45,6 +46,8 @@ |
46 | 47 | } |
47 | 48 | |
48 | 49 | $langs = Cli::parseLanguageCodes( '*' ); |
| 50 | +unset( $langs[array_search( 'en', $langs )] ); |
| 51 | +$langs = array_merge( array( 'en' ), $langs ); |
49 | 52 | $groups = MessageGroups::singleton()->getGroups(); |
50 | 53 | |
51 | 54 | $type = $options['type'] ; |
— | — | @@ -52,11 +55,9 @@ |
53 | 56 | // Open file handles. |
54 | 57 | STDOUT( "Opening file handles..." ); |
55 | 58 | $handles = array(); |
56 | | -$keys = array(); error_reporting( E_ALL | E_STRICT ); |
| 59 | +$messagesOld = array(); |
57 | 60 | foreach ( $groups as $group ) { |
58 | | - if ( !$group instanceof ExtensionMessageGroup ) { |
59 | | - continue; |
60 | | - } |
| 61 | + if ( !$group instanceof ExtensionMessageGroup ) continue; |
61 | 62 | |
62 | 63 | if ( $type === 'special' ) { |
63 | 64 | $filename = $group->getAliasFile(); |
— | — | @@ -64,21 +65,14 @@ |
65 | 66 | $filename = $group->getMagicFile(); |
66 | 67 | } |
67 | 68 | |
68 | | - if ( $filename === null ) { |
69 | | - continue; |
70 | | - } |
| 69 | + if ( $filename === null ) continue; |
71 | 70 | |
72 | 71 | $file = "$wgTranslateExtensionDirectory/$filename"; |
73 | | - if ( !file_exists( $file ) ) { |
74 | | - continue; |
75 | | - } |
| 72 | + if ( !file_exists( $file ) ) continue; |
76 | 73 | |
77 | 74 | include( $file ); |
78 | | - if ( !isset( $aliases ) ) { |
79 | | - continue; |
80 | | - } |
81 | | - |
82 | | - $keys[$group->getId()] = array_keys( $aliases['en'] ); |
| 75 | + if( !isset( $aliases ) ) continue; |
| 76 | + $messagesOld[$group->getId()] = $aliases; |
83 | 77 | unset( $aliases ); |
84 | 78 | |
85 | 79 | $handles[$group->getId()] = fopen( $options['target'] . '/' . $filename, 'w' ); |
— | — | @@ -86,6 +80,26 @@ |
87 | 81 | STDOUT( "\t{$group->getId()}" ); |
88 | 82 | } |
89 | 83 | |
| 84 | +// Write header. |
| 85 | +foreach( $handles as $handle ) { |
| 86 | + if( $type === 'special' ) { |
| 87 | + fwrite( $handle, <<<EOT |
| 88 | +<?php |
| 89 | + |
| 90 | +/** |
| 91 | + * Aliases for special pages |
| 92 | + * |
| 93 | + * @file |
| 94 | + * @ingroup Extensions |
| 95 | + */ |
| 96 | + |
| 97 | +\$aliases = array(); |
| 98 | +EOT |
| 99 | +); |
| 100 | + } else { |
| 101 | + } |
| 102 | +} |
| 103 | + |
90 | 104 | foreach ( $langs as $l ) { |
91 | 105 | switch ( $options['type'] ) { |
92 | 106 | case 'special': |
— | — | @@ -95,54 +109,63 @@ |
96 | 110 | $title = Title::newFromText( 'MediaWiki:Sp-translate-data-MagicWords/' . $l ); |
97 | 111 | break; |
98 | 112 | default: |
99 | | - STDERR( "Invalid type: must be one of: special, magic" ); |
100 | 113 | exit( 1 ); |
101 | 114 | } |
102 | 115 | |
103 | | - if ( !$title || !$title->exists() ) { |
| 116 | + if( !$title || !$title->exists() ) { |
104 | 117 | STDOUT( "Skiping $l..." ); |
105 | | - continue; |
| 118 | + |
| 119 | + $messagesNew = array(); |
106 | 120 | } else { |
107 | 121 | STDOUT( "Processing $l..." ); |
108 | | - } |
109 | 122 | |
110 | | - $article = new Article( $title ); |
111 | | - $data = $article->getContent(); |
| 123 | + $article = new Article( $title ); |
| 124 | + $data = $article->getContent(); |
112 | 125 | |
113 | | - // Parse message file. |
114 | | - $segments = explode( "\n", $data ); |
115 | | - array_shift( $segments ); |
116 | | - array_shift( $segments ); |
117 | | - unset( $segments[count( $segments ) -1] ); |
118 | | - unset( $segments[count( $segments ) -1] ); |
119 | | - |
120 | | - $messages = array(); |
121 | | - |
122 | | - foreach ( $segments as $segment ) { |
123 | | - $parts = explode( '=', $segment ); |
124 | | - $key = trim( array_shift( $parts ) ); |
125 | | - $translations = explode( ', ', implode( $parts ) ); |
126 | | - $messages[$key] = $translations; |
| 126 | + // Parse message file. |
| 127 | + $segments = explode( "\n", $data ); |
| 128 | + array_shift( $segments ); |
| 129 | + array_shift( $segments ); |
| 130 | + unset( $segments[count($segments)-1] ); |
| 131 | + unset( $segments[count($segments)-1] ); |
| 132 | + $messagesNew = array(); |
| 133 | + foreach( $segments as $segment ) { |
| 134 | + $parts = explode( '=', $segment ); |
| 135 | + $key = trim( array_shift( $parts ) ); |
| 136 | + $translations = explode( ', ', implode( $parts ) ); |
| 137 | + $messagesNew[$key] = $translations; |
| 138 | + } |
127 | 139 | } |
128 | 140 | |
129 | | - // Need to only provide the keys applicable to the file that is being written. |
130 | | - foreach ( $handles as $group => $handle ) { |
| 141 | + foreach( $handles as $group => $handle ) { |
131 | 142 | STDOUT( "\t{$group}... " ); |
132 | | - $thismessages = $messages; // TODO: Reduce. |
133 | | - $out = "\$aliases['{$group} '] = array(\n"; |
134 | | - |
135 | | - foreach ( $thismessages as $key => $translations ) { |
136 | | - $translations = implode( "', '", $translations ); |
137 | | - $out .= "\t'$key' => array( '$translations' ),\n"; |
| 143 | + $namesEn = LanguageNames::getNames( 'en' ); |
| 144 | + $namesNative = Language::getLanguageNames(); |
| 145 | + $messagesOut = array(); |
| 146 | + foreach( $messagesOld[$group]['en'] as $key => $message ) { |
| 147 | + if( array_key_exists( $key, $messagesNew ) ) { |
| 148 | + $messagesOut[$key] = $messagesNew[$key]; |
| 149 | + } elseif( isset( $messagesOld[$group][$l][$key] ) ) { |
| 150 | + $messagesOut[$key] = $messagesOld[$group][$l][$key]; |
| 151 | + } |
138 | 152 | } |
139 | | - |
140 | | - $out .= ");\n\n"; |
141 | | - fwrite( $handle, $out ); |
| 153 | + if( count( $messagesOut ) > 0 ) { |
| 154 | + $out = "\n\n/** {$namesEn[$l]} ({$namesNative[$l]}) */\n\$aliases['{$l}'] = array(\n"; |
| 155 | + foreach( $messagesOut as $key => $translations ) { |
| 156 | + foreach( $translations as $id => $translation ) { |
| 157 | + $translations[$id] = addslashes( $translation ); |
| 158 | + } |
| 159 | + $translations = implode( "', '", $translations ); |
| 160 | + $out .= "\t'$key' => array( '$translations' ),\n"; |
| 161 | + } |
| 162 | + $out .= ");"; |
| 163 | + fwrite( $handle, $out ); |
| 164 | + } |
142 | 165 | } |
143 | 166 | } |
144 | 167 | |
145 | 168 | // Close handles. |
146 | 169 | STDOUT( "Closing file handles..." ); |
147 | | -foreach ( $handles as $group => $handle ) { |
| 170 | +foreach( $handles as $group => $handle ) { |
148 | 171 | fclose( $handle ); |
149 | 172 | } |