Merge "Split mocks/media/MockBitmaphandler file"
[lhc/web/wiklou.git] / includes / api / ApiQueryImageInfo.php
1 <?php
2 /**
3 *
4 *
5 * Created on July 6, 2007
6 *
7 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @file
25 */
26
27 /**
28 * A query action to get image information and upload history.
29 *
30 * @ingroup API
31 */
32 class ApiQueryImageInfo extends ApiQueryBase {
33 const TRANSFORM_LIMIT = 50;
34 private static $transformCount = 0;
35
36 public function __construct( $query, $moduleName, $prefix = 'ii' ) {
37 // We allow a subclass to override the prefix, to create a related API
38 // module. Some other parts of MediaWiki construct this with a null
39 // $prefix, which used to be ignored when this only took two arguments
40 if ( is_null( $prefix ) ) {
41 $prefix = 'ii';
42 }
43 parent::__construct( $query, $moduleName, $prefix );
44 }
45
46 public function execute() {
47 $params = $this->extractRequestParams();
48
49 $prop = array_flip( $params['prop'] );
50
51 $scale = $this->getScale( $params );
52
53 $metadataOpts = array(
54 'version' => $params['metadataversion'],
55 'language' => $params['extmetadatalanguage'],
56 'multilang' => $params['extmetadatamultilang'],
57 'extmetadatafilter' => $params['extmetadatafilter'],
58 );
59
60 $pageIds = $this->getPageSet()->getAllTitlesByNamespace();
61 if ( !empty( $pageIds[NS_FILE] ) ) {
62 $titles = array_keys( $pageIds[NS_FILE] );
63 asort( $titles ); // Ensure the order is always the same
64
65 $fromTitle = null;
66 if ( !is_null( $params['continue'] ) ) {
67 $cont = explode( '|', $params['continue'] );
68 $this->dieContinueUsageIf( count( $cont ) != 2 );
69 $fromTitle = strval( $cont[0] );
70 $fromTimestamp = $cont[1];
71 // Filter out any titles before $fromTitle
72 foreach ( $titles as $key => $title ) {
73 if ( $title < $fromTitle ) {
74 unset( $titles[$key] );
75 } else {
76 break;
77 }
78 }
79 }
80
81 $result = $this->getResult();
82 //search only inside the local repo
83 if ( $params['localonly'] ) {
84 $images = RepoGroup::singleton()->getLocalRepo()->findFiles( $titles );
85 } else {
86 $images = RepoGroup::singleton()->findFiles( $titles );
87 }
88 foreach ( $titles as $title ) {
89 $pageId = $pageIds[NS_FILE][$title];
90 $start = $title === $fromTitle ? $fromTimestamp : $params['start'];
91
92 if ( !isset( $images[$title] ) ) {
93 if ( isset( $prop['uploadwarning'] ) ) {
94 // Uploadwarning needs info about non-existing files
95 $images[$title] = wfLocalFile( $title );
96 } else {
97 $result->addValue(
98 array( 'query', 'pages', intval( $pageId ) ),
99 'imagerepository', ''
100 );
101 // The above can't fail because it doesn't increase the result size
102 continue;
103 }
104 }
105
106 /** @var $img File */
107 $img = $images[$title];
108
109 if ( self::getTransformCount() >= self::TRANSFORM_LIMIT ) {
110 if ( count( $pageIds[NS_FILE] ) == 1 ) {
111 // See the 'the user is screwed' comment below
112 $this->setContinueEnumParameter( 'start',
113 $start !== null ? $start : wfTimestamp( TS_ISO_8601, $img->getTimestamp() )
114 );
115 } else {
116 $this->setContinueEnumParameter( 'continue',
117 $this->getContinueStr( $img, $start ) );
118 }
119 break;
120 }
121
122 $fit = $result->addValue(
123 array( 'query', 'pages', intval( $pageId ) ),
124 'imagerepository', $img->getRepoName()
125 );
126 if ( !$fit ) {
127 if ( count( $pageIds[NS_FILE] ) == 1 ) {
128 // The user is screwed. imageinfo can't be solely
129 // responsible for exceeding the limit in this case,
130 // so set a query-continue that just returns the same
131 // thing again. When the violating queries have been
132 // out-continued, the result will get through
133 $this->setContinueEnumParameter( 'start',
134 $start !== null ? $start : wfTimestamp( TS_ISO_8601, $img->getTimestamp() )
135 );
136 } else {
137 $this->setContinueEnumParameter( 'continue',
138 $this->getContinueStr( $img, $start ) );
139 }
140 break;
141 }
142
143 // Check if we can make the requested thumbnail, and get transform parameters.
144 $finalThumbParams = $this->mergeThumbParams( $img, $scale, $params['urlparam'] );
145
146 // Get information about the current version first
147 // Check that the current version is within the start-end boundaries
148 $gotOne = false;
149 if (
150 ( is_null( $start ) || $img->getTimestamp() <= $start ) &&
151 ( is_null( $params['end'] ) || $img->getTimestamp() >= $params['end'] )
152 ) {
153 $gotOne = true;
154
155 $fit = $this->addPageSubItem( $pageId,
156 self::getInfo( $img, $prop, $result,
157 $finalThumbParams, $metadataOpts
158 )
159 );
160 if ( !$fit ) {
161 if ( count( $pageIds[NS_FILE] ) == 1 ) {
162 // See the 'the user is screwed' comment above
163 $this->setContinueEnumParameter( 'start',
164 wfTimestamp( TS_ISO_8601, $img->getTimestamp() ) );
165 } else {
166 $this->setContinueEnumParameter( 'continue',
167 $this->getContinueStr( $img ) );
168 }
169 break;
170 }
171 }
172
173 // Now get the old revisions
174 // Get one more to facilitate query-continue functionality
175 $count = ( $gotOne ? 1 : 0 );
176 $oldies = $img->getHistory( $params['limit'] - $count + 1, $start, $params['end'] );
177 /** @var $oldie File */
178 foreach ( $oldies as $oldie ) {
179 if ( ++$count > $params['limit'] ) {
180 // We've reached the extra one which shows that there are
181 // additional pages to be had. Stop here...
182 // Only set a query-continue if there was only one title
183 if ( count( $pageIds[NS_FILE] ) == 1 ) {
184 $this->setContinueEnumParameter( 'start',
185 wfTimestamp( TS_ISO_8601, $oldie->getTimestamp() ) );
186 }
187 break;
188 }
189 $fit = self::getTransformCount() < self::TRANSFORM_LIMIT &&
190 $this->addPageSubItem( $pageId,
191 self::getInfo( $oldie, $prop, $result,
192 $finalThumbParams, $metadataOpts
193 )
194 );
195 if ( !$fit ) {
196 if ( count( $pageIds[NS_FILE] ) == 1 ) {
197 $this->setContinueEnumParameter( 'start',
198 wfTimestamp( TS_ISO_8601, $oldie->getTimestamp() ) );
199 } else {
200 $this->setContinueEnumParameter( 'continue',
201 $this->getContinueStr( $oldie ) );
202 }
203 break;
204 }
205 }
206 if ( !$fit ) {
207 break;
208 }
209 }
210 }
211 }
212
213 /**
214 * From parameters, construct a 'scale' array
215 * @param array $params Parameters passed to api.
216 * @return Array or Null: key-val array of 'width' and 'height', or null
217 */
218 public function getScale( $params ) {
219 $p = $this->getModulePrefix();
220
221 if ( $params['urlwidth'] != -1 ) {
222 $scale = array();
223 $scale['width'] = $params['urlwidth'];
224 $scale['height'] = $params['urlheight'];
225 } elseif ( $params['urlheight'] != -1 ) {
226 // Height is specified but width isn't
227 // Don't set $scale['width']; this signals mergeThumbParams() to fill it with the image's width
228 $scale = array();
229 $scale['height'] = $params['urlheight'];
230 } else {
231 $scale = null;
232 if ( $params['urlparam'] ) {
233 $this->dieUsage( "{$p}urlparam requires {$p}urlwidth", "urlparam_no_width" );
234 }
235 }
236
237 return $scale;
238 }
239
240 /** Validate and merge scale parameters with handler thumb parameters, give error if invalid.
241 *
242 * We do this later than getScale, since we need the image
243 * to know which handler, since handlers can make their own parameters.
244 * @param File $image Image that params are for.
245 * @param array $thumbParams thumbnail parameters from getScale
246 * @param string $otherParams of otherParams (iiurlparam).
247 * @return Array of parameters for transform.
248 */
249 protected function mergeThumbParams( $image, $thumbParams, $otherParams ) {
250 global $wgThumbLimits;
251
252 if ( !isset( $thumbParams['width'] ) && isset( $thumbParams['height'] ) ) {
253 // We want to limit only by height in this situation, so pass the
254 // image's full width as the limiting width. But some file types
255 // don't have a width of their own, so pick something arbitrary so
256 // thumbnailing the default icon works.
257 if ( $image->getWidth() <= 0 ) {
258 $thumbParams['width'] = max( $wgThumbLimits );
259 } else {
260 $thumbParams['width'] = $image->getWidth();
261 }
262 }
263
264 if ( !$otherParams ) {
265 return $thumbParams;
266 }
267 $p = $this->getModulePrefix();
268
269 $h = $image->getHandler();
270 if ( !$h ) {
271 $this->setWarning( 'Could not create thumbnail because ' .
272 $image->getName() . ' does not have an associated image handler' );
273
274 return $thumbParams;
275 }
276
277 $paramList = $h->parseParamString( $otherParams );
278 if ( !$paramList ) {
279 // Just set a warning (instead of dieUsage), as in many cases
280 // we could still render the image using width and height parameters,
281 // and this type of thing could happen between different versions of
282 // handlers.
283 $this->setWarning( "Could not parse {$p}urlparam for " . $image->getName()
284 . '. Using only width and height' );
285
286 return $thumbParams;
287 }
288
289 if ( isset( $paramList['width'] ) ) {
290 if ( intval( $paramList['width'] ) != intval( $thumbParams['width'] ) ) {
291 $this->setWarning( "Ignoring width value set in {$p}urlparam ({$paramList['width']}) "
292 . "in favor of width value derived from {$p}urlwidth/{$p}urlheight "
293 . "({$thumbParams['width']})" );
294 }
295 }
296
297 foreach ( $paramList as $name => $value ) {
298 if ( !$h->validateParam( $name, $value ) ) {
299 $this->dieUsage( "Invalid value for {$p}urlparam ($name=$value)", "urlparam" );
300 }
301 }
302
303 return $thumbParams + $paramList;
304 }
305
306 /**
307 * Get result information for an image revision
308 *
309 * @param $file File object
310 * @param array $prop of properties to get (in the keys)
311 * @param $result ApiResult object
312 * @param array $thumbParams containing 'width' and 'height' items, or null
313 * @param string|array $metadataOpts Options for metadata fetching.
314 * This is an array consisting of the keys:
315 * 'version': The metadata version for the metadata option
316 * 'language': The language for extmetadata property
317 * 'multilang': Return all translations in extmetadata property
318 * @return Array: result array
319 */
320 static function getInfo( $file, $prop, $result, $thumbParams = null, $metadataOpts = false ) {
321 global $wgContLang;
322
323 if ( !$metadataOpts || is_string( $metadataOpts ) ) {
324 $metadataOpts = array(
325 'version' => $metadataOpts ?: 'latest',
326 'language' => $wgContLang,
327 'multilang' => false,
328 'extmetadatafilter' => array(),
329 );
330 }
331 $version = $metadataOpts['version'];
332 $vals = array();
333 // Timestamp is shown even if the file is revdelete'd in interface
334 // so do same here.
335 if ( isset( $prop['timestamp'] ) ) {
336 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $file->getTimestamp() );
337 }
338
339 $user = isset( $prop['user'] );
340 $userid = isset( $prop['userid'] );
341
342 if ( $user || $userid ) {
343 if ( $file->isDeleted( File::DELETED_USER ) ) {
344 $vals['userhidden'] = '';
345 } else {
346 if ( $user ) {
347 $vals['user'] = $file->getUser();
348 }
349 if ( $userid ) {
350 $vals['userid'] = $file->getUser( 'id' );
351 }
352 if ( !$file->getUser( 'id' ) ) {
353 $vals['anon'] = '';
354 }
355 }
356 }
357
358 // This is shown even if the file is revdelete'd in interface
359 // so do same here.
360 if ( isset( $prop['size'] ) || isset( $prop['dimensions'] ) ) {
361 $vals['size'] = intval( $file->getSize() );
362 $vals['width'] = intval( $file->getWidth() );
363 $vals['height'] = intval( $file->getHeight() );
364
365 $pageCount = $file->pageCount();
366 if ( $pageCount !== false ) {
367 $vals['pagecount'] = $pageCount;
368 }
369 }
370
371 $pcomment = isset( $prop['parsedcomment'] );
372 $comment = isset( $prop['comment'] );
373
374 if ( $pcomment || $comment ) {
375 if ( $file->isDeleted( File::DELETED_COMMENT ) ) {
376 $vals['commenthidden'] = '';
377 } else {
378 if ( $pcomment ) {
379 $vals['parsedcomment'] = Linker::formatComment(
380 $file->getDescription(), $file->getTitle() );
381 }
382 if ( $comment ) {
383 $vals['comment'] = $file->getDescription();
384 }
385 }
386 }
387
388 $url = isset( $prop['url'] );
389 $sha1 = isset( $prop['sha1'] );
390 $meta = isset( $prop['metadata'] );
391 $extmetadata = isset( $prop['extmetadata'] );
392 $mime = isset( $prop['mime'] );
393 $mediatype = isset( $prop['mediatype'] );
394 $archive = isset( $prop['archivename'] );
395 $bitdepth = isset( $prop['bitdepth'] );
396 $uploadwarning = isset( $prop['uploadwarning'] );
397
398 if ( ( $url || $sha1 || $meta || $mime || $mediatype || $archive || $bitdepth )
399 && $file->isDeleted( File::DELETED_FILE )
400 ) {
401 $vals['filehidden'] = '';
402
403 //Early return, tidier than indenting all following things one level
404 return $vals;
405 }
406
407 if ( $url ) {
408 if ( !is_null( $thumbParams ) ) {
409 $mto = $file->transform( $thumbParams );
410 self::$transformCount++;
411 if ( $mto && !$mto->isError() ) {
412 $vals['thumburl'] = wfExpandUrl( $mto->getUrl(), PROTO_CURRENT );
413
414 // bug 23834 - If the URL's are the same, we haven't resized it, so shouldn't give the wanted
415 // thumbnail sizes for the thumbnail actual size
416 if ( $mto->getUrl() !== $file->getUrl() ) {
417 $vals['thumbwidth'] = intval( $mto->getWidth() );
418 $vals['thumbheight'] = intval( $mto->getHeight() );
419 } else {
420 $vals['thumbwidth'] = intval( $file->getWidth() );
421 $vals['thumbheight'] = intval( $file->getHeight() );
422 }
423
424 if ( isset( $prop['thumbmime'] ) && $file->getHandler() ) {
425 list( , $mime ) = $file->getHandler()->getThumbType(
426 $mto->getExtension(), $file->getMimeType(), $thumbParams );
427 $vals['thumbmime'] = $mime;
428 }
429 } elseif ( $mto && $mto->isError() ) {
430 $vals['thumberror'] = $mto->toText();
431 }
432 }
433 $vals['url'] = wfExpandUrl( $file->getFullURL(), PROTO_CURRENT );
434 $vals['descriptionurl'] = wfExpandUrl( $file->getDescriptionUrl(), PROTO_CURRENT );
435 }
436
437 if ( $sha1 ) {
438 $vals['sha1'] = wfBaseConvert( $file->getSha1(), 36, 16, 40 );
439 }
440
441 if ( $meta ) {
442 wfSuppressWarnings();
443 $metadata = unserialize( $file->getMetadata() );
444 wfRestoreWarnings();
445 if ( $metadata && $version !== 'latest' ) {
446 $metadata = $file->convertMetadataVersion( $metadata, $version );
447 }
448 $vals['metadata'] = $metadata ? self::processMetaData( $metadata, $result ) : null;
449 }
450
451 if ( $extmetadata ) {
452 // Note, this should return an array where all the keys
453 // start with a letter, and all the values are strings.
454 // Thus there should be no issue with format=xml.
455 $format = new FormatMetadata;
456 $format->setSingleLanguage( !$metadataOpts['multilang'] );
457 $format->getContext()->setLanguage( $metadataOpts['language'] );
458 $extmetaArray = $format->fetchExtendedMetadata( $file );
459 if ( $metadataOpts['extmetadatafilter'] ) {
460 $extmetaArray = array_intersect_key(
461 $extmetaArray, array_flip( $metadataOpts['extmetadatafilter'] )
462 );
463 }
464 $vals['extmetadata'] = $extmetaArray;
465 }
466
467 if ( $mime ) {
468 $vals['mime'] = $file->getMimeType();
469 }
470
471 if ( $mediatype ) {
472 $vals['mediatype'] = $file->getMediaType();
473 }
474
475 if ( $archive && $file->isOld() ) {
476 $vals['archivename'] = $file->getArchiveName();
477 }
478
479 if ( $bitdepth ) {
480 $vals['bitdepth'] = $file->getBitDepth();
481 }
482
483 if ( $uploadwarning ) {
484 $vals['html'] = SpecialUpload::getExistsWarning( UploadBase::getExistsWarning( $file ) );
485 }
486
487 return $vals;
488 }
489
490 /**
491 * Get the count of image transformations performed
492 *
493 * If this is >= TRANSFORM_LIMIT, you should probably stop processing images.
494 *
495 * @return integer count
496 */
497 static function getTransformCount() {
498 return self::$transformCount;
499 }
500
501 /**
502 *
503 * @param $metadata Array
504 * @param $result ApiResult
505 * @return Array
506 */
507 public static function processMetaData( $metadata, $result ) {
508 $retval = array();
509 if ( is_array( $metadata ) ) {
510 foreach ( $metadata as $key => $value ) {
511 $r = array( 'name' => $key );
512 if ( is_array( $value ) ) {
513 $r['value'] = self::processMetaData( $value, $result );
514 } else {
515 $r['value'] = $value;
516 }
517 $retval[] = $r;
518 }
519 }
520 $result->setIndexedTagName( $retval, 'metadata' );
521
522 return $retval;
523 }
524
525 public function getCacheMode( $params ) {
526 return 'public';
527 }
528
529 /**
530 * @param $img File
531 * @param null|string $start
532 * @return string
533 */
534 protected function getContinueStr( $img, $start = null ) {
535 if ( $start === null ) {
536 $start = $img->getTimestamp();
537 }
538
539 return $img->getOriginalTitle()->getDBkey() . '|' . $start;
540 }
541
542 public function getAllowedParams() {
543 global $wgContLang;
544
545 return array(
546 'prop' => array(
547 ApiBase::PARAM_ISMULTI => true,
548 ApiBase::PARAM_DFLT => 'timestamp|user',
549 ApiBase::PARAM_TYPE => self::getPropertyNames()
550 ),
551 'limit' => array(
552 ApiBase::PARAM_TYPE => 'limit',
553 ApiBase::PARAM_DFLT => 1,
554 ApiBase::PARAM_MIN => 1,
555 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
556 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
557 ),
558 'start' => array(
559 ApiBase::PARAM_TYPE => 'timestamp'
560 ),
561 'end' => array(
562 ApiBase::PARAM_TYPE => 'timestamp'
563 ),
564 'urlwidth' => array(
565 ApiBase::PARAM_TYPE => 'integer',
566 ApiBase::PARAM_DFLT => -1
567 ),
568 'urlheight' => array(
569 ApiBase::PARAM_TYPE => 'integer',
570 ApiBase::PARAM_DFLT => -1
571 ),
572 'metadataversion' => array(
573 ApiBase::PARAM_TYPE => 'string',
574 ApiBase::PARAM_DFLT => '1',
575 ),
576 'extmetadatalanguage' => array(
577 ApiBase::PARAM_TYPE => 'string',
578 ApiBase::PARAM_DFLT => $wgContLang->getCode(),
579 ),
580 'extmetadatamultilang' => array(
581 ApiBase::PARAM_TYPE => 'boolean',
582 ApiBase::PARAM_DFLT => false,
583 ),
584 'extmetadatafilter' => array(
585 ApiBase::PARAM_TYPE => 'string',
586 ApiBase::PARAM_ISMULTI => true,
587 ),
588 'urlparam' => array(
589 ApiBase::PARAM_DFLT => '',
590 ApiBase::PARAM_TYPE => 'string',
591 ),
592 'continue' => null,
593 'localonly' => false,
594 );
595 }
596
597 /**
598 * Returns all possible parameters to iiprop
599 *
600 * @param array $filter List of properties to filter out
601 *
602 * @return Array
603 */
604 public static function getPropertyNames( $filter = array() ) {
605 return array_diff( array_keys( self::getProperties() ), $filter );
606 }
607
608 /**
609 * Returns array key value pairs of properties and their descriptions
610 *
611 * @param string $modulePrefix
612 * @return array
613 */
614 private static function getProperties( $modulePrefix = '' ) {
615 return array(
616 'timestamp' => ' timestamp - Adds timestamp for the uploaded version',
617 'user' => ' user - Adds the user who uploaded the image version',
618 'userid' => ' userid - Add the user ID that uploaded the image version',
619 'comment' => ' comment - Comment on the version',
620 'parsedcomment' => ' parsedcomment - Parse the comment on the version',
621 'url' => ' url - Gives URL to the image and the description page',
622 'size' => ' size - Adds the size of the image in bytes ' .
623 'and the height, width and page count (if applicable)',
624 'dimensions' => ' dimensions - Alias for size', // B/C with Allimages
625 'sha1' => ' sha1 - Adds SHA-1 hash for the image',
626 'mime' => ' mime - Adds MIME type of the image',
627 'thumbmime' => ' thumbmime - Adds MIME type of the image thumbnail' .
628 ' (requires url and param ' . $modulePrefix . 'urlwidth)',
629 'mediatype' => ' mediatype - Adds the media type of the image',
630 'metadata' => ' metadata - Lists Exif metadata for the version of the image',
631 'extmetadata' => ' extmetadata - Lists formatted metadata combined ' .
632 'from multiple sources. Results are HTML formatted.',
633 'archivename' => ' archivename - Adds the file name of the archive ' .
634 'version for non-latest versions',
635 'bitdepth' => ' bitdepth - Adds the bit depth of the version',
636 'uploadwarning' => ' uploadwarning - Used by the Special:Upload page to ' .
637 'get information about an existing file. Not intended for use outside MediaWiki core',
638 );
639 }
640
641 /**
642 * Returns the descriptions for the properties provided by getPropertyNames()
643 *
644 * @param array $filter List of properties to filter out
645 * @param string $modulePrefix
646 * @return array
647 */
648 public static function getPropertyDescriptions( $filter = array(), $modulePrefix = '' ) {
649 return array_merge(
650 array( 'What image information to get:' ),
651 array_values( array_diff_key( self::getProperties( $modulePrefix ), array_flip( $filter ) ) )
652 );
653 }
654
655 /**
656 * Return the API documentation for the parameters.
657 * @return Array parameter documentation.
658 */
659 public function getParamDescription() {
660 $p = $this->getModulePrefix();
661
662 return array(
663 'prop' => self::getPropertyDescriptions( array(), $p ),
664 'urlwidth' => array(
665 "If {$p}prop=url is set, a URL to an image scaled to this width will be returned.",
666 'For performance reasons if this option is used, ' .
667 'no more than ' . self::TRANSFORM_LIMIT . ' scaled images will be returned.'
668 ),
669 'urlheight' => "Similar to {$p}urlwidth.",
670 'urlparam' => array( "A handler specific parameter string. For example, pdf's ",
671 "might use 'page15-100px'. {$p}urlwidth must be used and be consistent with {$p}urlparam" ),
672 'limit' => 'How many image revisions to return per image',
673 'start' => 'Timestamp to start listing from',
674 'end' => 'Timestamp to stop listing at',
675 'metadataversion'
676 => array( "Version of metadata to use. if 'latest' is specified, use latest version.",
677 "Defaults to '1' for backwards compatibility" ),
678 'extmetadatalanguage' => array(
679 'What language to fetch extmetadata in. This affects both which',
680 'translation to fetch, if multiple are available, as well as how things',
681 'like numbers and various values are formatted.'
682 ),
683 'extmetadatamultilang'
684 =>'If translations for extmetadata property are available, fetch all of them.',
685 'extmetadatafilter'
686 => "If specified and non-empty, only these keys will be returned for {$p}prop=extmetadata",
687 'continue' => 'If the query response includes a continue value, use it here to get another page of results',
688 'localonly' => 'Look only for files in the local repository',
689 );
690 }
691
692 public static function getResultPropertiesFiltered( $filter = array() ) {
693 $props = array(
694 'timestamp' => array(
695 'timestamp' => 'timestamp'
696 ),
697 'user' => array(
698 'userhidden' => 'boolean',
699 'user' => 'string',
700 'anon' => 'boolean'
701 ),
702 'userid' => array(
703 'userhidden' => 'boolean',
704 'userid' => 'integer',
705 'anon' => 'boolean'
706 ),
707 'size' => array(
708 'size' => 'integer',
709 'width' => 'integer',
710 'height' => 'integer',
711 'pagecount' => array(
712 ApiBase::PROP_TYPE => 'integer',
713 ApiBase::PROP_NULLABLE => true
714 )
715 ),
716 'dimensions' => array(
717 'size' => 'integer',
718 'width' => 'integer',
719 'height' => 'integer',
720 'pagecount' => array(
721 ApiBase::PROP_TYPE => 'integer',
722 ApiBase::PROP_NULLABLE => true
723 )
724 ),
725 'comment' => array(
726 'commenthidden' => 'boolean',
727 'comment' => array(
728 ApiBase::PROP_TYPE => 'string',
729 ApiBase::PROP_NULLABLE => true
730 )
731 ),
732 'parsedcomment' => array(
733 'commenthidden' => 'boolean',
734 'parsedcomment' => array(
735 ApiBase::PROP_TYPE => 'string',
736 ApiBase::PROP_NULLABLE => true
737 )
738 ),
739 'url' => array(
740 'filehidden' => 'boolean',
741 'thumburl' => array(
742 ApiBase::PROP_TYPE => 'string',
743 ApiBase::PROP_NULLABLE => true
744 ),
745 'thumbwidth' => array(
746 ApiBase::PROP_TYPE => 'integer',
747 ApiBase::PROP_NULLABLE => true
748 ),
749 'thumbheight' => array(
750 ApiBase::PROP_TYPE => 'integer',
751 ApiBase::PROP_NULLABLE => true
752 ),
753 'thumberror' => array(
754 ApiBase::PROP_TYPE => 'string',
755 ApiBase::PROP_NULLABLE => true
756 ),
757 'url' => array(
758 ApiBase::PROP_TYPE => 'string',
759 ApiBase::PROP_NULLABLE => true
760 ),
761 'descriptionurl' => array(
762 ApiBase::PROP_TYPE => 'string',
763 ApiBase::PROP_NULLABLE => true
764 )
765 ),
766 'sha1' => array(
767 'filehidden' => 'boolean',
768 'sha1' => array(
769 ApiBase::PROP_TYPE => 'string',
770 ApiBase::PROP_NULLABLE => true
771 )
772 ),
773 'mime' => array(
774 'filehidden' => 'boolean',
775 'mime' => array(
776 ApiBase::PROP_TYPE => 'string',
777 ApiBase::PROP_NULLABLE => true
778 )
779 ),
780 'thumbmime' => array(
781 'filehidden' => 'boolean',
782 'thumbmime' => array(
783 ApiBase::PROP_TYPE => 'string',
784 ApiBase::PROP_NULLABLE => true
785 )
786 ),
787 'mediatype' => array(
788 'filehidden' => 'boolean',
789 'mediatype' => array(
790 ApiBase::PROP_TYPE => 'string',
791 ApiBase::PROP_NULLABLE => true
792 )
793 ),
794 'archivename' => array(
795 'filehidden' => 'boolean',
796 'archivename' => array(
797 ApiBase::PROP_TYPE => 'string',
798 ApiBase::PROP_NULLABLE => true
799 )
800 ),
801 'bitdepth' => array(
802 'filehidden' => 'boolean',
803 'bitdepth' => array(
804 ApiBase::PROP_TYPE => 'integer',
805 ApiBase::PROP_NULLABLE => true
806 )
807 ),
808 );
809
810 return array_diff_key( $props, array_flip( $filter ) );
811 }
812
813 public function getResultProperties() {
814 return self::getResultPropertiesFiltered();
815 }
816
817 public function getDescription() {
818 return 'Returns image information and upload history';
819 }
820
821 public function getPossibleErrors() {
822 $p = $this->getModulePrefix();
823
824 return array_merge( parent::getPossibleErrors(), array(
825 array( 'code' => "{$p}urlwidth", 'info' => "{$p}urlheight cannot be used without {$p}urlwidth" ),
826 array( 'code' => 'urlparam', 'info' => "Invalid value for {$p}urlparam" ),
827 array( 'code' => 'urlparam_no_width', 'info' => "{$p}urlparam requires {$p}urlwidth" ),
828 ) );
829 }
830
831 public function getExamples() {
832 return array(
833 'api.php?action=query&titles=File:Albert%20Einstein%20Head.jpg&prop=imageinfo',
834 'api.php?action=query&titles=File:Test.jpg&prop=imageinfo&iilimit=50&' .
835 'iiend=20071231235959&iiprop=timestamp|user|url',
836 );
837 }
838
839 public function getHelpUrls() {
840 return 'https://www.mediawiki.org/wiki/API:Properties#imageinfo_.2F_ii';
841 }
842 }