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 use MediaWiki\MainConfigNames
;
28 use MediaWiki\ParamValidator\TypeDef\UserDef
;
29 use MediaWiki\Permissions\GroupPermissionsLookup
;
30 use MediaWiki\Title\Title
;
31 use Wikimedia\ParamValidator\ParamValidator
;
32 use Wikimedia\ParamValidator\TypeDef\IntegerDef
;
33 use Wikimedia\Rdbms\IReadableDatabase
;
36 * Query module to enumerate all images.
40 class ApiQueryAllImages
extends ApiQueryGeneratorBase
{
47 private GroupPermissionsLookup
$groupPermissionsLookup;
50 * @param ApiQuery $query
51 * @param string $moduleName
52 * @param RepoGroup $repoGroup
53 * @param GroupPermissionsLookup $groupPermissionsLookup
55 public function __construct(
59 GroupPermissionsLookup
$groupPermissionsLookup
61 parent
::__construct( $query, $moduleName, 'ai' );
62 $this->mRepo
= $repoGroup->getLocalRepo();
63 $this->groupPermissionsLookup
= $groupPermissionsLookup;
67 * Override parent method to make sure the repo's DB is used
68 * which may not necessarily be the same as the local DB.
70 * TODO: allow querying non-local repos.
71 * @return IReadableDatabase
73 protected function getDB() {
74 return $this->mRepo
->getReplicaDB();
77 public function execute() {
81 public function getCacheMode( $params ) {
86 * @param ApiPageSet $resultPageSet
89 public function executeGenerator( $resultPageSet ) {
90 if ( $resultPageSet->isResolvingRedirects() ) {
91 $this->dieWithError( 'apierror-allimages-redirect', 'invalidparammix' );
94 $this->run( $resultPageSet );
98 * @param ApiPageSet|null $resultPageSet
101 private function run( $resultPageSet = null ) {
102 $repo = $this->mRepo
;
103 if ( !$repo instanceof LocalRepo
) {
104 $this->dieWithError( 'apierror-unsupportedrepo' );
107 $prefix = $this->getModulePrefix();
109 $db = $this->getDB();
111 $params = $this->extractRequestParams();
113 // Table and return fields
114 $prop = array_fill_keys( $params['prop'], true );
116 $fileQuery = LocalFile
::getQueryInfo();
117 $this->addTables( $fileQuery['tables'] );
118 $this->addFields( $fileQuery['fields'] );
119 $this->addJoinConds( $fileQuery['joins'] );
121 $ascendingOrder = true;
122 if ( $params['dir'] == 'descending' ||
$params['dir'] == 'older' ) {
123 $ascendingOrder = false;
126 if ( $params['sort'] == 'name' ) {
127 // Check mutually exclusive params
128 $disallowed = [ 'start', 'end', 'user' ];
129 foreach ( $disallowed as $pname ) {
130 if ( isset( $params[$pname] ) ) {
133 'apierror-invalidparammix-mustusewith',
135 "{$prefix}sort=timestamp"
141 if ( $params['filterbots'] != 'all' ) {
144 'apierror-invalidparammix-mustusewith',
145 "{$prefix}filterbots",
146 "{$prefix}sort=timestamp"
153 if ( $params['continue'] !== null ) {
154 $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'string' ] );
155 $op = $ascendingOrder ?
'>=' : '<=';
156 $this->addWhere( $db->buildComparison( $op, [ 'img_name' => $cont[0] ] ) );
160 $from = $params['from'] === null ?
null : $this->titlePartToKey( $params['from'], NS_FILE
);
161 $to = $params['to'] === null ?
null : $this->titlePartToKey( $params['to'], NS_FILE
);
162 $this->addWhereRange( 'img_name', $ascendingOrder ?
'newer' : 'older', $from, $to );
164 if ( isset( $params['prefix'] ) ) {
165 $this->addWhere( 'img_name' . $db->buildLike(
166 $this->titlePartToKey( $params['prefix'], NS_FILE
),
167 $db->anyString() ) );
170 // Check mutually exclusive params
171 $disallowed = [ 'from', 'to', 'prefix' ];
172 foreach ( $disallowed as $pname ) {
173 if ( isset( $params[$pname] ) ) {
176 'apierror-invalidparammix-mustusewith',
184 if ( $params['user'] !== null && $params['filterbots'] != 'all' ) {
185 // Since filterbots checks if each user has the bot right, it
186 // doesn't make sense to use it with user
188 [ 'apierror-invalidparammix-cannotusewith', "{$prefix}user", "{$prefix}filterbots" ]
193 $this->addTimestampWhereRange(
195 $ascendingOrder ?
'newer' : 'older',
199 // Include in ORDER BY for uniqueness
200 $this->addWhereRange( 'img_name', $ascendingOrder ?
'newer' : 'older', null, null );
202 if ( $params['continue'] !== null ) {
203 $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'timestamp', 'string' ] );
204 $op = ( $ascendingOrder ?
'>=' : '<=' );
205 $this->addWhere( $db->buildComparison( $op, [
206 'img_timestamp' => $db->timestamp( $cont[0] ),
207 'img_name' => $cont[1],
212 if ( $params['user'] !== null ) {
213 $this->addWhereFld( $fileQuery['fields']['img_user_text'], $params['user'] );
215 if ( $params['filterbots'] != 'all' ) {
216 $this->addTables( 'user_groups' );
217 $this->addJoinConds( [ 'user_groups' => [
220 'ug_group' => $this->groupPermissionsLookup
->getGroupsWithPermission( 'bot' ),
221 'ug_user = actor_user',
222 'ug_expiry IS NULL OR ug_expiry >= ' . $db->addQuotes( $db->timestamp() )
225 $groupCond = $params['filterbots'] == 'nobots' ?
'NULL' : 'NOT NULL';
226 $this->addWhere( "ug_group IS $groupCond" );
230 // Filters not depending on sort
231 if ( isset( $params['minsize'] ) ) {
232 $this->addWhere( 'img_size>=' . (int)$params['minsize'] );
235 if ( isset( $params['maxsize'] ) ) {
236 $this->addWhere( 'img_size<=' . (int)$params['maxsize'] );
240 if ( isset( $params['sha1'] ) ) {
241 $sha1 = strtolower( $params['sha1'] );
242 if ( !$this->validateSha1Hash( $sha1 ) ) {
243 $this->dieWithError( 'apierror-invalidsha1hash' );
245 $sha1 = Wikimedia\base_convert
( $sha1, 16, 36, 31 );
246 } elseif ( isset( $params['sha1base36'] ) ) {
247 $sha1 = strtolower( $params['sha1base36'] );
248 if ( !$this->validateSha1Base36Hash( $sha1 ) ) {
249 $this->dieWithError( 'apierror-invalidsha1base36hash' );
253 $this->addWhereFld( 'img_sha1', $sha1 );
256 if ( $params['mime'] !== null ) {
257 if ( $this->getConfig()->get( MainConfigNames
::MiserMode
) ) {
258 $this->dieWithError( 'apierror-mimesearchdisabled' );
262 foreach ( $params['mime'] as $mime ) {
263 [ $major, $minor ] = File
::splitMime( $mime );
264 $mimeConds[] = $db->makeList(
266 'img_major_mime' => $major,
267 'img_minor_mime' => $minor,
272 // safeguard against internal_api_error_DBQueryError
273 if ( count( $mimeConds ) > 0 ) {
274 $this->addWhere( $db->makeList( $mimeConds, LIST_OR
) );
276 // no MIME types, no files
277 $this->getResult()->addValue( 'query', $this->getModuleName(), [] );
282 $limit = $params['limit'];
283 $this->addOption( 'LIMIT', $limit +
1 );
285 $res = $this->select( __METHOD__
);
289 $result = $this->getResult();
290 foreach ( $res as $row ) {
291 if ( ++
$count > $limit ) {
292 // We've reached the one extra which shows that there are
293 // additional pages to be had. Stop here...
294 if ( $params['sort'] == 'name' ) {
295 $this->setContinueEnumParameter( 'continue', $row->img_name
);
297 $this->setContinueEnumParameter( 'continue', "$row->img_timestamp|$row->img_name" );
302 if ( $resultPageSet === null ) {
303 $file = $repo->newFileFromRow( $row );
304 $info = array_merge( [ 'name' => $row->img_name
],
305 ApiQueryImageInfo
::getInfo( $file, $prop, $result ) );
306 self
::addTitleInfo( $info, $file->getTitle() );
308 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $info );
310 if ( $params['sort'] == 'name' ) {
311 $this->setContinueEnumParameter( 'continue', $row->img_name
);
313 $this->setContinueEnumParameter( 'continue', "$row->img_timestamp|$row->img_name" );
318 $titles[] = Title
::makeTitle( NS_FILE
, $row->img_name
);
322 if ( $resultPageSet === null ) {
323 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'img' );
325 $resultPageSet->populateFromTitles( $titles );
329 public function getAllowedParams() {
332 ParamValidator
::PARAM_DEFAULT
=> 'name',
333 ParamValidator
::PARAM_TYPE
=> [
339 ParamValidator
::PARAM_DEFAULT
=> 'ascending',
340 ParamValidator
::PARAM_TYPE
=> [
352 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
355 ParamValidator
::PARAM_TYPE
=> 'timestamp'
358 ParamValidator
::PARAM_TYPE
=> 'timestamp'
361 ParamValidator
::PARAM_TYPE
=> ApiQueryImageInfo
::getPropertyNames( $this->propertyFilter
),
362 ParamValidator
::PARAM_DEFAULT
=> 'timestamp|url',
363 ParamValidator
::PARAM_ISMULTI
=> true,
364 ApiBase
::PARAM_HELP_MSG
=> 'apihelp-query+imageinfo-param-prop',
365 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=>
366 ApiQueryImageInfo
::getPropertyMessages( $this->propertyFilter
),
370 ParamValidator
::PARAM_TYPE
=> 'integer',
373 ParamValidator
::PARAM_TYPE
=> 'integer',
376 'sha1base36' => null,
378 ParamValidator
::PARAM_TYPE
=> 'user',
379 UserDef
::PARAM_ALLOWED_USER_TYPES
=> [ 'name', 'ip', 'id', 'interwiki' ],
382 ParamValidator
::PARAM_DEFAULT
=> 'all',
383 ParamValidator
::PARAM_TYPE
=> [
390 ParamValidator
::PARAM_ISMULTI
=> true,
393 ParamValidator
::PARAM_DEFAULT
=> 10,
394 ParamValidator
::PARAM_TYPE
=> 'limit',
395 IntegerDef
::PARAM_MIN
=> 1,
396 IntegerDef
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
397 IntegerDef
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
401 if ( $this->getConfig()->get( MainConfigNames
::MiserMode
) ) {
402 $ret['mime'][ApiBase
::PARAM_HELP_MSG
] = 'api-help-param-disabled-in-miser-mode';
408 private $propertyFilter = [ 'archivename', 'thumbmime', 'uploadwarning' ];
410 protected function getExamplesMessages() {
412 'action=query&list=allimages&aifrom=B'
413 => 'apihelp-query+allimages-example-b',
414 'action=query&list=allimages&aiprop=user|timestamp|url&' .
415 'aisort=timestamp&aidir=older'
416 => 'apihelp-query+allimages-example-recent',
417 'action=query&list=allimages&aimime=image/png|image/gif'
418 => 'apihelp-query+allimages-example-mimetypes',
419 'action=query&generator=allimages&gailimit=4&' .
420 'gaifrom=T&prop=imageinfo'
421 => 'apihelp-query+allimages-example-generator',
425 public function getHelpUrls() {
426 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Allimages';