API: (bug 16858) Revamped list=deletedrevs to make listing deleted contributions...
[lhc/web/wiklou.git] / includes / api / ApiQueryAllimages.php
1 <?php
2
3 /*
4 * Created on Mar 16, 2008
5 *
6 * API for MediaWiki 1.12+
7 *
8 * Copyright (C) 2008 Vasiliev Victor vasilvv@gmail.com,
9 * based on ApiQueryAllpages.php
10 *
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.
15 *
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.
20 *
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
25 */
26
27 if (!defined('MEDIAWIKI')) {
28 // Eclipse helper - will be ignored in production
29 require_once ('ApiQueryBase.php');
30 }
31
32 /**
33 * Query module to enumerate all available pages.
34 *
35 * @ingroup API
36 */
37 class ApiQueryAllimages extends ApiQueryGeneratorBase {
38
39 public function __construct($query, $moduleName) {
40 parent :: __construct($query, $moduleName, 'ai');
41 }
42
43 public function execute() {
44 $this->run();
45 }
46
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');
50
51 $this->run($resultPageSet);
52 }
53
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');
58
59 $db = $this->getDB();
60
61 $params = $this->extractRequestParams();
62
63 // Image filters
64 $dir = ($params['dir'] == 'descending' ? 'older' : 'newer');
65 $from = (is_null($params['from']) ? null : $this->titlePartToKey($params['from']));
66 $this->addWhereRange('img_name', $dir, $from, null);
67 if (isset ($params['prefix']))
68 $this->addWhere("img_name LIKE '" . $db->escapeLike($this->titlePartToKey($params['prefix'])) . "%'");
69
70 if (isset ($params['minsize'])) {
71 $this->addWhere('img_size>=' . intval($params['minsize']));
72 }
73
74 if (isset ($params['maxsize'])) {
75 $this->addWhere('img_size<=' . intval($params['maxsize']));
76 }
77
78 $sha1 = false;
79 if( isset( $params['sha1'] ) ) {
80 $sha1 = wfBaseConvert( $params['sha1'], 16, 36, 31 );
81 } elseif( isset( $params['sha1base36'] ) ) {
82 $sha1 = $params['sha1base36'];
83 }
84 if( $sha1 ) {
85 $this->addWhere( 'img_sha1=' . $db->addQuotes( $sha1 ) );
86 }
87
88 $this->addTables('image');
89
90 $prop = array_flip($params['prop']);
91 $this->addFields( LocalFile::selectFields() );
92
93 $limit = $params['limit'];
94 $this->addOption('LIMIT', $limit+1);
95 $this->addOption('ORDER BY', 'img_name' .
96 ($params['dir'] == 'descending' ? ' DESC' : ''));
97
98 $res = $this->select(__METHOD__);
99
100 $data = array ();
101 $count = 0;
102 $result = $this->getResult();
103 while ($row = $db->fetchObject($res)) {
104 if (++ $count > $limit) {
105 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
106 // TODO: Security issue - if the user has no right to view next title, it will still be shown
107 $this->setContinueEnumParameter('from', $this->keyToTitle($row->img_name));
108 break;
109 }
110
111 if (is_null($resultPageSet)) {
112 $file = $repo->newFileFromRow( $row );
113 $data[] = array_merge(array('name' => $row->img_name),
114 ApiQueryImageInfo::getInfo($file, $prop, $result));
115 } else {
116 $data[] = Title::makeTitle(NS_FILE, $row->img_name);
117 }
118 }
119 $db->freeResult($res);
120
121 if (is_null($resultPageSet)) {
122 $result = $this->getResult();
123 $result->setIndexedTagName($data, 'img');
124 $result->addValue('query', $this->getModuleName(), $data);
125 } else {
126 $resultPageSet->populateFromTitles( $data );
127 }
128 }
129
130 public function getAllowedParams() {
131 return array (
132 'from' => null,
133 'prefix' => null,
134 'minsize' => array (
135 ApiBase :: PARAM_TYPE => 'integer',
136 ),
137 'maxsize' => array (
138 ApiBase :: PARAM_TYPE => 'integer',
139 ),
140 'limit' => array (
141 ApiBase :: PARAM_DFLT => 10,
142 ApiBase :: PARAM_TYPE => 'limit',
143 ApiBase :: PARAM_MIN => 1,
144 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
145 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
146 ),
147 'dir' => array (
148 ApiBase :: PARAM_DFLT => 'ascending',
149 ApiBase :: PARAM_TYPE => array (
150 'ascending',
151 'descending'
152 )
153 ),
154 'sha1' => null,
155 'sha1base36' => null,
156 'prop' => array (
157 ApiBase :: PARAM_TYPE => array(
158 'timestamp',
159 'user',
160 'comment',
161 'url',
162 'size',
163 'dimensions', // Obsolete
164 'mime',
165 'sha1',
166 'metadata',
167 'bitdepth',
168 ),
169 ApiBase :: PARAM_DFLT => 'timestamp|url',
170 ApiBase :: PARAM_ISMULTI => true
171 )
172 );
173 }
174
175 public function getParamDescription() {
176 return array (
177 'from' => 'The image title to start enumerating from.',
178 'prefix' => 'Search for all image titles that begin with this value.',
179 'dir' => 'The direction in which to list',
180 'minsize' => 'Limit to images with at least this many bytes',
181 'maxsize' => 'Limit to images with at most this many bytes',
182 'limit' => 'How many total images to return.',
183 'sha1' => 'SHA1 hash of image',
184 'sha1base36' => 'SHA1 hash of image in base 36 (used in MediaWiki)',
185 'prop' => 'Which properties to get',
186 );
187 }
188
189 public function getDescription() {
190 return 'Enumerate all images sequentially';
191 }
192
193 protected function getExamples() {
194 return array (
195 'Simple Use',
196 ' Show a list of images starting at the letter "B"',
197 ' api.php?action=query&list=allimages&aifrom=B',
198 'Using as Generator',
199 ' Show info about 4 images starting at the letter "T"',
200 ' api.php?action=query&generator=allimages&gailimit=4&gaifrom=T&prop=imageinfo',
201 );
202 }
203
204 public function getVersion() {
205 return __CLASS__ . ': $Id$';
206 }
207 }