MediaWiki REL1_39
ApiQueryMyStashedFiles.php
Go to the documentation of this file.
1<?php
25
33
34 public function __construct( ApiQuery $query, $moduleName ) {
35 parent::__construct( $query, $moduleName, 'msf' );
36 }
37
38 public function execute() {
39 $user = $this->getUser();
40
41 if ( !$user->isRegistered() ) {
42 $this->dieWithError( 'apierror-mustbeloggedin-uploadstash', 'stashnotloggedin' );
43 }
44
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.
47
48 $params = $this->extractRequestParams();
49
50 $this->addTables( 'uploadstash' );
51
52 $this->addFields( [ 'us_id', 'us_key', 'us_status' ] );
53
54 $this->addWhere( [ 'us_user' => $user->getId() ] );
55
56 if ( $params['continue'] !== null ) {
57 $cont = explode( '|', $params['continue'] );
58 $this->dieContinueUsageIf( count( $cont ) != 1 );
59 $cont_from = (int)$cont[0];
60 $this->dieContinueUsageIf( strval( $cont_from ) !== $cont[0] );
61 $this->addWhere( "us_id >= $cont_from" );
62 }
63
64 $this->addOption( 'LIMIT', $params['limit'] + 1 );
65 $this->addOption( 'ORDER BY', 'us_id' );
66
67 $prop = array_fill_keys( $params['prop'], true );
68 $this->addFieldsIf(
69 [
70 'us_size',
71 'us_image_width',
72 'us_image_height',
73 'us_image_bits'
74 ],
75
76 isset( $prop['size'] )
77 );
78 $this->addFieldsIf( [ 'us_mime', 'us_media_type' ], isset( $prop['type'] ) );
79
80 $res = $this->select( __METHOD__ );
81 $result = $this->getResult();
82 $count = 0;
83
84 foreach ( $res as $row ) {
85 if ( ++$count > $params['limit'] ) {
86 // We've reached the one extra which shows that there are
87 // additional files to be had. Stop here...
88 $this->setContinueEnumParameter( 'continue', $row->us_id );
89 break;
90 }
91
92 $item = [
93 'filekey' => $row->us_key,
94 'status' => $row->us_status,
95 ];
96
97 if ( isset( $prop['size'] ) ) {
98 $item['size'] = (int)$row->us_size;
99 $item['width'] = (int)$row->us_image_width;
100 $item['height'] = (int)$row->us_image_height;
101 $item['bits'] = (int)$row->us_image_bits;
102 }
103
104 if ( isset( $prop['type'] ) ) {
105 $item['mimetype'] = $row->us_mime;
106 $item['mediatype'] = $row->us_media_type;
107 }
108
109 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $item );
110
111 if ( !$fit ) {
112 $this->setContinueEnumParameter( 'continue', $row->us_id );
113 break;
114 }
115 }
116
117 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'file' );
118 }
119
120 public function getAllowedParams() {
121 return [
122 'prop' => [
123 ParamValidator::PARAM_ISMULTI => true,
124 ParamValidator::PARAM_DEFAULT => '',
125 ParamValidator::PARAM_TYPE => [ 'size', 'type' ],
127 ],
128
129 'limit' => [
130 ParamValidator::PARAM_TYPE => 'limit',
131 ParamValidator::PARAM_DEFAULT => 10,
132 IntegerDef::PARAM_MIN => 1,
133 IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
134 IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2,
135 ],
136
137 'continue' => [
138 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
139 ],
140 ];
141 }
142
143 protected function getExamplesMessages() {
144 return [
145 'action=query&list=mystashedfiles&msfprop=size'
146 => 'apihelp-query+mystashedfiles-example-simple',
147 ];
148 }
149
150 public function getHelpUrls() {
151 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:mystashedfiles';
152 }
153}
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1454
dieContinueUsageIf( $condition)
Die with the 'badcontinue' error.
Definition ApiBase.php:1643
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, this is an array mapping those values to $msg...
Definition ApiBase.php:196
const LIMIT_BIG1
Fast query, standard limit.
Definition ApiBase.php:221
getResult()
Get the result object.
Definition ApiBase.php:629
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:765
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:163
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition ApiBase.php:223
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:498
This is a base class for all Query modules.
setContinueEnumParameter( $paramName, $paramValue)
Set a query-continue value.
addFields( $value)
Add a set of fields to select to the internal array.
addOption( $name, $value=null)
Add an option such as LIMIT or USE INDEX.
addTables( $tables, $alias=null)
Add a set of tables to the internal array.
select( $method, $extraQuery=[], array &$hookData=null)
Execute a SELECT query based on the values in the internal arrays.
addFieldsIf( $value, $condition)
Same as addFields(), but add the fields only if a condition is met.
addWhere( $value)
Add a set of WHERE clauses to the internal array.
action=query&list=mystashedfiles module, gets all stashed files for the current user.
getExamplesMessages()
Returns usage examples for this module.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
getHelpUrls()
Return links to more detailed help pages about the module.
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
__construct(ApiQuery $query, $moduleName)
This is the main query class.
Definition ApiQuery.php:41
Service for formatting and validating API parameters.
Type definition for integer types.