From 72ebf55665eb46839947ad2a3633cc008f2f4512 Mon Sep 17 00:00:00 2001 From: robin Date: Thu, 10 May 2012 21:46:14 +0200 Subject: [PATCH] Use Xml::languageSelector in SpecialAllmessages Improve Xml::languageSelector: add parameters to change label message and id/name of the select tag. Use languageSelector in SpecialAllmessages (less code duplication) Patchset 10 July: add tests per MaxSem; add version for new parameters and change the message parameter to require a Message object per Nikerabbit Change-Id: I7fbb29ee2dc37a2b6a5e2cfc88207a0b47b83e9b --- includes/Xml.php | 26 +++++++++++++++--------- includes/specials/SpecialAllmessages.php | 19 +++++------------ tests/phpunit/includes/XmlTest.php | 9 ++++++++ 3 files changed, 30 insertions(+), 24 deletions(-) diff --git a/includes/Xml.php b/includes/Xml.php index 6e4bb3acb9..505cb7f6bb 100644 --- a/includes/Xml.php +++ b/includes/Xml.php @@ -210,15 +210,18 @@ class Xml { * @param string $selected The language code of the selected language * @param boolean $customisedOnly If true only languages which have some content are listed * @param string $inLanguage The ISO code of the language to display the select list in (optional) + * @param array $overrideAttrs Override the attributes of the select tag (since 1.20) + * @param Message|null $msg Label message key (since 1.20) * @return array containing 2 items: label HTML and select list HTML */ - public static function languageSelector( $selected, $customisedOnly = true, $inLanguage = null ) { + public static function languageSelector( $selected, $customisedOnly = true, $inLanguage = null, $overrideAttrs = array(), Message $msg = null ) { global $wgLanguageCode; - $languages = Language::fetchLanguageNames( $inLanguage, $customisedOnly ? 'mwfile' : 'mw' ); + $include = $customisedOnly ? 'mwfile' : 'mw'; + $languages = Language::fetchLanguageNames( $inLanguage, $include ); - // Make sure the site language is in the list; a custom language code might not have a - // defined name... + // Make sure the site language is in the list; + // a custom language code might not have a defined name... if( !array_key_exists( $wgLanguageCode, $languages ) ) { $languages[$wgLanguageCode] = $wgLanguageCode; } @@ -228,7 +231,7 @@ class Xml { /** * If a bogus value is set, default to the content language. * Otherwise, no default is selected and the user ends up - * with an Afrikaans interface since it's first in the list. + * with Afrikaans since it's first in the list. */ $selected = isset( $languages[$selected] ) ? $selected : $wgLanguageCode; $options = "\n"; @@ -236,12 +239,15 @@ class Xml { $options .= Xml::option( "$code - $name", $code, ($code == $selected) ) . "\n"; } + $attrs = array( 'id' => 'wpUserLanguage', 'name' => 'wpUserLanguage' ); + $attrs = array_merge( $attrs, $overrideAttrs ); + + if( $msg === null ) { + $msg = wfMessage( 'yourlanguage' ); + } return array( - Xml::label( wfMsg('yourlanguage'), 'wpUserLanguage' ), - Xml::tags( 'select', - array( 'id' => 'wpUserLanguage', 'name' => 'wpUserLanguage' ), - $options - ) + Xml::label( $msg->text(), $attrs['id'] ), + Xml::tags( 'select', $attrs, $options ) ); } diff --git a/includes/specials/SpecialAllmessages.php b/includes/specials/SpecialAllmessages.php index 0c03871422..fe9d41e516 100644 --- a/includes/specials/SpecialAllmessages.php +++ b/includes/specials/SpecialAllmessages.php @@ -146,8 +146,9 @@ class AllmessagesTablePager extends TablePager { function buildForm() { global $wgScript; - $languages = Language::fetchLanguageNames( null, 'mw' ); - ksort( $languages ); + $attrs = array( 'id' => 'mw-allmessages-form-lang', 'name' => 'lang' ); + $msg = wfMessage( 'allmessages-language' ); + $langSelect = Xml::languageSelector( $this->langcode, false, null, $attrs, $msg ); $out = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'id' => 'mw-allmessages-form' ) ) . Xml::fieldset( $this->msg( 'allmessages-filter-legend' )->text() ) . @@ -187,18 +188,8 @@ class AllmessagesTablePager extends TablePager { "\n \n - " . - Xml::label( $this->msg( 'allmessages-language' )->text(), 'mw-allmessages-form-lang' ) . - "\n - " . - Xml::openElement( 'select', array( 'id' => 'mw-allmessages-form-lang', 'name' => 'lang' ) ); - - foreach( $languages as $lang => $name ) { - $selected = $lang == $this->langcode; - $out .= Xml::option( $lang . ' - ' . $name, $lang, $selected ) . "\n"; - } - $out .= Xml::closeElement( 'select' ) . - "\n + " . $langSelect[0] . "\n + " . $langSelect[1] . "\n " . ' diff --git a/tests/phpunit/includes/XmlTest.php b/tests/phpunit/includes/XmlTest.php index 1d4a36f56a..93ed3dc710 100644 --- a/tests/phpunit/includes/XmlTest.php +++ b/tests/phpunit/includes/XmlTest.php @@ -251,6 +251,15 @@ class XmlTest extends MediaWikiTestCase { ); } + function testLanguageSelector() { + $select = Xml::languageSelector( 'en', true, null, + array( 'id' => 'testlang' ), wfMessage( 'yourlanguage' ) ); + $this->assertEquals( + '', + $select[0] + ); + } + # # JS # -- 2.20.1