Use Xml::languageSelector in SpecialAllmessages
authorrobin <robinp.1273@gmail.com>
Thu, 10 May 2012 19:46:14 +0000 (21:46 +0200)
committerrobin <robinp.1273@gmail.com>
Tue, 10 Jul 2012 17:43:38 +0000 (13:43 -0400)
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
includes/specials/SpecialAllmessages.php
tests/phpunit/includes/XmlTest.php

index 6e4bb3a..505cb7f 100644 (file)
@@ -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 )
                );
 
        }
index 0c03871..fe9d41e 100644 (file)
@@ -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 {
                                "</td>\n
                        </tr>
                        <tr>\n
-                               <td class=\"mw-label\">" .
-                                       Xml::label( $this->msg( 'allmessages-language' )->text(), 'mw-allmessages-form-lang' ) .
-                               "</td>\n
-                               <td class=\"mw-input\">" .
-                                       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' ) .
-                               "</td>\n
+                               <td class=\"mw-label\">" . $langSelect[0] . "</td>\n
+                               <td class=\"mw-input\">" . $langSelect[1] . "</td>\n
                        </tr>" .
 
                        '<tr>
index 1d4a36f..93ed3dc 100644 (file)
@@ -251,6 +251,15 @@ class XmlTest extends MediaWikiTestCase {
                );
        }
 
+       function testLanguageSelector() {
+               $select = Xml::languageSelector( 'en', true, null,
+                       array( 'id' => 'testlang' ), wfMessage( 'yourlanguage' ) );
+               $this->assertEquals(
+                       '<label for="testlang">Language:</label>',
+                       $select[0]
+               );
+       }
+
        #
        # JS
        #