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 to make sure the repo's DB is used
45 * which may not necesarilly 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 if ( count( $cont ) != 1 ) {
117 $this->dieUsage( 'Invalid continue param. You should pass the ' .
118 'original value returned by the previous query', '_badcontinue' );
120 $op = ( $ascendingOrder ?
'>' : '<' );
121 $continueFrom = $db->addQuotes( $cont[0] );
122 $this->addWhere( "img_name $op= $continueFrom" );
126 $from = ( is_null( $params['from'] ) ?
null : $this->titlePartToKey( $params['from'] ) );
127 $to = ( is_null( $params['to'] ) ?
null : $this->titlePartToKey( $params['to'] ) );
128 $this->addWhereRange( 'img_name', ( $ascendingOrder ?
'newer' : 'older' ), $from, $to );
130 if ( isset( $params['prefix'] ) ) {
131 $this->addWhere( 'img_name' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
134 // Check mutually exclusive params
135 $disallowed = array( 'from', 'to', 'prefix' );
136 foreach ( $disallowed as $pname ) {
137 if ( isset( $params[$pname] ) ) {
138 $this->dieUsage( "Parameter '{$prefix}{$pname}' can only be used with {$prefix}sort=name", 'badparams' );
141 if ( !is_null( $params['user'] ) && $params['filterbots'] != 'all' ) {
142 // Since filterbots checks if each user has the bot right, it doesn't make sense to use it with user
143 $this->dieUsage( "Parameters '{$prefix}user' and '{$prefix}filterbots' cannot be used together", 'badparams' );
147 $this->addTimestampWhereRange( 'img_timestamp', ( $ascendingOrder ?
'newer' : 'older' ), $params['start'], $params['end'] );
150 if ( !is_null( $params['user'] ) ) {
151 $this->addWhereFld( 'img_user_text', $params['user'] );
153 if ( $params['filterbots'] != 'all' ) {
154 $this->addTables( 'user_groups' );
155 $this->addJoinConds( array( 'user_groups' => array(
158 'ug_group' => User
::getGroupsWithPermission( 'bot' ),
162 $groupCond = ( $params['filterbots'] == 'nobots' ?
'NULL': 'NOT NULL' );
163 $this->addWhere( "ug_group IS $groupCond" );
167 // Filters not depending on sort
168 if ( isset( $params['minsize'] ) ) {
169 $this->addWhere( 'img_size>=' . intval( $params['minsize'] ) );
172 if ( isset( $params['maxsize'] ) ) {
173 $this->addWhere( 'img_size<=' . intval( $params['maxsize'] ) );
177 if ( isset( $params['sha1'] ) ) {
178 $sha1 = strtolower( $params['sha1'] );
179 if ( !$this->validateSha1Hash( $sha1 ) ) {
180 $this->dieUsage( 'The SHA1 hash provided is not valid', 'invalidsha1hash' );
182 $sha1 = wfBaseConvert( $sha1, 16, 36, 31 );
183 } elseif ( isset( $params['sha1base36'] ) ) {
184 $sha1 = strtolower( $params['sha1base36'] );
185 if ( !$this->validateSha1Base36Hash( $sha1 ) ) {
186 $this->dieUsage( 'The SHA1Base36 hash provided is not valid', 'invalidsha1base36hash' );
190 $this->addWhereFld( 'img_sha1', $sha1 );
193 if ( !is_null( $params['mime'] ) ) {
195 if ( $wgMiserMode ) {
196 $this->dieUsage( 'MIME search disabled in Miser Mode', 'mimesearchdisabled' );
199 list( $major, $minor ) = File
::splitMime( $params['mime'] );
201 $this->addWhereFld( 'img_major_mime', $major );
202 $this->addWhereFld( 'img_minor_mime', $minor );
205 $limit = $params['limit'];
206 $this->addOption( 'LIMIT', $limit +
1 );
208 if ( !$ascendingOrder ) {
211 if ( $params['sort'] == 'timestamp' ) {
212 $this->addOption( 'ORDER BY', 'img_timestamp' . $sortFlag );
213 if ( !is_null( $params['user'] ) ) {
214 $this->addOption( 'USE INDEX', array( 'image' => 'img_usertext_timestamp' ) );
216 $this->addOption( 'USE INDEX', array( 'image' => 'img_timestamp' ) );
219 $this->addOption( 'ORDER BY', 'img_name' . $sortFlag );
222 $res = $this->select( __METHOD__
);
226 $result = $this->getResult();
227 foreach ( $res as $row ) {
228 if ( ++
$count > $limit ) {
229 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
230 if ( $params['sort'] == 'name' ) {
231 $this->setContinueEnumParameter( 'continue', $row->img_name
);
233 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->img_timestamp
) );
238 if ( is_null( $resultPageSet ) ) {
239 $file = $repo->newFileFromRow( $row );
240 $info = array_merge( array( 'name' => $row->img_name
),
241 ApiQueryImageInfo
::getInfo( $file, $prop, $result ) );
242 self
::addTitleInfo( $info, $file->getTitle() );
244 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $info );
246 if ( $params['sort'] == 'name' ) {
247 $this->setContinueEnumParameter( 'continue', $row->img_name
);
249 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->img_timestamp
) );
254 $titles[] = Title
::makeTitle( NS_FILE
, $row->img_name
);
258 if ( is_null( $resultPageSet ) ) {
259 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'img' );
261 $resultPageSet->populateFromTitles( $titles );
265 public function getAllowedParams() {
268 ApiBase
::PARAM_DFLT
=> 'name',
269 ApiBase
::PARAM_TYPE
=> array(
275 ApiBase
::PARAM_DFLT
=> 'ascending',
276 ApiBase
::PARAM_TYPE
=> array(
289 ApiBase
::PARAM_TYPE
=> 'timestamp'
292 ApiBase
::PARAM_TYPE
=> 'timestamp'
295 ApiBase
::PARAM_TYPE
=> ApiQueryImageInfo
::getPropertyNames( $this->propertyFilter
),
296 ApiBase
::PARAM_DFLT
=> 'timestamp|url',
297 ApiBase
::PARAM_ISMULTI
=> true
301 ApiBase
::PARAM_TYPE
=> 'integer',
304 ApiBase
::PARAM_TYPE
=> 'integer',
307 'sha1base36' => null,
309 ApiBase
::PARAM_TYPE
=> 'user'
311 'filterbots' => array(
312 ApiBase
::PARAM_DFLT
=> 'all',
313 ApiBase
::PARAM_TYPE
=> array(
321 ApiBase
::PARAM_DFLT
=> 10,
322 ApiBase
::PARAM_TYPE
=> 'limit',
323 ApiBase
::PARAM_MIN
=> 1,
324 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
325 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
330 public function getParamDescription() {
331 $p = $this->getModulePrefix();
333 'sort' => 'Property to sort by',
334 'dir' => 'The direction in which to list',
335 'from' => "The image title to start enumerating from. Can only be used with {$p}sort=name",
336 'to' => "The image title to stop enumerating at. Can only be used with {$p}sort=name",
337 'continue' => 'When more results are available, use this to continue',
338 'start' => "The timestamp to start enumerating from. Can only be used with {$p}sort=timestamp",
339 'end' => "The timestamp to end enumerating. Can only be used with {$p}sort=timestamp",
340 'prop' => ApiQueryImageInfo
::getPropertyDescriptions( $this->propertyFilter
),
341 'prefix' => "Search for all image titles that begin with this value. Can only be used with {$p}sort=name",
342 'minsize' => 'Limit to images with at least this many bytes',
343 'maxsize' => 'Limit to images with at most this many bytes',
344 'sha1' => "SHA1 hash of image. Overrides {$p}sha1base36",
345 'sha1base36' => 'SHA1 hash of image in base 36 (used in MediaWiki)',
346 'user' => "Only return files uploaded by this user. Can only be used with {$p}sort=timestamp. Cannot be used together with {$p}filterbots",
347 'filterbots' => "How to filter files uploaded by bots. Can only be used with {$p}sort=timestamp. Cannot be used together with {$p}user",
348 'mime' => 'What MIME type to search for. e.g. image/jpeg. Disabled in Miser Mode',
349 'limit' => 'How many images in total to return',
353 private $propertyFilter = array( 'archivename', 'thumbmime' );
355 public function getResultProperties() {
364 ApiQueryImageInfo
::getResultPropertiesFiltered( $this->propertyFilter
)
368 public function getDescription() {
369 return 'Enumerate all images sequentially';
372 public function getPossibleErrors() {
373 $p = $this->getModulePrefix();
374 return array_merge( parent
::getPossibleErrors(), array(
375 array( 'code' => 'params', 'info' => 'Use "gaifilterredir=nonredirects" option instead of "redirects" when using allimages as a generator' ),
376 array( 'code' => 'badparams', 'info' => "Parameter'{$p}start' can only be used with {$p}sort=timestamp" ),
377 array( 'code' => 'badparams', 'info' => "Parameter'{$p}end' can only be used with {$p}sort=timestamp" ),
378 array( 'code' => 'badparams', 'info' => "Parameter'{$p}user' can only be used with {$p}sort=timestamp" ),
379 array( 'code' => 'badparams', 'info' => "Parameter'{$p}filterbots' can only be used with {$p}sort=timestamp" ),
380 array( 'code' => 'badparams', 'info' => "Parameter'{$p}from' can only be used with {$p}sort=name" ),
381 array( 'code' => 'badparams', 'info' => "Parameter'{$p}to' can only be used with {$p}sort=name" ),
382 array( 'code' => 'badparams', 'info' => "Parameter'{$p}prefix' can only be used with {$p}sort=name" ),
383 array( 'code' => 'badparams', 'info' => "Parameters '{$p}user' and '{$p}filterbots' cannot be used together" ),
384 array( 'code' => 'unsupportedrepo', 'info' => 'Local file repository does not support querying all images' ),
385 array( 'code' => 'mimesearchdisabled', 'info' => 'MIME search disabled in Miser Mode' ),
386 array( 'code' => 'invalidsha1hash', 'info' => 'The SHA1 hash provided is not valid' ),
387 array( 'code' => 'invalidsha1base36hash', 'info' => 'The SHA1Base36 hash provided is not valid' ),
388 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
392 public function getExamples() {
394 'api.php?action=query&list=allimages&aifrom=B' => array(
396 'Show a list of files starting at the letter "B"',
398 'api.php?action=query&list=allimages&aiprop=user|timestamp|url&aisort=timestamp&aidir=older' => array(
400 'Show a list of recently uploaded files similar to Special:NewFiles',
402 'api.php?action=query&generator=allimages&gailimit=4&gaifrom=T&prop=imageinfo' => array(
403 'Using as Generator',
404 'Show info about 4 files starting at the letter "T"',
409 public function getHelpUrls() {
410 return 'https://www.mediawiki.org/wiki/API:Allimages';
413 public function getVersion() {
414 return __CLASS__
. ': $Id$';