8b513f34a65c973c1f25292a019fe6940db8fef2
4 * Created on Mar 16, 2008
6 * API for MediaWiki 1.12+
8 * Copyright (C) 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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 * http://www.gnu.org/copyleft/gpl.html
27 if (!defined('MEDIAWIKI')) {
28 // Eclipse helper - will be ignored in production
29 require_once ('ApiQueryBase.php');
33 * Query module to enumerate all available pages.
37 class ApiQueryAllimages
extends ApiQueryGeneratorBase
{
39 public function __construct($query, $moduleName) {
40 parent
:: __construct($query, $moduleName, 'ai');
43 public function execute() {
47 public function executeGenerator($resultPageSet) {
48 if ($resultPageSet->isResolvingRedirects())
49 $this->dieUsage('Use "gaifilterredir=nonredirects" option instead of "redirects" when using allimages as a generator', 'params');
51 $this->run($resultPageSet);
54 private function run($resultPageSet = null) {
55 $repo = RepoGroup
::singleton()->getLocalRepo();
56 if ( !$repo instanceof LocalRepo
)
57 $this->dieUsage('Local file repository does not support querying all images', 'unsupportedrepo');
61 $params = $this->extractRequestParams();
64 if (!is_null($params['from']))
65 $this->addWhere('img_name>=' . $db->addQuotes($this->titlePartToKey($params['from'])));
66 if (isset ($params['prefix']))
67 $this->addWhere("img_name LIKE '" . $db->escapeLike($this->titlePartToKey($params['prefix'])) . "%'");
69 if (isset ($params['minsize'])) {
70 $this->addWhere('img_size>=' . intval($params['minsize']));
73 if (isset ($params['maxsize'])) {
74 $this->addWhere('img_size<=' . intval($params['maxsize']));
78 if( isset( $params['sha1'] ) ) {
79 $sha1 = wfBaseConvert( $params['sha1'], 16, 36, 31 );
80 } elseif( isset( $params['sha1base36'] ) ) {
81 $sha1 = $params['sha1base36'];
84 $this->addWhere( 'img_sha1=' . $db->addQuotes( $sha1 ) );
87 $this->addTables('image');
89 $prop = array_flip($params['prop']);
90 $this->addFields( LocalFile
::selectFields() );
92 $limit = $params['limit'];
93 $this->addOption('LIMIT', $limit+
1);
94 $this->addOption('ORDER BY', 'img_name' .
95 ($params['dir'] == 'descending' ?
' DESC' : ''));
97 $res = $this->select(__METHOD__
);
101 $result = $this->getResult();
102 while ($row = $db->fetchObject($res)) {
103 if (++
$count > $limit) {
104 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
105 // TODO: Security issue - if the user has no right to view next title, it will still be shown
106 $this->setContinueEnumParameter('from', $this->keyToTitle($row->img_name
));
110 if (is_null($resultPageSet)) {
111 $file = $repo->newFileFromRow( $row );
112 $data[] = array_merge(array('name' => $row->img_name
),
113 ApiQueryImageInfo
::getInfo($file, $prop, $result));
115 $data[] = Title
::makeTitle(NS_IMAGE
, $row->img_name
);
118 $db->freeResult($res);
120 if (is_null($resultPageSet)) {
121 $result = $this->getResult();
122 $result->setIndexedTagName($data, 'img');
123 $result->addValue('query', $this->getModuleName(), $data);
125 $resultPageSet->populateFromTitles( $data );
129 public function getAllowedParams() {
134 ApiBase
:: PARAM_TYPE
=> 'integer',
137 ApiBase
:: PARAM_TYPE
=> 'integer',
140 ApiBase
:: PARAM_DFLT
=> 10,
141 ApiBase
:: PARAM_TYPE
=> 'limit',
142 ApiBase
:: PARAM_MIN
=> 1,
143 ApiBase
:: PARAM_MAX
=> ApiBase
:: LIMIT_BIG1
,
144 ApiBase
:: PARAM_MAX2
=> ApiBase
:: LIMIT_BIG2
147 ApiBase
:: PARAM_DFLT
=> 'ascending',
148 ApiBase
:: PARAM_TYPE
=> array (
154 'sha1base36' => null,
156 ApiBase
:: PARAM_TYPE
=> array(
162 'dimensions', // Obsolete
168 ApiBase
:: PARAM_DFLT
=> 'timestamp|url',
169 ApiBase
:: PARAM_ISMULTI
=> true
174 public function getParamDescription() {
176 'from' => 'The image title to start enumerating from.',
177 'prefix' => 'Search for all image titles that begin with this value.',
178 'dir' => 'The direction in which to list',
179 'minsize' => 'Limit to images with at least this many bytes',
180 'maxsize' => 'Limit to images with at most this many bytes',
181 'limit' => 'How many total images to return.',
182 'sha1' => 'SHA1 hash of image',
183 'sha1base36' => 'SHA1 hash of image in base 36 (used in MediaWiki)',
184 'prop' => 'Which properties to get',
188 public function getDescription() {
189 return 'Enumerate all images sequentially';
192 protected function getExamples() {
195 ' Show a list of images starting at the letter "B"',
196 ' api.php?action=query&list=allimages&aifrom=B',
197 'Using as Generator',
198 ' Show info about 4 images starting at the letter "T"',
199 ' api.php?action=query&generator=allimages&gailimit=4&gaifrom=T&prop=imageinfo',
203 public function getVersion() {
204 return __CLASS__
. ': $Id$';