4 * API for MediaWiki 1.12+
6 * Copyright © 2008 Vasiliev Victor vasilvv@gmail.com,
7 * based on ApiQueryAllPages.php
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
27 namespace MediaWiki\Api
;
32 use MediaWiki\MainConfigNames
;
33 use MediaWiki\ParamValidator\TypeDef\UserDef
;
34 use MediaWiki\Permissions\GroupPermissionsLookup
;
35 use MediaWiki\Title\Title
;
37 use Wikimedia\ParamValidator\ParamValidator
;
38 use Wikimedia\ParamValidator\TypeDef\IntegerDef
;
39 use Wikimedia\Rdbms\IExpression
;
40 use Wikimedia\Rdbms\IReadableDatabase
;
41 use Wikimedia\Rdbms\LikeValue
;
44 * Query module to enumerate all images.
48 class ApiQueryAllImages
extends ApiQueryGeneratorBase
{
55 private GroupPermissionsLookup
$groupPermissionsLookup;
57 public function __construct(
61 GroupPermissionsLookup
$groupPermissionsLookup
63 parent
::__construct( $query, $moduleName, 'ai' );
64 $this->mRepo
= $repoGroup->getLocalRepo();
65 $this->groupPermissionsLookup
= $groupPermissionsLookup;
69 * Override parent method to make sure the repo's DB is used
70 * which may not necessarily be the same as the local DB.
72 * TODO: allow querying non-local repos.
73 * @return IReadableDatabase
75 protected function getDB() {
76 return $this->mRepo
->getReplicaDB();
79 public function execute() {
83 public function getCacheMode( $params ) {
88 * @param ApiPageSet $resultPageSet
91 public function executeGenerator( $resultPageSet ) {
92 if ( $resultPageSet->isResolvingRedirects() ) {
93 $this->dieWithError( 'apierror-allimages-redirect', 'invalidparammix' );
96 $this->run( $resultPageSet );
100 * @param ApiPageSet|null $resultPageSet
103 private function run( $resultPageSet = null ) {
104 $repo = $this->mRepo
;
105 if ( !$repo instanceof LocalRepo
) {
106 $this->dieWithError( 'apierror-unsupportedrepo' );
109 $prefix = $this->getModulePrefix();
111 $db = $this->getDB();
113 $params = $this->extractRequestParams();
115 // Table and return fields
116 $prop = array_fill_keys( $params['prop'], true );
118 $fileQuery = LocalFile
::getQueryInfo();
119 $this->addTables( $fileQuery['tables'] );
120 $this->addFields( $fileQuery['fields'] );
121 $this->addJoinConds( $fileQuery['joins'] );
123 $ascendingOrder = true;
124 if ( $params['dir'] == 'descending' ||
$params['dir'] == 'older' ) {
125 $ascendingOrder = false;
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] ) ) {
135 'apierror-invalidparammix-mustusewith',
137 "{$prefix}sort=timestamp"
143 if ( $params['filterbots'] != 'all' ) {
146 'apierror-invalidparammix-mustusewith',
147 "{$prefix}filterbots",
148 "{$prefix}sort=timestamp"
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] ) );
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 );
166 if ( isset( $params['prefix'] ) ) {
171 new LikeValue( $this->titlePartToKey( $params['prefix'], NS_FILE
), $db->anyString() )
176 // Check mutually exclusive params
177 $disallowed = [ 'from', 'to', 'prefix' ];
178 foreach ( $disallowed as $pname ) {
179 if ( isset( $params[$pname] ) ) {
182 'apierror-invalidparammix-mustusewith',
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
194 [ 'apierror-invalidparammix-cannotusewith', "{$prefix}user", "{$prefix}filterbots" ]
199 $this->addTimestampWhereRange(
201 $ascendingOrder ?
'newer' : 'older',
205 // Include in ORDER BY for uniqueness
206 $this->addWhereRange( 'img_name', $ascendingOrder ?
'newer' : 'older', null, null );
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],
218 if ( $params['user'] !== null ) {
219 $this->addWhereFld( $fileQuery['fields']['img_user_text'], $params['user'] );
221 if ( $params['filterbots'] != 'all' ) {
222 $this->addTables( 'user_groups' );
223 $this->addJoinConds( [ 'user_groups' => [
226 'ug_group' => $this->groupPermissionsLookup
->getGroupsWithPermission( 'bot' ),
227 'ug_user = actor_user',
228 $db->expr( 'ug_expiry', '=', null )->or( 'ug_expiry', '>=', $db->timestamp() )
231 $groupCond = $params['filterbots'] == 'nobots' ?
'NULL' : 'NOT NULL';
232 $this->addWhere( "ug_group IS $groupCond" );
236 // Filters not depending on sort
237 if ( isset( $params['minsize'] ) ) {
238 $this->addWhere( 'img_size>=' . (int)$params['minsize'] );
241 if ( isset( $params['maxsize'] ) ) {
242 $this->addWhere( 'img_size<=' . (int)$params['maxsize'] );
246 if ( isset( $params['sha1'] ) ) {
247 $sha1 = strtolower( $params['sha1'] );
248 if ( !$this->validateSha1Hash( $sha1 ) ) {
249 $this->dieWithError( 'apierror-invalidsha1hash' );
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' );
259 $this->addWhereFld( 'img_sha1', $sha1 );
262 if ( $params['mime'] !== null ) {
263 if ( $this->getConfig()->get( MainConfigNames
::MiserMode
) ) {
264 $this->dieWithError( 'apierror-mimesearchdisabled' );
268 foreach ( $params['mime'] as $mime ) {
269 [ $major, $minor ] = File
::splitMime( $mime );
271 $db->expr( 'img_major_mime', '=', $major )
272 ->and( 'img_minor_mime', '=', $minor );
274 if ( count( $mimeConds ) > 0 ) {
275 $this->addWhere( $db->orExpr( $mimeConds ) );
277 // no MIME types, no files
278 $this->getResult()->addValue( 'query', $this->getModuleName(), [] );
283 $limit = $params['limit'];
284 $this->addOption( 'LIMIT', $limit +
1 );
286 $res = $this->select( __METHOD__
);
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
);
298 $this->setContinueEnumParameter( 'continue', "$row->img_timestamp|$row->img_name" );
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() );
309 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $info );
311 if ( $params['sort'] == 'name' ) {
312 $this->setContinueEnumParameter( 'continue', $row->img_name
);
314 $this->setContinueEnumParameter( 'continue', "$row->img_timestamp|$row->img_name" );
319 $titles[] = Title
::makeTitle( NS_FILE
, $row->img_name
);
323 if ( $resultPageSet === null ) {
324 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'img' );
326 $resultPageSet->populateFromTitles( $titles );
330 public function getAllowedParams() {
333 ParamValidator
::PARAM_DEFAULT
=> 'name',
334 ParamValidator
::PARAM_TYPE
=> [
340 ParamValidator
::PARAM_DEFAULT
=> 'ascending',
341 ParamValidator
::PARAM_TYPE
=> [
353 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
356 ParamValidator
::PARAM_TYPE
=> 'timestamp'
359 ParamValidator
::PARAM_TYPE
=> 'timestamp'
362 ParamValidator
::PARAM_TYPE
=> ApiQueryImageInfo
::getPropertyNames( self
::PROPERTY_FILTER
),
363 ParamValidator
::PARAM_DEFAULT
=> 'timestamp|url',
364 ParamValidator
::PARAM_ISMULTI
=> true,
365 ApiBase
::PARAM_HELP_MSG
=> 'apihelp-query+imageinfo-param-prop',
366 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=>
367 ApiQueryImageInfo
::getPropertyMessages( self
::PROPERTY_FILTER
),
371 ParamValidator
::PARAM_TYPE
=> 'integer',
374 ParamValidator
::PARAM_TYPE
=> 'integer',
377 'sha1base36' => null,
379 ParamValidator
::PARAM_TYPE
=> 'user',
380 UserDef
::PARAM_ALLOWED_USER_TYPES
=> [ 'name', 'ip', 'temp', 'id', 'interwiki' ],
383 ParamValidator
::PARAM_DEFAULT
=> 'all',
384 ParamValidator
::PARAM_TYPE
=> [
391 ParamValidator
::PARAM_ISMULTI
=> true,
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
402 if ( $this->getConfig()->get( MainConfigNames
::MiserMode
) ) {
403 $ret['mime'][ApiBase
::PARAM_HELP_MSG
] = 'api-help-param-disabled-in-miser-mode';
409 private const PROPERTY_FILTER
= [ 'archivename', 'thumbmime', 'uploadwarning' ];
411 protected function getExamplesMessages() {
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',
426 public function getHelpUrls() {
427 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Allimages';
431 /** @deprecated class alias since 1.43 */
432 class_alias( ApiQueryAllImages
::class, 'ApiQueryAllImages' );