Sumit has uploaded a new change for review.

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

Change subject: WikidataPageBanner::getWikidataBanner added
......................................................................

WikidataPageBanner::getWikidataBanner added

A function getWikidataBanner() is added, which fetches a banner for an article
page if the page has an associated item page and a banner statement in wikidata.
The function returns the banner image name on wikimedia commons or 'null' if it
fails to achieve the above objective.

Bug: T98044
Change-Id: Ic05f6c132e4e5f4d96f0d986d068941eb27480a8
---
M WikidataPageBanner.php
M includes/WikidataPageBanner.hooks.php
2 files changed, 47 insertions(+), 1 deletion(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikidataPageBanner 
refs/changes/78/213478/1

diff --git a/WikidataPageBanner.php b/WikidataPageBanner.php
index c634f5b..5ffcff3 100644
--- a/WikidataPageBanner.php
+++ b/WikidataPageBanner.php
@@ -27,9 +27,11 @@
  *
  * $wgPBImage - static pagebanner image url, use only filename, do not prefix 
'File:'
  * $wgBannerNamespace - Namespaces on which to display banner
+ * $wgBannerProperty - Banner property on wikidata which holds commons media 
file
  */
 $wgPBImage = "";
 $wgBannerNamespaces = array( NS_MAIN );
+$wgBannerProperty = "P948";
 
 /* Setup */
 // autoloader
diff --git a/includes/WikidataPageBanner.hooks.php 
b/includes/WikidataPageBanner.hooks.php
index 3b67b7e..bb5bc2b 100644
--- a/includes/WikidataPageBanner.hooks.php
+++ b/includes/WikidataPageBanner.hooks.php
@@ -21,7 +21,13 @@
                } else if ( $article->getParserOutput()->getProperty( 
'articlebanner' ) == null ) {
                        // if the page uses no 'PAGEBANNER' invocation, insert 
default banner,
                        // only set articlebanner property on OutputPage
-                       $bannerurl = self::getBannerUrl( $wgPBImage );
+                       $bannername = self::getWikidataBanner( $title );
+                       $bannerurl = null;
+                       if ( $bannername == null ) {
+                               $bannerurl = self::getBannerUrl( $wgPBImage );
+                       } else {
+                               $bannerurl = self::getBannerUrl( $bannername );
+                       }
                        $ns = $title->getNamespace();
                        // banner only on specified namespaces, and not Main 
Page of wiki
                        // if no image exists with given bannerurl, no banner 
will be added
@@ -151,4 +157,42 @@
                $parser->setFunctionHook( 'PAGEBANNER', 
'WikidataPageBanner::addCustomBanner', SFH_NO_HASH );
                return true;
        }
+
+       /**
+        * Fetches banner from wikidata for the specified page
+        *
+        * @param   Title $title Title of the page
+        * @return  String|null file name of the banner from wikitata 
[description]
+        * or null if none found
+        */
+       public static function getWikidataBanner( $title ) {
+               global $wgBannerProperty;
+               $banner = null;
+               // Ensure Wikibase client is installed
+               if ( class_exists( 'Wikibase\Client\WikibaseClient' ) ) {
+                       $entityIdLookup = 
Wikibase\Client\WikibaseClient::getDefaultInstance()
+                       ->getStore()
+                       ->getEntityIdLookup();
+                       $itemId = $entityIdLookup->getEntityIdForTitle( $title 
);
+                       // check if this page has an associated item page
+                       $entityLookup = 
Wikibase\Client\WikibaseClient::getDefaultInstance()
+                       ->getStore()
+                       ->getEntityLookup();
+                       /** @var Wikibase\DataModel\Entity\Item $item */
+                       if ( $itemId != null ) {
+                               $item = $entityLookup->getEntity( $itemId );
+                               $statements = 
$item->getStatements()->getWithPropertyId(
+                                               new 
Wikibase\DataModel\Entity\PropertyId( $wgBannerProperty )
+                                       )->getBestStatements();
+                               if ( !$statements->isEmpty() ) {
+                                       $statements = $statements->toArray();
+                                       $snak = $statements[0]->getMainSnak();
+                                       if ( $snak instanceof 
Wikibase\DataModel\Snak\PropertyValueSnak ) {
+                                               $banner = 
$snak->getDataValue()->getValue();
+                                       }
+                               }
+                       }
+               }
+               return $banner;
+       }
 }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ic05f6c132e4e5f4d96f0d986d068941eb27480a8
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikidataPageBanner
Gerrit-Branch: master
Gerrit-Owner: Sumit <asthana.sumi...@gmail.com>

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

Reply via email to