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