Merge "Introduce PermissionManager service"
[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 } elseif ( $params['urlparam'] ) {
253 // Audio files might not have a width/height.
254 $scale = [];
255 } else {
256 $scale = null;
257 }
258
259 return $scale;
260 }
261
262 /** Validate and merge scale parameters with handler thumb parameters, give error if invalid.
263 *
264 * We do this later than getScale, since we need the image
265 * to know which handler, since handlers can make their own parameters.
266 * @param File $image Image that params are for.
267 * @param array $thumbParams Thumbnail parameters from getScale
268 * @param string $otherParams String of otherParams (iiurlparam).
269 * @return array Array of parameters for transform.
270 */
271 protected function mergeThumbParams( $image, $thumbParams, $otherParams ) {
272 if ( $thumbParams === null ) {
273 // No scaling requested
274 return null;
275 }
276 if ( !isset( $thumbParams['width'] ) && isset( $thumbParams['height'] ) ) {
277 // We want to limit only by height in this situation, so pass the
278 // image's full width as the limiting width. But some file types
279 // don't have a width of their own, so pick something arbitrary so
280 // thumbnailing the default icon works.
281 if ( $image->getWidth() <= 0 ) {
282 $thumbParams['width'] = max( $this->getConfig()->get( 'ThumbLimits' ) );
283 } else {
284 $thumbParams['width'] = $image->getWidth();
285 }
286 }
287
288 if ( !$otherParams ) {
289 $this->checkParameterNormalise( $image, $thumbParams );
290 return $thumbParams;
291 }
292 $p = $this->getModulePrefix();
293
294 $h = $image->getHandler();
295 if ( !$h ) {
296 $this->addWarning( [ 'apiwarn-nothumb-noimagehandler', wfEscapeWikiText( $image->getName() ) ] );
297
298 return $thumbParams;
299 }
300
301 $paramList = $h->parseParamString( $otherParams );
302 if ( !$paramList ) {
303 // Just set a warning (instead of dieWithError), as in many cases
304 // we could still render the image using width and height parameters,
305 // and this type of thing could happen between different versions of
306 // handlers.
307 $this->addWarning( [ 'apiwarn-badurlparam', $p, wfEscapeWikiText( $image->getName() ) ] );
308 $this->checkParameterNormalise( $image, $thumbParams );
309 return $thumbParams;
310 }
311
312 if ( isset( $paramList['width'] ) && isset( $thumbParams['width'] ) ) {
313 if ( (int)$paramList['width'] != (int)$thumbParams['width'] ) {
314 $this->addWarning(
315 [ 'apiwarn-urlparamwidth', $p, $paramList['width'], $thumbParams['width'] ]
316 );
317 }
318 }
319
320 foreach ( $paramList as $name => $value ) {
321 if ( !$h->validateParam( $name, $value ) ) {
322 $this->dieWithError(
323 [ 'apierror-invalidurlparam', $p, wfEscapeWikiText( $name ), wfEscapeWikiText( $value ) ]
324 );
325 }
326 }
327
328 $finalParams = $thumbParams + $paramList;
329 $this->checkParameterNormalise( $image, $finalParams );
330 return $finalParams;
331 }
332
333 /**
334 * Verify that the final image parameters can be normalised.
335 *
336 * This doesn't use the normalised parameters, since $file->transform
337 * expects the pre-normalised parameters, but doing the normalisation
338 * allows us to catch certain error conditions early (such as missing
339 * required parameter).
340 *
341 * @param File $image
342 * @param array $finalParams List of parameters to transform image with
343 */
344 protected function checkParameterNormalise( $image, $finalParams ) {
345 $h = $image->getHandler();
346 if ( !$h ) {
347 return;
348 }
349 // Note: normaliseParams modifies the array in place, but we aren't interested
350 // in the actual normalised version, only if we can actually normalise them,
351 // so we use the functions scope to throw away the normalisations.
352 if ( !$h->normaliseParams( $image, $finalParams ) ) {
353 $this->dieWithError( [ 'apierror-urlparamnormal', wfEscapeWikiText( $image->getName() ) ] );
354 }
355 }
356
357 /**
358 * Get result information for an image revision
359 *
360 * @param File $file
361 * @param array $prop Array of properties to get (in the keys)
362 * @param ApiResult $result
363 * @param array|null $thumbParams Containing 'width' and 'height' items, or null
364 * @param array|bool|string $opts Options for data fetching.
365 * This is an array consisting of the keys:
366 * 'version': The metadata version for the metadata option
367 * 'language': The language for extmetadata property
368 * 'multilang': Return all translations in extmetadata property
369 * 'revdelUser': User to use when checking whether to show revision-deleted fields.
370 * @return array Result array
371 */
372 public static function getInfo( $file, $prop, $result, $thumbParams = null, $opts = false ) {
373 $anyHidden = false;
374
375 if ( !$opts || is_string( $opts ) ) {
376 $opts = [
377 'version' => $opts ?: 'latest',
378 'language' => MediaWikiServices::getInstance()->getContentLanguage(),
379 'multilang' => false,
380 'extmetadatafilter' => [],
381 'revdelUser' => null,
382 ];
383 }
384 $version = $opts['version'];
385 $vals = [
386 ApiResult::META_TYPE => 'assoc',
387 ];
388 // Timestamp is shown even if the file is revdelete'd in interface
389 // so do same here.
390 if ( isset( $prop['timestamp'] ) ) {
391 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $file->getTimestamp() );
392 }
393
394 // Handle external callers who don't pass revdelUser
395 if ( isset( $opts['revdelUser'] ) && $opts['revdelUser'] ) {
396 $revdelUser = $opts['revdelUser'];
397 $canShowField = function ( $field ) use ( $file, $revdelUser ) {
398 return $file->userCan( $field, $revdelUser );
399 };
400 } else {
401 $canShowField = function ( $field ) use ( $file ) {
402 return !$file->isDeleted( $field );
403 };
404 }
405
406 $user = isset( $prop['user'] );
407 $userid = isset( $prop['userid'] );
408
409 if ( $user || $userid ) {
410 if ( $file->isDeleted( File::DELETED_USER ) ) {
411 $vals['userhidden'] = true;
412 $anyHidden = true;
413 }
414 if ( $canShowField( File::DELETED_USER ) ) {
415 if ( $user ) {
416 $vals['user'] = $file->getUser();
417 }
418 if ( $userid ) {
419 $vals['userid'] = $file->getUser( 'id' );
420 }
421 if ( !$file->getUser( 'id' ) ) {
422 $vals['anon'] = true;
423 }
424 }
425 }
426
427 // This is shown even if the file is revdelete'd in interface
428 // so do same here.
429 if ( isset( $prop['size'] ) || isset( $prop['dimensions'] ) ) {
430 $vals['size'] = (int)$file->getSize();
431 $vals['width'] = (int)$file->getWidth();
432 $vals['height'] = (int)$file->getHeight();
433
434 $pageCount = $file->pageCount();
435 if ( $pageCount !== false ) {
436 $vals['pagecount'] = $pageCount;
437 }
438
439 // length as in how many seconds long a video is.
440 $length = $file->getLength();
441 if ( $length ) {
442 // Call it duration, because "length" can be ambiguous.
443 $vals['duration'] = (float)$length;
444 }
445 }
446
447 $pcomment = isset( $prop['parsedcomment'] );
448 $comment = isset( $prop['comment'] );
449
450 if ( $pcomment || $comment ) {
451 if ( $file->isDeleted( File::DELETED_COMMENT ) ) {
452 $vals['commenthidden'] = true;
453 $anyHidden = true;
454 }
455 if ( $canShowField( File::DELETED_COMMENT ) ) {
456 if ( $pcomment ) {
457 $vals['parsedcomment'] = Linker::formatComment(
458 $file->getDescription( File::RAW ), $file->getTitle() );
459 }
460 if ( $comment ) {
461 $vals['comment'] = $file->getDescription( File::RAW );
462 }
463 }
464 }
465
466 $canonicaltitle = isset( $prop['canonicaltitle'] );
467 $url = isset( $prop['url'] );
468 $sha1 = isset( $prop['sha1'] );
469 $meta = isset( $prop['metadata'] );
470 $extmetadata = isset( $prop['extmetadata'] );
471 $commonmeta = isset( $prop['commonmetadata'] );
472 $mime = isset( $prop['mime'] );
473 $mediatype = isset( $prop['mediatype'] );
474 $archive = isset( $prop['archivename'] );
475 $bitdepth = isset( $prop['bitdepth'] );
476 $uploadwarning = isset( $prop['uploadwarning'] );
477
478 if ( $uploadwarning ) {
479 $vals['html'] = SpecialUpload::getExistsWarning( UploadBase::getExistsWarning( $file ) );
480 }
481
482 if ( $file->isDeleted( File::DELETED_FILE ) ) {
483 $vals['filehidden'] = true;
484 $anyHidden = true;
485 }
486
487 if ( $anyHidden && $file->isDeleted( File::DELETED_RESTRICTED ) ) {
488 $vals['suppressed'] = true;
489 }
490
491 if ( !$canShowField( File::DELETED_FILE ) ) {
492 // Early return, tidier than indenting all following things one level
493 return $vals;
494 }
495
496 if ( $canonicaltitle ) {
497 $vals['canonicaltitle'] = $file->getTitle()->getPrefixedText();
498 }
499
500 if ( $url ) {
501 if ( $file->exists() ) {
502 if ( !is_null( $thumbParams ) ) {
503 $mto = $file->transform( $thumbParams );
504 self::$transformCount++;
505 if ( $mto && !$mto->isError() ) {
506 $vals['thumburl'] = wfExpandUrl( $mto->getUrl(), PROTO_CURRENT );
507
508 // T25834 - If the URLs are the same, we haven't resized it, so shouldn't give the wanted
509 // thumbnail sizes for the thumbnail actual size
510 if ( $mto->getUrl() !== $file->getUrl() ) {
511 $vals['thumbwidth'] = (int)$mto->getWidth();
512 $vals['thumbheight'] = (int)$mto->getHeight();
513 } else {
514 $vals['thumbwidth'] = (int)$file->getWidth();
515 $vals['thumbheight'] = (int)$file->getHeight();
516 }
517
518 if ( isset( $prop['thumbmime'] ) && $file->getHandler() ) {
519 list( , $mime ) = $file->getHandler()->getThumbType(
520 $mto->getExtension(), $file->getMimeType(), $thumbParams );
521 $vals['thumbmime'] = $mime;
522 }
523 } elseif ( $mto && $mto->isError() ) {
524 $vals['thumberror'] = $mto->toText();
525 }
526 }
527 $vals['url'] = wfExpandUrl( $file->getFullUrl(), PROTO_CURRENT );
528 } else {
529 $vals['filemissing'] = true;
530 }
531 $vals['descriptionurl'] = wfExpandUrl( $file->getDescriptionUrl(), PROTO_CURRENT );
532
533 $shortDescriptionUrl = $file->getDescriptionShortUrl();
534 if ( $shortDescriptionUrl !== null ) {
535 $vals['descriptionshorturl'] = wfExpandUrl( $shortDescriptionUrl, PROTO_CURRENT );
536 }
537 }
538
539 if ( $sha1 ) {
540 $vals['sha1'] = Wikimedia\base_convert( $file->getSha1(), 36, 16, 40 );
541 }
542
543 if ( $meta ) {
544 Wikimedia\suppressWarnings();
545 $metadata = unserialize( $file->getMetadata() );
546 Wikimedia\restoreWarnings();
547 if ( $metadata && $version !== 'latest' ) {
548 $metadata = $file->convertMetadataVersion( $metadata, $version );
549 }
550 $vals['metadata'] = $metadata ? static::processMetaData( $metadata, $result ) : null;
551 }
552 if ( $commonmeta ) {
553 $metaArray = $file->getCommonMetaArray();
554 $vals['commonmetadata'] = $metaArray ? static::processMetaData( $metaArray, $result ) : [];
555 }
556
557 if ( $extmetadata ) {
558 // Note, this should return an array where all the keys
559 // start with a letter, and all the values are strings.
560 // Thus there should be no issue with format=xml.
561 $format = new FormatMetadata;
562 $format->setSingleLanguage( !$opts['multilang'] );
563 $format->getContext()->setLanguage( $opts['language'] );
564 $extmetaArray = $format->fetchExtendedMetadata( $file );
565 if ( $opts['extmetadatafilter'] ) {
566 $extmetaArray = array_intersect_key(
567 $extmetaArray, array_flip( $opts['extmetadatafilter'] )
568 );
569 }
570 $vals['extmetadata'] = $extmetaArray;
571 }
572
573 if ( $mime ) {
574 $vals['mime'] = $file->getMimeType();
575 }
576
577 if ( $mediatype ) {
578 $vals['mediatype'] = $file->getMediaType();
579 }
580
581 if ( $archive && $file->isOld() ) {
582 $vals['archivename'] = $file->getArchiveName();
583 }
584
585 if ( $bitdepth ) {
586 $vals['bitdepth'] = $file->getBitDepth();
587 }
588
589 return $vals;
590 }
591
592 /**
593 * Get the count of image transformations performed
594 *
595 * If this is >= TRANSFORM_LIMIT, you should probably stop processing images.
596 *
597 * @return int Count
598 */
599 static function getTransformCount() {
600 return self::$transformCount;
601 }
602
603 /**
604 *
605 * @param array $metadata
606 * @param ApiResult $result
607 * @return array
608 */
609 public static function processMetaData( $metadata, $result ) {
610 $retval = [];
611 if ( is_array( $metadata ) ) {
612 foreach ( $metadata as $key => $value ) {
613 $r = [
614 'name' => $key,
615 ApiResult::META_BC_BOOLS => [ 'value' ],
616 ];
617 if ( is_array( $value ) ) {
618 $r['value'] = static::processMetaData( $value, $result );
619 } else {
620 $r['value'] = $value;
621 }
622 $retval[] = $r;
623 }
624 }
625 ApiResult::setIndexedTagName( $retval, 'metadata' );
626
627 return $retval;
628 }
629
630 public function getCacheMode( $params ) {
631 if ( $this->userCanSeeRevDel() ) {
632 return 'private';
633 }
634
635 return 'public';
636 }
637
638 /**
639 * @param File $img
640 * @param null|string $start
641 * @return string
642 */
643 protected function getContinueStr( $img, $start = null ) {
644 if ( $start === null ) {
645 $start = $img->getTimestamp();
646 }
647
648 return $img->getOriginalTitle()->getDBkey() . '|' . $start;
649 }
650
651 public function getAllowedParams() {
652 return [
653 'prop' => [
654 ApiBase::PARAM_ISMULTI => true,
655 ApiBase::PARAM_DFLT => 'timestamp|user',
656 ApiBase::PARAM_TYPE => static::getPropertyNames(),
657 ApiBase::PARAM_HELP_MSG_PER_VALUE => static::getPropertyMessages(),
658 ],
659 'limit' => [
660 ApiBase::PARAM_TYPE => 'limit',
661 ApiBase::PARAM_DFLT => 1,
662 ApiBase::PARAM_MIN => 1,
663 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
664 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
665 ],
666 'start' => [
667 ApiBase::PARAM_TYPE => 'timestamp'
668 ],
669 'end' => [
670 ApiBase::PARAM_TYPE => 'timestamp'
671 ],
672 'urlwidth' => [
673 ApiBase::PARAM_TYPE => 'integer',
674 ApiBase::PARAM_DFLT => -1,
675 ApiBase::PARAM_HELP_MSG => [
676 'apihelp-query+imageinfo-param-urlwidth',
677 self::TRANSFORM_LIMIT,
678 ],
679 ],
680 'urlheight' => [
681 ApiBase::PARAM_TYPE => 'integer',
682 ApiBase::PARAM_DFLT => -1
683 ],
684 'metadataversion' => [
685 ApiBase::PARAM_TYPE => 'string',
686 ApiBase::PARAM_DFLT => '1',
687 ],
688 'extmetadatalanguage' => [
689 ApiBase::PARAM_TYPE => 'string',
690 ApiBase::PARAM_DFLT =>
691 MediaWikiServices::getInstance()->getContentLanguage()->getCode(),
692 ],
693 'extmetadatamultilang' => [
694 ApiBase::PARAM_TYPE => 'boolean',
695 ApiBase::PARAM_DFLT => false,
696 ],
697 'extmetadatafilter' => [
698 ApiBase::PARAM_TYPE => 'string',
699 ApiBase::PARAM_ISMULTI => true,
700 ],
701 'urlparam' => [
702 ApiBase::PARAM_DFLT => '',
703 ApiBase::PARAM_TYPE => 'string',
704 ],
705 'badfilecontexttitle' => [
706 ApiBase::PARAM_TYPE => 'string',
707 ],
708 'continue' => [
709 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
710 ],
711 'localonly' => false,
712 ];
713 }
714
715 /**
716 * Returns all possible parameters to iiprop
717 *
718 * @param array $filter List of properties to filter out
719 * @return array
720 */
721 public static function getPropertyNames( $filter = [] ) {
722 return array_keys( static::getPropertyMessages( $filter ) );
723 }
724
725 /**
726 * Returns messages for all possible parameters to iiprop
727 *
728 * @param array $filter List of properties to filter out
729 * @return array
730 */
731 public static function getPropertyMessages( $filter = [] ) {
732 return array_diff_key(
733 [
734 'timestamp' => 'apihelp-query+imageinfo-paramvalue-prop-timestamp',
735 'user' => 'apihelp-query+imageinfo-paramvalue-prop-user',
736 'userid' => 'apihelp-query+imageinfo-paramvalue-prop-userid',
737 'comment' => 'apihelp-query+imageinfo-paramvalue-prop-comment',
738 'parsedcomment' => 'apihelp-query+imageinfo-paramvalue-prop-parsedcomment',
739 'canonicaltitle' => 'apihelp-query+imageinfo-paramvalue-prop-canonicaltitle',
740 'url' => 'apihelp-query+imageinfo-paramvalue-prop-url',
741 'size' => 'apihelp-query+imageinfo-paramvalue-prop-size',
742 'dimensions' => 'apihelp-query+imageinfo-paramvalue-prop-dimensions',
743 'sha1' => 'apihelp-query+imageinfo-paramvalue-prop-sha1',
744 'mime' => 'apihelp-query+imageinfo-paramvalue-prop-mime',
745 'thumbmime' => 'apihelp-query+imageinfo-paramvalue-prop-thumbmime',
746 'mediatype' => 'apihelp-query+imageinfo-paramvalue-prop-mediatype',
747 'metadata' => 'apihelp-query+imageinfo-paramvalue-prop-metadata',
748 'commonmetadata' => 'apihelp-query+imageinfo-paramvalue-prop-commonmetadata',
749 'extmetadata' => 'apihelp-query+imageinfo-paramvalue-prop-extmetadata',
750 'archivename' => 'apihelp-query+imageinfo-paramvalue-prop-archivename',
751 'bitdepth' => 'apihelp-query+imageinfo-paramvalue-prop-bitdepth',
752 'uploadwarning' => 'apihelp-query+imageinfo-paramvalue-prop-uploadwarning',
753 'badfile' => 'apihelp-query+imageinfo-paramvalue-prop-badfile',
754 ],
755 array_flip( $filter )
756 );
757 }
758
759 /**
760 * Returns array key value pairs of properties and their descriptions
761 *
762 * @deprecated since 1.25
763 * @param string $modulePrefix
764 * @return array
765 */
766 private static function getProperties( $modulePrefix = '' ) {
767 return [
768 'timestamp' => ' timestamp - Adds timestamp for the uploaded version',
769 'user' => ' user - Adds the user who uploaded the image version',
770 'userid' => ' userid - Add the user ID that uploaded the image version',
771 'comment' => ' comment - Comment on the version',
772 'parsedcomment' => ' parsedcomment - Parse the comment on the version',
773 'canonicaltitle' => ' canonicaltitle - Adds the canonical title of the image file',
774 'url' => ' url - Gives URL to the image and the description page',
775 'size' => ' size - Adds the size of the image in bytes, ' .
776 'its height and its width. Page count and duration are added if applicable',
777 'dimensions' => ' dimensions - Alias for size', // B/C with Allimages
778 'sha1' => ' sha1 - Adds SHA-1 hash for the image',
779 'mime' => ' mime - Adds MIME type of the image',
780 'thumbmime' => ' thumbmime - Adds MIME type of the image thumbnail' .
781 ' (requires url and param ' . $modulePrefix . 'urlwidth)',
782 'mediatype' => ' mediatype - Adds the media type of the image',
783 'metadata' => ' metadata - Lists Exif metadata for the version of the image',
784 'commonmetadata' => ' commonmetadata - Lists file format generic metadata ' .
785 'for the version of the image',
786 'extmetadata' => ' extmetadata - Lists formatted metadata combined ' .
787 'from multiple sources. Results are HTML formatted.',
788 'archivename' => ' archivename - Adds the file name of the archive ' .
789 'version for non-latest versions',
790 'bitdepth' => ' bitdepth - Adds the bit depth of the version',
791 'uploadwarning' => ' uploadwarning - Used by the Special:Upload page to ' .
792 'get information about an existing file. Not intended for use outside MediaWiki core',
793 ];
794 }
795
796 /**
797 * Returns the descriptions for the properties provided by getPropertyNames()
798 *
799 * @deprecated since 1.25
800 * @param array $filter List of properties to filter out
801 * @param string $modulePrefix
802 * @return array
803 */
804 public static function getPropertyDescriptions( $filter = [], $modulePrefix = '' ) {
805 return array_merge(
806 [ 'What image information to get:' ],
807 array_values( array_diff_key( static::getProperties( $modulePrefix ), array_flip( $filter ) ) )
808 );
809 }
810
811 protected function getExamplesMessages() {
812 return [
813 'action=query&titles=File:Albert%20Einstein%20Head.jpg&prop=imageinfo'
814 => 'apihelp-query+imageinfo-example-simple',
815 'action=query&titles=File:Test.jpg&prop=imageinfo&iilimit=50&' .
816 'iiend=2007-12-31T23:59:59Z&iiprop=timestamp|user|url'
817 => 'apihelp-query+imageinfo-example-dated',
818 ];
819 }
820
821 public function getHelpUrls() {
822 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Imageinfo';
823 }
824 }