(bug 43177) API: Fix regression in case handling for sha1 params
[lhc/web/wiklou.git] / includes / api / ApiQueryAllImages.php
1 <?php
2
3 /**
4 * API for MediaWiki 1.12+
5 *
6 * Created on Mar 16, 2008
7 *
8 * Copyright © 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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 * http://www.gnu.org/copyleft/gpl.html
25 *
26 * @file
27 */
28
29 /**
30 * Query module to enumerate all available pages.
31 *
32 * @ingroup API
33 */
34 class ApiQueryAllImages extends ApiQueryGeneratorBase {
35
36 protected $mRepo;
37
38 public function __construct( $query, $moduleName ) {
39 parent::__construct( $query, $moduleName, 'ai' );
40 $this->mRepo = RepoGroup::singleton()->getLocalRepo();
41 }
42
43 /**
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.
46 *
47 * TODO: allow querying non-local repos.
48 * @return DatabaseBase
49 */
50 protected function getDB() {
51 return $this->mRepo->getSlaveDB();
52 }
53
54 public function execute() {
55 $this->run();
56 }
57
58 public function getCacheMode( $params ) {
59 return 'public';
60 }
61
62 /**
63 * @param $resultPageSet ApiPageSet
64 * @return void
65 */
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' );
69 }
70
71 $this->run( $resultPageSet );
72 }
73
74 /**
75 * @param $resultPageSet ApiPageSet
76 * @return void
77 */
78 private function run( $resultPageSet = null ) {
79 $repo = $this->mRepo;
80 if ( !$repo instanceof LocalRepo ) {
81 $this->dieUsage( 'Local file repository does not support querying all images', 'unsupportedrepo' );
82 }
83
84 $prefix = $this->getModulePrefix();
85
86 $db = $this->getDB();
87
88 $params = $this->extractRequestParams();
89
90 // Table and return fields
91 $this->addTables( 'image' );
92
93 $prop = array_flip( $params['prop'] );
94 $this->addFields( LocalFile::selectFields() );
95
96 $ascendingOrder = true;
97 if ( $params['dir'] == 'descending' || $params['dir'] == 'older') {
98 $ascendingOrder = false;
99 }
100
101 if ( $params['sort'] == 'name' ) {
102 // Check mutually exclusive params
103 $disallowed = array( 'start', 'end', 'user' );
104 foreach ( $disallowed as $pname ) {
105 if ( isset( $params[$pname] ) ) {
106 $this->dieUsage( "Parameter '{$prefix}{$pname}' can only be used with {$prefix}sort=timestamp", 'badparams' );
107 }
108 }
109 if ( $params['filterbots'] != 'all' ) {
110 $this->dieUsage( "Parameter '{$prefix}filterbots' can only be used with {$prefix}sort=timestamp", 'badparams' );
111 }
112
113 // Pagination
114 if ( !is_null( $params['continue'] ) ) {
115 $cont = explode( '|', $params['continue'] );
116 if ( count( $cont ) != 1 ) {
117 $this->dieUsage( 'Invalid continue param. You should pass the ' .
118 'original value returned by the previous query', '_badcontinue' );
119 }
120 $op = ( $ascendingOrder ? '>' : '<' );
121 $continueFrom = $db->addQuotes( $cont[0] );
122 $this->addWhere( "img_name $op= $continueFrom" );
123 }
124
125 // Image filters
126 $from = ( is_null( $params['from'] ) ? null : $this->titlePartToKey( $params['from'] ) );
127 $to = ( is_null( $params['to'] ) ? null : $this->titlePartToKey( $params['to'] ) );
128 $this->addWhereRange( 'img_name', ( $ascendingOrder ? 'newer' : 'older' ), $from, $to );
129
130 if ( isset( $params['prefix'] ) ) {
131 $this->addWhere( 'img_name' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
132 }
133 } else {
134 // Check mutually exclusive params
135 $disallowed = array( 'from', 'to', 'prefix' );
136 foreach ( $disallowed as $pname ) {
137 if ( isset( $params[$pname] ) ) {
138 $this->dieUsage( "Parameter '{$prefix}{$pname}' can only be used with {$prefix}sort=name", 'badparams' );
139 }
140 }
141 if ( !is_null( $params['user'] ) && $params['filterbots'] != 'all' ) {
142 // Since filterbots checks if each user has the bot right, it doesn't make sense to use it with user
143 $this->dieUsage( "Parameters '{$prefix}user' and '{$prefix}filterbots' cannot be used together", 'badparams' );
144 }
145
146 // Pagination
147 $this->addTimestampWhereRange( 'img_timestamp', ( $ascendingOrder ? 'newer' : 'older' ), $params['start'], $params['end'] );
148
149 // Image filters
150 if ( !is_null( $params['user'] ) ) {
151 $this->addWhereFld( 'img_user_text', $params['user'] );
152 }
153 if ( $params['filterbots'] != 'all' ) {
154 $this->addTables( 'user_groups' );
155 $this->addJoinConds( array( 'user_groups' => array(
156 'LEFT JOIN',
157 array(
158 'ug_group' => User::getGroupsWithPermission( 'bot' ),
159 'ug_user = img_user'
160 )
161 ) ) );
162 $groupCond = ( $params['filterbots'] == 'nobots' ? 'NULL': 'NOT NULL' );
163 $this->addWhere( "ug_group IS $groupCond" );
164 }
165 }
166
167 // Filters not depending on sort
168 if ( isset( $params['minsize'] ) ) {
169 $this->addWhere( 'img_size>=' . intval( $params['minsize'] ) );
170 }
171
172 if ( isset( $params['maxsize'] ) ) {
173 $this->addWhere( 'img_size<=' . intval( $params['maxsize'] ) );
174 }
175
176 $sha1 = false;
177 if ( isset( $params['sha1'] ) ) {
178 $sha1 = strtolower( $params['sha1'] );
179 if ( !$this->validateSha1Hash( $sha1 ) ) {
180 $this->dieUsage( 'The SHA1 hash provided is not valid', 'invalidsha1hash' );
181 }
182 $sha1 = wfBaseConvert( $sha1, 16, 36, 31 );
183 } elseif ( isset( $params['sha1base36'] ) ) {
184 $sha1 = strtolower( $params['sha1base36'] );
185 if ( !$this->validateSha1Base36Hash( $sha1 ) ) {
186 $this->dieUsage( 'The SHA1Base36 hash provided is not valid', 'invalidsha1base36hash' );
187 }
188 }
189 if ( $sha1 ) {
190 $this->addWhereFld( 'img_sha1', $sha1 );
191 }
192
193 if ( !is_null( $params['mime'] ) ) {
194 global $wgMiserMode;
195 if ( $wgMiserMode ) {
196 $this->dieUsage( 'MIME search disabled in Miser Mode', 'mimesearchdisabled' );
197 }
198
199 list( $major, $minor ) = File::splitMime( $params['mime'] );
200
201 $this->addWhereFld( 'img_major_mime', $major );
202 $this->addWhereFld( 'img_minor_mime', $minor );
203 }
204
205 $limit = $params['limit'];
206 $this->addOption( 'LIMIT', $limit + 1 );
207 $sortFlag = '';
208 if ( !$ascendingOrder ) {
209 $sortFlag = ' DESC';
210 }
211 if ( $params['sort'] == 'timestamp' ) {
212 $this->addOption( 'ORDER BY', 'img_timestamp' . $sortFlag );
213 if ( !is_null( $params['user'] ) ) {
214 $this->addOption( 'USE INDEX', array( 'image' => 'img_usertext_timestamp' ) );
215 } else {
216 $this->addOption( 'USE INDEX', array( 'image' => 'img_timestamp' ) );
217 }
218 } else {
219 $this->addOption( 'ORDER BY', 'img_name' . $sortFlag );
220 }
221
222 $res = $this->select( __METHOD__ );
223
224 $titles = array();
225 $count = 0;
226 $result = $this->getResult();
227 foreach ( $res as $row ) {
228 if ( ++ $count > $limit ) {
229 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
230 if ( $params['sort'] == 'name' ) {
231 $this->setContinueEnumParameter( 'continue', $row->img_name );
232 } else {
233 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->img_timestamp ) );
234 }
235 break;
236 }
237
238 if ( is_null( $resultPageSet ) ) {
239 $file = $repo->newFileFromRow( $row );
240 $info = array_merge( array( 'name' => $row->img_name ),
241 ApiQueryImageInfo::getInfo( $file, $prop, $result ) );
242 self::addTitleInfo( $info, $file->getTitle() );
243
244 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $info );
245 if ( !$fit ) {
246 if ( $params['sort'] == 'name' ) {
247 $this->setContinueEnumParameter( 'continue', $row->img_name );
248 } else {
249 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->img_timestamp ) );
250 }
251 break;
252 }
253 } else {
254 $titles[] = Title::makeTitle( NS_FILE, $row->img_name );
255 }
256 }
257
258 if ( is_null( $resultPageSet ) ) {
259 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'img' );
260 } else {
261 $resultPageSet->populateFromTitles( $titles );
262 }
263 }
264
265 public function getAllowedParams() {
266 return array (
267 'sort' => array(
268 ApiBase::PARAM_DFLT => 'name',
269 ApiBase::PARAM_TYPE => array(
270 'name',
271 'timestamp'
272 )
273 ),
274 'dir' => array(
275 ApiBase::PARAM_DFLT => 'ascending',
276 ApiBase::PARAM_TYPE => array(
277 // sort=name
278 'ascending',
279 'descending',
280 // sort=timestamp
281 'newer',
282 'older'
283 )
284 ),
285 'from' => null,
286 'to' => null,
287 'continue' => null,
288 'start' => array(
289 ApiBase::PARAM_TYPE => 'timestamp'
290 ),
291 'end' => array(
292 ApiBase::PARAM_TYPE => 'timestamp'
293 ),
294 'prop' => array(
295 ApiBase::PARAM_TYPE => ApiQueryImageInfo::getPropertyNames( $this->propertyFilter ),
296 ApiBase::PARAM_DFLT => 'timestamp|url',
297 ApiBase::PARAM_ISMULTI => true
298 ),
299 'prefix' => null,
300 'minsize' => array(
301 ApiBase::PARAM_TYPE => 'integer',
302 ),
303 'maxsize' => array(
304 ApiBase::PARAM_TYPE => 'integer',
305 ),
306 'sha1' => null,
307 'sha1base36' => null,
308 'user' => array(
309 ApiBase::PARAM_TYPE => 'user'
310 ),
311 'filterbots' => array(
312 ApiBase::PARAM_DFLT => 'all',
313 ApiBase::PARAM_TYPE => array(
314 'all',
315 'bots',
316 'nobots'
317 )
318 ),
319 'mime' => null,
320 'limit' => array(
321 ApiBase::PARAM_DFLT => 10,
322 ApiBase::PARAM_TYPE => 'limit',
323 ApiBase::PARAM_MIN => 1,
324 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
325 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
326 ),
327 );
328 }
329
330 public function getParamDescription() {
331 $p = $this->getModulePrefix();
332 return array(
333 'sort' => 'Property to sort by',
334 'dir' => 'The direction in which to list',
335 'from' => "The image title to start enumerating from. Can only be used with {$p}sort=name",
336 'to' => "The image title to stop enumerating at. Can only be used with {$p}sort=name",
337 'continue' => 'When more results are available, use this to continue',
338 'start' => "The timestamp to start enumerating from. Can only be used with {$p}sort=timestamp",
339 'end' => "The timestamp to end enumerating. Can only be used with {$p}sort=timestamp",
340 'prop' => ApiQueryImageInfo::getPropertyDescriptions( $this->propertyFilter ),
341 'prefix' => "Search for all image titles that begin with this value. Can only be used with {$p}sort=name",
342 'minsize' => 'Limit to images with at least this many bytes',
343 'maxsize' => 'Limit to images with at most this many bytes',
344 'sha1' => "SHA1 hash of image. Overrides {$p}sha1base36",
345 'sha1base36' => 'SHA1 hash of image in base 36 (used in MediaWiki)',
346 'user' => "Only return files uploaded by this user. Can only be used with {$p}sort=timestamp. Cannot be used together with {$p}filterbots",
347 'filterbots' => "How to filter files uploaded by bots. Can only be used with {$p}sort=timestamp. Cannot be used together with {$p}user",
348 'mime' => 'What MIME type to search for. e.g. image/jpeg. Disabled in Miser Mode',
349 'limit' => 'How many images in total to return',
350 );
351 }
352
353 private $propertyFilter = array( 'archivename', 'thumbmime' );
354
355 public function getResultProperties() {
356 return array_merge(
357 array(
358 '' => array(
359 'name' => 'string',
360 'ns' => 'namespace',
361 'title' => 'string'
362 )
363 ),
364 ApiQueryImageInfo::getResultPropertiesFiltered( $this->propertyFilter )
365 );
366 }
367
368 public function getDescription() {
369 return 'Enumerate all images sequentially';
370 }
371
372 public function getPossibleErrors() {
373 $p = $this->getModulePrefix();
374 return array_merge( parent::getPossibleErrors(), array(
375 array( 'code' => 'params', 'info' => 'Use "gaifilterredir=nonredirects" option instead of "redirects" when using allimages as a generator' ),
376 array( 'code' => 'badparams', 'info' => "Parameter'{$p}start' can only be used with {$p}sort=timestamp" ),
377 array( 'code' => 'badparams', 'info' => "Parameter'{$p}end' can only be used with {$p}sort=timestamp" ),
378 array( 'code' => 'badparams', 'info' => "Parameter'{$p}user' can only be used with {$p}sort=timestamp" ),
379 array( 'code' => 'badparams', 'info' => "Parameter'{$p}filterbots' can only be used with {$p}sort=timestamp" ),
380 array( 'code' => 'badparams', 'info' => "Parameter'{$p}from' can only be used with {$p}sort=name" ),
381 array( 'code' => 'badparams', 'info' => "Parameter'{$p}to' can only be used with {$p}sort=name" ),
382 array( 'code' => 'badparams', 'info' => "Parameter'{$p}prefix' can only be used with {$p}sort=name" ),
383 array( 'code' => 'badparams', 'info' => "Parameters '{$p}user' and '{$p}filterbots' cannot be used together" ),
384 array( 'code' => 'unsupportedrepo', 'info' => 'Local file repository does not support querying all images' ),
385 array( 'code' => 'mimesearchdisabled', 'info' => 'MIME search disabled in Miser Mode' ),
386 array( 'code' => 'invalidsha1hash', 'info' => 'The SHA1 hash provided is not valid' ),
387 array( 'code' => 'invalidsha1base36hash', 'info' => 'The SHA1Base36 hash provided is not valid' ),
388 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
389 ) );
390 }
391
392 public function getExamples() {
393 return array(
394 'api.php?action=query&list=allimages&aifrom=B' => array(
395 'Simple Use',
396 'Show a list of files starting at the letter "B"',
397 ),
398 'api.php?action=query&list=allimages&aiprop=user|timestamp|url&aisort=timestamp&aidir=older' => array(
399 'Simple Use',
400 'Show a list of recently uploaded files similar to Special:NewFiles',
401 ),
402 'api.php?action=query&generator=allimages&gailimit=4&gaifrom=T&prop=imageinfo' => array(
403 'Using as Generator',
404 'Show info about 4 files starting at the letter "T"',
405 ),
406 );
407 }
408
409 public function getHelpUrls() {
410 return 'https://www.mediawiki.org/wiki/API:Allimages';
411 }
412
413 public function getVersion() {
414 return __CLASS__ . ': $Id$';
415 }
416 }