Update git submodules
[mediawiki.git] / includes / api / ApiQueryAllImages.php
blobc33a490f60e87c4d8bcc6f8381f54a09635d8266
1 <?php
3 /**
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
24 * @file
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;
35 /**
36 * Query module to enumerate all images.
38 * @ingroup API
40 class ApiQueryAllImages extends ApiQueryGeneratorBase {
42 /**
43 * @var LocalRepo
45 protected $mRepo;
47 private GroupPermissionsLookup $groupPermissionsLookup;
49 /**
50 * @param ApiQuery $query
51 * @param string $moduleName
52 * @param RepoGroup $repoGroup
53 * @param GroupPermissionsLookup $groupPermissionsLookup
55 public function __construct(
56 ApiQuery $query,
57 $moduleName,
58 RepoGroup $repoGroup,
59 GroupPermissionsLookup $groupPermissionsLookup
60 ) {
61 parent::__construct( $query, $moduleName, 'ai' );
62 $this->mRepo = $repoGroup->getLocalRepo();
63 $this->groupPermissionsLookup = $groupPermissionsLookup;
66 /**
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() {
78 $this->run();
81 public function getCacheMode( $params ) {
82 return 'public';
85 /**
86 * @param ApiPageSet $resultPageSet
87 * @return void
89 public function executeGenerator( $resultPageSet ) {
90 if ( $resultPageSet->isResolvingRedirects() ) {
91 $this->dieWithError( 'apierror-allimages-redirect', 'invalidparammix' );
94 $this->run( $resultPageSet );
97 /**
98 * @param ApiPageSet|null $resultPageSet
99 * @return void
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] ) ) {
131 $this->dieWithError(
133 'apierror-invalidparammix-mustusewith',
134 "{$prefix}{$pname}",
135 "{$prefix}sort=timestamp"
137 'invalidparammix'
141 if ( $params['filterbots'] != 'all' ) {
142 $this->dieWithError(
144 'apierror-invalidparammix-mustusewith',
145 "{$prefix}filterbots",
146 "{$prefix}sort=timestamp"
148 'invalidparammix'
152 // Pagination
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] ] ) );
159 // Image filters
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() ) );
169 } else {
170 // Check mutually exclusive params
171 $disallowed = [ 'from', 'to', 'prefix' ];
172 foreach ( $disallowed as $pname ) {
173 if ( isset( $params[$pname] ) ) {
174 $this->dieWithError(
176 'apierror-invalidparammix-mustusewith',
177 "{$prefix}{$pname}",
178 "{$prefix}sort=name"
180 'invalidparammix'
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
187 $this->dieWithError(
188 [ 'apierror-invalidparammix-cannotusewith', "{$prefix}user", "{$prefix}filterbots" ]
192 // Pagination
193 $this->addTimestampWhereRange(
194 'img_timestamp',
195 $ascendingOrder ? 'newer' : 'older',
196 $params['start'],
197 $params['end']
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],
208 ] ) );
211 // Image filters
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' => [
218 'LEFT JOIN',
220 'ug_group' => $this->groupPermissionsLookup->getGroupsWithPermission( 'bot' ),
221 'ug_user = actor_user',
222 'ug_expiry IS NULL OR ug_expiry >= ' . $db->addQuotes( $db->timestamp() )
224 ] ] );
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'] );
239 $sha1 = false;
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' );
252 if ( $sha1 ) {
253 $this->addWhereFld( 'img_sha1', $sha1 );
256 if ( $params['mime'] !== null ) {
257 if ( $this->getConfig()->get( MainConfigNames::MiserMode ) ) {
258 $this->dieWithError( 'apierror-mimesearchdisabled' );
261 $mimeConds = [];
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,
269 LIST_AND
272 // safeguard against internal_api_error_DBQueryError
273 if ( count( $mimeConds ) > 0 ) {
274 $this->addWhere( $db->makeList( $mimeConds, LIST_OR ) );
275 } else {
276 // no MIME types, no files
277 $this->getResult()->addValue( 'query', $this->getModuleName(), [] );
278 return;
282 $limit = $params['limit'];
283 $this->addOption( 'LIMIT', $limit + 1 );
285 $res = $this->select( __METHOD__ );
287 $titles = [];
288 $count = 0;
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 );
296 } else {
297 $this->setContinueEnumParameter( 'continue', "$row->img_timestamp|$row->img_name" );
299 break;
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 );
309 if ( !$fit ) {
310 if ( $params['sort'] == 'name' ) {
311 $this->setContinueEnumParameter( 'continue', $row->img_name );
312 } else {
313 $this->setContinueEnumParameter( 'continue', "$row->img_timestamp|$row->img_name" );
315 break;
317 } else {
318 $titles[] = Title::makeTitle( NS_FILE, $row->img_name );
322 if ( $resultPageSet === null ) {
323 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'img' );
324 } else {
325 $resultPageSet->populateFromTitles( $titles );
329 public function getAllowedParams() {
330 $ret = [
331 'sort' => [
332 ParamValidator::PARAM_DEFAULT => 'name',
333 ParamValidator::PARAM_TYPE => [
334 'name',
335 'timestamp'
338 'dir' => [
339 ParamValidator::PARAM_DEFAULT => 'ascending',
340 ParamValidator::PARAM_TYPE => [
341 // sort=name
342 'ascending',
343 'descending',
344 // sort=timestamp
345 'newer',
346 'older'
349 'from' => null,
350 'to' => null,
351 'continue' => [
352 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
354 'start' => [
355 ParamValidator::PARAM_TYPE => 'timestamp'
357 'end' => [
358 ParamValidator::PARAM_TYPE => 'timestamp'
360 'prop' => [
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 ),
368 'prefix' => null,
369 'minsize' => [
370 ParamValidator::PARAM_TYPE => 'integer',
372 'maxsize' => [
373 ParamValidator::PARAM_TYPE => 'integer',
375 'sha1' => null,
376 'sha1base36' => null,
377 'user' => [
378 ParamValidator::PARAM_TYPE => 'user',
379 UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'ip', 'id', 'interwiki' ],
381 'filterbots' => [
382 ParamValidator::PARAM_DEFAULT => 'all',
383 ParamValidator::PARAM_TYPE => [
384 'all',
385 'bots',
386 'nobots'
389 'mime' => [
390 ParamValidator::PARAM_ISMULTI => true,
392 'limit' => [
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';
405 return $ret;
408 private $propertyFilter = [ 'archivename', 'thumbmime', 'uploadwarning' ];
410 protected function getExamplesMessages() {
411 return [
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';