Index: trunk/extensions/cldr/rebuild.php |
— | — | @@ -16,64 +16,49 @@ |
17 | 17 | $DATA = "$dir/core/common/main"; |
18 | 18 | $OUTPUT = $dir; |
19 | 19 | |
20 | | -if (isset( $options['datadir'] ) ) { |
| 20 | +if ( isset( $options['datadir'] ) ) { |
21 | 21 | $DATA = $options['datadir']; |
22 | 22 | } |
23 | 23 | |
24 | | -if (isset( $options['outputdir'] ) ) { |
| 24 | +if ( isset( $options['outputdir'] ) ) { |
25 | 25 | $OUTPUT = $options['outputdir']; |
26 | 26 | } |
27 | 27 | |
28 | | -$langs = Language::getLanguageNames( false ); |
| 28 | +// Get an array of all MediaWiki languages ( $wgLanguageNames + $wgExtraLanguageNames ) |
| 29 | +$languages = Language::getLanguageNames( false ); |
29 | 30 | # hack to get pt-pt too |
30 | | -$langs['pt-pt'] = 'Foo'; |
31 | | -ksort($langs); |
| 31 | +$languages['pt-pt'] = 'Foo'; |
| 32 | +ksort( $languages ); |
32 | 33 | |
33 | | -foreach ( $langs as $code => $name ) { |
34 | | - unset( $codePartStr ); |
35 | | - $codePartStr = explode( '-', $code ); |
36 | | - $countCode = count( $codePartStr ); |
37 | | - if ( count( $codePartStr ) > 1) { |
38 | | - unset( $codePart ); |
39 | | - for ($i = 0; $i < count( $codePartStr ); $i++) { |
40 | | - if ( isset( $codePartStr[$i] ) ) { |
41 | | - $codePart[$i] = $codePartStr[$i]; |
42 | | - } else { |
43 | | - $codePart[$i] = ''; |
44 | | - } |
45 | | - } |
| 34 | +foreach ( $languages as $code => $name ) { |
| 35 | + |
| 36 | + // Construct the correct name for the input file |
| 37 | + unset( $codeParts ); |
| 38 | + $codeParts = explode( '-', $code ); |
| 39 | + if ( count( $codeParts ) > 1 ) { |
| 40 | + |
46 | 41 | // ISO 15924 alpha-4 script code |
47 | | - if (strlen($codePart[1]) == 4 ) { |
48 | | - $codePart[1] = ucfirst( $codePart[1] ); |
| 42 | + if ( strlen( $codeParts[1] ) == 4 ) { |
| 43 | + $codeParts[1] = ucfirst( $codeParts[1] ); |
49 | 44 | } |
50 | 45 | |
51 | 46 | // ISO 3166-1 alpha-2 country code |
52 | | - if (strlen($codePart[1]) == 2 ) { |
53 | | - $codePart[2] = strtoupper( $codePart[1] ); |
54 | | - $codePart[1] = ''; |
| 47 | + if ( strlen( $codeParts[1] ) == 2 ) { |
| 48 | + $codeParts[2] = $codeParts[1]; |
| 49 | + unset( $codeParts[1] ); |
55 | 50 | } |
56 | | - if ( isset( $codePart[2] )) { |
57 | | - if ( strlen( $codePart[2] ) == 2 ) { |
58 | | - $codePart[2] = strtoupper( $codePart[2] ); |
| 51 | + if ( isset( $codeParts[2] ) ) { |
| 52 | + if ( strlen( $codeParts[2] ) == 2 ) { |
| 53 | + $codeParts[2] = strtoupper( $codeParts[2] ); |
59 | 54 | } |
60 | 55 | } |
61 | | - for ( $i = 0; $i < count($codePart); $i++ ) { |
62 | | - if ( $codePart[$i] === '' ) |
63 | | - unset( $codePart[$i] ); |
64 | | - } |
65 | | - $codeCLDR = implode( '-', $codePart ); |
| 56 | + $codeCLDR = implode( '_', $codeParts ); |
66 | 57 | } else { |
67 | 58 | $codeCLDR = $code; |
68 | 59 | } |
69 | | - |
70 | | - $codeCLDR = str_replace( |
71 | | - array( '-' ), |
72 | | - array( '_' ), |
73 | | - $codeCLDR |
74 | | - ); |
75 | | - |
76 | 60 | $input = "$DATA/$codeCLDR.xml"; |
77 | 61 | |
| 62 | + // If the file exists, parse it, otherwise display an error |
78 | 63 | if ( file_exists( $input ) ) { |
79 | 64 | $en = Language::factory('en'); |
80 | 65 | $p = new CLDRParser(); |
— | — | @@ -85,7 +70,7 @@ |
86 | 71 | $p->setAlias( false ); |
87 | 72 | $p->parse( $input, "$OUTPUT/" . LanguageNames::getFileName( getRealCode( $code ) ) ); |
88 | 73 | } |
89 | | - } elseif (isset( $options['verbose'] ) ) { |
| 74 | + } elseif ( isset( $options['verbose'] ) ) { |
90 | 75 | echo "File $input not found\n"; |
91 | 76 | } |
92 | 77 | } |
— | — | @@ -98,7 +83,7 @@ |
99 | 84 | private $output = "<?php\n\$names = array(\n"; |
100 | 85 | private $count = 0; |
101 | 86 | |
102 | | - function s($parser, $name, $attrs) { |
| 87 | + function start( $parser, $name, $attrs ) { |
103 | 88 | if ( $name === 'LANGUAGES' ) { |
104 | 89 | $this->languages = true; |
105 | 90 | } |
— | — | @@ -108,15 +93,15 @@ |
109 | 94 | |
110 | 95 | $this->ok = false; |
111 | 96 | if ( $this->languages && $name === 'LANGUAGE' ) { |
112 | | - if (!isset($attrs["ALT"]) && !isset($attrs["DRAFT"])) { |
| 97 | + if ( !isset($attrs["ALT"] ) && !isset( $attrs["DRAFT"] ) ) { |
113 | 98 | $this->ok = true; |
114 | | - $type = str_replace( '_', '-', strtolower($attrs['TYPE'])); |
| 99 | + $type = str_replace( '_', '-', strtolower($attrs['TYPE'] ) ); |
115 | 100 | $this->output .= "'$type' => '"; |
116 | 101 | } |
117 | 102 | } |
118 | 103 | } |
119 | 104 | |
120 | | - function e($parser, $name) { |
| 105 | + function end( $parser, $name ) { |
121 | 106 | if ( $name === 'LANGUAGES' ) { |
122 | 107 | $this->languages = false; |
123 | 108 | $this->ok = false; |
— | — | @@ -125,52 +110,52 @@ |
126 | 111 | if ( $name === 'ALIAS' ) { |
127 | 112 | return; |
128 | 113 | } |
129 | | - if (!$this->ok) return; |
| 114 | + if ( !$this->ok ) return; |
130 | 115 | $this->output .= "',\n"; |
131 | 116 | } |
132 | 117 | |
133 | | - function c($parser, $data) { |
134 | | - if (!$this->ok) return; |
135 | | - if (trim($data) === '') return; |
136 | | - $this->output .= preg_replace( "/(?<!\\\\)'/", "\'", trim($data)); |
| 118 | + function contents( $parser, $data ) { |
| 119 | + if ( !$this->ok ) return; |
| 120 | + if ( trim( $data ) === '' ) return; |
| 121 | + $this->output .= preg_replace( "/(?<!\\\\)'/", "\'", trim( $data ) ); |
137 | 122 | $this->count++; |
138 | 123 | } |
139 | 124 | |
140 | | - function parse($input, $output) { |
| 125 | + function parse( $input, $output ) { |
141 | 126 | |
142 | 127 | $xml_parser = xml_parser_create(); |
143 | | - xml_set_element_handler($xml_parser, array($this,'s'), array($this,'e')); |
144 | | - xml_set_character_data_handler($xml_parser, array($this,'c')); |
145 | | - if (!($fp = fopen($input, "r"))) { |
146 | | - die("could not open XML input"); |
| 128 | + xml_set_element_handler( $xml_parser, array( $this,'start' ), array( $this,'end' ) ); |
| 129 | + xml_set_character_data_handler( $xml_parser, array($this,'contents' ) ); |
| 130 | + if ( !( $fileHandle = fopen( $input, "r" ) ) ) { |
| 131 | + die( "could not open XML input" ); |
147 | 132 | } |
148 | 133 | |
149 | | - while ($data = fread($fp, filesize($input))) { |
150 | | - if (!xml_parse($xml_parser, $data, feof($fp))) { |
151 | | - die(sprintf("XML error: %s at line %d", |
152 | | - xml_error_string(xml_get_error_code($xml_parser)), |
153 | | - xml_get_current_line_number($xml_parser))); |
154 | | - } |
| 134 | + while ( $data = fread( $fileHandle, filesize( $input ) ) ) { |
| 135 | + if ( !xml_parse( $xml_parser, $data, feof( $fileHandle ) ) ) { |
| 136 | + die( sprintf( "XML error: %s at line %d", |
| 137 | + xml_error_string(xml_get_error_code( $xml_parser ) ), |
| 138 | + xml_get_current_line_number( $xml_parser ) ) ); |
| 139 | + } |
155 | 140 | } |
156 | | - xml_parser_free($xml_parser); |
| 141 | + xml_parser_free( $xml_parser ); |
157 | 142 | |
158 | | - fclose($fp); |
| 143 | + fclose( $fileHandle ); |
159 | 144 | |
160 | 145 | if ( !$this->count ) { return; } |
161 | 146 | |
162 | | - if ($this->alias === false) |
163 | | - $this->output .= ");\n"; |
164 | | - if ( $this->count == 1 ) |
| 147 | + if ( $this->alias === false ) $this->output .= ");\n"; |
| 148 | + if ( $this->count == 1 ) { |
165 | 149 | echo "Wrote $this->count entry to $output\n"; |
166 | | - else |
| 150 | + } else { |
167 | 151 | echo "Wrote $this->count entries to $output\n"; |
168 | | - |
169 | | - if (!($fp = fopen($output, "w+"))) { |
170 | | - die("could not open putput input"); |
171 | 152 | } |
| 153 | + if ( !( $fileHandle = fopen( $output, "w+" ) ) ) { |
| 154 | + die( "could not open output file" ); |
| 155 | + } |
172 | 156 | |
173 | | - fwrite($fp, $this->output); |
174 | | - fclose($fp); |
| 157 | + // |
| 158 | + fwrite( $fileHandle, $this->output ); |
| 159 | + fclose( $fileHandle ); |
175 | 160 | |
176 | 161 | } |
177 | 162 | |