Ladsgroup has uploaded a new change for review.

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

Change subject: [WIP] Add InfoAction handler in repo for tracking subscriptions
......................................................................

[WIP] Add InfoAction handler in repo for tracking subscriptions

What needs to be done:
 - Generalize
 - Send the sitelookup from hooks
 - CI tests

Bug: T145189
Change-Id: I24e9a01e71a99cd5f8789ccd141872ecb014dbed
---
M repo/Wikibase.hooks.php
M repo/Wikibase.php
A repo/includes/Hooks/InfoActionHookHandler.php
M repo/includes/Store/Sql/SqlSubscriptionLookup.php
4 files changed, 187 insertions(+), 11 deletions(-)


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

diff --git a/repo/Wikibase.hooks.php b/repo/Wikibase.hooks.php
index ed95f95..bdbe744 100644
--- a/repo/Wikibase.hooks.php
+++ b/repo/Wikibase.hooks.php
@@ -11,6 +11,7 @@
 use ExtensionRegistry;
 use HistoryPager;
 use Html;
+use IContextSource;
 use Linker;
 use LogEntry;
 use MWException;
@@ -28,12 +29,15 @@
 use StubUserLang;
 use Title;
 use User;
+use wfGetLB;
 use Wikibase\DataModel\Term\DescriptionsProvider;
 use Wikibase\Lib\AutoCommentFormatter;
 use Wikibase\Lib\Store\EntityChangeLookup;
 use Wikibase\Repo\Content\EntityHandler;
+use Wikibase\Repo\Hooks\InfoActionHookHandler;
 use Wikibase\Repo\Hooks\OutputPageEntityIdReader;
 use Wikibase\Repo\WikibaseRepo;
+use Wikibase\Store\Sql\SqlSubscriptionLookup;
 use WikiPage;
 
 /**
@@ -1136,4 +1140,34 @@
                return true;
        }
 
+
+       /**
+        * Adds the Wikis using the entity in action=info
+        *
+        * @param IContextSource $context
+        * @param array $pageInfo
+        *
+        * @return bool
+        */
+       public static function onInfoAction( IContextSource $context, array 
&$pageInfo ) {
+               $wikibaseRepo = WikibaseRepo::getDefaultInstance();
+
+               $namespaceChecker = $wikibaseRepo->getEntityNamespaceLookup();
+               $subscriptionLookup = new SqlSubscriptionLookup( wfGetLB() );
+
+               if ( !$namespaceChecker->isEntityNamespace( 
$context->getTitle()->getNamespace() ) ) {
+                       // shorten out
+                       return true;
+               }
+
+               $infoActionHookHandler = new InfoActionHookHandler(
+                       $namespaceChecker,
+                       $subscriptionLookup
+               );
+
+               $pageInfo = $infoActionHookHandler->handle( $context, $pageInfo 
);
+
+               return true;
+       }
+
 }
diff --git a/repo/Wikibase.php b/repo/Wikibase.php
index ec403ca..660eb98 100644
--- a/repo/Wikibase.php
+++ b/repo/Wikibase.php
@@ -252,6 +252,7 @@
        $wgHooks['ResourceLoaderRegisterModules'][] = 
'Wikibase\RepoHooks::onResourceLoaderRegisterModules';
        $wgHooks['ContentHandlerForModelID'][] = 
'Wikibase\RepoHooks::onContentHandlerForModelID';
        $wgHooks['BeforeDisplayNoArticleText'][] = 
'Wikibase\ViewEntityAction::onBeforeDisplayNoArticleText';
+       $wgHooks['InfoAction'][] = '\Wikibase\RepoHooks::onInfoAction';
 
        // CirrusSearch hooks
        $wgHooks['CirrusSearchMappingConfig'][] = 
