MediaWiki master
ApiQueryAllImages.php
Go to the documentation of this file.
1<?php
2
36
43
47 protected $mRepo;
48
49 private GroupPermissionsLookup $groupPermissionsLookup;
50
57 public function __construct(
58 ApiQuery $query,
59 $moduleName,
60 RepoGroup $repoGroup,
61 GroupPermissionsLookup $groupPermissionsLookup
62 ) {
63 parent::__construct( $query, $moduleName, 'ai' );
64 $this->mRepo = $repoGroup->getLocalRepo();
65 $this->groupPermissionsLookup = $groupPermissionsLookup;
66 }
67
75 protected function getDB() {
76 return $this->mRepo->getReplicaDB();
77 }
78
79 public function execute() {
80 $this->run();
81 }
82
83 public function getCacheMode( $params ) {
84 return 'public';
85 }
86
91 public function executeGenerator( $resultPageSet ) {
92 if ( $resultPageSet->isResolvingRedirects() ) {
93 $this->dieWithError( 'apierror-allimages-redirect', 'invalidparammix' );
94 }
95
96 $this->run( $resultPageSet );
97 }
98
103 private function run( $resultPageSet = null ) {
104 $repo = $this->mRepo;
105 if ( !$repo instanceof LocalRepo ) {
106 $this->dieWithError( 'apierror-unsupportedrepo' );
107 }
108
109 $prefix = $this->getModulePrefix();
110
111 $db = $this->getDB();
112
113 $params = $this->extractRequestParams();
114
115 // Table and return fields
116 $prop = array_fill_keys( $params['prop'], true );
117
118 $fileQuery = LocalFile::getQueryInfo();
119 $this->addTables( $fileQuery['tables'] );
120 $this->addFields( $fileQuery['fields'] );
121 $this->addJoinConds( $fileQuery['joins'] );
122
123 $ascendingOrder = true;
124 if ( $params['dir'] == 'descending' || $params['dir'] == 'older' ) {
125 $ascendingOrder = false;
126 }
127
128 if ( $params['sort'] == 'name' ) {
129 // Check mutually exclusive params
130 $disallowed = [ 'start', 'end', 'user' ];
131 foreach ( $disallowed as $pname ) {
132 if ( isset( $params[$pname] ) ) {
133 $this->dieWithError(
134 [
135 'apierror-invalidparammix-mustusewith',
136 "{$prefix}{$pname}",
137 "{$prefix}sort=timestamp"
138 ],
139 'invalidparammix'
140 );
141 }
142 }
143 if ( $params['filterbots'] != 'all' ) {
144 $this->dieWithError(
145 [
146 'apierror-invalidparammix-mustusewith',
147 "{$prefix}filterbots",
148 "{$prefix}sort=timestamp"
149 ],
150 'invalidparammix'
151 );
152 }
153
154 // Pagination
155 if ( $params['continue'] !== null ) {
156 $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'string' ] );
157 $op = $ascendingOrder ? '>=' : '<=';
158 $this->addWhere( $db->expr( 'img_name', $op, $cont[0] ) );
159 }
160
161 // Image filters
162 $from = $params['from'] === null ? null : $this->titlePartToKey( $params['from'], NS_FILE );
163 $to = $params['to'] === null ? null : $this->titlePartToKey( $params['to'], NS_FILE );
164 $this->addWhereRange( 'img_name', $ascendingOrder ? 'newer' : 'older', $from, $to );
165
166 if ( isset( $params['prefix'] ) ) {
167 $this->addWhere(
168 $db->expr(
169 'img_name',
170 IExpression::LIKE,
171 new LikeValue( $this->titlePartToKey( $params['prefix'], NS_FILE ), $db->anyString() )
172 )
173 );
174 }
175 } else {
176 // Check mutually exclusive params
177 $disallowed = [ 'from', 'to', 'prefix' ];
178 foreach ( $disallowed as $pname ) {
179 if ( isset( $params[$pname] ) ) {
180 $this->dieWithError(
181 [
182 'apierror-invalidparammix-mustusewith',
183 "{$prefix}{$pname}",
184 "{$prefix}sort=name"
185 ],
186 'invalidparammix'
187 );
188 }
189 }
190 if ( $params['user'] !== null && $params['filterbots'] != 'all' ) {
191 // Since filterbots checks if each user has the bot right, it
192 // doesn't make sense to use it with user
193 $this->dieWithError(
194 [ 'apierror-invalidparammix-cannotusewith', "{$prefix}user", "{$prefix}filterbots" ]
195 );
196 }
197
198 // Pagination
200 'img_timestamp',
201 $ascendingOrder ? 'newer' : 'older',
202 $params['start'],
203 $params['end']
204 );
205 // Include in ORDER BY for uniqueness
206 $this->addWhereRange( 'img_name', $ascendingOrder ? 'newer' : 'older', null, null );
207
208 if ( $params['continue'] !== null ) {
209 $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'timestamp', 'string' ] );
210 $op = ( $ascendingOrder ? '>=' : '<=' );
211 $this->addWhere( $db->buildComparison( $op, [
212 'img_timestamp' => $db->timestamp( $cont[0] ),
213 'img_name' => $cont[1],
214 ] ) );
215 }
216
217 // Image filters
218 if ( $params['user'] !== null ) {
219 $this->addWhereFld( $fileQuery['fields']['img_user_text'], $params['user'] );
220 }
221 if ( $params['filterbots'] != 'all' ) {
222 $this->addTables( 'user_groups' );
223 $this->addJoinConds( [ 'user_groups' => [
224 'LEFT JOIN',
225 [
226 'ug_group' => $this->groupPermissionsLookup->getGroupsWithPermission( 'bot' ),
227 'ug_user = actor_user',
228 $db->expr( 'ug_expiry', '=', null )->or( 'ug_expiry', '>=', $db->timestamp() )
229 ]
230 ] ] );
231 $groupCond = $params['filterbots'] == 'nobots' ? 'NULL' : 'NOT NULL';
232 $this->addWhere( "ug_group IS $groupCond" );
233 }
234 }
235
236 // Filters not depending on sort
237 if ( isset( $params['minsize'] ) ) {
238 $this->addWhere( 'img_size>=' . (int)$params['minsize'] );
239 }
240
241 if ( isset( $params['maxsize'] ) ) {
242 $this->addWhere( 'img_size<=' . (int)$params['maxsize'] );
243 }
244
245 $sha1 = false;
246 if ( isset( $params['sha1'] ) ) {
247 $sha1 = strtolower( $params['sha1'] );
248 if ( !$this->validateSha1Hash( $sha1 ) ) {
249 $this->dieWithError( 'apierror-invalidsha1hash' );
250 }
251 $sha1 = Wikimedia\base_convert( $sha1, 16, 36, 31 );
252 } elseif ( isset( $params['sha1base36'] ) ) {
253 $sha1 = strtolower( $params['sha1base36'] );
254 if ( !$this->validateSha1Base36Hash( $sha1 ) ) {
255 $this->dieWithError( 'apierror-invalidsha1base36hash' );
256 }
257 }
258 if ( $sha1 ) {
259 $this->addWhereFld( 'img_sha1', $sha1 );
260 }
261
262 if ( $params['mime'] !== null ) {
263 if ( $this->getConfig()->get( MainConfigNames::MiserMode ) ) {
264 $this->dieWithError( 'apierror-mimesearchdisabled' );
265 }
266
267 $mimeConds = [];
268 foreach ( $params['mime'] as $mime ) {
269 [ $major, $minor ] = File::splitMime( $mime );
270 $mimeConds[] =
271 $db->expr( 'img_major_mime', '=', $major )
272 ->and( 'img_minor_mime', '=', $minor );
273 }
274 if ( count( $mimeConds ) > 0 ) {
275 $this->addWhere( $db->orExpr( $mimeConds ) );
276 } else {
277 // no MIME types, no files
278 $this->getResult()->addValue( 'query', $this->getModuleName(), [] );
279 return;
280 }
281 }
282
283 $limit = $params['limit'];
284 $this->addOption( 'LIMIT', $limit + 1 );
285
286 $res = $this->select( __METHOD__ );
287
288 $titles = [];
289 $count = 0;
290 $result = $this->getResult();
291 foreach ( $res as $row ) {
292 if ( ++$count > $limit ) {
293 // We've reached the one extra which shows that there are
294 // additional pages to be had. Stop here...
295 if ( $params['sort'] == 'name' ) {
296 $this->setContinueEnumParameter( 'continue', $row->img_name );
297 } else {
298 $this->setContinueEnumParameter( 'continue', "$row->img_timestamp|$row->img_name" );
299 }
300 break;
301 }
302
303 if ( $resultPageSet === null ) {
304 $file = $repo->newFileFromRow( $row );
305 $info = array_merge( [ 'name' => $row->img_name ],
306 ApiQueryImageInfo::getInfo( $file, $prop, $result ) );
307 self::addTitleInfo( $info, $file->getTitle() );
308
309 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $info );
310 if ( !$fit ) {
311 if ( $params['sort'] == 'name' ) {
312 $this->setContinueEnumParameter( 'continue', $row->img_name );
313 } else {
314 $this->setContinueEnumParameter( 'continue', "$row->img_timestamp|$row->img_name" );
315 }
316 break;
317 }
318 } else {
319 $titles[] = Title::makeTitle( NS_FILE, $row->img_name );
320 }
321 }
322
323 if ( $resultPageSet === null ) {
324 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'img' );
325 } else {
326 $resultPageSet->populateFromTitles( $titles );
327 }
328 }
329
330 public function getAllowedParams() {
331 $ret = [
332 'sort' => [
333 ParamValidator::PARAM_DEFAULT => 'name',
334 ParamValidator::PARAM_TYPE => [
335 'name',
336 'timestamp'
337 ]
338 ],
339 'dir' => [
340 ParamValidator::PARAM_DEFAULT => 'ascending',
341 ParamValidator::PARAM_TYPE => [
342 // sort=name
343 'ascending',
344 'descending',
345 // sort=timestamp
346 'newer',
347 'older'
348 ]
349 ],
350 'from' => null,
351 'to' => null,
352 'continue' => [
353 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
354 ],
355 'start' => [
356 ParamValidator::PARAM_TYPE => 'timestamp'
357 ],
358 'end' => [
359 ParamValidator::PARAM_TYPE => 'timestamp'
360 ],
361 'prop' => [
362 ParamValidator::PARAM_TYPE => ApiQueryImageInfo::getPropertyNames( $this->propertyFilter ),
363 ParamValidator::PARAM_DEFAULT => 'timestamp|url',
364 ParamValidator::PARAM_ISMULTI => true,
365 ApiBase::PARAM_HELP_MSG => 'apihelp-query+imageinfo-param-prop',
367 ApiQueryImageInfo::getPropertyMessages( $this->propertyFilter ),
368 ],
369 'prefix' => null,
370 'minsize' => [
371 ParamValidator::PARAM_TYPE => 'integer',
372 ],
373 'maxsize' => [
374 ParamValidator::PARAM_TYPE => 'integer',
375 ],
376 'sha1' => null,
377 'sha1base36' => null,
378 'user' => [
379 ParamValidator::PARAM_TYPE => 'user',
380 UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'ip', 'temp', 'id', 'interwiki' ],
381 ],
382 'filterbots' => [
383 ParamValidator::PARAM_DEFAULT => 'all',
384 ParamValidator::PARAM_TYPE => [
385 'all',
386 'bots',
387 'nobots'
388 ]
389 ],
390 'mime' => [
391 ParamValidator::PARAM_ISMULTI => true,
392 ],
393 'limit' => [
394 ParamValidator::PARAM_DEFAULT => 10,
395 ParamValidator::PARAM_TYPE => 'limit',
396 IntegerDef::PARAM_MIN => 1,
397 IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
398 IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
399 ],
400 ];
401
402 if ( $this->getConfig()->get( MainConfigNames::MiserMode ) ) {
403 $ret['mime'][ApiBase::PARAM_HELP_MSG] = 'api-help-param-disabled-in-miser-mode';
404 }
405
406 return $ret;
407 }
408
409 private $propertyFilter = [ 'archivename', 'thumbmime', 'uploadwarning' ];
410
411 protected function getExamplesMessages() {
412 return [
413 'action=query&list=allimages&aifrom=B'
414 => 'apihelp-query+allimages-example-b',
415 'action=query&list=allimages&aiprop=user|timestamp|url&' .
416 'aisort=timestamp&aidir=older'
417 => 'apihelp-query+allimages-example-recent',
418 'action=query&list=allimages&aimime=image/png|image/gif'
419 => 'apihelp-query+allimages-example-mimetypes',
420 'action=query&generator=allimages&gailimit=4&' .
421 'gaifrom=T&prop=imageinfo'
422 => 'apihelp-query+allimages-example-generator',
423 ];
424 }
425
426 public function getHelpUrls() {
427 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Allimages';
428 }
429}
const NS_FILE
Definition Defines.php:71
array $params
The job parameters.
run()
Run the job.
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1540
getModulePrefix()
Get parameter prefix (usually two letters or an empty string).
Definition ApiBase.php:551
parseContinueParamOrDie(string $continue, array $types)
Parse the 'continue' parameter in the usual format and validate the types of each part,...
Definition ApiBase.php:1731
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, or 'string' with PARAM_ISMULTI,...
Definition ApiBase.php:212
const LIMIT_BIG1
Fast query, standard limit.
Definition ApiBase.php:237
getResult()
Get the result object.
Definition ApiBase.php:681
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:821
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:172
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition ApiBase.php:239
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:542
Query module to enumerate all images.
getExamplesMessages()
Returns usage examples for this module.
getCacheMode( $params)
Get the cache mode for the data generated by this module.
getDB()
Override parent method to make sure the repo's DB is used which may not necessarily be the same as th...
executeGenerator( $resultPageSet)
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, $moduleName, RepoGroup $repoGroup, GroupPermissionsLookup $groupPermissionsLookup)
getHelpUrls()
Return links to more detailed help pages about the module.
static addTitleInfo(&$arr, $title, $prefix='')
Add information (title and namespace) about a Title object to a result array.
validateSha1Base36Hash( $hash)
addWhereRange( $field, $dir, $start, $end, $sort=true)
Add a WHERE clause corresponding to a range, and an ORDER BY clause to sort in the right direction.
validateSha1Hash( $hash)
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.
addTimestampWhereRange( $field, $dir, $start, $end, $sort=true)
Add a WHERE clause corresponding to a range, similar to addWhereRange, but converts $start and $end t...
select( $method, $extraQuery=[], array &$hookData=null)
Execute a SELECT query based on the values in the internal arrays.
addJoinConds( $join_conds)
Add a set of JOIN conditions to the internal array.
addWhereFld( $field, $value)
Equivalent to addWhere( [ $field => $value ] )
titlePartToKey( $titlePart, $namespace=NS_MAIN)
Convert an input title or title prefix into a dbkey.
addWhere( $value)
Add a set of WHERE clauses to the internal array.
setContinueEnumParameter( $paramName, $paramValue)
Overridden to set the generator param if in generator mode.
static getPropertyNames( $filter=[])
Returns all possible parameters to iiprop.
static getInfo( $file, $prop, $result, $thumbParams=null, $opts=false)
Get result information for an image revision.
static getPropertyMessages( $filter=[])
Returns messages for all possible parameters to iiprop.
This is the main query class.
Definition ApiQuery.php:43
static splitMime(?string $mime)
Split an internet media type into its two components; if not a two-part name, set the minor type to '...
Definition File.php:315
static getQueryInfo(array $options=[])
Return the tables, fields, and join conditions to be selected to create a new localfile object.
Local repository that stores files in the local filesystem and registers them in the wiki's own datab...
Definition LocalRepo.php:48
A class containing constants representing the names of configuration variables.
Type definition for user types.
Definition UserDef.php:27
Represents a title within MediaWiki.
Definition Title.php:79
Prioritized list of file repositories.
Definition RepoGroup.php:30
getLocalRepo()
Get the local repository, i.e.
Service for formatting and validating API parameters.
Type definition for integer types.
Content of like value.
Definition LikeValue.php:14
A database connection without write operations.