r102494 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r102493‎ | r102494 | r102495 >
Date:07:56, 9 November 2011
Author:kaldari
Status:ok (Comments)
Tags:
Comment:
style clean-up and code deobfuscation
Modified paths:
  • /trunk/extensions/cldr/rebuild.php (modified) (history)

Diff [purge]

Index: trunk/extensions/cldr/rebuild.php
@@ -16,64 +16,49 @@
1717 $DATA = "$dir/core/common/main";
1818 $OUTPUT = $dir;
1919
20 -if (isset( $options['datadir'] ) ) {
 20+if ( isset( $options['datadir'] ) ) {
2121 $DATA = $options['datadir'];
2222 }
2323
24 -if (isset( $options['outputdir'] ) ) {
 24+if ( isset( $options['outputdir'] ) ) {
2525 $OUTPUT = $options['outputdir'];
2626 }
2727
28 -$langs = Language::getLanguageNames( false );
 28+// Get an array of all MediaWiki languages ( $wgLanguageNames + $wgExtraLanguageNames )
 29+$languages = Language::getLanguageNames( false );
2930 # hack to get pt-pt too
30 -$langs['pt-pt'] = 'Foo';
31 -ksort($langs);
 31+$languages['pt-pt'] = 'Foo';
 32+ksort( $languages );
3233
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+
4641 // 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] );
4944 }
5045
5146 // 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] );
5550 }
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] );
5954 }
6055 }
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 );
6657 } else {
6758 $codeCLDR = $code;
6859 }
69 -
70 - $codeCLDR = str_replace(
71 - array( '-' ),
72 - array( '_' ),
73 - $codeCLDR
74 - );
75 -
7660 $input = "$DATA/$codeCLDR.xml";
7761
 62+ // If the file exists, parse it, otherwise display an error
7863 if ( file_exists( $input ) ) {
7964 $en = Language::factory('en');
8065 $p = new CLDRParser();
@@ -85,7 +70,7 @@
8671 $p->setAlias( false );
8772 $p->parse( $input, "$OUTPUT/" . LanguageNames::getFileName( getRealCode( $code ) ) );
8873 }
89 - } elseif (isset( $options['verbose'] ) ) {
 74+ } elseif ( isset( $options['verbose'] ) ) {
9075 echo "File $input not found\n";
9176 }
9277 }
@@ -98,7 +83,7 @@
9984 private $output = "<?php\n\$names = array(\n";
10085 private $count = 0;
10186
102 - function s($parser, $name, $attrs) {
 87+ function start( $parser, $name, $attrs ) {
10388 if ( $name === 'LANGUAGES' ) {
10489 $this->languages = true;
10590 }
@@ -108,15 +93,15 @@
10994
11095 $this->ok = false;
11196 if ( $this->languages && $name === 'LANGUAGE' ) {
112 - if (!isset($attrs["ALT"]) && !isset($attrs["DRAFT"])) {
 97+ if ( !isset($attrs["ALT"] ) && !isset( $attrs["DRAFT"] ) ) {
11398 $this->ok = true;
114 - $type = str_replace( '_', '-', strtolower($attrs['TYPE']));
 99+ $type = str_replace( '_', '-', strtolower($attrs['TYPE'] ) );
115100 $this->output .= "'$type' => '";
116101 }
117102 }
118103 }
119104
120 - function e($parser, $name) {
 105+ function end( $parser, $name ) {
121106 if ( $name === 'LANGUAGES' ) {
122107 $this->languages = false;
123108 $this->ok = false;
@@ -125,52 +110,52 @@
126111 if ( $name === 'ALIAS' ) {
127112 return;
128113 }
129 - if (!$this->ok) return;
 114+ if ( !$this->ok ) return;
130115 $this->output .= "',\n";
131116 }
132117
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 ) );
137122 $this->count++;
138123 }
139124
140 - function parse($input, $output) {
 125+ function parse( $input, $output ) {
141126
142127 $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" );
147132 }
148133
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+ }
155140 }
156 - xml_parser_free($xml_parser);
 141+ xml_parser_free( $xml_parser );
157142
158 - fclose($fp);
 143+ fclose( $fileHandle );
159144
160145 if ( !$this->count ) { return; }
161146
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 ) {
165149 echo "Wrote $this->count entry to $output\n";
166 - else
 150+ } else {
167151 echo "Wrote $this->count entries to $output\n";
168 -
169 - if (!($fp = fopen($output, "w+"))) {
170 - die("could not open putput input");
171152 }
 153+ if ( !( $fileHandle = fopen( $output, "w+" ) ) ) {
 154+ die( "could not open output file" );
 155+ }
172156
173 - fwrite($fp, $this->output);
174 - fclose($fp);
 157+ //
 158+ fwrite( $fileHandle, $this->output );
 159+ fclose( $fileHandle );
175160
176161 }
177162

Follow-up revisions

RevisionCommit summaryAuthorDate
r102495follow-up to r102494 - a little bit more clean-upkaldari08:21, 9 November 2011

Comments

#Comment by Kaldari (talk | contribs)   08:05, 9 November 2011

There were a couple sections of code that didn't actually accomplish anything, so I just got rid of them. Mainly the part where $codePartStr was reproduced as $codePart.

Status & tagging log