r85043 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r85042‎ | r85043 | r85044 >
Date:20:06, 30 March 2011
Author:krinkle
Status:deferred
Tags:
Comment:
sync with TS wc
Modified paths:
  • /trunk/tools/ToolserverI18N/TsIntuition.php (modified) (history)
  • /trunk/tools/ToolserverI18N/public_html/index.php (modified) (history)

Diff [purge]

Index: trunk/tools/ToolserverI18N/TsIntuition.php
@@ -30,7 +30,7 @@
3131 private $localBaseDir = __DIR__; // to be moved to p_i18n
3232
3333 private static $registeredTextdomains = array(
34 - 'General' => 'General.i18n.php',
 34+ 'general' => 'General.i18n.php',
3535 'Getwikiapi' => 'Getwikiapi.i18n.php',
3636 'Jarry' => 'Jarry.i18n.php',
3737 'Svgtranslate' => 'Svgtranslate.i18n.php',
@@ -53,7 +53,7 @@
5454 private $paramNames = array( 'userlang' => 'userlang' );
5555
5656 // Here everything will be stored as arrays in arrays
57 - // Such as: $messageBlob['textdomain']['messagename']['langcode'] = 'Message string';
 57+ // Such as: $messageBlob['Textdomain']['messagename']['langcode'] = 'Message string';
5858 private $messageBlob = array();
5959
6060 // $loadedTextdomains[0] = 'general';
@@ -131,7 +131,7 @@
132132 // Otherwise defaults to 'general'. See also documentation of msg()
133133 // First character is case-insensitive
134134 if ( isset( $options['domain'] ) ) {
135 - $this->setDomain( trim( lcfirst( $options['domain'] ) ) );
 135+ $this->setDomain( $options['domain'] );
136136 }
137137
138138 // TsIntuition will choose the language based on a cookie. However it
@@ -208,7 +208,7 @@
209209 }
210210
211211 /**
212 - * DOCME
 212+ * Return the currently selected text domain.
213213 * @return string
214214 */
215215 public function getDomain(){
@@ -216,11 +216,11 @@
217217 }
218218
219219 /**
220 - * DOCME
 220+ * Get an array of all registered textd domains.
221221 * @return array
222222 */
223 - public function getAllRegisteredDomain(){
224 - return $this->registeredTextdomains;
 223+ public function getAllRegisteredDomains(){
 224+ return self::$registeredTextdomains;
225225 }
226226
227227 /**
@@ -228,7 +228,7 @@
229229 * @return true
230230 */
231231 public function setDomain( $domain ) {
232 - $this->currentTextdomain = $domain;
 232+ $this->currentTextdomain = ucfirst( strtolower( $domain ) );
233233 return true;
234234 }
235235
@@ -341,29 +341,29 @@
342342 }
343343
344344 // First character of the message-key is case-insensitive.
345 - $key = trim( lcfirst( $key ) );
 345+ $key = lcfirst( $key );
 346+ $domain = ucfirst( strtolower( $options['domain'] ) );
346347
347348 // Load if not already loaded
348 - $this->loadTextdomain( $options['domain'] );
 349+ $this->loadTextdomain( $domain );
