MediaWiki master
ApiQueryMyStashedFiles.php
Go to the documentation of this file.
1<?php
23namespace MediaWiki\Api;
24
27
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
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' ],
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
157class_alias( ApiQueryMyStashedFiles::class, 'ApiQueryMyStashedFiles' );
array $params
The job parameters.
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1577
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:571
parseContinueParamOrDie(string $continue, array $types)
Parse the 'continue' parameter in the usual format and validate the types of each part,...
Definition ApiBase.php:1768
getResult()
Get the result object.
Definition ApiBase.php:710
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, or 'string' with PARAM_ISMULTI,...
Definition ApiBase.php:224
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:184
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition ApiBase.php:251
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:851
const LIMIT_BIG1
Fast query, standard limit.
Definition ApiBase.php:249
This is a base class for all Query modules.
addOption( $name, $value=null)
Add an option such as LIMIT or USE INDEX.
addFieldsIf( $value, $condition)
Same as addFields(), but add the fields only if a condition is met.
addTables( $tables, $alias=null)
Add a set of tables to the internal array.
getDB()
Get the Query database connection (read-only)
select( $method, $extraQuery=[], ?array &$hookData=null)
Execute a SELECT query based on the values in the internal arrays.
addWhere( $value)
Add a set of WHERE clauses to the internal array.
setContinueEnumParameter( $paramName, $paramValue)
Set a query-continue value.
addFields( $value)
Add a set of fields to select to the internal array.
action=query&list=mystashedfiles module, gets all stashed files for the current user.
getHelpUrls()
Return links to more detailed help pages about the module.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
__construct(ApiQuery $query, string $moduleName)
getExamplesMessages()
Returns usage examples for this module.
This is the main query class.
Definition ApiQuery.php:48
Service for formatting and validating API parameters.
Type definition for integer types.