From 44c199ea0c01fabea8001f2a6664f3cc352a662e Mon Sep 17 00:00:00 2001 From: Roan Kattouw Date: Mon, 7 Jun 2010 18:53:30 +0000 Subject: [PATCH] API: Add Accept-Language echoer, useful for JS that wants to grab this (not available in JS through conventional means, can't embed it in the page because of Squid caching). Stuck this in meta=userinfo because it doesn't really fit anywhere and userinfo is the only module that's slightly, tangentially related. --- includes/api/ApiQueryUserInfo.php | 42 +++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/includes/api/ApiQueryUserInfo.php b/includes/api/ApiQueryUserInfo.php index 3f4903beb8..5fa4059695 100644 --- a/includes/api/ApiQueryUserInfo.php +++ b/includes/api/ApiQueryUserInfo.php @@ -121,8 +121,48 @@ class ApiQueryUserInfo extends ApiQueryBase { $vals['emailauthenticated'] = wfTimestamp( TS_ISO_8601, $auth ); } } + + if ( isset( $this->prop['acceptlang'] ) ) { + $vals['acceptlang'] = $this->getAcceptLang(); + $result->setIndexedTagName( $vals['acceptlang'], 'lang' ); + } return $vals; } + + protected function getAcceptLang() { + // Modified version of code found at http://www.thefutureoftheweb.com/blog/use-accept-language-header + if ( !isset( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) ) { + return array(); + } + + // Break up string into pieces (languages and q factors) + $lang_parse = null; + preg_match_all( '/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0(\.[0-9]+))?)?/i', + $_SERVER['HTTP_ACCEPT_LANGUAGE'], $lang_parse ); + + if ( !count( $lang_parse[1] ) ) { + return array(); + } + // Create a list like "en" => 0.8 + $langs = array_combine( $lang_parse[1], $lang_parse[4] ); + // Set default q factor to 1 + foreach ( $langs as $lang => $val ) { + if ( $val === '' ) { + $langs[$lang] = 1; + } + } + // Sort list + arsort( $langs, SORT_NUMERIC ); + + // Format for API output + $retval = array(); + foreach ( $langs as $lang => $val ) { + $r = array( 'q' => $val ); + ApiResult::setContent( $r, $lang ); + $retval[] = $r; + } + return $retval; + } protected function getRateLimits() { global $wgUser, $wgRateLimits; @@ -174,6 +214,7 @@ class ApiQueryUserInfo extends ApiQueryBase { 'editcount', 'ratelimits', 'email', + 'acceptlang', ) ) ); @@ -192,6 +233,7 @@ class ApiQueryUserInfo extends ApiQueryBase { ' editcount - adds the current user\'s edit count', ' ratelimits - lists all rate limits applying to the current user', ' email - adds the user\'s email address and email authentication date', + ' acceptlang - echoes the Accept-Language header sent by the client in a structured format', ) ); } -- 2.20.1