MarkTraceur has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/382604 )
Change subject: Add wbgetmediainfo API module ...................................................................... Add wbgetmediainfo API module This is basically a shortcut to wbgetentities that allows the API user to get a MediaInfo entity without querying for the MediaInfo ID (either by getting the page info and looking at the page_id, or by querying the new page_props entry), and using the file page title instead. n.b. https://gerrit.wikimedia.org/r/382603 must be merged first. Bug: T177022 Change-Id: I1db3b2f1ea303af99da86ea1635d4f623a9139e3 --- M extension.json A src/Api/ApiGetMediaInfo.php M src/WikibaseMediaInfoHooks.php 3 files changed, 173 insertions(+), 0 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikibaseMediaInfo refs/changes/04/382604/1 diff --git a/extension.json b/extension.json index ccf7452..98e1789 100644 --- a/extension.json +++ b/extension.json @@ -13,6 +13,7 @@ "MediaInfoNamespace": 144, "MediaInfoNamespaceTalk": 145 }, + "callback": "Wikibase\\MediaInfo\\WikibaseMediaInfoHooks::onRegistration", "namespaces": [ { "id": 144, diff --git a/src/Api/ApiGetMediaInfo.php b/src/Api/ApiGetMediaInfo.php new file mode 100644 index 0000000..c65b655 --- /dev/null +++ b/src/Api/ApiGetMediaInfo.php @@ -0,0 +1,138 @@ +<?php + +namespace Wikibase\MediaInfo\Api; + +use Wikibase\Repo\Api\GetEntities; +use Wikibase\Repo\WikibaseRepo; +use Title; +use ApiMain; +use InvalidArgumentException; +use MediaWiki\MediaWikiServices; +use Wikibase\StringNormalizer; +use Wikibase\LanguageFallbackChainFactory; +use Wikibase\Repo\SiteLinkTargetProvider; +use Wikibase\DataModel\Services\Entity\EntityPrefetcher; +use Wikibase\Repo\Api\ApiErrorReporter; +use Wikibase\Repo\Api\ResultBuilder; +use Wikibase\Lib\Store\EntityRevisionLookup; +use Wikibase\DataModel\Entity\EntityIdParser; +use Wikibase\MediaInfo\DataModel\MediaInfo; + + +/** + * MediaWiki API module for fetching MediaInfo page by ID or by + * associated filename. + * + * @licanse GPL-2.0+ + * @author Mark Holmquist <[email protected]> + */ +class ApiGetMediaInfo extends GetEntities { + /** + * @param ApiMain $mainModule + * @param string $moduleName + * @param StringNormalizer $stringNormalizer + * @param LanguageFallbackChainFactory $languageFallbackChainFactory + * @param SiteLinkTargetProvider $siteLinkTargetProvider + * @param EntityPrefetcher $entityPrefetcher + * @param string[] $siteLinkGroups + * @param ApiErrorReporter $errorReporter + * @param ResultBuilder $resultBuilder + * @param EntityRevisionLookup $entityRevisionLookup + * @param EntityIdParser $idParser + * + * @see ApiBase::__construct + */ + public function __construct( + ApiMain $mainModule, + $moduleName, + StringNormalizer $stringNormalizer, + LanguageFallbackChainFactory $languageFallbackChainFactory, + SiteLinkTargetProvider $siteLinkTargetProvider, + EntityPrefetcher $entityPrefetcher, + array $siteLinkGroups, + ApiErrorReporter $errorReporter, + ResultBuilder $resultBuilder, + EntityRevisionLookup $entityRevisionLookup, + EntityIdParser $idParser + ) { + parent::__construct( + $mainModule, + $moduleName, + $stringNormalizer, + $languageFallbackChainFactory, + $siteLinkTargetProvider, + $entityPrefetcher, + $siteLinkGroups, + $errorReporter, + $resultBuilder, + $entityRevisionLookup, + $idParser + ); + } + + /** + * @see ApiBase::execute() + */ + public function execute() { + $this->getMain()->setCacheMode( 'public' ); + + $wikibaseRepo = WikibaseRepo::getDefaultInstance(); + $params = $this->extractRequestParams(); + + if ( !isset( $params['titles'] ) ) { + $this->errorReporter->dieWithError( + 'mediainfo-api-no-titles', + 'param-missing' + ); + } + + $params['sitefilter'] = null; + + $resolveRedirects = $params['redirects'] === 'yes'; + + $titles = $params['titles']; + + $entityIds = []; + + foreach ( $titles as $title ) { + $titleObj = Title::newFromText( $title ); + $pageId = $titleObj->getArticleID(); + $entityId = $wikibaseRepo->getEntityIdComposer()->composeEntityId( + '', + MediaInfo::ENTITY_TYPE, + $pageId + ); + + $entityIds[] = $entityId; + } + + $stats = MediaWikiServices::getInstance()->getStatsdDataFactory(); + $stats->updateCount( 'wikibase.repo.api.getmediainfo.entities', count( $entityIds ) ); + + $entityRevisions = $this->getEntityRevisionsFromEntityIds( $entityIds, $resolveRedirects ); + + foreach ( $entityRevisions as $sourceEntityId => $entityRevision ) { + $this->handleEntity( $sourceEntityId, $entityRevision, $params ); + } + + $this->resultBuilder->markSuccess( 1 ); + } + + /** + * @see ApiBase::getAllowedParams + */ + protected function getAllowedParams() { + $prior = parent::getAllowedParams(); + + unset( $prior['ids'] ); + unset( $prior['sites'] ); + unset( $prior['sitefilter'] ); + + return $prior; + } + + protected function getExamplesMessages() { + return [ + ]; + } +} diff --git a/src/WikibaseMediaInfoHooks.php b/src/WikibaseMediaInfoHooks.php index 18100fc..724bb76 100644 --- a/src/WikibaseMediaInfoHooks.php +++ b/src/WikibaseMediaInfoHooks.php @@ -3,9 +3,12 @@ namespace Wikibase\MediaInfo; use ImagePage; +use ApiMain; use MediaWiki\MediaWikiServices; use Wikibase\MediaInfo\DataModel\MediaInfo; use Wikibase\Repo\WikibaseRepo; +use Wikibase\MediaInfo\Api\ApiGetMediaInfo; +use Wikibase\Repo\SiteLinkTargetProvider; /** * MediaWiki hook handlers for the Wikibase MediaInfo extension. @@ -65,4 +68,35 @@ $html .= '<h2>' . $linkHtml . '</h2>'; } + public static function onRegistration() { + global $wgAPIModules; + + $wgAPIModules['wbgetmediainfo'] = [ + 'class' => ApiGetMediaInfo::class, + 'factory' => function( ApiMain $apiMain, $moduleName ) { + $wikibaseRepo = WikibaseRepo::getDefaultInstance(); + $settings = $wikibaseRepo->getSettings(); + $apiHelperFactory = $wikibaseRepo->getApiHelperFactory( $apiMain->getContext() ); + + $siteLinkTargetProvider = new SiteLinkTargetProvider( + $wikibaseRepo->getSiteLookup(), + $settings->getSetting( 'specialSiteLinkGroups' ) + ); + + return new ApiGetMediaInfo( + $apiMain, + $moduleName, + $wikibaseRepo->getStringNormalizer(), + $wikibaseRepo->getLanguageFallbackChainFactory(), + $siteLinkTargetProvider, + $wikibaseRepo->getStore()->getEntityPrefetcher(), + $settings->getSetting( 'siteLinkGroups' ), + $apiHelperFactory->getErrorReporter( $apiMain ), + $apiHelperFactory->getResultBuilder( $apiMain ), + $wikibaseRepo->getEntityRevisionLookup(), + $wikibaseRepo->getEntityIdParser() + ); + } + ]; + } } -- To view, visit https://gerrit.wikimedia.org/r/382604 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I1db3b2f1ea303af99da86ea1635d4f623a9139e3 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/WikibaseMediaInfo Gerrit-Branch: master Gerrit-Owner: MarkTraceur <[email protected]> _______________________________________________ MediaWiki-commits mailing list [email protected] https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits
