diff --git a/UniversalLanguageSelector.hooks.php b/UniversalLanguageSelector.hooks.php index ae97eb67..ccde16a5 100644 --- a/UniversalLanguageSelector.hooks.php +++ b/UniversalLanguageSelector.hooks.php @@ -19,15 +19,17 @@ */ class UniversalLanguageSelectorHooks { + /** + * Whether ULS user toolbar (language selection and settings) is enabled. + * @return bool + */ public static function isToolbarEnabled( $user ) { global $wgULSEnable, $wgULSEnableAnon; if ( !$wgULSEnable ) { return false; } - if ( !$wgULSEnableAnon ) { - if ( $user->isAnon() ) { - return false; - } + if ( !$wgULSEnableAnon && $user->isAnon() ) { + return false; } return true; } @@ -141,7 +143,7 @@ class UniversalLanguageSelectorHooks { * @return bool */ public static function getLanguage( $user, &$code, $context = null ) { - global $wgUser, $wgRequest, $wgULSLanguageDetection; + global $wgUser, $wgRequest, $wgULSAnonCanChangeLanguage, $wgULSLanguageDetection; if ( !self::isToolbarEnabled( $user ) ) { return true; } @@ -167,27 +169,40 @@ class UniversalLanguageSelectorHooks { return true; } - $languageToUse = null; - if ( self::isSupportedLanguage( $languageToSave ) ) { - if ( $user->isAnon() ) { - $request->response()->setcookie( 'language', $languageToSave ); - } else { + // Registered users - simple + if ( !$user->isAnon() ) { + // Language change + if ( self::isSupportedLanguage( $languageToSave ) ) { $user->setOption( 'language', $languageToSave ); $user->saveSettings(); + // Apply immediately + $code = $languageToSave; } - $languageToUse = $languageToSave; + // Otherwise just use what is stored in preferences + return true; } - // Load from cookie unless overriden - if ( $languageToUse === null && $user->isAnon() ) { - $languageToUse = $request->getCookie( 'language' ); + // Logged out users - less simple + if ( !$wgULSAnonCanChangeLanguage ) { + return true; } - // Check whether we got valid language from store or - // explicit language change. + // Language change + if ( self::isSupportedLanguage( $languageToSave ) ) { + $request->response()->setcookie( 'language', $languageToSave ); + $code = $languageToSave; + return true; + } + + // Try cookie + $languageToUse = $request->getCookie( 'language' ); if ( self::isSupportedLanguage( $languageToUse ) ) { $code = $languageToUse; - } elseif ( $user->isAnon() && $wgULSLanguageDetection ) { + return true; + } + + // As last resort, try Accept-Language headers if allowed + if ( $wgULSLanguageDetection ) { $preferred = $request->getAcceptLang(); $default = self::getDefaultLanguage( $preferred ); if ( $default !== '' ) { @@ -195,7 +210,7 @@ class UniversalLanguageSelectorHooks { } } - // Fall back to content language + // Fall back to other hooks or content language return true; } @@ -205,10 +220,14 @@ class UniversalLanguageSelectorHooks { * @return bool */ public static function addConfig( &$vars ) { - global $wgULSGeoService, $wgULSIMEEnabled; - $vars['wgULSGeoService'] = $wgULSGeoService; + global $wgULSGeoService, $wgULSIMEEnabled, $wgULSPosition, + $wgULSAnonCanChangeLanguage; + // Place constant stuff here (not depending on request context) + $vars['wgULSGeoService'] = $wgULSGeoService; $vars['wgULSIMEEnabled'] = $wgULSIMEEnabled; + $vars['wgULSPosition'] = $wgULSPosition; + $vars['wgULSAnonCanChangeLanguage'] = $wgULSAnonCanChangeLanguage; // ULS is localized using jquery.i18n library. Unless it knows // the localized locales, it can create 404 response. To avoid that, @@ -253,12 +272,11 @@ class UniversalLanguageSelectorHooks { * @return bool */ public static function addVariables( &$vars, OutputPage $out ) { + // Place request context dependent stuff here $vars['wgULSLanguages'] = Language::fetchLanguageNames( $out->getLanguage()->getCode(), 'mwfile' ); $vars['wgULSAcceptLanguageList'] = array_keys( $out->getRequest()->getAcceptLang() ); - global $wgULSPosition; - $vars['wgULSPosition'] = $wgULSPosition; return true; } @@ -284,6 +302,10 @@ class UniversalLanguageSelectorHooks { return true; } + if ( !self::isToolbarEnabled( $skin->getUser() ) ) { + return true; + } + // A dummy link, just to make sure that the section appears $template->data['language_urls'][] = array( 'href' => '#', diff --git a/UniversalLanguageSelector.php b/UniversalLanguageSelector.php index b5cf0490..1cb2c2e6 100644 --- a/UniversalLanguageSelector.php +++ b/UniversalLanguageSelector.php @@ -25,7 +25,7 @@ if ( !defined( 'MEDIAWIKI' ) ) { /** * Version number used in extension credits and in other placed where needed. */ -define( 'ULS_VERSION', '2013-04-01' ); +define( 'ULS_VERSION', '2013-04-27' ); $wgExtensionCredits['other'][] = array( 'path' => __FILE__, @@ -57,32 +57,53 @@ $wgExtensionCredits['other'][] = array( $wgULSGeoService = 'http://freegeoip.net/json/'; /** - * IME system of ULS can be disabled by setting this value false; - */ -$wgULSIMEEnabled = true; - -/** - * Try to use preferred interface language for anonymous users. - * Do not use if you are caching anonymous page views without - * taking Accept-Language into account. - */ -$wgULSLanguageDetection = true; - -/** - * Enable language selection. If language selection is disabled, the classes - * and RL modules are registered for the use of other extensions, but no - * language selection toolbar is shown, and it will not be possible to change - * the interface language using a cookie. + * Enable language selection, input methods and webfonts for everyone, unless + * the behavior is overridden by the configuration variables below. + * + * Even if false the classes and resource loader modules are registered for the + * use of other extensions. Language changing via cookie or setlang query + * parameter is not possible. */ $wgULSEnable = true; /** - * Enable ULS language selection for anonymous users. Equivalent to $wgULSEnable - * except that it only applies to anonymous users. Setting this to false will - * avoid breaking Squid caches (see bug 41451). + * Equivalent to $wgULSEnable for anonymous users only. + * + * Does not have any effect if $wgULSEnable is false. */ $wgULSEnableAnon = true; +/** + * Allow anonymous users to change language with cookie and setlang + * query param. + + * Do not use if you are caching anonymous page views without + * taking cookies into account. + * + * Does not have any effect if either of $wgULSEnable or + * $wgULSEnableAnon is set to false. + * + * @since 2013.04 + */ +$wgULSAnonCanChangeLanguage = true; + +/** + * Try to use preferred interface language for anonymous users. + * + * Do not use if you are caching anonymous page views without + * taking Accept-Language into account. + * + * Does not have any effect if any of $wgULSEnable, $wgULSEnableAnon + * or $wgULSAnonCanChangeLanguage is set to false. + */ +$wgULSLanguageDetection = true; + +/** + * Disable the input methods feature for all users by default. Can still + * be enabled manually by the user. + */ +$wgULSIMEEnabled = true; + /** * The location and the form of the language selection trigger. * The possible values are: