raise timeout for CdbTest::testCdb()
[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 $this->dieContinueUsageIf( count( $cont ) != 1 );
117 $op = ( $ascendingOrder ? '>' : '<' );
118 $continueFrom = $db->addQuotes( $cont[0] );
119 $this->addWhere( "img_name $op= $continueFrom" );
120 }
121
122 // Image filters
123 $from = ( is_null( $params['from'] ) ? null : $this->titlePartToKey( $params['from'] ) );
124 $to = ( is_null( $params['to'] ) ? null : $this->titlePartToKey( $params['to'] ) );
125 $this->addWhereRange( 'img_name', ( $ascendingOrder ? 'newer' : 'older' ), $from, $to );
126
127 if ( isset( $params['prefix'] ) ) {
128 $this->addWhere( 'img_name' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
129 }
130 } else {
131 // Check mutually exclusive params
132 $disallowed = array( 'from', 'to', 'prefix' );
133 foreach ( $disallowed as $pname ) {
134 if ( isset( $params[$pname] ) ) {
135 $this->dieUsage( "Parameter '{$prefix}{$pname}' can only be used with {$prefix}sort=name", 'badparams' );
136 }
137 }
138 if ( !is_null( $params['user'] ) && $params['filterbots'] != 'all' ) {
139 // Since filterbots checks if each user has the bot right, it doesn't make sense to use it with user
140 $this->dieUsage( "Parameters '{$prefix}user' and '{$prefix}filterbots' cannot be used together", 'badparams' );
141 }
142
143 // Pagination
144 $this->addTimestampWhereRange( 'img_timestamp', ( $ascendingOrder ? 'newer' : 'older' ), $params['start'], $params['end'] );
145
146 // Image filters
147 if ( !is_null( $params['user'] ) ) {
148 $this->addWhereFld( 'img_user_text', $params['user'] );
149 }
150 if ( $params['filterbots'] != 'all' ) {
151 $this->addTables( 'user_groups' );
152 $this->addJoinConds( array( 'user_groups' => array(
153 'LEFT JOIN',
154 array(
155 'ug_group' => User::getGroupsWithPermission( 'bot' ),
156 'ug_user = img_user'
157 )
158 ) ) );
159 $groupCond = ( $params['filterbots'] == 'nobots' ? 'NULL': 'NOT NULL' );
160 $this->addWhere( "ug_group IS $groupCond" );
161 }
162 }
163
164 // Filters not depending on sort
165 if ( isset( $params['minsize'] ) ) {
166 $this->addWhere( 'img_size>=' . intval( $params['minsize'] ) );
167 }
168
169 if ( isset( $params['maxsize'] ) ) {
170 $this->addWhere( 'img_size<=' . intval( $params['maxsize'] ) );
171 }
172
173 $sha1 = false;
174 if ( isset( $params['sha1'] ) ) {
175 $sha1 = strtolower( $params['sha1'] );
176 if ( !$this->validateSha1Hash( $sha1 ) ) {
177 $this->dieUsage( 'The SHA1 hash provided is not valid', 'invalidsha1hash' );
178 }
179 $sha1 = wfBaseConvert( $sha1, 16, 36, 31 );
180 } elseif ( isset( $params['sha1base36'] ) ) {
181 $sha1 = strtolower( $params['sha1base36'] );
182 if ( !$this->validateSha1Base36Hash( $sha1 ) ) {
183 $this->dieUsage( 'The SHA1Base36 hash provided is not valid', 'invalidsha1base36hash' );
184 }
185 }
186 if ( $sha1 ) {
187 $this->addWhereFld( 'img_sha1', $sha1 );
188 }
189
190 if ( !is_null( $params['mime'] ) ) {
191 global $wgMiserMode;
192 if ( $wgMiserMode ) {
193 $this->dieUsage( 'MIME search disabled in Miser Mode', 'mimesearchdisabled' );
194 }
195
196 list( $major, $minor ) = File::splitMime( $params['mime'] );
197
198 $this->addWhereFld( 'img_major_mime', $major );
199 $this->addWhereFld( 'img_minor_mime', $minor );
200 }
201
202 $limit = $params['limit'];
203 $this->addOption( 'LIMIT', $limit + 1 );
204 $sortFlag = '';
205 if ( !$ascendingOrder ) {
206 $sortFlag = ' DESC';
207 }
208 if ( $params['sort'] == 'timestamp' ) {
209 $this->addOption( 'ORDER BY', 'img_timestamp' . $sortFlag );
210 if ( !is_null( $params['user'] ) ) {
211 $this->addOption( 'USE INDEX', array( 'image' => 'img_usertext_timestamp' ) );
212 } else {
213 $this->addOption( 'USE INDEX', array( 'image' => 'img_timestamp' ) );
214 }
215 } else {
216 $this->addOption( 'ORDER BY', 'img_name' . $sortFlag );
217 }
218
219 $res = $this->select( __METHOD__ );
220
221 $titles = array();
222 $count = 0;
223 $result = $this->getResult();
224 foreach ( $res as $row ) {
225 if ( ++ $count > $limit ) {
226 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
227 if ( $params['sort'] == 'name' ) {
228 $this->setContinueEnumParameter( 'continue', $row->img_name );
229 } else {
230 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->img_timestamp ) );
231 }
232 break;
233 }
234
235 if ( is_null( $resultPageSet ) ) {
236 $file = $repo->newFileFromRow( $row );
237 $info = array_merge( array( 'name' => $row->img_name ),
238 ApiQueryImageInfo::getInfo( $file, $prop, $result ) );
239 self::addTitleInfo( $info, $file->getTitle() );
240
241 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $info );
242 if ( !$fit ) {
243 if ( $params['sort'] == 'name' ) {
244 $this->setContinueEnumParameter( 'continue', $row->img_name );
245 } else {
246 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->img_timestamp ) );
247 }
248 break;
249 }
250 } else {
251 $titles[] = Title::makeTitle( NS_FILE, $row->img_name );
252 }
253 }
254
255 if ( is_null( $resultPageSet ) ) {
256 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'img' );
257 } else {
258 $resultPageSet->populateFromTitles( $titles );
259 }
260 }
261
262 public function getAllowedParams() {
263 return array (
264 'sort' => array(
265 ApiBase::PARAM_DFLT => 'name',
266 ApiBase::PARAM_TYPE => array(
267 'name',
268 'timestamp'
269 )
270 ),
271 'dir' => array(
272 ApiBase::PARAM_DFLT => 'ascending',
273 ApiBase::PARAM_TYPE => array(
274 // sort=name
275 'ascending',
276 'descending',
277 // sort=timestamp
278 'newer',
279 'older'
280 )
281 ),
282 'from' => null,
283 'to' => null,
284 'continue' => null,
285 'start' => array(
286 ApiBase::PARAM_TYPE => 'timestamp'
287 ),
288 'end' => array(
289 ApiBase::PARAM_TYPE => 'timestamp'
290 ),
291 'prop' => array(
292 ApiBase::PARAM_TYPE => ApiQueryImageInfo::getPropertyNames( $this->propertyFilter ),
293 ApiBase::PARAM_DFLT => 'timestamp|url',
294 ApiBase::PARAM_ISMULTI => true
295 ),
296 'prefix' => null,
297 'minsize' => array(
298 ApiBase::PARAM_TYPE => 'integer',
299 ),
300 'maxsize' => array(
301 ApiBase::PARAM_TYPE => 'integer',
302 ),
303 'sha1' => null,
304 'sha1base36' => null,
305 'user' => array(
306 ApiBase::PARAM_TYPE => 'user'
307 ),
308 'filterbots' => array(
309 ApiBase::PARAM_DFLT => 'all',
310 ApiBase::PARAM_TYPE => array(
311 'all',
312 'bots',
313 'nobots'
314 )
315 ),
316 'mime' => null,
317 'limit' => array(
318 ApiBase::PARAM_DFLT => 10,
319 ApiBase::PARAM_TYPE => 'limit',
320 ApiBase::PARAM_MIN => 1,
321 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
322 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
323 ),
324 );
325 }
326
327 public function getParamDescription() {
328 $p = $this->getModulePrefix();
329 return array(
330 'sort' => 'Property to sort by',
331 'dir' => 'The direction in which to list',
332 'from' => "The image title to start enumerating from. Can only be used with {$p}sort=name",
333 'to' => "The image title to stop enumerating at. Can only be used with {$p}sort=name",
334 'continue' => 'When more results are available, use this to continue',
335 'start' => "The timestamp to start enumerating from. Can only be used with {$p}sort=timestamp",
336 'end' => "The timestamp to end enumerating. Can only be used with {$p}sort=timestamp",
337 'prop' => ApiQueryImageInfo::getPropertyDescriptions( $this->propertyFilter ),
338 'prefix' => "Search for all image titles that begin with this value. Can only be used with {$p}sort=name",
339 'minsize' => 'Limit to images with at least this many bytes',
340 'maxsize' => 'Limit to images with at most this many bytes',
341 'sha1' => "SHA1 hash of image. Overrides {$p}sha1base36",
342 'sha1base36' => 'SHA1 hash of image in base 36 (used in MediaWiki)',
343 'user' => "Only return files uploaded by this user. Can only be used with {$p}sort=timestamp. Cannot be used together with {$p}filterbots",
344 'filterbots' => "How to filter files uploaded by bots. Can only be used with {$p}sort=timestamp. Cannot be used together with {$p}user",
345 'mime' => 'What MIME type to search for. e.g. image/jpeg. Disabled in Miser Mode',
346 'limit' => 'How many images in total to return',
347 );
348 }
349
350 private $propertyFilter = array( 'archivename', 'thumbmime' );
351
352 public function getResultProperties() {
353 return array_merge(
354 array(
355 '' => array(
356 'name' => 'string',
357 'ns' => 'namespace',
358 'title' => 'string'
359 )
360 ),
361 ApiQueryImageInfo::getResultPropertiesFiltered( $this->propertyFilter )
362 );
363 }
364
365 public function getDescription() {
366 return 'Enumerate all images sequentially';
367 }
368
369 public function getPossibleErrors() {
370 $p = $this->getModulePrefix();
371 return array_merge( parent::getPossibleErrors(), array(
372 array( 'code' => 'params', 'info' => 'Use "gaifilterredir=nonredirects" option instead of "redirects" when using allimages as a generator' ),
373 array( 'code' => 'badparams', 'info' => "Parameter'{$p}start' can only be used with {$p}sort=timestamp" ),
374 array( 'code' => 'badparams', 'info' => "Parameter'{$p}end' can only be used with {$p}sort=timestamp" ),
375 array( 'code' => 'badparams', 'info' => "Parameter'{$p}user' can only be used with {$p}sort=timestamp" ),
376 array( 'code' => 'badparams', 'info' => "Parameter'{$p}filterbots' can only be used with {$p}sort=timestamp" ),
377 array( 'code' => 'badparams', 'info' => "Parameter'{$p}from' can only be used with {$p}sort=name" ),
378 array( 'code' => 'badparams', 'info' => "Parameter'{$p}to' can only be used with {$p}sort=name" ),
379 array( 'code' => 'badparams', 'info' => "Parameter'{$p}prefix' can only be used with {$p}sort=name" ),
380 array( 'code' => 'badparams', 'info' => "Parameters '{$p}user' and '{$p}filterbots' cannot be used together" ),
381 array( 'code' => 'unsupportedrepo', 'info' => 'Local file repository does not support querying all images' ),
382 array( 'code' => 'mimesearchdisabled', 'info' => 'MIME search disabled in Miser Mode' ),
383 array( 'code' => 'invalidsha1hash', 'info' => 'The SHA1 hash provided is not valid' ),
384 array( 'code' => 'invalidsha1base36hash', 'info' => 'The SHA1Base36 hash provided is not valid' ),
385 ) );
386 }
387
388 public function getExamples() {
389 return array(
390 'api.php?action=query&list=allimages&aifrom=B' => array(
391 'Simple Use',
392 'Show a list of files starting at the letter "B"',
393 ),
394 'api.php?action=query&list=allimages&aiprop=user|timestamp|url&aisort=timestamp&aidir=older' => array(
395 'Simple Use',
396 'Show a list of recently uploaded files similar to Special:NewFiles',
397 ),
398 'api.php?action=query&generator=allimages&gailimit=4&gaifrom=T&prop=imageinfo' => array(
399 'Using as Generator',
400 'Show info about 4 files starting at the letter "T"',
401 ),
402 );
403 }
404
405 public function getHelpUrls() {
406 return 'https://www.mediawiki.org/wiki/API:Allimages';
407 }
408 }