Update git submodules
[mediawiki.git] / includes / api / ApiQueryMyStashedFiles.php
blobf760bbefe101464777d6330dce7577781822d5ae
1 <?php
2 /**
3 * API for MediaWiki 1.27+
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
23 use Wikimedia\ParamValidator\ParamValidator;
24 use Wikimedia\ParamValidator\TypeDef\IntegerDef;
26 /**
27 * action=query&list=mystashedfiles module, gets all stashed files for
28 * the current user.
30 * @ingroup API
32 class ApiQueryMyStashedFiles extends ApiQueryBase {
34 public function __construct( ApiQuery $query, $moduleName ) {
35 parent::__construct( $query, $moduleName, 'msf' );
38 public function execute() {
39 $user = $this->getUser();
41 if ( !$user->isRegistered() ) {
42 $this->dieWithError( 'apierror-mustbeloggedin-uploadstash', 'stashnotloggedin' );
45 // Note: If user is logged in but cannot upload, they can still see
46 // the list of stashed uploads...but it will probably be empty.
48 $params = $this->extractRequestParams();
50 $this->addTables( 'uploadstash' );
52 $this->addFields( [ 'us_id', 'us_key', 'us_status' ] );
54 $this->addWhere( [ 'us_user' => $user->getId() ] );
56 if ( $params['continue'] !== null ) {
57 $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'int' ] );
58 $this->addWhere( $this->getDB()->buildComparison( '>=', [
59 'us_id' => (int)$cont[0],
60 ] ) );
63 $this->addOption( 'LIMIT', $params['limit'] + 1 );
64 $this->addOption( 'ORDER BY', 'us_id' );
66 $prop = array_fill_keys( $params['prop'], true );
67 $this->addFieldsIf(
69 'us_size',
70 'us_image_width',
71 'us_image_height',
72 'us_image_bits'
75 isset( $prop['size'] )
77 $this->addFieldsIf( [ 'us_mime', 'us_media_type' ], isset( $prop['type'] ) );
79 $res = $this->select( __METHOD__ );
80 $result = $this->getResult();
81 $count = 0;
83 foreach ( $res as $row ) {
84 if ( ++$count > $params['limit'] ) {
85 // We've reached the one extra which shows that there are
86 // additional files to be had. Stop here...
87 $this->setContinueEnumParameter( 'continue', $row->us_id );
88 break;
91 $item = [
92 'filekey' => $row->us_key,
93 'status' => $row->us_status,
96 if ( isset( $prop['size'] ) ) {
97 $item['size'] = (int)$row->us_size;
98 $item['width'] = (int)$row->us_image_width;
99 $item['height'] = (int)$row->us_image_height;
100 $item['bits'] = (int)$row->us_image_bits;
103 if ( isset( $prop['type'] ) ) {
104 $item['mimetype'] = $row->us_mime;
105 $item['mediatype'] = $row->us_media_type;
108 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $item );
110 if ( !$fit ) {
111 $this->setContinueEnumParameter( 'continue', $row->us_id );
112 break;
116 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'file' );
119 public function getAllowedParams() {
120 return [
121 'prop' => [
122 ParamValidator::PARAM_ISMULTI => true,
123 ParamValidator::PARAM_DEFAULT => '',
124 ParamValidator::PARAM_TYPE => [ 'size', 'type' ],
125 ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
128 'limit' => [
129 ParamValidator::PARAM_TYPE => 'limit',
130 ParamValidator::PARAM_DEFAULT => 10,
131 IntegerDef::PARAM_MIN => 1,
132 IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
133 IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2,
136 'continue' => [
137 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
142 protected function getExamplesMessages() {
143 return [
144 'action=query&list=mystashedfiles&msfprop=size'
145 => 'apihelp-query+mystashedfiles-example-simple',
149 public function getHelpUrls() {
150 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:mystashedfiles';