Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 74
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiQueryMyStashedFiles
0.00% covered (danger)
0.00%
0 / 73
0.00% covered (danger)
0.00%
0 / 5
156
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 49
0.00% covered (danger)
0.00%
0 / 1
72
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
2
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 getHelpUrls
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * API for MediaWiki 1.27+
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 */
22
23namespace MediaWiki\Api;
24
25use Wikimedia\ParamValidator\ParamValidator;
26use Wikimedia\ParamValidator\TypeDef\IntegerDef;
27
28/**
29 * action=query&list=mystashedfiles module, gets all stashed files for
30 * the current user.
31 *
32 * @ingroup API
33 */
34class ApiQueryMyStashedFiles extends ApiQueryBase {
35
36    public function __construct( ApiQuery $query, string $moduleName ) {
37        parent::__construct( $query, $moduleName, 'msf' );
38    }
39
40    public function execute() {
41        $user = $this->getUser();
42
43        if ( !$user->isRegistered() ) {
44            $this->dieWithError( 'apierror-mustbeloggedin-uploadstash', 'stashnotloggedin' );
45        }
46
47        // Note: If user is logged in but cannot upload, they can still see
48        // the list of stashed uploads...but it will probably be empty.
49
50        $params = $this->extractRequestParams();
51
52        $this->addTables( 'uploadstash' );
53
54        $this->addFields( [ 'us_id', 'us_key', 'us_status' ] );
55
56        $this->addWhere( [ 'us_user' => $user->getId() ] );
57
58        if ( $params['continue'] !== null ) {
59            $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'int' ] );
60            $this->addWhere( $this->getDB()->buildComparison( '>=', [
61                'us_id' => (int)$cont[0],
62            ] ) );
63        }
64
65        $this->addOption( 'LIMIT', $params['limit'] + 1 );
66        $this->addOption( 'ORDER BY', 'us_id' );
67
68        $prop = array_fill_keys( $params['prop'], true );
69        $this->addFieldsIf(
70            [
71                'us_size',
72                'us_image_width',
73                'us_image_height',
74                'us_image_bits'
75            ],
76
77            isset( $prop['size'] )
78        );
79        $this->addFieldsIf( [ 'us_mime', 'us_media_type' ], isset( $prop['type'] ) );
80
81        $res = $this->select( __METHOD__ );
82        $result = $this->getResult();
83        $count = 0;
84
85        foreach ( $res as $row ) {
86            if ( ++$count > $params['limit'] ) {
87                // We've reached the one extra which shows that there are
88                // additional files to be had. Stop here...
89                $this->setContinueEnumParameter( 'continue', $row->us_id );
90                break;
91            }
92
93            $item = [
94                'filekey' => $row->us_key,
95                'status' => $row->us_status,
96            ];
97
98            if ( isset( $prop['size'] ) ) {
99                $item['size'] = (int)$row->us_size;
100                $item['width'] = (int)$row->us_image_width;
101                $item['height'] = (int)$row->us_image_height;
102                $item['bits'] = (int)$row->us_image_bits;
103            }
104
105            if ( isset( $prop['type'] ) ) {
106                $item['mimetype'] = $row->us_mime;
107                $item['mediatype'] = $row->us_media_type;
108            }
109
110            $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $item );
111
112            if ( !$fit ) {
113                $this->setContinueEnumParameter( 'continue', $row->us_id );
114                break;
115            }
116        }
117
118        $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'file' );
119    }
120
121    public function getAllowedParams() {
122        return [
123            'prop' => [
124                ParamValidator::PARAM_ISMULTI => true,
125                ParamValidator::PARAM_DEFAULT => '',
126                ParamValidator::PARAM_TYPE => [ 'size', 'type' ],
127                ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
128            ],
129
130            'limit' => [
131                ParamValidator::PARAM_TYPE => 'limit',
132                ParamValidator::PARAM_DEFAULT => 10,
133                IntegerDef::PARAM_MIN => 1,
134                IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
135                IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2,
136            ],
137
138            'continue' => [
139                ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
140            ],
141        ];
142    }
143
144    protected function getExamplesMessages() {
145        return [
146            'action=query&list=mystashedfiles&msfprop=size'
147                => 'apihelp-query+mystashedfiles-example-simple',
148        ];
149    }
150
151    public function getHelpUrls() {
152        return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:mystashedfiles';
153    }
154}
155
156/** @deprecated class alias since 1.43 */
157class_alias( ApiQueryMyStashedFiles::class, 'ApiQueryMyStashedFiles' );