r36461 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r36460‎ | r36461 | r36462 >
Date:15:34, 19 June 2008
Author:minuteelectron
Status:old
Tags:
Comment:
* Merge LanguageCodes class into Babel class.
* Remove ending newline from Babel class.
Modified paths:
  • /trunk/extensions/Babel/Babel.class.php (modified) (history)
  • /trunk/extensions/Babel/Babel.php (modified) (history)
  • /trunk/extensions/Babel/LanguageCodes.class.php (deleted) (history)

Diff [purge]

Index: trunk/extensions/Babel/LanguageCodes.class.php
@@ -1,155 +0,0 @@
2 -<?php
3 -
4 -/**
5 - * LanguageCodes class for Babel extension, deals with ISO 639-1 and ISO 639-3
6 - * code checking and ordering.
7 - *
8 - * @addtogroup Extensions
9 - */
10 -
11 -class LanguageCodes {
12 -
13 - private $_codes;
14 - private $_order = array(
15 - ISO_639_1,
16 - ISO_639_3,
17 - );
18 -
19 - /**
20 - * Class constructor.
21 - *
22 - * @param array $file Array of files to load language codes from.
23 - */
24 - public function __construct( $files ) {
25 -
26 - /* Load the codes from the passed file array.
27 - */
28 - $this->_loadAll( $files );
29 -
30 - }
31 -
32 - /**
33 - * Load the language codes from an array of standards to files into the
34 - * language codes array.
35 - *
36 - * @param array $file Array of files to load language codes from.
37 - */
38 - private function _loadAll( $files ) {
39 -
40 - /* Loop through all standards.
41 - */
42 - foreach( $this->_order as $standard ) {
43 -
44 - /* Load file for the current standard.
45 - */
46 - $this->_load( $standard, $files[ $standard ] );
47 -
48 - }
49 -
50 - }
51 -
52 - /**
53 - * Load the language codes from a given file into the language codes array.
54 - *
55 - * @param const $standard Standard for the codes being loaded.
56 - * @param string $file File to load language codes from.
57 - */
58 - private function _load( $standard, $file ) {
59 -
60 - /* Include the codes file.
61 - */
62 - include( $file );
63 -
64 - /* Push the array of codes into the class method.
65 - */
66 - $this->_codes[ $standard ] = $codes;
67 -
68 - }
69 -
70 - /**
71 - * Check if the specified code is a valid language code.
72 - *
73 - * @param string $code Code to check.
74 - * @return boolean Whether or not the code is valid.
75 - */
76 - public function check( $code ) {
77 -
78 - /* Check if the specified code has a key in the codes array for each of the
79 - * standards and return result.
80 - */
81 - foreach( $this->_order as $index ) {
82 -
83 - if( array_key_exists( strtolower( $code ), $this->_codes[ $index ] ) ) {
84 - return true;
85 - }
86 -
87 - }
88 -
89 - }
90 -
91 - /**
92 - * Get the language code to use for a specific language, in the highest
93 - * ordered standard possible.
94 - *
95 - * @param string $code Code to get language code for.
96 - * @return string Correct code.
97 - */
98 - public function get( $code ) {
99 -
100 - /* Loop through all the standards trying to find the language code
101 - * specified.
102 - */
103 - foreach( $this->_order as $standard1 ) {
104 -
105 - if( array_key_exists( strtolower( $code ), $this->_codes[ $standard1 ] ) ) {
106 -
107 - /* Loop through all the standards again to find the highest
108 - * level alternate code.
109 - */
110 - foreach( $this->_order as $standard2 ) {
111 -
112 - if( $standard1 == $standard2 ) {
113 -
114 - return $code;
115 -
116 - } elseif( array_key_exists( $standard2, $this->_codes[ $standard1 ][ $code ] ) ) {
117 -
118 - return $this->_codes[ $standard1 ][ $code ][ $standard2 ];
119 -
120 - }
121 -
122 - }
123 -
124 - }
125 -
126 - }
127 -
128 - /* Nothing found, return input.
129 - */
130 - return $code;
131 -
132 - }
133 -
134 - /**
135 - * Get the name of a language in a specific language (currently only eng
136 - * supported until a index of ISO 639-1 is built with language names).
137 - *
138 - * @param string $code Code to get name for.
139 - * @param string $lang Language to get name of code in.
140 - * @return string Name of language in specified language.
141 - */
142 - public function name( $code, $lang = 'eng' ) {
143 -
144 - $code = $this->get( $code );
145 -
146 - if( array_key_exists( $code, $this->_codes[ ISO_639_3 ] ) && array_key_exists( "name_$lang", $this->_codes[ ISO_639_3 ][ $code ] ) ) {
147 - return $this->_codes[ ISO_639_3 ][ $code ][ "name_$lang" ];
148 - }
149 -
150 - /* Nothing found, return input.
151 - */
152 - return $code;
153 -
154 - }
155 -
156 -}
Index: trunk/extensions/Babel/Babel.class.php
@@ -23,22 +23,52 @@
2424 */
2525 private $_gender = self::GENDER_NEUTER;
2626
 27+ /* Array of language codes.
 28+ */
 29+ private $_codes;
 30+
 31+ /* Preferred order of ISO language code standards.
 32+ */
 33+ private $_order = array(
 34+ ISO_639_1,
 35+ ISO_639_3,
 36+ );
 37+
