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' );
86 $params = $this->extractRequestParams();
88 if ( !is_null( $params['continue'] ) ) {
89 $cont = explode( '|', $params['continue'] );
90 if ( count( $cont ) != 1 ) {
91 $this->dieUsage( "Invalid continue param. You should pass the " .
92 "original value returned by the previous query", "_badcontinue" );
94 $op = $params['dir'] == 'descending' ?
'<' : '>';
95 $cont_from = $db->addQuotes( $cont[0] );
96 $this->addWhere( "img_name $op= $cont_from" );
100 $dir = ( $params['dir'] == 'descending' ?
'older' : 'newer' );
101 $from = ( is_null( $params['from'] ) ?
null : $this->titlePartToKey( $params['from'] ) );
102 $to = ( is_null( $params['to'] ) ?
null : $this->titlePartToKey( $params['to'] ) );
103 $this->addWhereRange( 'img_name', $dir, $from, $to );
105 if ( isset( $params['prefix'] ) )
106 $this->addWhere( 'img_name' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
108 if ( isset( $params['minsize'] ) ) {
109 $this->addWhere( 'img_size>=' . intval( $params['minsize'] ) );
112 if ( isset( $params['maxsize'] ) ) {
113 $this->addWhere( 'img_size<=' . intval( $params['maxsize'] ) );
117 if ( isset( $params['sha1'] ) ) {
118 if ( !$this->validateSha1Hash( $params['sha1'] ) ) {
119 $this->dieUsage( 'The SHA1 hash provided is not valid', 'invalidsha1hash' );
121 $sha1 = wfBaseConvert( $params['sha1'], 16, 36, 31 );
122 } elseif ( isset( $params['sha1base36'] ) ) {
123 $sha1 = $params['sha1base36'];
124 if ( !$this->validateSha1Base36Hash( $sha1 ) ) {
125 $this->dieUsage( 'The SHA1Base36 hash provided is not valid', 'invalidsha1base36hash' );
129 $this->addWhereFld( 'img_sha1', $sha1 );
132 if ( !is_null( $params['mime'] ) ) {
134 if ( $wgMiserMode ) {
135 $this->dieUsage( 'MIME search disabled in Miser Mode', 'mimesearchdisabled' );
138 list( $major, $minor ) = File
::splitMime( $params['mime'] );
140 $this->addWhereFld( 'img_major_mime', $major );
141 $this->addWhereFld( 'img_minor_mime', $minor );
144 $this->addTables( 'image' );
146 $prop = array_flip( $params['prop'] );
147 $this->addFields( LocalFile
::selectFields() );
149 $limit = $params['limit'];
150 $this->addOption( 'LIMIT', $limit +
1 );
151 $sort = ( $params['dir'] == 'descending' ?
' DESC' : '' );
152 $this->addOption( 'ORDER BY', 'img_name' . $sort );
154 $res = $this->select( __METHOD__
);
158 $result = $this->getResult();
159 foreach ( $res as $row ) {
160 if ( ++
$count > $limit ) {
161 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
162 $this->setContinueEnumParameter( 'continue', $row->img_name
);
166 if ( is_null( $resultPageSet ) ) {
167 $file = $repo->newFileFromRow( $row );
168 $info = array_merge( array( 'name' => $row->img_name
),
169 ApiQueryImageInfo
::getInfo( $file, $prop, $result ) );
170 self
::addTitleInfo( $info, $file->getTitle() );
172 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $info );
174 $this->setContinueEnumParameter( 'continue', $row->img_name
);
178 $titles[] = Title
::makeTitle( NS_FILE
, $row->img_name
);
182 if ( is_null( $resultPageSet ) ) {
183 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'img' );
185 $resultPageSet->populateFromTitles( $titles );
189 public function getAllowedParams() {
196 ApiBase
::PARAM_TYPE
=> 'integer',
199 ApiBase
::PARAM_TYPE
=> 'integer',
202 ApiBase
::PARAM_DFLT
=> 10,
203 ApiBase
::PARAM_TYPE
=> 'limit',
204 ApiBase
::PARAM_MIN
=> 1,
205 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
206 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
209 ApiBase
::PARAM_DFLT
=> 'ascending',
210 ApiBase
::PARAM_TYPE
=> array(
216 'sha1base36' => null,
218 ApiBase
::PARAM_TYPE
=> ApiQueryImageInfo
::getPropertyNames( $this->propertyFilter
),
219 ApiBase
::PARAM_DFLT
=> 'timestamp|url',
220 ApiBase
::PARAM_ISMULTI
=> true
226 public function getParamDescription() {
228 'from' => 'The image title to start enumerating from',
229 'continue' => 'When more results are available, use this to continue',
230 'to' => 'The image title to stop enumerating at',
231 'prefix' => 'Search for all image titles that begin with this value',
232 'dir' => 'The direction in which to list',
233 'minsize' => 'Limit to images with at least this many bytes',
234 'maxsize' => 'Limit to images with at most this many bytes',
235 'limit' => 'How many images in total to return',
236 'sha1' => "SHA1 hash of image. Overrides {$this->getModulePrefix()}sha1base36",
237 'sha1base36' => 'SHA1 hash of image in base 36 (used in MediaWiki)',
238 'prop' => ApiQueryImageInfo
::getPropertyDescriptions( $this->propertyFilter
),
239 'mime' => 'What MIME type to search for. e.g. image/jpeg. Disabled in Miser Mode',
243 private $propertyFilter = array( 'archivename' );
245 public function getResultProperties() {
254 ApiQueryImageInfo
::getResultPropertiesFiltered( $this->propertyFilter
)
258 public function getDescription() {
259 return 'Enumerate all images sequentially';
262 public function getPossibleErrors() {
263 return array_merge( parent
::getPossibleErrors(), array(
264 array( 'code' => 'params', 'info' => 'Use "gaifilterredir=nonredirects" option instead of "redirects" when using allimages as a generator' ),
265 array( 'code' => 'unsupportedrepo', 'info' => 'Local file repository does not support querying all images' ),
266 array( 'code' => 'mimesearchdisabled', 'info' => 'MIME search disabled in Miser Mode' ),
267 array( 'code' => 'invalidsha1hash', 'info' => 'The SHA1 hash provided is not valid' ),
268 array( 'code' => 'invalidsha1base36hash', 'info' => 'The SHA1Base36 hash provided is not valid' ),
269 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
273 public function getExamples() {
275 'api.php?action=query&list=allimages&aifrom=B' => array(
277 'Show a list of images starting at the letter "B"',
279 'api.php?action=query&generator=allimages&gailimit=4&gaifrom=T&prop=imageinfo' => array(
280 'Using as Generator',
281 'Show info about 4 images starting at the letter "T"',
286 public function getHelpUrls() {
287 return 'https://www.mediawiki.org/wiki/API:Allimages';
290 public function getVersion() {
291 return __CLASS__
. ': $Id$';