'Wikibase\Repo\Hooks\CirrusSearchHookHandlers::onCirrusSearchMappingConfig';
diff --git a/repo/includes/Hooks/InfoActionHookHandler.php 
b/repo/includes/Hooks/InfoActionHookHandler.php
new file mode 100644
index 0000000..2ff407b
--- /dev/null
+++ b/repo/includes/Hooks/InfoActionHookHandler.php
@@ -0,0 +1,123 @@
+<?php
+
+namespace Wikibase\Repo\Hooks;
+
+use Html;
+use IContextSource;
+use Linker;
+use MediaWikiServices;
+use Title;
+use Wikibase\DataModel\SiteLink;
+use Wikibase\Store\Sql\SqlSubscriptionLookup;
+use Wikibase\Lib\Store\EntityNamespaceLookup;
+use Wikibase\NamespaceChecker;
+
+/**
+ * @since 0.5
+ *
+ * @license GPL-2.0+
+ * @author Amir Sarabadani <ladsgr...@gmail.com>
+ */
+class InfoActionHookHandler {
+
+       /**
+        * @var EntityNamespaceLookup
+        */
+       private $namespaceChecker;
+
+       /**
+        * @var SqlSubscriptionLookup
+        */
+       private $subLookup;
+
+       public function __construct( EntityNamespaceLookup $namespaceChecker, 
SqlSubscriptionLookup $subLookup ) {
+               $this->namespaceChecker = $namespaceChecker;
+               $this->subLookup = $subLookup;
+       }
+
+       /**
+        * @param IContextSource $context
+        * @param array $pageInfo
+        *
+        * @return array
+        */
+       public function handle( IContextSource $context, array $pageInfo ) {
+               // Check if wikibase namespace is enabled
+               $title = $context->getTitle();
+
+               if ( $this->namespaceChecker->isEntityNamespace( 
$title->getNamespace() ) && $title->exists() ) {
+                       $pageInfo['header-properties'][] = 
$this->getPageInfoRow( $context, $title );
+               }
+
+
+               return $pageInfo;
+       }
+
+       /**
+        * @param IContextSource $context
+        * @param Title $title
+        *
+        * @return array
+        */
+       public function getPageInfoRow( IContextSource $context, Title $title ) 
{
+               $entities = [ $title->getText() ];
+               $subscriptions = $this->subLookup->queryIdBasedSubscriptions( 
$entities );
+               if ( !$subscriptions ) {
+                       return $this->getUnsubscribedEntities( $context );
+               } else {
+                       return $this->formatSubscriptions( $context, 
$subscriptions, $title );
+               }
+       }
+
+       /**
+        * @param IContextSource $context
+        * @param array $usage
+        * @param Title $title
+        *
+        * @return string[]
+        */
+       private function formatSubscriptions( IContextSource $context, array 
$subscriptions, Title $title ) {
+               $output = '';
+               $siteLookup = 
\MediaWiki\MediaWikiServices::getInstance()->getSiteLookup();
+
+               foreach ( $subscriptions as $subscription ) {
+                       $link = $this->formatSubscription( $subscription, 
$context, $title, $siteLookup );
+                       $output .= Html::rawElement( 'li', [], $link );
+
+               }
+               $output = Html::rawElement( 'ul', [], $output );
+               return array( $context->msg( 'wikibase-pageinfo-subscription' 
), $output );
+       }
+
+       /**
+        * @param IContextSource $context
+        *
+        * @return string[]
+        */
+       private function getUnsubscribedEntities( IContextSource $context ) {
+               return array(
+                       $context->msg( 'wikibase-pageinfo-subscription' ),
+                       $context->msg( 'wikibase-pageinfo-subscription-none' )
+               );
+       }
+
+       /**
+        * @param string $subscription
+        * @param IContextSource $context
+        * @param Title $title
+        *
+        * @return string[]
+        */
+       private function formatSubscription( $subscription, IContextSource 
$context, Title $title, $siteLookup ) {
+               $site = $siteLookup->getSite( $subscription );
+               if ( !$site ) {
+                       return $subscription;
+               }
+               if ( !$site->getInterwikiIds() ) {
+                       return $subscription;
+               }
+               
+               $title = Title::makeTitle( '', 'Special:EntityUsage/' . 
$title->getText(), '', $site->getInterwikiIds()[0] );
+               return Linker::link( $title, $subscription);
+       }
+}
diff --git a/repo/includes/Store/Sql/SqlSubscriptionLookup.php 
b/repo/includes/Store/Sql/SqlSubscriptionLookup.php
index 0d9e9a6..2de140c 100644
--- a/repo/includes/Store/Sql/SqlSubscriptionLookup.php
+++ b/repo/includes/Store/Sql/SqlSubscriptionLookup.php
@@ -63,26 +63,44 @@
        }
 
        /**
-        * For a set of potential subscriptions, returns the existing 
subscriptions.
+        * Returnthe existing subscriptions for given Ids to check
+        *
+        * @param string[]|null $idsToCheck Id strings to check
+        *
+        * @return string[] subscriptions for the given Ids
+        */
+       public function queryIdBasedSubscriptions( array $idsToCheck ) {
+
+               $where['cs_entity_id'] = $idsToCheck;
+               $dbr = $this->dbLoadBalancer->getConnection( DB_SLAVE );
+
+               $rows = $dbr->select(
+                       'wb_changes_subscription',
+                       'cs_subscriber_id',
+                       $where,
+                       __METHOD__
+               );
+
+               $subscriptions = $this->extractColumn( $rows, 
'cs_subscriber_id' );
+               $this->dbLoadBalancer->reuseConnection( $dbr );
+
+               return $subscriptions;
+       }
+
+       /**
+        * Return the existing subscriptions for given entities.
         *
         * @param DatabaseBase $db
-        * @param string $subscriber
         * @param string[]|null $idsToCheck Id strings to check
         *
         * @return string[] Entity ID strings from $subscriptions which 
$subscriber is subscribed to.
         */
-       private function querySubscriptions( DatabaseBase $db, $subscriber, 
array $idsToCheck = null ) {
-               $where = array(
-                       'cs_subscriber_id' => $subscriber,
-               );
-
-               if ( $idsToCheck ) {
-                       $where['cs_entity_id'] = $idsToCheck;
-               }
+       private function querySubscriptions( DatabaseBase $db, array 
$idsToCheck ) {
+               $where['cs_entity_id'] = $idsToCheck;
 
                $rows = $db->select(
                        'wb_changes_subscription',
-                       'cs_entity_id',
+                       'cs_subscriber_id',
                        $where,
                        __METHOD__
                );

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I24e9a01e71a99cd5f8789ccd141872ecb014dbed
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Ladsgroup <ladsgr...@gmail.com>

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

Reply via email to