r36717 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r36716‎ | r36717 | r36718 >
Date:09:58, 27 June 2008
Author:nikerabbit
Status:old
Tags:
Comment:
* Split class to body while to speed up parsing
Modified paths:
  • /trunk/extensions/cldr/LanguageNames.body.php (added) (history)
  • /trunk/extensions/cldr/LanguageNames.php (modified) (history)

Diff [purge]

Index: trunk/extensions/cldr/LanguageNames.php
@@ -1,18 +1,19 @@
22 <?php
33 if (!defined('MEDIAWIKI')) die();
44 /**
5 - * An extension which provised localised language names for other extensions
 5+ * An extension which provised localised language names for other extensions.
66 *
77 * @addtogroup Extensions
 8+ * @file
89 *
910 * @author Niklas Laxström
10 - * @copyright Copyright © 2007, Niklas Laxström
 11+ * @copyright Copyright © 2007-2008, Niklas Laxström
1112 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
1213 */
1314
1415 $wgExtensionCredits['other'][] = array(
15 - 'name' => 'LanguageNames',
16 - 'version' => '1.2',
 16+ 'name' => 'Language Names',
 17+ 'version' => '1.3',
1718 'author' => 'Niklas Laxström',
1819 'url' => 'http://unicode.org/cldr/repository_access.html',
1920 'description' => 'Extension which provides localised language names',
@@ -21,98 +22,4 @@
2223
2324 $dir = dirname(__FILE__) . '/';
2425 $wgExtensionMessagesFiles['cldr'] = $dir . 'LanguageNames.i18n.php';
25 -
26 -class LanguageNames {
27 -
28 - private static $cache = array();
29 -
30 - const FALLBACK_NATIVE = 0;
31 - const FALLBACK_NORMAL = 1;
32 - const LIST_MW_SUPPORTED = 0;
33 - const LIST_MW = 1;
34 - const LIST_MW_AND_CLDR = 2;
35 -
36 -
37 - public static function getNames( $code, $fallback = self::FALLBACK_NATIVE, $list = self::LIST_MW ) {
38 - $xx = self::loadLanguage( $code );
39 - $native = Language::getLanguageNames( $list === self::LIST_MW_SUPPORTED );
40 -
41 - if ( $fallback === self::FALLBACK_NATIVE ) {
42 - $names = array_merge( $native, $xx );
43 - } elseif ( $fallback === self::FALLBACK_NORMAL ) {
44 - $fallback = $code;
45 - $fb = $xx;
46 - while ( $fallback = Language::getFallbackFor( $fallback ) ) {
47 - /* Over write the things in fallback with what we have already */
48 - $fb = array_merge( self::loadLanguage( $fallback ), $fb );
49 - }
50 -
51 - /* Add native names for codes that are not in cldr */
52 - $names = array_merge( $native, $fb );
53 -
54 - /* Special case for native name for $code language, which is already
55 - * provided by MediaWiki.
56 - */
57 - if ( isset( $native[$code] ) ) {
58 - $names[$code] = $native[$code];
59 - }
60 - } else {
61 - throw new MWException( "Invalid value for 2:\$fallback in ".__METHOD__ );
62 - }
63 -
64 - switch ( $list ) {
65 - case self::LIST_MW:
66 - case self::LIST_MW_SUPPORTED:
67 - /* Remove entries that are not in fb */
68 - $names = array_intersect_key( $names, $native );
69 - /* And fall to the return */
70 - case self::LIST_MW_AND_CLDR:
71 - return $names;
72 - default:
73 - throw new MWException( "Invalid value for 3:\$list in ".__METHOD__ );
74 - }
75 -
76 - }
77 -
78 - private static function loadLanguage( $code ) {
79 - if ( !isset(self::$cache[$code]) ) {
80 -
81 - /** Load override for wrong or missing entries in cldr */
82 - $override = dirname(__FILE__) . '/' . self::getOverrideFileName( $code );
83 - if ( file_exists( $override ) ) {
84 - $names = false;
85 - require( $override );
86 - if ( is_array( $names ) ) {
87 - self::$cache[$code] = $names;
88 - }
89 - }
90 -
91 - $filename = dirname(__FILE__) . '/' . self::getFileName( $code );
92 - if ( file_exists( $filename ) ) {
93 - $names = false;
94 - require( $filename );
95 - if ( is_array( $names ) ) {
96 - if ( isset(self::$cache[$code]) ) {
97 - self::$cache[$code] = self::$cache[$code] + $names; # Don't override
98 - } else {
99 - self::$cache[$code] = $names; # No override list
100 - }
101 - }
102 - } else {
103 - wfDebug( __METHOD__ . ": Unable to load language names for $filename\n" );
104 - }
105 - }
106 -
107 - return isset( self::$cache[$code] ) ? self::$cache[$code] : array();
108 - }
109 -
110 - public static function getFileName( $code ) {
111 - return Language::getFileName( "LanguageNames", $code, '.php' );
112 - }
113 -
114 - public static function getOverrideFileName( $code ) {
115 - return Language::getFileName( "LocalNames", $code, '.php' );
116 - }
117 -
118 -
119 -}
 26+$wgAutoloadClasses['LanguageNames'] = $dir . 'LanguageNames.body.php';
\ No newline at end of file
Index: trunk/extensions/cldr/LanguageNames.body.php
@@ -0,0 +1,103 @@
 2+<?php
 3+
 4+/**
 5+ * A class for querying translated language names from CLDR data.
 6+ *
 7+ * @author Niklas Laxström
 8+ * @copyright Copyright © 2007-2008, Niklas Laxström
 9+ * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
 10+ */
 11+class LanguageNames {
 12+
 13+ private static $cache = array();
 14+
 15+ const FALLBACK_NATIVE = 0;
 16+ const FALLBACK_NORMAL = 1;
 17+ const LIST_MW_SUPPORTED = 0;
 18+ const LIST_MW = 1;
 19+ const LIST_MW_AND_CLDR = 2;
 20+
 21+
 22+ public static function getNames( $code, $fallback = self::FALLBACK_NATIVE, $list = self::LIST_MW ) {
 23+ $xx = self::loadLanguage( $code );
 24+ $native = Language::getLanguageNames( $list === self::LIST_MW_SUPPORTED );
 25+
 26+ if ( $fallback === self::FALLBACK_NATIVE ) {
 27+ $names = array_merge( $native, $xx );
 28+ } elseif ( $fallback === self::FALLBACK_NORMAL ) {
 29+ $fallback = $code;
 30+ $fb = $xx;
 31+ while ( $fallback = Language::getFallbackFor( $fallback ) ) {
 32+ /* Over write the things in fallback with what we have already */
 33+ $fb = array_merge( self::loadLanguage( $fallback ), $fb );
 34+ }
 35+
 36+ /* Add native names for codes that are not in cldr */
 37+ $names = array_merge( $native, $fb );
 38+
 39+ /* Special case for native name for $code language, which is already
 40+ * provided by MediaWiki.
 41+ */
 42+ if ( isset( $native[$code] ) ) {
 43+ $names[$code] = $native[$code];
 44+ }
 45+ } else {
 46+ throw new MWException( "Invalid value for 2:\$fallback in ".__METHOD__ );
 47+ }
 48+
 49+ switch ( $list ) {
 50+ case self::LIST_MW:
 51+ case self::LIST_MW_SUPPORTED:
 52+ /* Remove entries that are not in fb */
 53+ $names = array_intersect_key( $names, $native );
 54+ /* And fall to the return */
 55+ case self::LIST_MW_AND_CLDR:
 56+ return $names;
 57+ default:
 58+ throw new MWException( "Invalid value for 3:\$list in ".__METHOD__ );
 59+ }
 60+
 61+ }
 62+
 63+ private static function loadLanguage( $code ) {
 64+ if ( !isset(self::$cache[$code]) ) {
 65+
 66+ /** Load override for wrong or missing entries in cldr */
 67+ $override = dirname(__FILE__) . '/' . self::getOverrideFileName( $code );
 68+ if ( file_exists( $override ) ) {
 69+ $names = false;
 70+ require( $override );
 71+ if ( is_array( $names ) ) {
 72+ self::$cache[$code] = $names;
 73+ }
 74+ }
 75+
 76+ $filename = dirname(__FILE__) . '/' . self::getFileName( $code );
 77+ if ( file_exists( $filename ) ) {
 78+ $names = false;
 79+ require( $filename );
 80+ if ( is_array( $names ) ) {
 81+ if ( isset(self::$cache[$code]) ) {
 82+ self::$cache[$code] = self::$cache[$code] + $names; # Don't override
 83+ } else {
 84+ self::$cache[$code] = $names; # No override list
 85+ }
 86+ }
 87+ } else {
 88+ wfDebug( __METHOD__ . ": Unable to load language names for $filename\n" );
 89+ }
 90+ }
 91+
 92+ return isset( self::$cache[$code] ) ? self::$cache[$code] : array();
 93+ }
 94+
 95+ public static function getFileName( $code ) {
 96+ return Language::getFileName( "LanguageNames", $code, '.php' );
 97+ }
 98+
 99+ public static function getOverrideFileName( $code ) {
 100+ return Language::getFileName( "LocalNames", $code, '.php' );
 101+ }
 102+
 103+
 104+}
Property changes on: trunk/extensions/cldr/LanguageNames.body.php
___________________________________________________________________
Added: svn:eol-style
1105 + native

Status & tagging log