Bene has uploaded a new change for review.

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


Change subject: Add an option in the user preferences to choose additional 
languages
......................................................................

Add an option in the user preferences to choose additional languages

Created a new option in the user preferences to choose several additional
languages that will be displayed in the "In other languages" section at
every entity.

Change-Id: I634d606c660676edbd6cd608ca681b779e9478fb
---
M repo/Wikibase.hooks.php
M repo/Wikibase.i18n.php
M repo/Wikibase.php
M repo/includes/EntityView.php
4 files changed, 66 insertions(+), 11 deletions(-)


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

diff --git a/repo/Wikibase.hooks.php b/repo/Wikibase.hooks.php
index 29e0833..036345a 100644
--- a/repo/Wikibase.hooks.php
+++ b/repo/Wikibase.hooks.php
@@ -397,16 +397,64 @@
                wfProfileIn( __METHOD__ );
 
                $preferences['wb-languages'] = array(
-                       'type' => 'multiselect',
-                       'usecheckboxes' => false,
+                       'type' => 'text',
                        'label-message' => 'wikibase-setting-languages',
-                       'options' => $preferences['language']['options'], // 
all languages available in 'language' selector
+                       'help-message' => 'wikibase-setting-languages-help',
+                       'default' => implode( ',', self::getDefaultLanguages( 
$user ) ),
+                       'validation-callback' => 
'Wikibase\RepoHooks::onLanguagesPreferencesValidated',
+                       'filter-callback' => 
'Wikibase\RepoHooks::onLanguagesPreferencesFiltered',
                        'section' => 'personal/i18n',
-                       'prefix' => 'wb-languages-',
                );
 
                wfProfileOut( __METHOD__ );
                return true;
+       }
+
+       /**
+        * Returns the default languages to use.
+        *
+        * @param User $user
+        *
+        * @return string[]
+        */
+       private static function getDefaultLanguages( User $user ) {
+               $defaultLangs = array();
+               // if the Babel extension is installed, add all languages of 
the user
+               if ( class_exists( 'Babel' ) ) {
+                       $defaultLangs = \Babel::getUserLanguages( $user );
+               }
+               return array_merge( explode( ',', $user->getOption( 
'wb-languages' ) ), $defaultLangs );
+       }
+
+       /**
+        * Called when language settings are changed.
+        * @see https://www.mediawiki.org/wiki/HTMLForm
+        *
+        * @param string $value
+        *
+        * @return bool|string
+        */
+       public static function onLanguagesPreferencesValidated( $value ) {
+               $values = explode( ',', $value );
+               if( count( array_intersect( $values, array_keys( 
Language::fetchLanguageNames() ) ) ) === count( $values ) ) {
+                       return true;
+               }
+               else {
+                       $invalidLangs = array_diff( $values, array_keys( 
Language::fetchLanguageNames() ) );
+                       return wfMessage( 
'wikibase-setting-languages-invalidlang', implode( ',', $invalidLangs ) 
)->text();
+               }
+       }
+
+       /**
+        * Called when language settings are filtered.
+        * @see https://www.mediawiki.org/wiki/HTMLForm
+        *
+        * @param string $value
+        *
+        * @return string
+        */
+       public static function onLanguagesPreferencesFiltered( $value ) {
+               return str_replace( ' ', '', $value );
        }
 
        /**
@@ -421,8 +469,7 @@
                wfProfileIn( __METHOD__ );
 
                // pre-select default language in the list of fallback languages
-               $defaultLang = $defaultOptions['language'];
-               $defaultOptions[ 'wb-languages-' . $defaultLang ] = 1;
+               $defaultOptions['wb-languages'] = $defaultOptions['language'];
 
                wfProfileOut( __METHOD__ );
                return true;
diff --git a/repo/Wikibase.i18n.php b/repo/Wikibase.i18n.php
index 43c1f80..164544f 100644
--- a/repo/Wikibase.i18n.php
+++ b/repo/Wikibase.i18n.php
@@ -116,6 +116,10 @@
        'wikibase-itemlink-userlang-wrapper' => ' ($1: $2)',
        'wikibase-itemlink-title' => '$1 | $2',
 
+       'wikibase-setting-languages' => 'Additional languages: ',
+       'wikibase-setting-languages-help' => 'Enter the language codes of the 
languages you want to see in the "In other languages" section seperated by a 
comma.',
+       'wikibase-setting-languages-invalidlang' => 'The following language 
codes are not valid: $1',
+
        // Special pages
        'special-itembytitle' => 'Item by title',
        'wikibase-itembytitle-lookup-fieldset' => 'Search for items by site and 
title',
@@ -736,6 +740,10 @@
 Title attribute set in links to an item. $1 is the items label or (if not 
available) the prefixed page title of the item. $2 is the items description. 
Parameters:
 * $1 label as a directional string
 * $2 description as a directional string',
+       'wikibase-setting-languages' => 'The label besides the text field in 
the user preferences to choose the extra languages.',
+       'wikibase-setting-languages-help' => 'The help message below the text 
field in the user preferences to choose the extra languages.',
+       'wikibase-setting-languages-invalidlang' => 'Error shown if an invalid 
language has been chosen. Parameters:
+* $1 is a string containing the language codes separated by commas',
        'special-itembytitle' => "{{doc-special|ItemByTitle}}
 The item is identified through use of the site and title, but the lookup 
failed and further qualification must be done. See also Wikidata's glossary for 
[[d:Wikidata:Glossary#languageattribute-label|label]] and 
[[d:Wikidata:Glossary#Items|items]].",
        'wikibase-itembytitle-lookup-fieldset' => 'This is the title for the 
fieldset on the special page for further refining the search. This is the 
search by site and title.',
diff --git a/repo/Wikibase.php b/repo/Wikibase.php
index 5667e41..a2298ff 100644
--- a/repo/Wikibase.php
+++ b/repo/Wikibase.php
@@ -168,6 +168,7 @@
        $wgHooks['RecentChange_save'][]                                         
= 'Wikibase\RepoHooks::onRecentChangeSave';
        $wgHooks['ArticleDeleteComplete'][]                             = 
'Wikibase\RepoHooks::onArticleDeleteComplete';
        $wgHooks['ArticleUndelete'][]                                           
= 'Wikibase\RepoHooks::onArticleUndelete';
+       $wgHooks['GetPreferences'][]                                            
= 'Wikibase\RepoHooks::onGetPreferences';
        $wgHooks['LinkBegin'][]                                                 
        = 'Wikibase\RepoHooks::onLinkBegin';
        $wgHooks['OutputPageBodyAttributes'][]                          = 
'Wikibase\RepoHooks::onOutputPageBodyAttributes';
        //FIXME: handle other types of entities with autocomments too!
diff --git a/repo/includes/EntityView.php b/repo/includes/EntityView.php
index 04f5b3f..869c68d 100644
--- a/repo/includes/EntityView.php
+++ b/repo/includes/EntityView.php
@@ -416,9 +416,10 @@
                wfProfileIn( __METHOD__ );
                $result = array();
 
-               // if the Babel extension is installed, add all languages of 
the user
-               if ( class_exists( 'Babel' ) && ( ! $user->isAnon() ) ) {
-                       $result = \Babel::getUserLanguages( $user );
+               if( !$user->isAnon() ) {
+                       // add languages from user preferences
+                       $result = explode( ',', $user->getOption( 
'wb-languages' ) );
+                       // remove the language itself
                        if( $lang !== null ) {
                                $result = array_diff( $result, array( 
$lang->getCode() ) );
                        }
@@ -451,8 +452,6 @@
                $descriptions = $entity->getDescriptions();
 
                $html .= wfTemplate( 'wb-terms-heading', wfMessage( 
'wikibase-terms' ) );
-
-               $languages = $this->getExtraUserLanguages( $lang, 
$this->getUser() );
 
                $specialLabelPage = \SpecialPageFactory::getPage( "SetLabel" );
                $specialDescriptionPage = \SpecialPageFactory::getPage( 
"SetDescription" );

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I634d606c660676edbd6cd608ca681b779e9478fb
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Bene <benestar.wikime...@googlemail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to