r70383 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r70382‎ | r70383 | r70384 >
Date:13:23, 3 August 2010
Author:platonides
Status:ok
Tags:
Comment:
Follow up r70356.
Improve WebRequest getAcceptLang(): Add support for q=0 language and the special range "*", always return the language codes in lowercase.
Modified paths:
  • /trunk/phase3/includes/WebRequest.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/WebRequest.php
@@ -730,6 +730,8 @@
731731 /**
732732 * Parse the Accept-Language header sent by the client into an array
733733 * @return array( languageCode => q-value ) sorted by q-value in descending order
 734+ * May contain the "language" '*', which applies to languages other than those explicitly listed.
 735+ * This is aligned with rfc2616 section 14.4
734736 */
735737 public function getAcceptLang() {
736738 // Modified version of code found at http://www.thefutureoftheweb.com/blog/use-accept-language-header
@@ -738,22 +740,29 @@
739741 return array();
740742 }
741743
 744+ // Return the language codes in lower case
 745+ $acceptLang = strtolower( $acceptLang );
 746+
742747 // Break up string into pieces (languages and q factors)
743748 $lang_parse = null;
744 - preg_match_all( '/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0(\.[0-9]+))?)?/i',
 749+ preg_match_all( '/([a-z]{1,8}(-[a-z]{1,8})?|\*)\s*(;\s*q\s*=\s*(1|0(\.[0-9]+)?)?)?/',
745750 $acceptLang, $lang_parse );
746751
747752 if ( !count( $lang_parse[1] ) ) {
748753 return array();
749754 }
 755+
750756 // Create a list like "en" => 0.8
751757 $langs = array_combine( $lang_parse[1], $lang_parse[4] );
752758 // Set default q factor to 1
753759 foreach ( $langs as $lang => $val ) {
754760 if ( $val === '' ) {
755761 $langs[$lang] = 1;
 762+ } else if ( $val == 0 ) {
 763+ unset($langs[$lang]);
756764 }
757765 }
 766+
758767 // Sort list
759768 arsort( $langs, SORT_NUMERIC );
760769 return $langs;

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r70356Follow-up to r64587:...maxsem20:16, 2 August 2010

Status & tagging log