Ladsgroup has uploaded a new change for review.

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

Change subject: Introduce prop=wbcentityusage in API to expose wbc_entity_usage
......................................................................

Introduce prop=wbcentityusage in API to expose wbc_entity_usage

Bug: T103091
Change-Id: Ic69ef8e4d6ebeecb423234f2aac8e3209e466c51
---
M client/WikibaseClient.php
A client/includes/Api/EntityUsage.php
2 files changed, 123 insertions(+), 0 deletions(-)


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

diff --git a/client/WikibaseClient.php b/client/WikibaseClient.php
index 7516dbf..977cd74 100644
--- a/client/WikibaseClient.php
+++ b/client/WikibaseClient.php
@@ -195,6 +195,8 @@
                }
        );
 
+       $wgAPIPropModules['wbcentityusage'] = 
Wikibase\Client\Api\ApiQueryEntityUsage::class;
+
        // Special page registration
        $wgSpecialPages['UnconnectedPages'] = 
'Wikibase\Client\Specials\SpecialUnconnectedPages';
        $wgSpecialPages['PagesWithBadges'] = 
'Wikibase\Client\Specials\SpecialPagesWithBadges';
diff --git a/client/includes/Api/EntityUsage.php 
b/client/includes/Api/EntityUsage.php
new file mode 100644
index 0000000..9acd07e
--- /dev/null
+++ b/client/includes/Api/EntityUsage.php
@@ -0,0 +1,121 @@
+<?php
+
+namespace Wikibase\Client\Api;
+
+use ApiBase;
+use ApiQuery;
+use ApiQueryBase;
+use ApiResult;
+
+/**
+ * API module to get the usage of entities.
+ *
+ * @since 0.1
+ *
+ * @license GPL-2.0+
+ * @author Amir Sarabadani < ladsgr...@gmail.com >
+ */
+class ApiQueryEntityUsage extends ApiQueryBase {
+
+       public function __construct( ApiQuery $query, $moduleName ) {
+               parent::__construct( $query, $moduleName, 'euuu' );
+       }
+
+       public function execute() {
+               if ( $this->getPageSet()->getGoodTitleCount() == 0 ) {
+                       return;
+               }
+
+               $params = $this->extractRequestParams();
+
+               $this->addFields( [
+                       'eu_page_id',
+                       'eu_entity_id',
+                       'eu_aspect'
+               ] );
+
+               $this->addTables( 'wbc_entity_usage' );
+               $this->addWhereFld( 'eu_page_id', array_keys( 
$this->getPageSet()->getGoodTitles() ) );
+
+               if ( !is_null( $params['continue'] ) ) {
+                       $op = $params['dir'] == 'descending' ? '<' : '>';
+                       $db = $this->getDB();
+                       $pagecontinue = $params['continue'];
+                       $this->addWhere(
+                               "eu_page_id $op $pagecontinue"
+                       );
+               }
+
+               $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
+               if ( isset( $params['aspect'] ) ) {
+                       $this->addWhereFld( 'eu_aspect', $params['aspect'] );
+               }
+
+               $this->addOption( 'LIMIT', $params['limit'] + 1 );
+               $res = $this->select( __METHOD__ );
+
+               $count = 0;
+               foreach ( $res as $row ) {
+                       if ( ++$count > $params['limit'] ) {
+                               // We've reached the one extra which shows that
+                               // there are additional pages to be had. Stop 
here...
+                               $this->setContinueEnumParameter(
+                                       'continue',
+                                       "{$row->eu_page_id}"
+                               );
+                               break;
+                       }
+                       $entry = [ 'aspect' => $row->eu_aspect ];
+
+                       ApiResult::setContentValue( $entry, 'entity', 
$row->eu_entity_id );
+
+                       $fit = $this->addPageSubItem( $row->eu_page_id, $entry 
);
+                       if ( !$fit ) {
+                               $this->setContinueEnumParameter(
+                                       'continue',
+                                       "{$row->eu_page_id}"
+                               );
+                               break;
+                       }
+               }
+       }
+
+       public function getCacheMode( $params ) {
+               return 'public';
+       }
+
+       public function getAllowedParams() {
+               return [
+                       'aspect' => null,
+                       'dir' => [
+                               ApiBase::PARAM_DFLT => 'ascending',
+                               ApiBase::PARAM_TYPE => [
+                                       'ascending',
+                                       'descending'
+                               ]
+                       ],
+                       'limit' => [
+                               ApiBase::PARAM_DFLT => 10,
+                               ApiBase::PARAM_TYPE => 'limit',
+                               ApiBase::PARAM_MIN => 1,
+                               ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
+                               ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
+                       ],
+                       'continue' => [
+                               ApiBase::PARAM_HELP_MSG => 
'api-help-param-continue',
+                       ],
+               ];
+       }
+
+       protected function getExamplesMessages() {
+               return [
+                       'action=query&prop=wbcentityusage&titles=Main%20Page'
+                               => 'apihelp-query+entityusage-example-simple',
+               ];
+       }
+
+       public function getHelpUrls() {
+               return 
'https://www.mediawiki.org/wiki/API:Wikibase/EntityUsage';
+       }
+
+}

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ic69ef8e4d6ebeecb423234f2aac8e3209e466c51
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