r106546 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r106545‎ | r106546 | r106547 >
Date:01:49, 18 December 2011
Author:mah
Status:reverted (Comments)
Tags:
Comment:
Bug 30047 - Language selection does not change the Content-language
Patch from
Author: Harald Stowasser
Modified paths:
  • /trunk/extensions/LanguageSelector/LanguageSelector.php (modified) (history)

Diff [purge]

Index: trunk/extensions/LanguageSelector/LanguageSelector.php
@@ -32,6 +32,7 @@
3333 define( 'LANGUAGE_SELECTOR_USE_CONTENT_LANG', 0 ); #no detection
3434 define( 'LANGUAGE_SELECTOR_PREFER_CONTENT_LANG', 1 ); #use content language if accepted by the client
3535 define( 'LANGUAGE_SELECTOR_PREFER_CLIENT_LANG', 2 ); #use language most preferred by the client
 36+define( 'LANGUAGE_SELECTOR_ADJUST_CONTENT', 3 );
3637
3738 /**
3839 * Language detection mode for anonymous visitors.
@@ -89,7 +90,32 @@
9091 $dir = dirname(__FILE__) . '/';
9192 $wgExtensionMessagesFiles['LanguageSelector'] = $dir . 'LanguageSelector.i18n.php';
9293 $wgJSAutoloadClasses['LanguageSelector'] = 'extensions/LanguageSelector/LanguageSelector.js';
 94+$wgHooks['BeforeInitialize'][] = 'wfLanguageSelectorAdjustContent';
9395
 96+function wfLanguageSelectorAdjustContent( &$title, &$article, &$output, &$user, $request, $this ) {
 97+ global $wgLanguageSelectorRequestedLanguage, $wgLanguageNames, $wgLanguageSelectorAdjustContent;
 98+ if( isset($wgLanguageSelectorRequestedLanguage) && strlen($wgLanguageSelectorRequestedLanguage)>0){
 99+ if( isset($wgLanguageSelectorAdjustContent) && $wgLanguageSelectorAdjustContent=== LANGUAGE_SELECTOR_ADJUST_CONTENT){
 100+ $langstr="/".$wgLanguageSelectorRequestedLanguage;
 101+ $searchstr="#/(". implode('|',array_keys($wgLanguageNames)) ."$)#";
 102+ if(preg_match($searchstr,$title->mTextform)){
 103+ $title->mTextform =preg_replace($searchstr,$langstr, $title->mTextform);
 104+ $title->mUrlform =preg_replace($searchstr,$langstr, $title->mUrlform);
 105+ $title->mDbkeyform =preg_replace($searchstr,$langstr, $title->mDbkeyform);
 106+ $title->mUserCaseDBKey =preg_replace($searchstr,$langstr, $title->mUserCaseDBKey);
 107+ }else{
 108+ if($_GET['setlang']== $wgLanguageSelectorRequestedLanguage){
 109+ $title->mTextform.=$langstr;
 110+ $title->mUrlform.=$langstr;
 111+ $title->mDbkeyform.=$langstr;
 112+ $title->mUserCaseDBKey.=$langstr;
 113+ }
 114+ }
 115+ }
 116+ }
 117+ return true;
 118+}
 119+
94120 /**
95121 * @param $parser Parser
96122 * @return bool
@@ -403,4 +429,3 @@
404430
405431 return $html;
406432 }
407 -

Follow-up revisions

RevisionCommit summaryAuthorDate
r107380Revert r106546 based on IAlex's CR comments. I've already updated...mah13:43, 27 December 2011

Comments

#Comment by IAlex (talk | contribs)   16:16, 18 December 2011

I don't see what this has to do in the LanguageSelector extension, since this extension is meant to allow anonymous users to select their interface language (or even have it directly set from the Accept-Language header), but not to alter the Title object, this is more for Extension:Polyglot or similar.

Some other notes:

  • $wgLanguageSelectorRequestedLanguage doesn't exist anymore
  • $wgLanguageSelectorAdjustContent is not defined when including the file (in other words this is a register_globals vulnerability) and it would be better to be a boolean
  • Altering those members variables of the Title object is really not a good since other member variable may depend on the page title (restrictions, etc.); it would much better to create a new Title object; also altering it in the BeforeInitialize hook is too late, if file cache is enabled, it will be loaded based on the older title from MediaWiki::main().

Status & tagging log