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
{
38 public function __construct( $query, $moduleName ) {
39 parent
::__construct( $query, $moduleName, 'ai' );
40 $this->mRepo
= RepoGroup
::singleton()->getLocalRepo();
44 * Override parent method to make sure the repo's DB is used
45 * which may not necessarily be the same as the local DB.
47 * TODO: allow querying non-local repos.
48 * @return DatabaseBase
50 protected function getDB() {
51 return $this->mRepo
->getSlaveDB();
54 public function execute() {
58 public function getCacheMode( $params ) {
63 * @param $resultPageSet ApiPageSet
66 public function executeGenerator( $resultPageSet ) {
67 if ( $resultPageSet->isResolvingRedirects() ) {
68 $this->dieUsage( 'Use "gaifilterredir=nonredirects" option instead of "redirects" when using allimages as a generator', 'params' );
71 $this->run( $resultPageSet );
75 * @param $resultPageSet ApiPageSet
78 private function run( $resultPageSet = null ) {
80 if ( !$repo instanceof LocalRepo
) {
81 $this->dieUsage( 'Local file repository does not support querying all images', 'unsupportedrepo' );
84 $prefix = $this->getModulePrefix();
88 $params = $this->extractRequestParams();
90 // Table and return fields
91 $this->addTables( 'image' );
93 $prop = array_flip( $params['prop'] );
94 $this->addFields( LocalFile
::selectFields() );
96 $ascendingOrder = true;
97 if ( $params['dir'] == 'descending' ||
$params['dir'] == 'older' ) {
98 $ascendingOrder = false;
101 if ( $params['sort'] == 'name' ) {
102 // Check mutually exclusive params
103 $disallowed = array( 'start', 'end', 'user' );
104 foreach ( $disallowed as $pname ) {
105 if ( isset( $params[$pname] ) ) {
106 $this->dieUsage( "Parameter '{$prefix}{$pname}' can only be used with {$prefix}sort=timestamp", 'badparams' );
109 if ( $params['filterbots'] != 'all' ) {
110 $this->dieUsage( "Parameter '{$prefix}filterbots' can only be used with {$prefix}sort=timestamp", 'badparams' );
114 if ( !is_null( $params['continue'] ) ) {
115 $cont = explode( '|', $params['continue'] );
116 $this->dieContinueUsageIf( count( $cont ) != 1 );
117 $op = ( $ascendingOrder ?
'>' : '<' );
118 $continueFrom = $db->addQuotes( $cont[0] );
119 $this->addWhere( "img_name $op= $continueFrom" );
123 $from = ( is_null( $params['from'] ) ?
null : $this->titlePartToKey( $params['from'] ) );
124 $to = ( is_null( $params['to'] ) ?
null : $this->titlePartToKey( $params['to'] ) );
125 $this->addWhereRange( 'img_name', ( $ascendingOrder ?
'newer' : 'older' ), $from, $to );
127 if ( isset( $params['prefix'] ) ) {
128 $this->addWhere( 'img_name' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
131 // Check mutually exclusive params
132 $disallowed = array( 'from', 'to', 'prefix' );
133 foreach ( $disallowed as $pname ) {
134 if ( isset( $params[$pname] ) ) {
135 $this->dieUsage( "Parameter '{$prefix}{$pname}' can only be used with {$prefix}sort=name", 'badparams' );
138 if ( !is_null( $params['user'] ) && $params['filterbots'] != 'all' ) {
139 // Since filterbots checks if each user has the bot right, it doesn't make sense to use it with user
140 $this->dieUsage( "Parameters '{$prefix}user' and '{$prefix}filterbots' cannot be used together", 'badparams' );
144 $this->addTimestampWhereRange( 'img_timestamp', ( $ascendingOrder ?
'newer' : 'older' ), $params['start'], $params['end'] );
147 if ( !is_null( $params['user'] ) ) {
148 $this->addWhereFld( 'img_user_text', $params['user'] );
150 if ( $params['filterbots'] != 'all' ) {
151 $this->addTables( 'user_groups' );
152 $this->addJoinConds( array( 'user_groups' => array(
155 'ug_group' => User
::getGroupsWithPermission( 'bot' ),
159 $groupCond = ( $params['filterbots'] == 'nobots' ?
'NULL': 'NOT NULL' );
160 $this->addWhere( "ug_group IS $groupCond" );
164 // Filters not depending on sort
165 if ( isset( $params['minsize'] ) ) {
166 $this->addWhere( 'img_size>=' . intval( $params['minsize'] ) );
169 if ( isset( $params['maxsize'] ) ) {
170 $this->addWhere( 'img_size<=' . intval( $params['maxsize'] ) );
174 if ( isset( $params['sha1'] ) ) {
175 $sha1 = strtolower( $params['sha1'] );
176 if ( !$this->validateSha1Hash( $sha1 ) ) {
177 $this->dieUsage( 'The SHA1 hash provided is not valid', 'invalidsha1hash' );
179 $sha1 = wfBaseConvert( $sha1, 16, 36, 31 );
180 } elseif ( isset( $params['sha1base36'] ) ) {
181 $sha1 = strtolower( $params['sha1base36'] );
182 if ( !$this->validateSha1Base36Hash( $sha1 ) ) {
183 $this->dieUsage( 'The SHA1Base36 hash provided is not valid', 'invalidsha1base36hash' );
187 $this->addWhereFld( 'img_sha1', $sha1 );
190 if ( !is_null( $params['mime'] ) ) {
192 if ( $wgMiserMode ) {
193 $this->dieUsage( 'MIME search disabled in Miser Mode', 'mimesearchdisabled' );
196 list( $major, $minor ) = File
::splitMime( $params['mime'] );
198 $this->addWhereFld( 'img_major_mime', $major );
199 $this->addWhereFld( 'img_minor_mime', $minor );
202 $limit = $params['limit'];
203 $this->addOption( 'LIMIT', $limit +
1 );
205 if ( !$ascendingOrder ) {
208 if ( $params['sort'] == 'timestamp' ) {
209 $this->addOption( 'ORDER BY', 'img_timestamp' . $sortFlag );
210 if ( !is_null( $params['user'] ) ) {
211 $this->addOption( 'USE INDEX', array( 'image' => 'img_usertext_timestamp' ) );
213 $this->addOption( 'USE INDEX', array( 'image' => 'img_timestamp' ) );
216 $this->addOption( 'ORDER BY', 'img_name' . $sortFlag );
219 $res = $this->select( __METHOD__
);
223 $result = $this->getResult();
224 foreach ( $res as $row ) {
225 if ( ++
$count > $limit ) {
226 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
227 if ( $params['sort'] == 'name' ) {
228 $this->setContinueEnumParameter( 'continue', $row->img_name
);
230 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->img_timestamp
) );
235 if ( is_null( $resultPageSet ) ) {
236 $file = $repo->newFileFromRow( $row );
237 $info = array_merge( array( 'name' => $row->img_name
),
238 ApiQueryImageInfo
::getInfo( $file, $prop, $result ) );
239 self
::addTitleInfo( $info, $file->getTitle() );
241 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $info );
243 if ( $params['sort'] == 'name' ) {
244 $this->setContinueEnumParameter( 'continue', $row->img_name
);
246 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->img_timestamp
) );
251 $titles[] = Title
::makeTitle( NS_FILE
, $row->img_name
);
255 if ( is_null( $resultPageSet ) ) {
256 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'img' );
258 $resultPageSet->populateFromTitles( $titles );
262 public function getAllowedParams() {
265 ApiBase
::PARAM_DFLT
=> 'name',
266 ApiBase
::PARAM_TYPE
=> array(
272 ApiBase
::PARAM_DFLT
=> 'ascending',
273 ApiBase
::PARAM_TYPE
=> array(
286 ApiBase
::PARAM_TYPE
=> 'timestamp'
289 ApiBase
::PARAM_TYPE
=> 'timestamp'
292 ApiBase
::PARAM_TYPE
=> ApiQueryImageInfo
::getPropertyNames( $this->propertyFilter
),
293 ApiBase
::PARAM_DFLT
=> 'timestamp|url',
294 ApiBase
::PARAM_ISMULTI
=> true
298 ApiBase
::PARAM_TYPE
=> 'integer',
301 ApiBase
::PARAM_TYPE
=> 'integer',
304 'sha1base36' => null,
306 ApiBase
::PARAM_TYPE
=> 'user'
308 'filterbots' => array(
309 ApiBase
::PARAM_DFLT
=> 'all',
310 ApiBase
::PARAM_TYPE
=> array(
318 ApiBase
::PARAM_DFLT
=> 10,
319 ApiBase
::PARAM_TYPE
=> 'limit',
320 ApiBase
::PARAM_MIN
=> 1,
321 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
322 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
327 public function getParamDescription() {
328 $p = $this->getModulePrefix();
330 'sort' => 'Property to sort by',
331 'dir' => 'The direction in which to list',
332 'from' => "The image title to start enumerating from. Can only be used with {$p}sort=name",
333 'to' => "The image title to stop enumerating at. Can only be used with {$p}sort=name",
334 'continue' => 'When more results are available, use this to continue',
335 'start' => "The timestamp to start enumerating from. Can only be used with {$p}sort=timestamp",
336 'end' => "The timestamp to end enumerating. Can only be used with {$p}sort=timestamp",
337 'prop' => ApiQueryImageInfo
::getPropertyDescriptions( $this->propertyFilter
),
338 'prefix' => "Search for all image titles that begin with this value. Can only be used with {$p}sort=name",
339 'minsize' => 'Limit to images with at least this many bytes',
340 'maxsize' => 'Limit to images with at most this many bytes',
341 'sha1' => "SHA1 hash of image. Overrides {$p}sha1base36",
342 'sha1base36' => 'SHA1 hash of image in base 36 (used in MediaWiki)',
343 'user' => "Only return files uploaded by this user. Can only be used with {$p}sort=timestamp. Cannot be used together with {$p}filterbots",
344 'filterbots' => "How to filter files uploaded by bots. Can only be used with {$p}sort=timestamp. Cannot be used together with {$p}user",
345 'mime' => 'What MIME type to search for. e.g. image/jpeg. Disabled in Miser Mode',
346 'limit' => 'How many images in total to return',
350 private $propertyFilter = array( 'archivename', 'thumbmime' );
352 public function getResultProperties() {
361 ApiQueryImageInfo
::getResultPropertiesFiltered( $this->propertyFilter
)
365 public function getDescription() {
366 return 'Enumerate all images sequentially';
369 public function getPossibleErrors() {
370 $p = $this->getModulePrefix();
371 return array_merge( parent
::getPossibleErrors(), array(
372 array( 'code' => 'params', 'info' => 'Use "gaifilterredir=nonredirects" option instead of "redirects" when using allimages as a generator' ),
373 array( 'code' => 'badparams', 'info' => "Parameter'{$p}start' can only be used with {$p}sort=timestamp" ),
374 array( 'code' => 'badparams', 'info' => "Parameter'{$p}end' can only be used with {$p}sort=timestamp" ),
375 array( 'code' => 'badparams', 'info' => "Parameter'{$p}user' can only be used with {$p}sort=timestamp" ),
376 array( 'code' => 'badparams', 'info' => "Parameter'{$p}filterbots' can only be used with {$p}sort=timestamp" ),
377 array( 'code' => 'badparams', 'info' => "Parameter'{$p}from' can only be used with {$p}sort=name" ),
378 array( 'code' => 'badparams', 'info' => "Parameter'{$p}to' can only be used with {$p}sort=name" ),
379 array( 'code' => 'badparams', 'info' => "Parameter'{$p}prefix' can only be used with {$p}sort=name" ),
380 array( 'code' => 'badparams', 'info' => "Parameters '{$p}user' and '{$p}filterbots' cannot be used together" ),
381 array( 'code' => 'unsupportedrepo', 'info' => 'Local file repository does not support querying all images' ),
382 array( 'code' => 'mimesearchdisabled', 'info' => 'MIME search disabled in Miser Mode' ),
383 array( 'code' => 'invalidsha1hash', 'info' => 'The SHA1 hash provided is not valid' ),
384 array( 'code' => 'invalidsha1base36hash', 'info' => 'The SHA1Base36 hash provided is not valid' ),
388 public function getExamples() {
390 'api.php?action=query&list=allimages&aifrom=B' => array(
392 'Show a list of files starting at the letter "B"',
394 'api.php?action=query&list=allimages&aiprop=user|timestamp|url&aisort=timestamp&aidir=older' => array(
396 'Show a list of recently uploaded files similar to Special:NewFiles',
398 'api.php?action=query&generator=allimages&gailimit=4&gaifrom=T&prop=imageinfo' => array(
399 'Using as Generator',
400 'Show info about 4 files starting at the letter "T"',
405 public function getHelpUrls() {
406 return 'https://www.mediawiki.org/wiki/API:Allimages';