r69133 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r69132‎ | r69133 | r69134 >
Date:08:35, 7 July 2010
Author:philip
Status:ok
Tags:
Comment:
Clean up LanguageConverter.php.
Modified paths:
  • /trunk/phase3/languages/LanguageConverter.php (modified) (history)

Diff [purge]

Index: trunk/phase3/languages/LanguageConverter.php
@@ -51,21 +51,14 @@
5252 $variantfallbacks = array(),
5353 $flags = array(),
5454 $manualLevel = array() ) {
 55+ global $wgDisabledVariants, $wgLanguageNames;
5556 $this->mLangObj = $langobj;
5657 $this->mMainLanguageCode = $maincode;
57 -
58 - global $wgDisabledVariants;
59 - $this->mVariants = array();
60 - foreach ( $variants as $variant ) {
61 - if ( !in_array( $variant, $wgDisabledVariants ) ) {
62 - $this->mVariants[] = $variant;
63 - }
64 - }
 58+ $this->mVariants = array_diff( $variants, $wgDisabledVariants );
6559 $this->mVariantFallbacks = $variantfallbacks;
66 - global $wgLanguageNames;
6760 $this->mVariantNames = $wgLanguageNames;
6861 $this->mCacheKey = wfMemcKey( 'conversiontables', $maincode );
69 - $f = array(
 62+ $defaultflags = array(
7063 // 'S' show converted text
7164 // '+' add rules for alltext
7265 // 'E' the gave flags is error
@@ -79,7 +72,7 @@
8073 // (but no display in placed code )
8174 'N' => 'N' // current variant name
8275 );
83 - $this->mFlags = array_merge( $f, $flags );
 76+ $this->mFlags = array_merge( $defaultflags, $flags );
8477 foreach ( $this->mVariants as $v ) {
8578 if ( array_key_exists( $v, $manualLevel ) ) {
8679 $this->mManualLevel[$v] = $manualLevel[$v];
@@ -91,9 +84,12 @@
9285 }
9386
9487 /**
95 - * @public
 88+ * Get all valid variants.
 89+ * Call this instead of using $this->mVariants directly.
 90+ *
 91+ * @return Array: contains all valid variants
9692 */
97 - function getVariants() {
 93+ public function getVariants() {
9894 return $this->mVariants;
9995 }
10096
@@ -104,27 +100,27 @@
105101 * when zh-sg is preferred but not defined, we will pick zh-hans
106102 * in this case. Right now this is only used by zh.
107103 *
108 - * @param $v String: the language code of the variant
109 - * @return string array The code of the fallback language or false if there
110 - * is no fallback
 104+ * @param $variant String: the language code of the variant
 105+ * @return String: The code of the fallback language or the
 106+ * main code if there is no fallback
111107 */
112 - public function getVariantFallbacks( $v ) {
113 - if ( isset( $this->mVariantFallbacks[$v] ) ) {
114 - return $this->mVariantFallbacks[$v];
 108+ public function getVariantFallbacks( $variant ) {
 109+ if ( isset( $this->mVariantFallbacks[$variant] ) ) {
 110+ return $this->mVariantFallbacks[$variant];
115111 }
116112 return $this->mMainLanguageCode;
117113 }
118114
119115 /**
120116 * Get the title produced by the conversion rule.
121 - * @returns String
 117+ * @return String: The converted title text
122118 */
123 - function getConvRuleTitle() {
 119+ public function getConvRuleTitle() {
124120 return $this->mConvRuleTitle;
125121 }
126122
127123 /**
128 - * Get preferred language variants.
 124+ * Get preferred language variant.
129125 * @param $fromUser Boolean: get it from $wgUser's preferences
130126 * @param $fromHeader Boolean: get it from Accept-Language
131127 * @return String: the preferred language code
@@ -158,12 +154,13 @@
159155
160156 /**
161157 * Validate the variant
162 - * @param $v String: the variant to validate
 158+ * @param $variant String: the variant to validate
163159 * @return Mixed: returns the variant if it is valid, null otherwise
164160 */
165 - function validateVariant( $v = null ) {
166 - if ( $v !== null && in_array( $v, $this->mVariants ) ) {
167 - return $v;
 161+ protected function validateVariant( $variant = null ) {
 162+ if ( $variant !== null &&
 163+ in_array( $variant, $this->mVariants ) ) {
 164+ return $variant;
168165 }
169166 return null;
170167 }
@@ -173,7 +170,7 @@
174171 *
175172 * @return Mixed: variant if one found, false otherwise.
176173 */
177 - function getURLVariant() {
 174+ protected function getURLVariant() {
178175 global $wgRequest;
179176 $ret = null;
180177
@@ -196,7 +193,7 @@
197194 *
198195 * @return Mixed: variant if one found, false otherwise.
199196 */
200 - function getUserVariant() {
 197+ protected function getUserVariant() {
201198 global $wgUser;
202199 $ret = null;
203200
@@ -226,7 +223,7 @@
227224 *
228225 * @return Mixed: variant if one found, false otherwise.
229226 */
230 - function getHeaderVariant() {
 227+ protected function getHeaderVariant() {
231228 global $wgRequest;
232229 $ret = null;
233230
@@ -236,7 +233,6 @@
237234
238235 // see if some supported language variant is set in the
239236 // http header.
240 -
241237 $acceptLanguage = $wgRequest->getHeader( 'Accept-Language' );
242238 if ( !$acceptLanguage ) {
243239 return null;
@@ -299,9 +295,8 @@
300296 * or '<span title="text" ... '
301297 *
302298 * @return String like ' alt="yyyy"' or ' title="yyyy"'
303 - * @private
304299 */
305 - function captionConvert( $matches ) {
 300+ protected function captionConvert( $matches ) {
306301 $toVariant = $this->getPreferredVariant();
307302 $title = $matches[1];
308303 $text = $matches[2];
@@ -314,13 +309,15 @@
315310
316311 /**
317312 * Dictionary-based conversion.
 313+ * This function would not parse the conversion rules.
 314+ * If you want to parse rules, try to use convert() or
 315+ * convertTo().
318316 *
319317 * @param $text String: the text to be converted
320318 * @param $toVariant String: the target language code
321319 * @return String: the converted text
322 - * @private
323320 */
324 - function autoConvert( $text, $toVariant = false ) {
 321+ public function autoConvert( $text, $toVariant = false ) {
325322 $fname = 'LanguageConverter::autoConvert';
326323
327324 wfProfileIn( $fname );
@@ -399,15 +396,14 @@
400397
401398 /**
402399 * Translate a string to a variant.
403 - * Doesn't process markup or do any of that other stuff, for that use
404 - * convert().
 400+ * Doesn't parse rules or do any of that other stuff, for that use
 401+ * convert() or convertTo().
405402 *
406403 * @param $text String: text to convert
407404 * @param $variant String: variant language code
408405 * @return String: translated text
409 - * @private
410406 */
411 - function translate( $text, $variant ) {
 407+ protected function translate( $text, $variant ) {
412408 wfProfileIn( __METHOD__ );
413409 // If $text is empty or only includes spaces, do nothing
414410 // Otherwise translate it
@@ -422,10 +418,10 @@
423419 }
424420
425421 /**
426 - * Convert text to all supported variants.
 422+ * Call translate() to convert text to all valid variants.
427423 *
428424 * @param $text String: the text to be converted
429 - * @return Array of string
 425+ * @return Array: variant => converted text
430426 */
431427 public function autoConvertToAllVariants( $text ) {
432428 $fname = 'LanguageConverter::autoConvertToAllVariants';
@@ -447,7 +443,7 @@
448444 * Convert link text to all supported variants.
449445 *
450446 * @param $text String: the text to be converted
451 - * @return Array of string
 447+ * @return Array: variant => converted text
452448 */
453449 public function convertLinkToAllVariants( $text ) {
454450 if ( !$this->mTablesLoaded ) {

Status & tagging log