MarkTraceur has uploaded a new change for review.

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

Change subject: [WIP] API to fetch data about stashed images
......................................................................

[WIP] 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
---
M autoload.php
M includes/api/ApiQuery.php
A includes/api/ApiQueryAllStashedFiles.php
3 files changed, 147 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/84/258484/1

diff --git a/autoload.php b/autoload.php
index fce9cd4..3f30645 100644
--- a/autoload.php
+++ b/autoload.php
@@ -72,6 +72,7 @@
        'ApiQueryAllMessages' => __DIR__ . 
'/includes/api/ApiQueryAllMessages.php',
        'ApiQueryAllPages' => __DIR__ . '/includes/api/ApiQueryAllPages.php',
        'ApiQueryAllRevisions' => __DIR__ . 
'/includes/api/ApiQueryAllRevisions.php',
+       'ApiQueryAllStashedFiles' => __DIR__ . 
'/includes/api/ApiQueryAllStashedFiles.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 902bca7..c823b77 100644
--- a/includes/api/ApiQuery.php
+++ b/includes/api/ApiQuery.php
@@ -77,6 +77,7 @@
                'allpages' => 'ApiQueryAllPages',
                'allredirects' => 'ApiQueryAllLinks',
                'allrevisions' => 'ApiQueryAllRevisions',
+               'allstashedfiles' => 'ApiQueryAllStashedFiles',
                'alltransclusions' => 'ApiQueryAllLinks',
                'allusers' => 'ApiQueryAllUsers',
                'backlinks' => 'ApiQueryBacklinks',
diff --git a/includes/api/ApiQueryAllStashedFiles.php 
b/includes/api/ApiQueryAllStashedFiles.php
new file mode 100644
index 0000000..b64eae0
--- /dev/null
+++ b/includes/api/ApiQueryAllStashedFiles.php
@@ -0,0 +1,145 @@
+<?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=allstashedfiles module, gets all stashed files for
+ * the current user.
+ *
+ * @ingroup API
+ */
+class ApiQueryAllStashedFiles extends ApiQueryBase {
+
+       public function __construct( ApiQuery $query, $moduleName ) {
+               parent::__construct( $query, $moduleName, 'asf' );
+       }
+
+       public function execute() {
+               $user = $this->getUser();
+
+               if ( $user->isAnon() ) {
+                       $this->dieUsage( 'The upload stash is only available to 
logged-in users.' );
+               }
+
+               if ( !$user->isAllowed( 'upload' ) ) {
+                       $this->dieUsage( 'This API method requires the upload 
permission.' );
+               }
+
+               $db = $this->getDB();
+               $params = $this->extractRequestParams();
+
+               $this->addTables( 'uploadstash' );
+
+               $this->addWhere( array( 'us_user' => $user->getId() ) );
+
+               if ( !is_null( $params[ 'continue' ] ) ) {
+                       $cont = explode( '|', $params[ 'continue' ] );
+                       $this->dieContinueUsageIf( count( $cont ) != 1 );
+                       $op = $params[ 'dir' ] == 'descending' ? '<' : '>';
+                       $cont_from = $db->addQuotes( $cont[0] );
+                       $this->addWhere( "us_key $op= $cont_from" );
+               }
+
+               $dir = ( $params[ 'dir' ] == 'descending' ? 'older' : 'newer' );
+               $from = $params[ 'from' ];
+               $to = $params[ 'to' ];
+               $this->addWhereRange( 'us_key', $dir, $from, $to );
+
+               $this->addOption( 'LIMIT', $params[ 'limit' ] + 1 );
+               $sort = ( $params[ 'dir' ] == 'descending' ? ' DESC' : '' );
+               $this->addOption( 'ORDER BY', 'us_key' . $sort );
+
+               $prop = array_flip( $params[ 'prop' ] );
+               $this->addFieldsIf( array( 'us_key' ), isset( $prop[ 'filekey' 
] ) );
+               $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__ );
+               $files = array();
+               $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_key );
+                               break;
+                       }
+
+                       $item = array();
+
+                       if ( isset( $prop[ 'filekey' ] ) ) {
+                               $item[ 'filekey' ] = $row->us_key;
+                       }
+
+                       if ( isset( $prop[ 'size' ] ) ) {
+                               $item[ 'size' ] = $row->us_size;
+                               $item[ 'width' ] = $row->us_image_width;
+                               $item[ 'height' ] = $row->us_image_height;
+                               $item[ 'bits' ] = $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_key );
+                       }
+               }
+
+               $result->addIndexedTagName( array( 'query', 
$this->getModuleName() ), 'file' );
+       }
+
+       public function getAllowedParams() {
+               return array(
+                       'prop' => array(
+                               ApiBase::PARAM_ISMULTI => true,
+                               ApiBase::PARAM_DFLT => 'filekey',
+                               ApiBase::PARAM_TYPE => array( 'filekey', 
'size', 'type' ),
+                       ),
+
+                       'limit' => array(
+                               ApiBase::PARAM_TYPE => 'integer',
+                               ApiBase::PARAM_DFLT => 20,
+                               ApiBase::PARAM_MIN => 0,
+                               ApiBase::PARAM_MAX => 50,
+                               ApiBase::PARAM_MAX2 => 500,
+                               ApiBase::PARAM_RANGE_ENFORCE => true,
+                       ),
+               );
+       }
+
+       protected function getExamplesMessages() {
+               return array(
+                       'action=query&list=allstashedfiles&asfprop=filekey|size'
+                               => 
'apihelp-query+stashimageinfo-example-simple',
+               );
+       }
+
+       public function getHelpUrls() {
+               return 'https://www.mediawiki.org/wiki/API:Allstashedfiles';
+       }
+}

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I215ac6936185563f4c7b42a4bced65e4b096fd15
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: MarkTraceur <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to