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