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 allpages as a generator', 'params');
51 $this->run($resultPageSet);
54 private function run($resultPageSet = null) {
58 $params = $this->extractRequestParams();
61 if (!is_null($params['from']))
62 $this->addWhere('img_name>=' . $db->addQuotes(ApiQueryBase
:: titleToKey($params['from'])));
63 if (isset ($params['prefix']))
64 $this->addWhere("img_name LIKE '" . $db->escapeLike(ApiQueryBase
:: titleToKey($params['prefix'])) . "%'");
66 if (isset ($params['minsize'])) {
67 $this->addWhere('img_size>=' . intval($params['minsize']));
70 if (isset ($params['maxsize'])) {
71 $this->addWhere('img_size<=' . intval($params['maxsize']));
75 if( isset( $params['sha1'] ) ) {
76 $sha1 = wfBaseConvert( $params['sha1'], 16, 36, 31 );
77 } elseif( isset( $params['sha1base36'] ) ) {
78 $sha1 = $params['sha1base36'];
81 $this->addWhere( 'img_sha1=' . $db->addQuotes( $sha1 ) );
84 $this->addTables('image');
86 $this->addFields(array (
97 $limit = $params['limit'];
98 $this->addOption('LIMIT', $limit+
1);
99 $this->addOption('ORDER BY', 'img_name' .
100 ($params['dir'] == 'descending' ?
' DESC' : ''));
102 $res = $this->select(__METHOD__
);
106 while ($row = $db->fetchObject($res)) {
107 if (++
$count > $limit) {
108 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
109 // TODO: Security issue - if the user has no right to view next title, it will still be shown
110 $this->setContinueEnumParameter('from', ApiQueryBase
:: keyToTitle($row->img_name
));
114 if (is_null($resultPageSet)) {
115 $file = wfLocalFile( $row->img_name
);
117 'name' => $row->img_name
,
118 'size' => $file->getSize(),
119 'width' => $file->getWidth(),
120 'height' => $file->getHeight(),
121 'mime' => $row->img_major_mime
. '/' . $row->img_minor_mime
,
122 'sha1' => wfBaseConvert( $file->getSha1(), 36, 16, 31 ),
123 'timestamp' => wfTimestamp(TS_ISO_8601
, $file->getTimestamp()),
124 'url' => $file->getFullUrl()
127 $data[] = Title
::makeTitle( NS_IMAGE
, $row->img_name
);
130 $db->freeResult($res);
132 if (is_null($resultPageSet)) {
133 $result = $this->getResult();
134 $result->setIndexedTagName($data, 'img');
135 $result->addValue('query', $this->getModuleName(), $data);
137 $resultPageSet->populateFromTitles( $data );
141 public function getAllowedParams() {
142 global $wgRestrictionTypes, $wgRestrictionLevels;
148 ApiBase
:: PARAM_TYPE
=> 'integer',
151 ApiBase
:: PARAM_TYPE
=> 'integer',
154 ApiBase
:: PARAM_DFLT
=> 10,
155 ApiBase
:: PARAM_TYPE
=> 'limit',
156 ApiBase
:: PARAM_MIN
=> 1,
157 ApiBase
:: PARAM_MAX
=> ApiBase
:: LIMIT_BIG1
,
158 ApiBase
:: PARAM_MAX2
=> ApiBase
:: LIMIT_BIG2
161 ApiBase
:: PARAM_DFLT
=> 'ascending',
162 ApiBase
:: PARAM_TYPE
=> array (
168 'sha1base36' => null,
172 public function getParamDescription() {
174 'from' => 'The image title to start enumerating from.',
175 'prefix' => 'Search for all image titles that begin with this value.',
176 'dir' => 'The direction in which to list',
177 'minsize' => 'Limit to images with at least this many bytes',
178 'maxsize' => 'Limit to images with at most this many bytes',
179 'limit' => 'How many total pages to return.',
180 'sha1' => 'SHA1 hash of image',
181 'sha1base36' => 'SHA1 hash of image in base 36 (used in MediaWiki)'
185 public function getDescription() {
186 return 'Enumerate all images sequentially';
189 protected function getExamples() {
192 ' Show a list of images starting at the letter "B"',
193 ' api.php?action=query&list=allimages&apfrom=B',
194 'Using as Generator',
195 ' Show info about 4 images starting at the letter "T"',
196 ' api.php?action=query&generator=allimages&gailimit=4&gaifrom=T&prop=imageinfo',
200 public function getVersion() {
201 return __CLASS__
. ': $Id$';