jenkins-bot has submitted this change and it was merged.

Change subject: Add wikidata descriptions
......................................................................


Add wikidata descriptions

Change-Id: I90d72c3485a35d12774a6c24659a609765405582
---
M MobileFrontend.php
M includes/MobileFrontend.hooks.php
2 files changed, 52 insertions(+), 4 deletions(-)

Approvals:
  Kaldari: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/MobileFrontend.php b/MobileFrontend.php
index fc01c44..6ec9458 100644
--- a/MobileFrontend.php
+++ b/MobileFrontend.php
@@ -559,3 +559,9 @@
        // Just don't
        NS_MEDIA,
 );
+
+/**
+ * If set to true and running alpha, will add Wikidata description to page JS 
as
+ * wgMFDescription variable
+ */
+$wgMFUseWikibaseDescription = false;
diff --git a/includes/MobileFrontend.hooks.php 
b/includes/MobileFrontend.hooks.php
index 19048cd..6d94eeb 100644
--- a/includes/MobileFrontend.hooks.php
+++ b/includes/MobileFrontend.hooks.php
@@ -1,7 +1,7 @@
 <?php
-/**
- * MobileFrontend.hooks.php
- */
+
+use Wikibase\Client\WikibaseClient;
+use Wikibase\DataModel\Entity\ItemId;
 
 /**
  * Hook handlers for MobileFrontend extension
@@ -930,9 +930,22 @@
         * @return bool
         */
        public static function onOutputPageParserOutput( $outputPage, 
ParserOutput $po ) {
-               if ( MobileContext::singleton()->shouldDisplayMobileView() ) {
+               global $wgMFUseWikibaseDescription;
+
+               $context = MobileContext::singleton();
+               if ( $context->shouldDisplayMobileView() ) {
                        $outputPage->enableTOC( false );
                        $outputPage->setProperty( 'MinervaTOC', 
$po->getTOCHTML() !== '' );
+
+                       if ( $wgMFUseWikibaseDescription && 
$context->isAlphaGroupMember() ) {
+                               $item = $po->getProperty( 'wikibase_item' );
+                               if ( $item ) {
+                                       $desc = self::getWikibaseDescription( 
$item );
+                                       if ( $desc ) {
+                                               $outputPage->addJsConfigVars( 
'wgMFDescription', $desc );
+                                       }
+                               }
+                       }
                }
                return true;
        }
@@ -954,4 +967,33 @@
        public static function onLoginFormValidErrorMessages( &$messages ) {
                $messages[] = 'mobile-frontend-donate-image-anon';
        }
+
+       /**
+        * Returns a short description of a page from Wikidata
+        *
+        * @param string $item Wikibase id of the page
+        * @return string|null
+        */
+       private static function getWikibaseDescription( $item ) {
+               global $wgContLang;
+
+               if ( !class_exists( 'Wikibase\\Client\\WikibaseClient' ) ) {
+                       return null;
+               }
+
+               $profileSection = new ProfileSection( __METHOD__ );
+               try {
+                       $entityLookup = WikibaseClient::getDefaultInstance()
+                               ->getStore()
+                               ->getEntityLookup();
+                       $entity = $entityLookup->getEntity( new ItemId( $item ) 
);
+                       if ( !$entity ) {
+                               return null;
+                       }
+                       return $entity->getFingerprint()->getDescription( 
$wgContLang->getCode() )->getText();
+               } catch ( Exception $ex) {
+                       // Do nothing, exception mostly due to description 
being unavailable in needed language
+               }
+               return null;
+       }
 }

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I90d72c3485a35d12774a6c24659a609765405582
Gerrit-PatchSet: 4
Gerrit-Project: mediawiki/extensions/MobileFrontend
Gerrit-Branch: master
Gerrit-Owner: MaxSem <[email protected]>
Gerrit-Reviewer: Awjrichards <[email protected]>
Gerrit-Reviewer: Kaldari <[email protected]>
Gerrit-Reviewer: MaxSem <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to