From 0f9c0b286f39ab5fca2b476054831eea1e0370cc Mon Sep 17 00:00:00 2001 From: Mark Holmquist Date: Fri, 11 Dec 2015 11:50:35 -0600 Subject: [PATCH] API to fetch data about stashed images Only works for the currently logged-in user (I'm not sure how that works with bots, if OAuth will do that correctly or whatever) but will help us do some neat stuff with tools that use the stash - including resuming uploads. Bug: T85561 Change-Id: I215ac6936185563f4c7b42a4bced65e4b096fd15 --- autoload.php | 1 + includes/api/ApiQuery.php | 1 + includes/api/ApiQueryMyStashedFiles.php | 151 ++++++++++++++++++++++++ includes/api/i18n/en.json | 7 ++ includes/api/i18n/qqq.json | 6 + 5 files changed, 166 insertions(+) create mode 100644 includes/api/ApiQueryMyStashedFiles.php diff --git a/autoload.php b/autoload.php index 1f8c93a85c..02af18c038 100644 --- a/autoload.php +++ b/autoload.php @@ -72,6 +72,7 @@ $wgAutoloadLocalClasses = array( 'ApiQueryAllMessages' => __DIR__ . '/includes/api/ApiQueryAllMessages.php', 'ApiQueryAllPages' => __DIR__ . '/includes/api/ApiQueryAllPages.php', 'ApiQueryAllRevisions' => __DIR__ . '/includes/api/ApiQueryAllRevisions.php', + 'ApiQueryMyStashedFiles' => __DIR__ . '/includes/api/ApiQueryMyStashedFiles.php', 'ApiQueryAllUsers' => __DIR__ . '/includes/api/ApiQueryAllUsers.php', 'ApiQueryBacklinks' => __DIR__ . '/includes/api/ApiQueryBacklinks.php', 'ApiQueryBacklinksprop' => __DIR__ . '/includes/api/ApiQueryBacklinksprop.php', diff --git a/includes/api/ApiQuery.php b/includes/api/ApiQuery.php index d12a68fc98..8015d65bb9 100644 --- a/includes/api/ApiQuery.php +++ b/includes/api/ApiQuery.php @@ -77,6 +77,7 @@ class ApiQuery extends ApiBase { 'allpages' => 'ApiQueryAllPages', 'allredirects' => 'ApiQueryAllLinks', 'allrevisions' => 'ApiQueryAllRevisions', + 'mystashedfiles' => 'ApiQueryMyStashedFiles', 'alltransclusions' => 'ApiQueryAllLinks', 'allusers' => 'ApiQueryAllUsers', 'backlinks' => 'ApiQueryBacklinks', diff --git a/includes/api/ApiQueryMyStashedFiles.php b/includes/api/ApiQueryMyStashedFiles.php new file mode 100644 index 0000000000..d33dbfd1f8 --- /dev/null +++ b/includes/api/ApiQueryMyStashedFiles.php @@ -0,0 +1,151 @@ +getUser(); + + if ( $user->isAnon() ) { + $this->dieUsage( 'The upload stash is only available to logged-in users.', 'stashnotloggedin' ); + } + + // Note: If user is logged in but cannot upload, they can still see + // the list of stashed uploads...but it will probably be empty. + + $db = $this->getDB(); + $params = $this->extractRequestParams(); + + $this->addTables( 'uploadstash' ); + + $this->addFields( array( 'us_id', 'us_key', 'us_status' ) ); + + $this->addWhere( array( 'us_user' => $user->getId() ) ); + + if ( $params['continue'] !== null ) { + $cont = explode( '|', $params['continue'] ); + $this->dieContinueUsageIf( count( $cont ) != 1 ); + $cont_from = (int)$cont[0]; + $this->dieContinueUsageIf( strval( $cont_from ) !== $cont[0] ); + $this->addWhere( "us_id >= $cont_from" ); + } + + $this->addOption( 'LIMIT', $params['limit'] + 1 ); + $this->addOption( 'ORDER BY', 'us_id' ); + + $prop = array_flip( $params['prop'] ); + $this->addFieldsIf( + array( + 'us_size', + 'us_image_width', + 'us_image_height', + 'us_image_bits' + ), + + isset( $prop['size'] ) + ); + $this->addFieldsIf( array( 'us_mime', 'us_media_type' ), isset( $prop['type'] ) ); + + $res = $this->select( __METHOD__ ); + $result = $this->getResult(); + $count = 0; + + foreach ( $res as $row ) { + if ( ++$count > $params['limit'] ) { + // We've reached the one extra which shows that there are + // additional files to be had. Stop here... + $this->setContinueEnumParameter( 'continue', $row->us_id ); + break; + } + + $item = array( + 'filekey' => $row->us_key, + 'status' => $row->us_status, + ); + + if ( isset( $prop['size'] ) ) { + $item['size'] = (int) $row->us_size; + $item['width'] = (int) $row->us_image_width; + $item['height'] = (int) $row->us_image_height; + $item['bits'] = (int) $row->us_image_bits; + } + + if ( isset( $prop['type'] ) ) { + $item['mimetype'] = $row->us_mime; + $item['mediatype'] = $row->us_media_type; + } + + $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $item ); + + if ( !$fit ) { + $this->setContinueEnumParameter( 'continue', $row->us_id ); + break; + } + } + + $result->addIndexedTagName( array( 'query', $this->getModuleName() ), 'file' ); + } + + public function getAllowedParams() { + return array( + 'prop' => array( + ApiBase::PARAM_ISMULTI => true, + ApiBase::PARAM_DFLT => '', + ApiBase::PARAM_TYPE => array( 'size', 'type' ), + ApiBase::PARAM_HELP_MSG_PER_VALUE => array(), + ), + + 'limit' => array( + ApiBase::PARAM_TYPE => 'limit', + ApiBase::PARAM_DFLT => 10, + ApiBase::PARAM_MIN => 1, + ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1, + ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2, + ), + + 'continue' => array( + ApiBase::PARAM_HELP_MSG => 'api-help-param-continue', + ), + ); + } + + protected function getExamplesMessages() { + return array( + 'action=query&list=mystashedfiles&msfprop=size' + => 'apihelp-query+mystashedfiles-example-simple', + ); + } + + public function getHelpUrls() { + return 'https://www.mediawiki.org/wiki/API:mystashedfiles'; + } +} diff --git a/includes/api/i18n/en.json b/includes/api/i18n/en.json index ea854116fe..2fb9a50961 100644 --- a/includes/api/i18n/en.json +++ b/includes/api/i18n/en.json @@ -514,6 +514,13 @@ "apihelp-query+allrevisions-example-user": "List the last 50 contributions by user Example.", "apihelp-query+allrevisions-example-ns-main": "List the first 50 revisions in the main namespace.", + "apihelp-query+mystashedfiles-description": "Get a list of files in the current user's upload stash.", + "apihelp-query+mystashedfiles-param-prop": "Which properties to fetch for the files.", + "apihelp-query+mystashedfiles-paramvalue-prop-size": "Fetch the file size and image dimensions.", + "apihelp-query+mystashedfiles-paramvalue-prop-type": "Fetch the file's MIME type and media type.", + "apihelp-query+mystashedfiles-param-limit": "How many files to get.", + "apihelp-query+mystashedfiles-example-simple": "Get the filekey, file size, and pixel size of files in the current user's upload stash.", + "apihelp-query+alltransclusions-description": "List all transclusions (pages embedded using {{x}}), including non-existing.", "apihelp-query+alltransclusions-param-from": "The title of the transclusion to start enumerating from.", "apihelp-query+alltransclusions-param-to": "The title of the transclusion to stop enumerating at.", diff --git a/includes/api/i18n/qqq.json b/includes/api/i18n/qqq.json index 31541a5947..804a8d6e96 100644 --- a/includes/api/i18n/qqq.json +++ b/includes/api/i18n/qqq.json @@ -484,6 +484,12 @@ "apihelp-query+allrevisions-param-generatetitles": "{{doc-apihelp-param|query+allrevisions|generatetitles}}", "apihelp-query+allrevisions-example-user": "{{doc-apihelp-example|query+allrevisions}}", "apihelp-query+allrevisions-example-ns-main": "{{doc-apihelp-example|query+allrevisions}}", + "apihelp-query+mystashedfiles-description": "{{doc-apihelp-description|query+mystashedfiles}}", + "apihelp-query+mystashedfiles-param-prop": "{{doc-apihelp-param|query+mystashedfiles|prop|paramvalues=1}}", + "apihelp-query+mystashedfiles-paramvalue-prop-size": "{{doc-apihelp-paramvalue|query+mystashedfiles|prop|size}}", + "apihelp-query+mystashedfiles-paramvalue-prop-type": "{{doc-apihelp-paramvalue|query+mystashedfiles|prop|type}}", + "apihelp-query+mystashedfiles-param-limit": "{{doc-apihelp-param|query+mystashedfiles|limit}}", + "apihelp-query+mystashedfiles-example-simple": "{{doc-apihelp-example|query+mystashedfiles}}", "apihelp-query+alltransclusions-description": "{{doc-apihelp-description|query+alltransclusions}}", "apihelp-query+alltransclusions-param-from": "{{doc-apihelp-param|query+alltransclusions|from}}", "apihelp-query+alltransclusions-param-to": "{{doc-apihelp-param|query+alltransclusions|to}}", -- 2.20.1