Liangent has uploaded a new change for review.

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


Change subject: Remove Utils::lookupMultilangText(): replaced by language 
fallback chain
......................................................................

Remove Utils::lookupMultilangText(): replaced by language fallback chain

Change-Id: I40ad9647ba489a7950773f21a2ec04273ef1fe9e
---
M lib/includes/Utils.php
M repo/Wikibase.hooks.php
2 files changed, 27 insertions(+), 71 deletions(-)


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

diff --git a/lib/includes/Utils.php b/lib/includes/Utils.php
index c6ce523..95412a2 100644
--- a/lib/includes/Utils.php
+++ b/lib/includes/Utils.php
@@ -298,46 +298,6 @@
        }
 
        /**
-        * Find the first multilingual string that can be used for constructing 
a language object. The
-        * global chain is always used.
-        *
-        * Note that a multilingual string from the global chain will always be 
globally cachable.
-        *
-        * @since 0.1
-        *
-        * @param array $texts the key-value pairs to check for existence
-        * @param array $sequence the list of keys that should exist
-        * @param array $fallback an array of values that are used as a 
replacement if nothing is found
-        *              The fallback is in the form array( code, text, language 
)
-        * @return array|null triplet with the initial language code, the text, 
and the language object
-        */
-       static public function lookupMultilangText( array $texts = null, array 
$sequence = null, array $fallback = null ) {
-
-               // Prerequisites for further processing
-               if ( is_null( $texts ) || is_null( $sequence ) ) {
-                       return $fallback; // makes the simplest use case
-               }
-
-               // Filter down the result
-               $texts = \Wikibase\Utils::filterMultilangText( $texts, 
$sequence );
-               if ( is_null( $texts ) || empty( $texts ) ) {
-                       return $fallback;
-               }
-
-               // Find the first language code we can turn into a language 
object
-               // Note that the factory call do a pretty dumb cleaning up that 
can make this vejjy slow
-               foreach ( $texts as $code => $text ) {
-                       $lang = Language::factory( $code );
-                       if ( !is_null( $lang ) ) {
-                               return array( $code, $text, $lang );
-                       }
-               }
-
-               // Use the fallback if the previous fails
-               return $fallback;
-       }
-
-       /**
         * Find the first multilingual string that can be used for constructing 
a language object
         * for the current user. If a preferred language can't be identified 
the global chain is
         * used.
diff --git a/repo/Wikibase.hooks.php b/repo/Wikibase.hooks.php
index 2848577..73cb80d 100644
--- a/repo/Wikibase.hooks.php
+++ b/repo/Wikibase.hooks.php
@@ -1,8 +1,8 @@
 <?php
 
 namespace Wikibase;
-use Title, Language, User, Revision, WikiPage, EditPage, ContentHandler, Html, 
MWException;
-
+use Title, Language, User, Revision, WikiPage, EditPage, ContentHandler, Html, 
MWException, RequestContext;
+use Wikibase\Repo\WikibaseRepo;
 
 /**
  * File defining the hook handlers for the Wikibase extension.
@@ -781,8 +781,6 @@
                        return true;
                }
 
-               global $wgLang, $wgOut;
-
                // The following three vars should all exist, unless there is a 
failurre
                // somewhere, and then it will fail hard. Better test it now!
                $page = new WikiPage( $target );
@@ -819,39 +817,36 @@
                        return true;
                }
 
-               // If this fails we will not find labels and descriptions later,
-               // but we will try to get a list of alternate languages. The 
following
-               // uses the user language as a starting point for the fallback 
chain.
-               // It could be argued that the fallbacks should be limited to 
the user
-               // selected languages.
-               $lang = $wgLang->getCode();
-               static $langStore = array();
-               if ( !isset( $langStore[$lang] ) ) {
-                       $langStore[$lang] = array_merge( array( $lang ), 
Language::getFallbacksFor( $lang ) );
+               // Try to find the most preferred available language to display 
data in current context.
+               $languageFallbackChainFactory = 
WikibaseRepo::getDefaultInstance()->getLanguageFallbackChainFactory();
+               $context = RequestContext::getMain();
+               $languageFallbackChain = 
$languageFallbackChainFactory->newFromContext( $context );
+
+               $labelData = $languageFallbackChain->extractPreferredValue( 
$entity->getLabels() );
+               $descriptionData = 
$languageFallbackChain->extractPreferredValue( $entity->getDescriptions() );
+
+               if ( $labelData ) {
+                       $labelText = $labelData['value'];
+                       $labelLang = Language::factory( $labelData['language'] 
);
+               } else {
+                       $labelText = '';
+                       $labelLang = $context->getLanguage();
                }
 
-               // Get the label and description for the first languages on the 
chain
-               // that doesn't fail, use a fallback if everything fails. This 
could
-               // use the user supplied list of acceptable languages as a 
filter.
-               list( , $labelText, $labelLang) = $labelTriplet =
-                       Utils::lookupMultilangText(
-                               $entity->getLabels( $langStore[$lang] ),
-                               $langStore[$lang],
-                               array( $wgLang->getCode(), null, $wgLang )
-                       );
-               list( , $descriptionText, $descriptionLang) = 
$descriptionTriplet =
-                       Utils::lookupMultilangText(
-                               $entity->getDescriptions( $langStore[$lang] ),
-                               $langStore[$lang],
-                               array( $wgLang->getCode(), null, $wgLang )
-                       );
+               if ( $descriptionData ) {
+                       $descriptionText = $descriptionData['value'];
+                       $descriptionLang = Language::factory( 
$descriptionData['language'] );
+               } else {
+                       $descriptionText = '';
+                       $descriptionLang = $context->getLanguage();
+               }
 
                // Go on and construct the link
                $idHtml = Html::openElement( 'span', array( 'class' => 
'wb-itemlink-id' ) )
                        . wfMessage( 'wikibase-itemlink-id-wrapper', 
$target->getText() )->inContentLanguage()->escaped()
                        . Html::closeElement( 'span' );
 
-               $labelHtml = Html::openElement( 'span', array( 'class' => 
'wb-itemlink-label', 'lang' => $labelLang->getCode(), 'dir' => 
$labelLang->getDir() ) )
+               $labelHtml = Html::openElement( 'span', array( 'class' => 
'wb-itemlink-label', 'lang' => $labelLang->getHtmlCode(), 'dir' => 
$labelLang->getDir() ) )
                        . htmlspecialchars( $labelText )
                        . Html::closeElement( 'span' );
 
@@ -861,17 +856,18 @@
 
                // Set title attribute for constructed link, and make tricks 
with the directionality to get it right
                $titleText = ( $labelText !== null )
-                       ? $labelLang->getDirMark() . $labelText . 
$wgLang->getDirMark()
+                       ? $labelLang->getDirMark() . $labelText . 
$context->getLanguage()->getDirMark()
                        : $target->getPrefixedText();
                $customAttribs[ 'title' ] = ( $descriptionText !== null ) ?
                        wfMessage(
                                'wikibase-itemlink-title',
                                $titleText,
-                               $descriptionLang->getDirMark() . 
$descriptionText . $wgLang->getDirMark()
+                               $descriptionLang->getDirMark() . 
$descriptionText . $context->getLanguage()->getDirMark()
                        )->inContentLanguage()->text() :
                        $titleText; // no description, just display the title 
then
 
                // add wikibase styles in all cases, so we can format the link 
properly:
+               global $wgOut;
                $wgOut->addModuleStyles( array( 'wikibase.common' ) );
 
                wfProfileOut( __METHOD__ );

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

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

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

Reply via email to