4 * API for MediaWiki 1.12+
6 * Created on Mar 16, 2008
8 * Copyright © 2008 Vasiliev Victor vasilvv@gmail.com,
9 * based on ApiQueryAllPages.php
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 * http://www.gnu.org/copyleft/gpl.html
30 * Query module to enumerate all available pages.
34 class ApiQueryAllImages
extends ApiQueryGeneratorBase
{
37 public function __construct( ApiQuery
$query, $moduleName ) {
38 parent
::__construct( $query, $moduleName, 'ai' );
39 $this->mRepo
= RepoGroup
::singleton()->getLocalRepo();
43 * Override parent method to make sure the repo's DB is used
44 * which may not necessarily be the same as the local DB.
46 * TODO: allow querying non-local repos.
49 protected function getDB() {
50 return $this->mRepo
->getReplicaDB();
53 public function execute() {
57 public function getCacheMode( $params ) {
62 * @param ApiPageSet $resultPageSet
65 public function executeGenerator( $resultPageSet ) {
66 if ( $resultPageSet->isResolvingRedirects() ) {
67 $this->dieWithError( 'apierror-allimages-redirect', 'invalidparammix' );
70 $this->run( $resultPageSet );
74 * @param ApiPageSet $resultPageSet
77 private function run( $resultPageSet = null ) {
79 if ( !$repo instanceof LocalRepo
) {
80 $this->dieWithError( 'apierror-unsupportedrepo' );
83 $prefix = $this->getModulePrefix();
87 $params = $this->extractRequestParams();
89 // Table and return fields
90 $this->addTables( 'image' );
92 $prop = array_flip( $params['prop'] );
93 $this->addFields( LocalFile
::selectFields() );
95 $ascendingOrder = true;
96 if ( $params['dir'] == 'descending' ||
$params['dir'] == 'older' ) {
97 $ascendingOrder = false;
100 if ( $params['sort'] == 'name' ) {
101 // Check mutually exclusive params
102 $disallowed = [ 'start', 'end', 'user' ];
103 foreach ( $disallowed as $pname ) {
104 if ( isset( $params[$pname] ) ) {
107 'apierror-invalidparammix-mustusewith',
109 "{$prefix}sort=timestamp"
115 if ( $params['filterbots'] != 'all' ) {
118 'apierror-invalidparammix-mustusewith',
119 "{$prefix}filterbots",
120 "{$prefix}sort=timestamp"
127 if ( !is_null( $params['continue'] ) ) {
128 $cont = explode( '|', $params['continue'] );
129 $this->dieContinueUsageIf( count( $cont ) != 1 );
130 $op = ( $ascendingOrder ?
'>' : '<' );
131 $continueFrom = $db->addQuotes( $cont[0] );
132 $this->addWhere( "img_name $op= $continueFrom" );
136 $from = ( $params['from'] === null ?
null : $this->titlePartToKey( $params['from'], NS_FILE
) );
137 $to = ( $params['to'] === null ?
null : $this->titlePartToKey( $params['to'], NS_FILE
) );
138 $this->addWhereRange( 'img_name', ( $ascendingOrder ?
'newer' : 'older' ), $from, $to );
140 if ( isset( $params['prefix'] ) ) {
141 $this->addWhere( 'img_name' . $db->buildLike(
142 $this->titlePartToKey( $params['prefix'], NS_FILE
),
143 $db->anyString() ) );
146 // Check mutually exclusive params
147 $disallowed = [ 'from', 'to', 'prefix' ];
148 foreach ( $disallowed as $pname ) {
149 if ( isset( $params[$pname] ) ) {
152 'apierror-invalidparammix-mustusewith',
160 if ( !is_null( $params['user'] ) && $params['filterbots'] != 'all' ) {
161 // Since filterbots checks if each user has the bot right, it
162 // doesn't make sense to use it with user
164 [ 'apierror-invalidparammix-cannotusewith', "{$prefix}user", "{$prefix}filterbots" ]
169 $this->addTimestampWhereRange(
171 $ascendingOrder ?
'newer' : 'older',
175 // Include in ORDER BY for uniqueness
176 $this->addWhereRange( 'img_name', $ascendingOrder ?
'newer' : 'older', null, null );
178 if ( !is_null( $params['continue'] ) ) {
179 $cont = explode( '|', $params['continue'] );
180 $this->dieContinueUsageIf( count( $cont ) != 2 );
181 $op = ( $ascendingOrder ?
'>' : '<' );
182 $continueTimestamp = $db->addQuotes( $db->timestamp( $cont[0] ) );
183 $continueName = $db->addQuotes( $cont[1] );
184 $this->addWhere( "img_timestamp $op $continueTimestamp OR " .
185 "(img_timestamp = $continueTimestamp AND " .
186 "img_name $op= $continueName)"
191 if ( !is_null( $params['user'] ) ) {
192 $this->addWhereFld( 'img_user_text', $params['user'] );
194 if ( $params['filterbots'] != 'all' ) {
195 $this->addTables( 'user_groups' );
196 $this->addJoinConds( [ 'user_groups' => [
199 'ug_group' => User
::getGroupsWithPermission( 'bot' ),
203 $groupCond = ( $params['filterbots'] == 'nobots' ?
'NULL' : 'NOT NULL' );
204 $this->addWhere( "ug_group IS $groupCond" );
208 // Filters not depending on sort
209 if ( isset( $params['minsize'] ) ) {
210 $this->addWhere( 'img_size>=' . intval( $params['minsize'] ) );
213 if ( isset( $params['maxsize'] ) ) {
214 $this->addWhere( 'img_size<=' . intval( $params['maxsize'] ) );
218 if ( isset( $params['sha1'] ) ) {
219 $sha1 = strtolower( $params['sha1'] );
220 if ( !$this->validateSha1Hash( $sha1 ) ) {
221 $this->dieWithError( 'apierror-invalidsha1hash' );
223 $sha1 = Wikimedia\base_convert
( $sha1, 16, 36, 31 );
224 } elseif ( isset( $params['sha1base36'] ) ) {
225 $sha1 = strtolower( $params['sha1base36'] );
226 if ( !$this->validateSha1Base36Hash( $sha1 ) ) {
227 $this->dieWithError( 'apierror-invalidsha1base36hash' );
231 $this->addWhereFld( 'img_sha1', $sha1 );
234 if ( !is_null( $params['mime'] ) ) {
235 if ( $this->getConfig()->get( 'MiserMode' ) ) {
236 $this->dieWithError( 'apierror-mimesearchdisabled' );
240 foreach ( $params['mime'] as $mime ) {
241 list( $major, $minor ) = File
::splitMime( $mime );
242 $mimeConds[] = $db->makeList(
244 'img_major_mime' => $major,
245 'img_minor_mime' => $minor,
250 // safeguard against internal_api_error_DBQueryError
251 if ( count( $mimeConds ) > 0 ) {
252 $this->addWhere( $db->makeList( $mimeConds, LIST_OR
) );
254 // no MIME types, no files
255 $this->getResult()->addValue( 'query', $this->getModuleName(), [] );
260 $limit = $params['limit'];
261 $this->addOption( 'LIMIT', $limit +
1 );
263 if ( !$ascendingOrder ) {
266 if ( $params['sort'] == 'timestamp' ) {
267 $this->addOption( 'ORDER BY', 'img_timestamp' . $sortFlag );
268 if ( !is_null( $params['user'] ) ) {
269 $this->addOption( 'USE INDEX', [ 'image' => 'img_usertext_timestamp' ] );
271 $this->addOption( 'USE INDEX', [ 'image' => 'img_timestamp' ] );
274 $this->addOption( 'ORDER BY', 'img_name' . $sortFlag );
277 $res = $this->select( __METHOD__
);
281 $result = $this->getResult();
282 foreach ( $res as $row ) {
283 if ( ++
$count > $limit ) {
284 // We've reached the one extra which shows that there are
285 // additional pages to be had. Stop here...
286 if ( $params['sort'] == 'name' ) {
287 $this->setContinueEnumParameter( 'continue', $row->img_name
);
289 $this->setContinueEnumParameter( 'continue', "$row->img_timestamp|$row->img_name" );
294 if ( is_null( $resultPageSet ) ) {
295 $file = $repo->newFileFromRow( $row );
296 $info = array_merge( [ 'name' => $row->img_name
],
297 ApiQueryImageInfo
::getInfo( $file, $prop, $result ) );
298 self
::addTitleInfo( $info, $file->getTitle() );
300 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $info );
302 if ( $params['sort'] == 'name' ) {
303 $this->setContinueEnumParameter( 'continue', $row->img_name
);
305 $this->setContinueEnumParameter( 'continue', "$row->img_timestamp|$row->img_name" );
310 $titles[] = Title
::makeTitle( NS_FILE
, $row->img_name
);
314 if ( is_null( $resultPageSet ) ) {
315 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'img' );
317 $resultPageSet->populateFromTitles( $titles );
321 public function getAllowedParams() {
324 ApiBase
::PARAM_DFLT
=> 'name',
325 ApiBase
::PARAM_TYPE
=> [
331 ApiBase
::PARAM_DFLT
=> 'ascending',
332 ApiBase
::PARAM_TYPE
=> [
344 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
347 ApiBase
::PARAM_TYPE
=> 'timestamp'
350 ApiBase
::PARAM_TYPE
=> 'timestamp'
353 ApiBase
::PARAM_TYPE
=> ApiQueryImageInfo
::getPropertyNames( $this->propertyFilter
),
354 ApiBase
::PARAM_DFLT
=> 'timestamp|url',
355 ApiBase
::PARAM_ISMULTI
=> true,
356 ApiBase
::PARAM_HELP_MSG
=> 'apihelp-query+imageinfo-param-prop',
357 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=>
358 ApiQueryImageInfo
::getPropertyMessages( $this->propertyFilter
),
362 ApiBase
::PARAM_TYPE
=> 'integer',
365 ApiBase
::PARAM_TYPE
=> 'integer',
368 'sha1base36' => null,
370 ApiBase
::PARAM_TYPE
=> 'user'
373 ApiBase
::PARAM_DFLT
=> 'all',
374 ApiBase
::PARAM_TYPE
=> [
381 ApiBase
::PARAM_ISMULTI
=> true,
384 ApiBase
::PARAM_DFLT
=> 10,
385 ApiBase
::PARAM_TYPE
=> 'limit',
386 ApiBase
::PARAM_MIN
=> 1,
387 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
388 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
392 if ( $this->getConfig()->get( 'MiserMode' ) ) {
393 $ret['mime'][ApiBase
::PARAM_HELP_MSG
] = 'api-help-param-disabled-in-miser-mode';
399 private $propertyFilter = [ 'archivename', 'thumbmime', 'uploadwarning' ];
401 protected function getExamplesMessages() {
403 'action=query&list=allimages&aifrom=B'
404 => 'apihelp-query+allimages-example-B',
405 'action=query&list=allimages&aiprop=user|timestamp|url&' .
406 'aisort=timestamp&aidir=older'
407 => 'apihelp-query+allimages-example-recent',
408 'action=query&list=allimages&aimime=image/png|image/gif'
409 => 'apihelp-query+allimages-example-mimetypes',
410 'action=query&generator=allimages&gailimit=4&' .
411 'gaifrom=T&prop=imageinfo'
412 => 'apihelp-query+allimages-example-generator',
416 public function getHelpUrls() {
417 return 'https://www.mediawiki.org/wiki/API:Allimages';