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
29 if ( !defined( 'MEDIAWIKI' ) ) {
30 // Eclipse helper - will be ignored in production
31 require_once( 'ApiQueryBase.php' );
35 * Query module to enumerate all available pages.
39 class ApiQueryAllimages
extends ApiQueryGeneratorBase
{
41 public function __construct( $query, $moduleName ) {
42 parent
::__construct( $query, $moduleName, 'ai' );
43 $this->mRepo
= RepoGroup
::singleton()->getLocalRepo();
47 * Overide parent method to make sure to make sure the repo's DB is used
48 * which may not necesarilly be the same as the local DB.
50 * TODO: allow querying non-local repos.
52 protected function getDB() {
53 return $this->mRepo
->getSlaveDB();
56 public function execute() {
60 public function getCacheMode( $params ) {
64 public function executeGenerator( $resultPageSet ) {
65 if ( $resultPageSet->isResolvingRedirects() ) {
66 $this->dieUsage( 'Use "gaifilterredir=nonredirects" option instead of "redirects" when using allimages as a generator', 'params' );
69 $this->run( $resultPageSet );
72 private function run( $resultPageSet = null ) {
74 if ( !$repo instanceof LocalRepo
) {
75 $this->dieUsage( 'Local file repository does not support querying all images', 'unsupportedrepo' );
80 $params = $this->extractRequestParams();
83 $dir = ( $params['dir'] == 'descending' ?
'older' : 'newer' );
84 $from = ( is_null( $params['from'] ) ?
null : $this->titlePartToKey( $params['from'] ) );
85 $to = ( is_null( $params['to'] ) ?
null : $this->titlePartToKey( $params['to'] ) );
86 $this->addWhereRange( 'img_name', $dir, $from, $to );
88 if ( isset( $params['prefix'] ) )
89 $this->addWhere( 'img_name' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
91 if ( isset( $params['minsize'] ) ) {
92 $this->addWhere( 'img_size>=' . intval( $params['minsize'] ) );
95 if ( isset( $params['maxsize'] ) ) {
96 $this->addWhere( 'img_size<=' . intval( $params['maxsize'] ) );
100 if ( isset( $params['sha1'] ) ) {
101 $sha1 = wfBaseConvert( $params['sha1'], 16, 36, 31 );
102 } elseif ( isset( $params['sha1base36'] ) ) {
103 $sha1 = $params['sha1base36'];
106 $this->addWhere( 'img_sha1=' . $db->addQuotes( $sha1 ) );
109 $this->addTables( 'image' );
111 $prop = array_flip( $params['prop'] );
112 $this->addFields( LocalFile
::selectFields() );
114 $limit = $params['limit'];
115 $this->addOption( 'LIMIT', $limit +
1 );
116 $this->addOption( 'ORDER BY', 'img_name' .
117 ( $params['dir'] == 'descending' ?
' DESC' : '' ) );
119 $res = $this->select( __METHOD__
);
123 $result = $this->getResult();
124 foreach ( $res as $row ) {
125 if ( ++
$count > $limit ) {
126 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
127 // TODO: Security issue - if the user has no right to view next title, it will still be shown
128 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->img_name
) );
132 if ( is_null( $resultPageSet ) ) {
133 $file = $repo->newFileFromRow( $row );
134 $info = array_merge( array( 'name' => $row->img_name
),
135 ApiQueryImageInfo
::getInfo( $file, $prop, $result ) );
136 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $info );
138 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->img_name
) );
142 $titles[] = Title
::makeTitle( NS_IMAGE
, $row->img_name
);
146 if ( is_null( $resultPageSet ) ) {
147 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'img' );
149 $resultPageSet->populateFromTitles( $titles );
153 public function getAllowedParams() {
159 ApiBase
::PARAM_TYPE
=> 'integer',
162 ApiBase
::PARAM_TYPE
=> 'integer',
165 ApiBase
::PARAM_DFLT
=> 10,
166 ApiBase
::PARAM_TYPE
=> 'limit',
167 ApiBase
::PARAM_MIN
=> 1,
168 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
169 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
172 ApiBase
::PARAM_DFLT
=> 'ascending',
173 ApiBase
::PARAM_TYPE
=> array(
179 'sha1base36' => null,
181 ApiBase
::PARAM_TYPE
=> ApiQueryImageInfo
::getPropertyNames(),
182 ApiBase
::PARAM_DFLT
=> 'timestamp|url',
183 ApiBase
::PARAM_ISMULTI
=> true
188 public function getParamDescription() {
190 'from' => 'The image title to start enumerating from',
191 'to' => 'The image title to stop enumerating at',
192 'prefix' => 'Search for all image titles that begin with this value',
193 'dir' => 'The direction in which to list',
194 'minsize' => 'Limit to images with at least this many bytes',
195 'maxsize' => 'Limit to images with at most this many bytes',
196 'limit' => 'How many images in total to return',
197 'sha1' => "SHA1 hash of image. Overrides {$this->getModulePrefix()}sha1base36",
198 'sha1base36' => 'SHA1 hash of image in base 36 (used in MediaWiki)',
200 'Which properties to get',
201 ' timestamp - Adds the timestamp when the image was upload',
202 ' user - Adds the username of the last uploader',
203 ' userid - Adds the user id of the last uploader',
204 ' comment - Adds the comment of the last upload',
205 ' url - Adds the URL of the image and its description page',
206 ' size - Adds the size of the image in bytes and its height and width',
207 ' dimensions - Alias of size',
208 ' sha1 - Adds the sha1 of the image',
209 ' mime - Adds the MIME of the image',
210 ' thumbmime - Adds the MIME of the tumbnail for the image',
211 ' archivename - Adds the file name of the archive version for non-latest versions',
212 ' bitdepth - Adds the bit depth of the version',
217 public function getDescription() {
218 return 'Enumerate all images sequentially';
221 public function getPossibleErrors() {
222 return array_merge( parent
::getPossibleErrors(), array(
223 array( 'code' => 'params', 'info' => 'Use "gaifilterredir=nonredirects" option instead of "redirects" when using allimages as a generator' ),
224 array( 'code' => 'unsupportedrepo', 'info' => 'Local file repository does not support querying all images' ),
228 protected function getExamples() {
231 ' Show a list of images starting at the letter "B"',
232 ' api.php?action=query&list=allimages&aifrom=B',
233 'Using as Generator',
234 ' Show info about 4 images starting at the letter "T"',
235 ' api.php?action=query&generator=allimages&gailimit=4&gaifrom=T&prop=imageinfo',
239 public function getVersion() {
240 return __CLASS__
. ': $Id$';