API to fetch data about stashed images
authorMark Holmquist <mtraceur@member.fsf.org>
Fri, 11 Dec 2015 17:50:35 +0000 (11:50 -0600)
committerMark Holmquist <mtraceur@member.fsf.org>
Tue, 12 Jan 2016 20:57:46 +0000 (14:57 -0600)
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
includes/api/ApiQuery.php
includes/api/ApiQueryMyStashedFiles.php [new file with mode: 0644]
includes/api/i18n/en.json
includes/api/i18n/qqq.json

index 1f8c93a..02af18c 100644 (file)
@@ -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',
index d12a68f..8015d65 100644 (file)
@@ -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 (file)
index 0000000..d33dbfd
--- /dev/null
@@ -0,0 +1,151 @@
+<?php
+/**
+ * API for MediaWiki 1.27+
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ */
+
+/**
+ * action=query&list=mystashedfiles module, gets all stashed files for
+ * the current user.
+ *
+ * @ingroup API
+ */
+class ApiQueryMyStashedFiles extends ApiQueryBase {
+
+       public function __construct( ApiQuery $query, $moduleName ) {
+               parent::__construct( $query, $moduleName, 'msf' );
+       }
+
+       public function execute() {
+               $user = $this->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';
+       }
+}
index ea85411..2fb9a50 100644 (file)
        "apihelp-query+allrevisions-example-user": "List the last 50 contributions by user <kbd>Example</kbd>.",
        "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 &#123;&#123;x&#125;&#125;), 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.",
index 31541a5..804a8d6 100644 (file)
        "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}}",