Render namespace lists in the user's language
authorErik Bernhardson <ebernhardson@wikimedia.org>
Mon, 10 Sep 2018 20:14:20 +0000 (13:14 -0700)
committerErik Bernhardson <ebernhardson@wikimedia.org>
Fri, 12 Oct 2018 18:27:20 +0000 (11:27 -0700)
Asking users to select from a list that is in a namespace
other than their user language can be quite difficult.
Transition a number of use cases. This was applied via a flag
to avoid unexpectedly changing extensions also utilizing
the namespace selector.

Pages updated:
* Special:Search
* Special:Watchlist
* Special:RecentChanges
* Special:Contributions
* Special:Import
* Special:WhatLinksHere

Bug: T174057
Change-Id: I3fdac72179a124849ef7ad1e0e54eb66396c3c6e

includes/Html.php
includes/specials/SpecialContributions.php
includes/specials/SpecialImport.php
includes/specials/SpecialRecentchanges.php
includes/specials/SpecialWatchlist.php
includes/specials/SpecialWhatlinkshere.php
includes/widget/search/SearchFormWidget.php
tests/phpunit/includes/HtmlTest.php

index aac492c..d066eff 100644 (file)
@@ -840,9 +840,14 @@ class Html {
                        // Value is provided by user, the name shown is localized for the user.
                        $options[$params['all']] = wfMessage( 'namespacesall' )->text();
                }
-               // Add all namespaces as options (in the content language)
-               $options +=
-                       MediaWikiServices::getInstance()->getContentLanguage()->getFormattedNamespaces();
+               if ( $params['in-user-lang'] ?? false ) {
+                       global $wgLang;
+                       $lang = $wgLang;
+               } else {
+                       $lang = MediaWikiServices::getInstance()->getContentLanguage();
+               }
+               // Add all namespaces as options
+               $options += $lang->getFormattedNamespaces();
 
                $optionsOut = [];
                // Filter out namespaces below 0 and massage labels
@@ -855,8 +860,7 @@ class Html {
                                // main we don't use "" but the user message describing it (e.g. "(Main)" or "(Article)")
                                $nsName = wfMessage( 'blanknamespace' )->text();
                        } elseif ( is_int( $nsId ) ) {
-                               $nsName = MediaWikiServices::getInstance()->getContentLanguage()->
-                                       convertNamespace( $nsId );
+                               $nsName = $lang->convertNamespace( $nsId );
                        }
                        $optionsOut[$nsId] = $nsName;
                }
index 63b64ea..72ab94c 100644 (file)
@@ -607,7 +607,7 @@ class SpecialContributions extends IncludableSpecialPage {
                                ''
                        ) . "\u{00A0}" .
                        Html::namespaceSelector(
-                               [ 'selected' => $this->opts['namespace'], 'all' => '' ],
+                               [ 'selected' => $this->opts['namespace'], 'all' => '', 'in-user-lang' => true ],
                                [
                                        'name' => 'namespace',
                                        'id' => 'namespace',
index 153b7d1..3b3c801 100644 (file)
@@ -279,6 +279,7 @@ class SpecialImport extends SpecialPage {
                                                        'selected' => ( $isSameSourceAsBefore ?
                                                                $this->namespace :
                                                                ( $defaultNamespace || '' ) ),
+                                                       'in-user-lang' => true,
                                                ], [
                                                        'name' => "namespace",
                                                        // mw-import-namespace-interwiki, mw-import-namespace-upload
index 170f792..a857bb3 100644 (file)
@@ -678,7 +678,7 @@ class SpecialRecentChanges extends ChangesListSpecialPage {
         */
        protected function namespaceFilterForm( FormOptions $opts ) {
                $nsSelect = Html::namespaceSelector(
-                       [ 'selected' => $opts['namespace'], 'all' => '' ],
+                       [ 'selected' => $opts['namespace'], 'all' => '', 'in-user-lang' => true ],
                        [ 'name' => 'namespace', 'id' => 'namespace' ]
                );
                $nsLabel = Xml::label( $this->msg( 'namespace' )->text(), 'namespace' );
index feb449c..0ca6f93 100644 (file)
@@ -652,7 +652,8 @@ class SpecialWatchlist extends ChangesListSpecialPage {
                        [
                                'selected' => $opts['namespace'],
                                'all' => '',
-                               'label' => $this->msg( 'namespace' )->text()
+                               'label' => $this->msg( 'namespace' )->text(),
+                               'in-user-lang' => true,
                        ], [
                                'name' => 'namespace',
                                'id' => 'namespace',
index 1d3ffd7..8eee37c 100644 (file)
@@ -515,7 +515,8 @@ class SpecialWhatLinksHere extends IncludableSpecialPage {
                        [
                                'selected' => $namespace,
                                'all' => '',
-                               'label' => $this->msg( 'namespace' )->text()
+                               'label' => $this->msg( 'namespace' )->text(),
+                               'in-user-lang' => true,
                        ], [
                                'name' => 'namespace',
                                'id' => 'namespace',
index 7eb92fd..0c80951 100644 (file)
@@ -238,7 +238,7 @@ class SearchFormWidget {
        protected function powerSearchBox( $term, array $opts ) {
                $rows = [];
                $activeNamespaces = $this->specialSearch->getNamespaces();
-               $langConverter = MediaWikiServices::getInstance()->getContentLanguage()->getConverter();
+               $langConverter = $this->specialSearch->getLanguage();
                foreach ( $this->searchConfig->searchableNamespaces() as $namespace => $name ) {
                        $subject = MWNamespace::getSubject( $namespace );
                        if ( !isset( $rows[$subject] ) ) {
index 71e733b..5663a60 100644 (file)
@@ -418,6 +418,33 @@ class HtmlTest extends MediaWikiTestCase {
                        ),
                        'Basic namespace selector with a custom label but no id attribtue for the <select>'
                );
+
+               $this->assertEquals(
+                       '<select id="namespace" name="namespace">' . "\n" .
+                               '<option value="0">(Principal)</option>' . "\n" .
+                               '<option value="1">Discusión</option>' . "\n" .
+                               '<option value="2">Usuario</option>' . "\n" .
+                               '<option value="3">Usuario discusión</option>' . "\n" .
+                               '<option value="4">Wiki</option>' . "\n" .
+                               '<option value="5">Wiki discusión</option>' . "\n" .
+                               '<option value="6">Archivo</option>' . "\n" .
+                               '<option value="7">Archivo discusión</option>' . "\n" .
+                               '<option value="8">MediaWiki</option>' . "\n" .
+                               '<option value="9">MediaWiki discusión</option>' . "\n" .
+                               '<option value="10">Plantilla</option>' . "\n" .
+                               '<option value="11">Plantilla discusión</option>' . "\n" .
+                               '<option value="12">Ayuda</option>' . "\n" .
+                               '<option value="13">Ayuda discusión</option>' . "\n" .
+                               '<option value="14">Categoría</option>' . "\n" .
+                               '<option value="15">Categoría discusión</option>' . "\n" .
+                               '<option value="100">Personalizado</option>' . "\n" .
+                               '<option value="101">Personalizado discusión</option>' . "\n" .
+                               '</select>',
+                       Html::namespaceSelector(
+                               [ 'in-user-lang' => true ]
+                       ),
+                       'Basic namespace selector in user language'
+               );
        }
 
        /**