Avoid sorting the language array twice
authorFomafix <fomafix@googlemail.com>
Thu, 22 Feb 2018 21:09:04 +0000 (22:09 +0100)
committerKunal Mehta <legoktm@member.fsf.org>
Wed, 23 May 2018 06:02:34 +0000 (23:02 -0700)
Language::fetchLanguageNames returns already a sorted array. An
additional ksort is only needed when inserting a new value.

Change-Id: If8c7b16fa6e7dfe1545f72ac9c742a2f43eaee57

includes/Xml.php
includes/installer/WebInstallerLanguage.php
includes/preferences/DefaultPreferencesFactory.php
includes/specials/SpecialPageLanguage.php
languages/Language.php

index 7dcd4a4..10d0d8b 100644 (file)
@@ -214,10 +214,10 @@ class Xml {
                // a custom language code might not have a defined name...
                if ( !array_key_exists( $wgLanguageCode, $languages ) ) {
                        $languages[$wgLanguageCode] = $wgLanguageCode;
+                       // Sort the array again
+                       ksort( $languages );
                }
 
-               ksort( $languages );
-
                /**
                 * If a bogus value is set, default to the content language.
                 * Otherwise, no default is selected and the user ends up
index bce07d3..846be6c 100644 (file)
@@ -108,7 +108,6 @@ class WebInstallerLanguage extends WebInstallerPage {
                $unwantedLanguageCodes = $wgExtraLanguageCodes +
                        LanguageCode::getDeprecatedCodeMapping();
                $languages = Language::fetchLanguageNames();
-               ksort( $languages );
                foreach ( $languages as $code => $lang ) {
                        if ( isset( $unwantedLanguageCodes[$code] ) ) {
                                continue;
index 2d7d73f..d5a706a 100644 (file)
@@ -413,8 +413,9 @@ class DefaultPreferencesFactory implements PreferencesFactory {
                $languageCode = $this->config->get( 'LanguageCode' );
                if ( !array_key_exists( $languageCode, $languages ) ) {
                        $languages[$languageCode] = $languageCode;
+                       // Sort the array again
+                       ksort( $languages );
                }
-               ksort( $languages );
 
                $options = [];
                foreach ( $languages as $code => $name ) {
index a68f08f..e3485ff 100644 (file)
@@ -82,7 +82,6 @@ class SpecialPageLanguage extends FormSpecialPage {
                // Building a language selector
                $userLang = $this->getLanguage()->getCode();
                $languages = Language::fetchLanguageNames( $userLang, 'mwfile' );
-               ksort( $languages );
                $options = [];
                foreach ( $languages as $code => $name ) {
                        $options["$code - $name"] = $code;
index db302d7..be0c037 100644 (file)
@@ -797,7 +797,7 @@ class Language {
         *              'all' all available languages
         *              'mw' only if the language is defined in MediaWiki or wgExtraLanguageNames (default)
         *              'mwfile' only if the language is in 'mw' *and* has a message file
-        * @return array Language code => language name
+        * @return array Language code => language name (sorted by key)
         * @since 1.20
         */
        public static function fetchLanguageNames( $inLanguage = null, $include = 'mw' ) {
@@ -823,7 +823,7 @@ class Language {
         *              'all' all available languages
         *              'mw' only if the language is defined in MediaWiki or wgExtraLanguageNames (default)
         *              'mwfile' only if the language is in 'mw' *and* has a message file
-        * @return array Language code => language name
+        * @return array Language code => language name (sorted by key)
         */
        private static function fetchLanguageNamesUncached( $inLanguage = null, $include = 'mw' ) {
                global $wgExtraLanguageNames, $wgUsePigLatinVariant;