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