2738 /**
 39+ * Load the language codes from an array of standards to files into the
 40+ * language codes array.
 41+ *
 42+ * @param array $file Array of files to load language codes from.
 43+ */
 44+ public function __construct( $files ) {
 45+
 46+ /* Loop through all standards.
 47+ */
 48+ foreach( $this->_order as $standard ) {
 49+
 50+ /* Load file for the current standard.
 51+ */
 52+ $this->_loadCodes( $standard, $files[ $standard ] );
 53+
 54+ }
 55+
 56+ }
 57+
 58+ /**
2859 * Registers the parser function hook.
2960 *
3061 * @return true
3162 */
3263 static public function Setup() {
3364
34 - /* Initialise the languages code object.
 65+ /* Get the location of the language codes file.
3566 */
36 - global $wgLanguageCodes, $wgLanguageCodesFiles;
37 - $wgLanguageCodes = new LanguageCodes( $wgLanguageCodesFiles );
 67+ global $wgLanguageCodesFiles;
3868
3969 /* Initialise the Babel object.
4070 */
4171 global $wgBabel;
42 - $wgBabel = new Babel;
 72+ $wgBabel = new Babel( $wgLanguageCodesFiles );
4373
4474 /* Register the hook within the parser object.
4575 */
@@ -366,10 +396,6 @@
367397 */
368398 private function _parseParameter( $parameter ) {
369399
370 - /* Get language codes object.
371 - */
372 - global $wgLanguageCodes;
373 -
374400 /* Break up the parameter on - (which seperates it's two parts).
375401 */
376402 $chunks = explode( '-', $parameter );
@@ -387,11 +413,11 @@
388414
389415 /* Check whether the language code is valid.
390416 */
391 - if( $wgLanguageCodes->check( $chunks[ 0 ] ) ) {
 417+ if( $this->_checkCode( $chunks[ 0 ] ) ) {
392418
393419 /* Set the code for returning.
394420 */
395 - $return[ 'code' ] = $wgLanguageCodes->get( $chunks[ 0 ] );
 421+ $return[ 'code' ] = $this->_getCode( $chunks[ 0 ] );
396422
397423 /* This form defaults to level 'N'.
398424 */
@@ -416,11 +442,11 @@
417443
418444 /* Check whether the language code is valid.
419445 */
420 - if( $wgLanguageCodes->check( $chunks[ 0 ] ) ) {
 446+ if( $this->_checkCode( $chunks[ 0 ] ) ) {
421447
422448 /* Set the code for returning.
423449 */
424 - $return[ 'code' ] = $wgLanguageCodes->get( $chunks[ 0 ] );
 450+ $return[ 'code' ] = $this->_getCode( $chunks[ 0 ] );
425451
426452 } else {
427453
@@ -470,13 +496,9 @@
471497 */
472498 private function _generateBox( $code, $level ) {
473499
474 - /* Get language codes class.
475 - */
476 - global $wgLanguageCodes;
477 -
478500 /* Get code in favoured standard.
479501 */
480 - $code = $wgLanguageCodes->get( $code );
 502+ $code = $this->_getCode( $code );
481503
482504 /* Generate the text displayed on the left hand side of the
483505 * box.
@@ -508,7 +530,7 @@
509531 if( array_key_exists( $code, $names ) ) {
510532 $name = $names[ $code ];
511533 } else {
512 - $name = $wgLanguageCodes->name( $code, 'en' );
 534+ $name = $this->_nameCode( $code, 'en' );
513535 }
514536
515537 /* Generate the text displayed on the right hand side of the
@@ -718,4 +740,108 @@
719741
720742 }
721743
722 -}
 744+ /**
 745+ * Load the language codes from a given file into the language codes array.
 746+ *
 747+ * @param const $standard Standard for the codes being loaded.
 748+ * @param string $file File to load language codes from.
 749+ */
 750+ private function _loadCodes( $standard, $file ) {
 751+
 752+ /* Include the codes file.
 753+ */
 754+ include( $file );
 755+
 756+ /* Push the array of codes into the class method.
 757+ */
 758+ $this->_codes[ $standard ] = $codes;
 759+
 760+ }
 761+
 762+ /**
 763+ * Check if the specified code is a valid language code.
 764+ *
 765+ * @param string $code Code to check.
 766+ * @return boolean Whether or not the code is valid.
 767+ */
 768+ private function _checkCode( $code ) {
 769+
 770+ /* Check if the specified code has a key in the codes array for each of the
 771+ * standards and return result.
 772+ */
 773+ foreach( $this->_order as $index ) {
 774+
 775+ if( array_key_exists( strtolower( $code ), $this->_codes[ $index ] ) ) {
 776+ return true;
 777+ }
 778+
 779+ }
 780+
 781+ }
 782+
 783+ /**
 784+ * Get the language code to use for a specific language, in the highest
 785+ * ordered standard possible.
 786+ *
 787+ * @param string $code Code to get language code for.
 788+ * @return string Correct code.
 789+ */
 790+ private function _getCode( $code ) {
 791+
 792+ /* Loop through all the standards trying to find the language code
 793+ * specified.
 794+ */
 795+ foreach( $this->_order as $standard1 ) {
 796+
 797+ if( array_key_exists( strtolower( $code ), $this->_codes[ $standard1 ] ) ) {
 798+
 799+ /* Loop through all the standards again to find the highest
 800+ * level alternate code.
 801+ */
 802+ foreach( $this->_order as $standard2 ) {
 803+
 804+ if( $standard1 == $standard2 ) {
 805+
 806+ return $code;
 807+
 808+ } elseif( array_key_exists( $standard2, $this->_codes[ $standard1 ][ $code ] ) ) {
 809+
 810+ return $this->_codes[ $standard1 ][ $code ][ $standard2 ];
 811+
 812+ }
 813+
 814+ }
 815+
 816+ }
 817+
 818+ }
 819+
 820+ /* Nothing found, return input.
 821+ */
 822+ return $code;
 823+
 824+ }
 825+
 826+ /**
 827+ * Get the name of a language in a specific language (currently only eng
 828+ * supported until a index of ISO 639-1 is built with language names).
 829+ *
 830+ * @param string $code Code to get name for.
 831+ * @param string $lang Language to get name of code in.
 832+ * @return string Name of language in specified language.
 833+ */
 834+ private function _nameCode( $code, $lang = 'eng' ) {
 835+
 836+ $code = $this->_getCode( $code );
 837+
 838+ if( array_key_exists( $code, $this->_codes[ ISO_639_3 ] ) && array_key_exists( "name_$lang", $this->_codes[ ISO_639_3 ][ $code ] ) ) {
 839+ return $this->_codes[ ISO_639_3 ][ $code ][ "name_$lang" ];
 840+ }
 841+
 842+ /* Nothing found, return input.
 843+ */
 844+ return $code;
 845+
 846+ }
 847+
 848+}
\ No newline at end of file
Index: trunk/extensions/Babel/Babel.php
@@ -42,8 +42,7 @@
4343 $wgExtensionMessagesFiles[ 'Babel' ] = dirname( __FILE__ ) . '/Babel.i18n.php';
4444
4545 // Register autoload classes.
46 -$wgAutoloadClasses[ 'Babel' ] = dirname( __FILE__ ) . '/Babel.class.php';
47 -$wgAutoloadClasses[ 'LanguageCodes' ] = dirname( __FILE__ ) . '/LanguageCodes.class.php';
 46+$wgAutoloadClasses[ 'Babel' ] = dirname( __FILE__ ) . '/Babel.class.php';
4847
4948 // Definitions.
5049 define( 'ISO_639_1', 1 );

Status & tagging log