Merge "Improve wording and tense in some "page language" strings"
[lhc/web/wiklou.git] / includes / api / ApiQueryMyStashedFiles.php
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
23 /**
24 * action=query&list=mystashedfiles module, gets all stashed files for
25 * the current user.
26 *
27 * @ingroup API
28 */
29 class ApiQueryMyStashedFiles extends ApiQueryBase {
30
31 public function __construct( ApiQuery $query, $moduleName ) {
32 parent::__construct( $query, $moduleName, 'msf' );
33 }
34
35 public function execute() {
36 $user = $this->getUser();
37
38 if ( $user->isAnon() ) {
39 $this->dieUsage( 'The upload stash is only available to logged-in users.', 'stashnotloggedin' );
40 }
41
42 // Note: If user is logged in but cannot upload, they can still see
43 // the list of stashed uploads...but it will probably be empty.
44
45 $db = $this->getDB();
46 $params = $this->extractRequestParams();
47
48 $this->addTables( 'uploadstash' );
49
50 $this->addFields( array( 'us_id', 'us_key', 'us_status' ) );
51
52 $this->addWhere( array( 'us_user' => $user->getId() ) );
53
54 if ( $params['continue'] !== null ) {
55 $cont = explode( '|', $params['continue'] );
56 $this->dieContinueUsageIf( count( $cont ) != 1 );
57 $cont_from = (int)$cont[0];
58 $this->dieContinueUsageIf( strval( $cont_from ) !== $cont[0] );
59 $this->addWhere( "us_id >= $cont_from" );
60 }
61
62 $this->addOption( 'LIMIT', $params['limit'] + 1 );
63 $this->addOption( 'ORDER BY', 'us_id' );
64
65 $prop = array_flip( $params['prop'] );
66 $this->addFieldsIf(
67 array(
68 'us_size',
69 'us_image_width',
70 'us_image_height',
71 'us_image_bits'
72 ),
73
74 isset( $prop['size'] )
75 );
76 $this->addFieldsIf( array( 'us_mime', 'us_media_type' ), isset( $prop['type'] ) );
77
78 $res = $this->select( __METHOD__ );
79 $result = $this->getResult();
80 $count = 0;
81
82 foreach ( $res as $row ) {
83 if ( ++$count > $params['limit'] ) {
84 // We've reached the one extra which shows that there are
85 // additional files to be had. Stop here...
86 $this->setContinueEnumParameter( 'continue', $row->us_id );
87 break;
88 }
89
90 $item = array(
91 'filekey' => $row->us_key,
92 'status' => $row->us_status,
93 );
94
95 if ( isset( $prop['size'] ) ) {
96 $item['size'] = (int) $row->us_size;
97 $item['width'] = (int) $row->us_image_width;
98 $item['height'] = (int) $row->us_image_height;
99 $item['bits'] = (int) $row->us_image_bits;
100 }
101
102 if ( isset( $prop['type'] ) ) {
103 $item['mimetype'] = $row->us_mime;
104 $item['mediatype'] = $row->us_media_type;
105 }
106
107 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $item );
108
109 if ( !$fit ) {
110 $this->setContinueEnumParameter( 'continue', $row->us_id );
111 break;
112 }
113 }
114
115 $result->addIndexedTagName( array( 'query', $this->getModuleName() ), 'file' );
116 }
117
118 public function getAllowedParams() {
119 return array(
120 'prop' => array(
121 ApiBase::PARAM_ISMULTI => true,
122 ApiBase::PARAM_DFLT => '',
123 ApiBase::PARAM_TYPE => array( 'size', 'type' ),
124 ApiBase::PARAM_HELP_MSG_PER_VALUE => array(),
125 ),
126
127 'limit' => array(
128 ApiBase::PARAM_TYPE => 'limit',
129 ApiBase::PARAM_DFLT => 10,
130 ApiBase::PARAM_MIN => 1,
131 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
132 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2,
133 ),
134
135 'continue' => array(
136 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
137 ),
138 );
139 }
140
141 protected function getExamplesMessages() {
142 return array(
143 'action=query&list=mystashedfiles&msfprop=size'
144 => 'apihelp-query+mystashedfiles-example-simple',
145 );
146 }
147
148 public function getHelpUrls() {
149 return 'https://www.mediawiki.org/wiki/API:mystashedfiles';
150 }
151 }