Hoo man has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/190485

Change subject: Remove Utils::getLanguageCodes
......................................................................

Remove Utils::getLanguageCodes

This makes BabelUserLanguageLookup return all languages
specified in Babel, which is what we actually want, as
we in some cases want to allow more languages than
in others.
Filtering the user languages now has to be done by the
caller based on their ContentLanguages.

Change-Id: If600130618b6473ad13c0ff3c4691f8e44e7f9e3
---
M lib/includes/UserLanguageLookup.php
M lib/includes/Utils.php
M lib/includes/WikibaseContentLanguages.php
M lib/tests/phpunit/UtilsTest.php
A lib/tests/phpunit/WikibaseContentLanguagesTest.php
M repo/Wikibase.hooks.php
M repo/includes/BabelUserLanguageLookup.php
M repo/includes/View/EntityViewPlaceholderExpander.php
8 files changed, 52 insertions(+), 57 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase 
refs/changes/85/190485/1

diff --git a/lib/includes/UserLanguageLookup.php 
b/lib/includes/UserLanguageLookup.php
index 25ccdc8..0865c8f 100644
--- a/lib/includes/UserLanguageLookup.php
+++ b/lib/includes/UserLanguageLookup.php
@@ -19,6 +19,7 @@
        /**
         * Returns a list of languages the user specified in addition to the 
non-optional interface
         * language.
+        * Note: This can contain language codes not actually valid to 
MediaWiki or valid at all.
         *
         * @param User $user The current user.
         *
@@ -28,6 +29,7 @@
 
        /**
         * Collects all languages from all user settings we can reach at this 
point.
+        * Note: This can contain language codes not actually valid to 
MediaWiki or valid at all.
         *
         * @param User $user The current user.
         *
diff --git a/lib/includes/Utils.php b/lib/includes/Utils.php
index 978d72d..703c596 100644
--- a/lib/includes/Utils.php
+++ b/lib/includes/Utils.php
@@ -3,8 +3,6 @@
 namespace Wikibase;
 
 use Language;
-use MWException;
-
 /**
  * Utility functions for Wikibase.
  *
@@ -17,29 +15,6 @@
  * @author John Erling Blad < [email protected] >
  */
 final class Utils {
-
-       /**
-        * Returns a list of language codes that Wikibase supports,
-        * ie the languages that a label or description can be in.
-        *
-        * @since 0.1
-        *
-        * @throws MWException if the list can not be obtained.
-        * @return string[]
-        */
-       public static function getLanguageCodes() {
-               static $languageCodes = null;
-
-               if ( $languageCodes === null ) {
-                       $languageCodes = array_keys( 
Language::fetchLanguageNames() );
-
-                       if ( empty( $languageCodes ) ) {
-                               throw new MWException( 'List of language names 
is empty' );
-                       }
-               }
-
-               return $languageCodes;
-       }
 
        /**
         * @see Language::fetchLanguageName()
diff --git a/lib/includes/WikibaseContentLanguages.php 
b/lib/includes/WikibaseContentLanguages.php
index e6dc85e..9d9da68 100644
--- a/lib/includes/WikibaseContentLanguages.php
+++ b/lib/includes/WikibaseContentLanguages.php
@@ -2,12 +2,14 @@
 
 namespace Wikibase\Lib;
 
+use Language;
 use Wikibase\Utils;
 
 /**
  * Provide languages supported as content language based on Wikibase\Utils
  *
  * @author Adrian Heine < [email protected] >
+ * @author Marius Hoch < [email protected] >
  */
 class WikibaseContentLanguages implements ContentLanguages {
 
@@ -15,7 +17,8 @@
         * @return string[] Array of language codes supported as content 
language
         */
        public function getLanguages() {
-               return Utils::getLanguageCodes();
+               $languageCodes = array_keys( Language::fetchLanguageNames() );
+               return $languageCodes;
        }
 
        /**
diff --git a/lib/tests/phpunit/UtilsTest.php b/lib/tests/phpunit/UtilsTest.php
index a462f97..62aee38 100644
--- a/lib/tests/phpunit/UtilsTest.php
+++ b/lib/tests/phpunit/UtilsTest.php
@@ -18,28 +18,6 @@
  */
 class UtilsTest extends \MediaWikiTestCase {
 
-       /**
-        * @group WikibaseUtils
-        * @dataProvider providerGetLanguageCodes
-        */
-       public function testGetLanguageCodes( $lang ) {
-               $result = Utils::getLanguageCodes();
-               $this->assertContains(
-                       $lang,
-                       $result,
-                       "The language code {$lang} could not be found in the 
returned result"
-               );
-       }
-
-       public function providerGetLanguageCodes() {
-               return array(
-                       array( 'de' ),
-                       array( 'en' ),
-                       array( 'no' ),
-                       array( 'nn' ),
-               );
-       }
-
        public function provideFetchLanguageName() {
                return array(
                        array( // #0
diff --git a/lib/tests/phpunit/WikibaseContentLanguagesTest.php 
b/lib/tests/phpunit/WikibaseContentLanguagesTest.php
new file mode 100644
index 0000000..1905f02
--- /dev/null
+++ b/lib/tests/phpunit/WikibaseContentLanguagesTest.php
@@ -0,0 +1,31 @@
+<?php
+
+namespace Wikibase\Lib\Test;
+
+use Wikibase\Lib\WikibaseContentLanguages;
+
+/**
+ * @covers Wikibase\Lib\WikibaseContentLanguages
+ *
+ * @group WikibaseLib
+ * @group Wikibase
+ *
+ * @license GNU GPL v2+
+ * @author Marius Hoch < [email protected] >
+ */
+class WikibaseContentLanguagesTest extends \MediaWikiTestCase {
+
+       public function testGetLanguages() {
+               $wbContentLanguages = new WikibaseContentLanguages();
+               $result = $wbContentLanguages->getLanguages();
+
+               $this->assertInternalType( 'array', $result );
+
+               // Just check for some langs
+               $knownLangCodes = array( 'en', 'de', 'es', 'fr', 'nl', 'ru', 
'zh' );
+               $this->assertSame(
+                       $knownLangCodes,
+                       array_intersect( $knownLangCodes, $result )
+               );
+       }
+}
diff --git a/repo/Wikibase.hooks.php b/repo/Wikibase.hooks.php
index 3ac6c19..a058c3f 100644
--- a/repo/Wikibase.hooks.php
+++ b/repo/Wikibase.hooks.php
@@ -1002,6 +1002,7 @@
                if ( !empty( $placeholders ) ) {
                        $injector = new TextInjector( $placeholders );
                        $userLanguageLookup = new BabelUserLanguageLookup();
+                       $termsLanguages = 
WikibaseRepo::getDefaultInstance()->getTermsLanguages();
                        $expander = new EntityViewPlaceholderExpander(
                                new TemplateFactory( 
TemplateRegistry::getDefaultInstance() ),
                                $out->getTitle(),
@@ -1015,8 +1016,14 @@
 
                        $html = $injector->inject( $html, array( $expander, 
'getHtmlForPlaceholder' ) );
 
-                       $out->addJsConfigVars( 'wbUserSpecifiedLanguages',
-                               $userLanguageLookup->getUserSpecifiedLanguages( 
$out->getUser() ) );
+                       $out->addJsConfigVars(
+                               'wbUserSpecifiedLanguages',
+                               // All user-specified languages, that are valid 
term languages
+                               array_intersect(
+                                       
$userLanguageLookup->getUserSpecifiedLanguages( $out->getUser() ),
+                                       $termsLanguages->getLanguages()
+                               )
+                       );
                }
 
                return true;
diff --git a/repo/includes/BabelUserLanguageLookup.php 
b/repo/includes/BabelUserLanguageLookup.php
index e505fbf..8fd002f 100644
--- a/repo/includes/BabelUserLanguageLookup.php
+++ b/repo/includes/BabelUserLanguageLookup.php
@@ -3,7 +3,6 @@
 namespace Wikibase\Repo;
 
 use User;
-use Wikibase\Utils;
 use Wikibase\Lib\UserLanguageLookup;
 
 /**
@@ -51,6 +50,7 @@
        /**
         * Returns a list of languages the user specified in addition to the 
non-optional interface
         * language.
+        * Note: This can contain language codes not actually valid to 
MediaWiki or valid at all.
         *
         * @param User $user The current user.
         *
@@ -66,10 +66,6 @@
                // that for regions.
                $languages = array_map( 'strtolower', $languages );
 
-               $supportedLanguages = Utils::getLanguageCodes();
-               $languages = array_intersect( $languages, $supportedLanguages );
-               $languages = array_values( $languages ); // Reindex
-
                return $languages;
        }
 
@@ -78,6 +74,7 @@
         * preference, duplicates stripped:
         * 1. The interface language from the user's settings
         * 2. All languages in the user's Babel box
+        * Note: This can contain language codes not actually valid to 
MediaWiki or valid at all.
         *
         * @param User $user The current user.
         *
diff --git a/repo/includes/View/EntityViewPlaceholderExpander.php 
b/repo/includes/View/EntityViewPlaceholderExpander.php
index 3070812..c779878 100644
--- a/repo/includes/View/EntityViewPlaceholderExpander.php
+++ b/repo/includes/View/EntityViewPlaceholderExpander.php
@@ -87,7 +87,7 @@
         * @param EntityIdParser $entityIdParser
         * @param EntityRevisionLookup $entityRevisionLookup
         * @param UserLanguageLookup $userLanguageLookup
-        * @param ContentLanguages $termLanguages
+        * @param ContentLanguages $termsLanguages
         */
        public function __construct(
                TemplateFactory $templateFactory,
@@ -124,10 +124,12 @@
                        } else {
                                // ignore current interface language
                                $skip = array( $this->uiLanguage->getCode() );
-                               $this->extraLanguages = array_diff(
+                               $langs = array_diff(
                                        
$this->userLanguageLookup->getAllUserLanguages( $this->user ),
                                        $skip
                                );
+                               // Make sure we only report actual term 
languages
+                               $this->extraLanguages = array_intersect( 
$langs, $this->termsLanguages->getLanguages() );
                        }
                }
 

-- 
To view, visit https://gerrit.wikimedia.org/r/190485
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: If600130618b6473ad13c0ff3c4691f8e44e7f9e3
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Hoo man <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to