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