Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / api / ApiQueryAllImages.php
blob2eaeb2e9ac20839da5e38c80fce639ee715b1aa5
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 namespace MediaWiki\Api;
29 use File;
30 use LocalFile;
31 use LocalRepo;
32 use MediaWiki\MainConfigNames;
33 use MediaWiki\ParamValidator\TypeDef\UserDef;
34 use MediaWiki\Permissions\GroupPermissionsLookup;
35 use MediaWiki\Title\Title;
36 use RepoGroup;
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;
43 /**
44 * Query module to enumerate all images.
46 * @ingroup API
48 class ApiQueryAllImages extends ApiQueryGeneratorBase {
50 /**
51 * @var LocalRepo
53 protected $mRepo;
55 private GroupPermissionsLookup $groupPermissionsLookup;
57 public function __construct(
58 ApiQuery $query,
59 string $moduleName,
60 RepoGroup $repoGroup,
61 GroupPermissionsLookup $groupPermissionsLookup
62 ) {
63 parent::__construct( $query, $moduleName, 'ai' );
64 $this->mRepo = $repoGroup->getLocalRepo();
65 $this->groupPermissionsLookup = $groupPermissionsLookup;
68 /**
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() {
80 $this->run();
83 public function getCacheMode( $params ) {
84 return 'public';
87 /**
88 * @param ApiPageSet $resultPageSet
89 * @return void
91 public function executeGenerator( $resultPageSet ) {
92 if ( $resultPageSet->isResolvingRedirects() ) {
93 $this->dieWithError( 'apierror-allimages-redirect', 'invalidparammix' );
96 $this->run( $resultPageSet );
99 /**
100 * @param ApiPageSet|null $resultPageSet
101 * @return void
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] ) ) {
133 $this->dieWithError(
135 'apierror-invalidparammix-mustusewith',
136 "{$prefix}{$pname}",
137 "{$prefix}sort=timestamp"
139 'invalidparammix'
143 if ( $params['filterbots'] != 'all' ) {
144 $this->dieWithError(
146 'apierror-invalidparammix-mustusewith',
147 "{$prefix}filterbots",
148 "{$prefix}sort=timestamp"
150 'invalidparammix'
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] ) );
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 );
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() )
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(
182 'apierror-invalidparammix-mustusewith',
183 "{$prefix}{$pname}",
184 "{$prefix}sort=name"
186 'invalidparammix'
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" ]
198 // Pagination
199 $this->addTimestampWhereRange(
200 'img_timestamp',
201 $ascendingOrder ? 'newer' : 'older',
202 $params['start'],
203 $params['end']
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],
214 ] ) );
217 // Image filters
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' => [
224 'LEFT JOIN',
226 'ug_group' => $this->groupPermissionsLookup->getGroupsWithPermission( 'bot' ),
227 'ug_user = actor_user',
228 $db->expr( 'ug_expiry', '=', null )->or( 'ug_expiry', '>=', $db->timestamp() )
230 ] ] );
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'] );
245 $sha1 = false;
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' );
258 if ( $sha1 ) {
259 $this->addWhereFld( 'img_sha1', $sha1 );
262 if ( $params['mime'] !== null ) {
263 if ( $this->getConfig()->get( MainConfigNames::MiserMode ) ) {
264 $this->dieWithError( 'apierror-mimesearchdisabled' );
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 );
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;
283 $limit = $params['limit'];
284 $this->addOption( 'LIMIT', $limit + 1 );
286 $res = $this->select( __METHOD__ );
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" );
300 break;
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 );
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" );
316 break;
318 } else {
319 $titles[] = Title::makeTitle( NS_FILE, $row->img_name );
323 if ( $resultPageSet === null ) {
324 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'img' );
325 } else {
326 $resultPageSet->populateFromTitles( $titles );
330 public function getAllowedParams() {
331 $ret = [
332 'sort' => [
333 ParamValidator::PARAM_DEFAULT => 'name',
334 ParamValidator::PARAM_TYPE => [
335 'name',
336 'timestamp'
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'
350 'from' => null,
351 'to' => null,
352 'continue' => [
353 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
355 'start' => [
356 ParamValidator::PARAM_TYPE => 'timestamp'
358 'end' => [
359 ParamValidator::PARAM_TYPE => 'timestamp'
361 'prop' => [
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 ),
369 'prefix' => null,
370 'minsize' => [
371 ParamValidator::PARAM_TYPE => 'integer',
373 'maxsize' => [
374 ParamValidator::PARAM_TYPE => 'integer',
376 'sha1' => null,
377 'sha1base36' => null,
378 'user' => [
379 ParamValidator::PARAM_TYPE => 'user',
380 UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'ip', 'temp', 'id', 'interwiki' ],
382 'filterbots' => [
383 ParamValidator::PARAM_DEFAULT => 'all',
384 ParamValidator::PARAM_TYPE => [
385 'all',
386 'bots',
387 'nobots'
390 'mime' => [
391 ParamValidator::PARAM_ISMULTI => true,
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
402 if ( $this->getConfig()->get( MainConfigNames::MiserMode ) ) {
403 $ret['mime'][ApiBase::PARAM_HELP_MSG] = 'api-help-param-disabled-in-miser-mode';
406 return $ret;
409 private const PROPERTY_FILTER = [ 'archivename', 'thumbmime', 'uploadwarning' ];
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',
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' );