349350
350 - // In case the domainname was invalid or inexistant
351 - if ( !isset( $this->messageBlob[$options['domain']] ) ) {
 351+ // In case the domain name was invalid or inexistant
 352+ if ( !isset( $this->messageBlob[$domain] ) ) {
352353 return $this->bracketMsg( $key );
353354 }
354355
355356 // Use fallback if this message doesn't exist in the current language
356 - $lang = $this->getLangForTextdomain( $options['lang'], $options['domain'], $key );
 357+ $lang = $this->getLangForTextdomain( $options['lang'], $domain, $key );
357358
358359 // Just in case, one last check:
359 - if ( isset( $this->messageBlob[$options['domain']][$lang][$key] ) ) {
360 - // We're ok, let's grab it
361 - $msg = $this->messageBlob[$options['domain']][$lang][$key];
362 - } else {
 360+ $rawMsg = $this->rawMsg( $domain, $lang, $key );
 361+ if ( is_null( $rawMsg ) ) {
363362 // Fall back to a simple [keyname]
364363 return $this->bracketMsg( $key );
365364 }
366365
367366 /* Now that we've got the message, do post-processing. */
 367+ $msg = $rawMsg;
368368
369369 $escapeDone = false;
370370
@@ -391,6 +391,21 @@
392392 }
393393
394394 /**
 395+ * Access MessageBlob
 396+ * @param $domain
 397+ * @param $lang
 398+ * @param $key
 399+ * @return string value or null.
 400+ */
 401+ private function rawMsg( $domain, $lang, $key ) {
 402+ if ( isset( $this->messageBlob[$domain][$lang][$key] ) ) {
 403+ return $this->messageBlob[$domain][$lang][$key];
 404+ } else {
 405+ return null;
 406+ }
 407+ }
 408+
 409+ /**
395410 * Don't show [brackets] when suppressing errors.
396411 * In that case there could be message files missing and invalid language codes chosen.
397412 * Just return a somewhat readable string.
@@ -416,6 +431,8 @@
417432
418433 if ( !TsIntuitionUtil::nonEmptyStr( $domain ) ) {
419434 $domain = $this->getDomain();
 435+ } else {
 436+ $domain = ucfirst( strtolower( $domain ) );
420437 }
421438 if ( !TsIntuitionUtil::nonEmptyStr( $lang ) ) {
422439 $lang = $this->getLang();
@@ -444,8 +461,8 @@
445462 * passed right away, otherwise it looks for a suitable falback
446463 *
447464 * @param $lang string Preferred language
448 - * @param $domain string Domain to search within (the existance of this domain should be checked
449 - * before calling this function).
 465+ * @param $domain string Domain to search within (the existence of this domain should be checked
 466+ * before calling this function). Note that the domainname should've been sanatized by now.
450467 * @param $key string Key of the wanted message
451468 * @return string Language code
452469 */
@@ -517,24 +534,28 @@
518535 /* Textdomain functions
519536 * ------------------------------------------------- */
520537
 538+ /**
 539+ * Load a textdomain (if not loaded already).
 540+ *
 541+ * @param $domain string Name of the textdomain (case-insensitive)
 542+ */
521543 public function loadTextdomain( $domain ) {
522544
523545 // Generally validate input and protect against path traversal
524 - if ( !TsIntuitionUtil::nonEmptyStr( $domain ) || $domain !== trim( strtolower( $domain ) ) || strcspn( $domain, ":/\\\000" ) !== strlen( $domain ) ) {
 546+ if ( !TsIntuitionUtil::nonEmptyStr( $domain ) || strcspn( $domain, ":/\\\000" ) !== strlen( $domain ) ) {
525547 $this->errTrigger( "Invalid textdomain \"$domain\"", __METHOD__, E_NOTICE );
526548 return false;
527549 }
 550+ // Sanatize domainnames (case-insensitive)
 551+ $domain = ucfirst( strtolower( $domain ) );
528552
529553 // Already loaded ?
530554 if ( in_array( $domain, $this->loadedTextdomains ) ) {
531555 return false;
532556 }
533557
534 - $this->loadedTextdomains[] = $domain;
535 -
536558 // File exists ?
537 - $displayname = ucfirst( strtolower( $domain ) );
538 - $path = $this->localBaseDir . "/language/messages/$displayname.i18n.php";
 559+ $path = $this->localBaseDir . "/language/messages/$domain.i18n.php";
539560 if ( !file_exists( $path ) ) {
540561 $this->errTrigger( "Textdomain file not found for \"$domain\" at $path. Ignoring", __METHOD__, E_NOTICE, __FILE__, __LINE__ );
541562 return false;
@@ -576,10 +597,12 @@
577598 // If you need to add or overwrite some messages temporarily, use Itui::setMsg() or Itui::setMsgs() instead
578599 foreach ( $data['messages'] as $langcode => $messages ) {
579600 $this->availableLanguages[$langcode] = true;
580 - $this->messageBlob[$domain][$langcode] = (array)$messages;
 601+ $this->setMsgs( (array)$messages, $domain, $langcode );
581602 }
582603
 604+ $this->loadedTextdomains[] = $domain;
583605
 606+
584607 return true;
585608 }
586609
@@ -733,9 +756,9 @@
734757
735758 return true;
736759 }
737 -
738760
739761
 762+
740763 /* Other functions
741764 * ------------------------------------------------- */
742765
Index: trunk/tools/ToolserverI18N/public_html/index.php
@@ -30,6 +30,12 @@
3131 );
3232 $I18N = new TsIntuition( $opts );
3333
 34+// Load all domains so can get some statistics later on and
 35+// make sure "getAvailableLangs" is compelte
 36+foreach ( $I18N->getAllRegisteredDomains() as $domain => $file ) {
 37+ $I18N->loadTextdomain( $domain );
 38+}
 39+
3440 /**
3541 * Configuration
3642 * -------------------------------------------------

Status & tagging log