media: Avoid deprecated wfMemcKey()
[lhc/web/wiklou.git] / includes / media / FormatMetadata.php
1 <?php
2 /**
3 * Formatting of image metadata values into human readable form.
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 * @ingroup Media
21 * @author Ævar Arnfjörð Bjarmason <avarab@gmail.com>
22 * @copyright Copyright © 2005, Ævar Arnfjörð Bjarmason, 2009 Brent Garber, 2010 Brian Wolff
23 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
24 * @see http://exif.org/Exif2-2.PDF The Exif 2.2 specification
25 * @file
26 */
27 use MediaWiki\MediaWikiServices;
28 use Wikimedia\Timestamp\TimestampException;
29
30 /**
31 * Format Image metadata values into a human readable form.
32 *
33 * Note lots of these messages use the prefix 'exif' even though
34 * they may not be exif properties. For example 'exif-ImageDescription'
35 * can be the Exif ImageDescription, or it could be the iptc-iim caption
36 * property, or it could be the xmp dc:description property. This
37 * is because these messages should be independent of how the data is
38 * stored, sine the user doesn't care if the description is stored in xmp,
39 * exif, etc only that its a description. (Additionally many of these properties
40 * are merged together following the MWG standard, such that for example,
41 * exif properties override XMP properties that mean the same thing if
42 * there is a conflict).
43 *
44 * It should perhaps use a prefix like 'metadata' instead, but there
45 * is already a large number of messages using the 'exif' prefix.
46 *
47 * @ingroup Media
48 * @since 1.23 the class extends ContextSource and various formerly-public
49 * internal methods are private
50 */
51 class FormatMetadata extends ContextSource {
52 /**
53 * Only output a single language for multi-language fields
54 * @var bool
55 * @since 1.23
56 */
57 protected $singleLang = false;
58
59 /**
60 * Trigger only outputting single language for multilanguage fields
61 *
62 * @param bool $val
63 * @since 1.23
64 */
65 public function setSingleLanguage( $val ) {
66 $this->singleLang = $val;
67 }
68
69 /**
70 * Numbers given by Exif user agents are often magical, that is they
71 * should be replaced by a detailed explanation depending on their
72 * value which most of the time are plain integers. This function
73 * formats Exif (and other metadata) values into human readable form.
74 *
75 * This is the usual entry point for this class.
76 *
77 * @param array $tags The Exif data to format ( as returned by
78 * Exif::getFilteredData() or BitmapMetadataHandler )
79 * @param bool|IContextSource $context Context to use (optional)
80 * @return array
81 */
82 public static function getFormattedData( $tags, $context = false ) {
83 $obj = new FormatMetadata;
84 if ( $context ) {
85 $obj->setContext( $context );
86 }
87
88 return $obj->makeFormattedData( $tags );
89 }
90
91 /**
92 * Numbers given by Exif user agents are often magical, that is they
93 * should be replaced by a detailed explanation depending on their
94 * value which most of the time are plain integers. This function
95 * formats Exif (and other metadata) values into human readable form.
96 *
97 * @param array $tags The Exif data to format ( as returned by
98 * Exif::getFilteredData() or BitmapMetadataHandler )
99 * @return array
100 * @since 1.23
101 */
102 public function makeFormattedData( $tags ) {
103 $resolutionunit = !isset( $tags['ResolutionUnit'] ) || $tags['ResolutionUnit'] == 2 ? 2 : 3;
104 unset( $tags['ResolutionUnit'] );
105 // Width and height are for internal use and already available & displayed outside of metadata
106 unset( $tags['Width'] );
107 unset( $tags['Height'] );
108
109 foreach ( $tags as $tag => &$vals ) {
110 // This seems ugly to wrap non-array's in an array just to unwrap again,
111 // especially when most of the time it is not an array
112 if ( !is_array( $tags[$tag] ) ) {
113 $vals = [ $vals ];
114 }
115
116 // _type is a special value to say what array type
117 if ( isset( $tags[$tag]['_type'] ) ) {
118 $type = $tags[$tag]['_type'];
119 unset( $vals['_type'] );
120 } else {
121 $type = 'ul'; // default unordered list.
122 }
123
124 // This is done differently as the tag is an array.
125 if ( $tag == 'GPSTimeStamp' && count( $vals ) === 3 ) {
126 // hour min sec array
127
128 $h = explode( '/', $vals[0] );
129 $m = explode( '/', $vals[1] );
130 $s = explode( '/', $vals[2] );
131
132 // this should already be validated
133 // when loaded from file, but it could
134 // come from a foreign repo, so be
135 // paranoid.
136 if ( !isset( $h[1] )
137 || !isset( $m[1] )
138 || !isset( $s[1] )
139 || $h[1] == 0
140 || $m[1] == 0
141 || $s[1] == 0
142 ) {
143 continue;
144 }
145 $tags[$tag] = str_pad( intval( $h[0] / $h[1] ), 2, '0', STR_PAD_LEFT )
146 . ':' . str_pad( intval( $m[0] / $m[1] ), 2, '0', STR_PAD_LEFT )
147 . ':' . str_pad( intval( $s[0] / $s[1] ), 2, '0', STR_PAD_LEFT );
148
149 try {
150 $time = wfTimestamp( TS_MW, '1971:01:01 ' . $tags[$tag] );
151 // the 1971:01:01 is just a placeholder, and not shown to user.
152 if ( $time && intval( $time ) > 0 ) {
153 $tags[$tag] = $this->getLanguage()->time( $time );
154 }
155 } catch ( TimestampException $e ) {
156 // This shouldn't happen, but we've seen bad formats
157 // such as 4-digit seconds in the wild.
158 // leave $tags[$tag] as-is
159 }
160 continue;
161 }
162
163 // The contact info is a multi-valued field
164 // instead of the other props which are single
165 // valued (mostly) so handle as a special case.
166 if ( $tag === 'Contact' ) {
167 $vals = $this->collapseContactInfo( $vals );
168 continue;
169 }
170
171 foreach ( $vals as &$val ) {
172 switch ( $tag ) {
173 case 'Compression':
174 switch ( $val ) {
175 case 1:
176 case 2:
177 case 3:
178 case 4:
179 case 5:
180 case 6:
181 case 7:
182 case 8:
183 case 32773:
184 case 32946:
185 case 34712:
186 $val = $this->exifMsg( $tag, $val );
187 break;
188 default:
189 /* If not recognized, display as is. */
190 break;
191 }
192 break;
193
194 case 'PhotometricInterpretation':
195 switch ( $val ) {
196 case 0:
197 case 1:
198 case 2:
199 case 3:
200 case 4:
201 case 5:
202 case 6:
203 case 8:
204 case 9:
205 case 10:
206 case 32803:
207 case 34892:
208 $val = $this->exifMsg( $tag, $val );
209 break;
210 default:
211 /* If not recognized, display as is. */
212 break;
213 }
214 break;
215
216 case 'Orientation':
217 switch ( $val ) {
218 case 1:
219 case 2:
220 case 3:
221 case 4:
222 case 5:
223 case 6:
224 case 7:
225 case 8:
226 $val = $this->exifMsg( $tag, $val );
227 break;
228 default:
229 /* If not recognized, display as is. */
230 break;
231 }
232 break;
233
234 case 'PlanarConfiguration':
235 switch ( $val ) {
236 case 1:
237 case 2:
238 $val = $this->exifMsg( $tag, $val );
239 break;
240 default:
241 /* If not recognized, display as is. */
242 break;
243 }
244 break;
245
246 // TODO: YCbCrSubSampling
247 case 'YCbCrPositioning':
248 switch ( $val ) {
249 case 1:
250 case 2:
251 $val = $this->exifMsg( $tag, $val );
252 break;
253 default:
254 /* If not recognized, display as is. */
255 break;
256 }
257 break;
258
259 case 'XResolution':
260 case 'YResolution':
261 switch ( $resolutionunit ) {
262 case 2:
263 $val = $this->exifMsg( 'XYResolution', 'i', $this->formatNum( $val ) );
264 break;
265 case 3:
266 $val = $this->exifMsg( 'XYResolution', 'c', $this->formatNum( $val ) );
267 break;
268 default:
269 /* If not recognized, display as is. */
270 break;
271 }
272 break;
273
274 // TODO: YCbCrCoefficients #p27 (see annex E)
275 case 'ExifVersion':
276 case 'FlashpixVersion':
277 $val = "$val" / 100;
278 break;
279
280 case 'ColorSpace':
281 switch ( $val ) {
282 case 1:
283 case 65535:
284 $val = $this->exifMsg( $tag, $val );
285 break;
286 default:
287 /* If not recognized, display as is. */
288 break;
289 }
290 break;
291
292 case 'ComponentsConfiguration':
293 switch ( $val ) {
294 case 0:
295 case 1:
296 case 2:
297 case 3:
298 case 4:
299 case 5:
300 case 6:
301 $val = $this->exifMsg( $tag, $val );
302 break;
303 default:
304 /* If not recognized, display as is. */
305 break;
306 }
307 break;
308
309 case 'DateTime':
310 case 'DateTimeOriginal':
311 case 'DateTimeDigitized':
312 case 'DateTimeReleased':
313 case 'DateTimeExpires':
314 case 'GPSDateStamp':
315 case 'dc-date':
316 case 'DateTimeMetadata':
317 if ( $val == '0000:00:00 00:00:00' || $val == ' : : : : ' ) {
318 $val = $this->msg( 'exif-unknowndate' )->text();
319 } elseif ( preg_match(
320 '/^(?:\d{4}):(?:\d\d):(?:\d\d) (?:\d\d):(?:\d\d):(?:\d\d)$/D',
321 $val
322 ) ) {
323 // Full date.
324 $time = wfTimestamp( TS_MW, $val );
325 if ( $time && intval( $time ) > 0 ) {
326 $val = $this->getLanguage()->timeanddate( $time );
327 }
328 } elseif ( preg_match( '/^(?:\d{4}):(?:\d\d):(?:\d\d) (?:\d\d):(?:\d\d)$/D', $val ) ) {
329 // No second field. Still format the same
330 // since timeanddate doesn't include seconds anyways,
331 // but second still available in api
332 $time = wfTimestamp( TS_MW, $val . ':00' );
333 if ( $time && intval( $time ) > 0 ) {
334 $val = $this->getLanguage()->timeanddate( $time );
335 }
336 } elseif ( preg_match( '/^(?:\d{4}):(?:\d\d):(?:\d\d)$/D', $val ) ) {
337 // If only the date but not the time is filled in.
338 $time = wfTimestamp( TS_MW, substr( $val, 0, 4 )
339 . substr( $val, 5, 2 )
340 . substr( $val, 8, 2 )
341 . '000000' );
342 if ( $time && intval( $time ) > 0 ) {
343 $val = $this->getLanguage()->date( $time );
344 }
345 }
346 // else it will just output $val without formatting it.
347 break;
348
349 case 'ExposureProgram':
350 switch ( $val ) {
351 case 0:
352 case 1:
353 case 2:
354 case 3:
355 case 4:
356 case 5:
357 case 6:
358 case 7:
359 case 8:
360 $val = $this->exifMsg( $tag, $val );
361 break;
362 default:
363 /* If not recognized, display as is. */
364 break;
365 }
366 break;
367
368 case 'SubjectDistance':
369 $val = $this->exifMsg( $tag, '', $this->formatNum( $val ) );
370 break;
371
372 case 'MeteringMode':
373 switch ( $val ) {
374 case 0:
375 case 1:
376 case 2:
377 case 3:
378 case 4:
379 case 5:
380 case 6:
381 case 7:
382 case 255:
383 $val = $this->exifMsg( $tag, $val );
384 break;
385 default:
386 /* If not recognized, display as is. */
387 break;
388 }
389 break;
390
391 case 'LightSource':
392 switch ( $val ) {
393 case 0:
394 case 1:
395 case 2:
396 case 3:
397 case 4:
398 case 9:
399 case 10:
400 case 11:
401 case 12:
402 case 13:
403 case 14:
404 case 15:
405 case 17:
406 case 18:
407 case 19:
408 case 20:
409 case 21:
410 case 22:
411 case 23:
412 case 24:
413 case 255:
414 $val = $this->exifMsg( $tag, $val );
415 break;
416 default:
417 /* If not recognized, display as is. */
418 break;
419 }
420 break;
421
422 case 'Flash':
423 $flashDecode = [
424 'fired' => $val & 0b00000001,
425 'return' => ( $val & 0b00000110 ) >> 1,
426 'mode' => ( $val & 0b00011000 ) >> 3,
427 'function' => ( $val & 0b00100000 ) >> 5,
428 'redeye' => ( $val & 0b01000000 ) >> 6,
429 // 'reserved' => ( $val & 0b10000000 ) >> 7,
430 ];
431 $flashMsgs = [];
432 # We do not need to handle unknown values since all are used.
433 foreach ( $flashDecode as $subTag => $subValue ) {
434 # We do not need any message for zeroed values.
435 if ( $subTag != 'fired' && $subValue == 0 ) {
436 continue;
437 }
438 $fullTag = $tag . '-' . $subTag;
439 $flashMsgs[] = $this->exifMsg( $fullTag, $subValue );
440 }
441 $val = $this->getLanguage()->commaList( $flashMsgs );
442 break;
443
444 case 'FocalPlaneResolutionUnit':
445 switch ( $val ) {
446 case 2:
447 $val = $this->exifMsg( $tag, $val );
448 break;
449 default:
450 /* If not recognized, display as is. */
451 break;
452 }
453 break;
454
455 case 'SensingMethod':
456 switch ( $val ) {
457 case 1:
458 case 2:
459 case 3:
460 case 4:
461 case 5:
462 case 7:
463 case 8:
464 $val = $this->exifMsg( $tag, $val );
465 break;
466 default:
467 /* If not recognized, display as is. */
468 break;
469 }
470 break;
471
472 case 'FileSource':
473 switch ( $val ) {
474 case 3:
475 $val = $this->exifMsg( $tag, $val );
476 break;
477 default:
478 /* If not recognized, display as is. */
479 break;
480 }
481 break;
482
483 case 'SceneType':
484 switch ( $val ) {
485 case 1:
486 $val = $this->exifMsg( $tag, $val );
487 break;
488 default:
489 /* If not recognized, display as is. */
490 break;
491 }
492 break;
493
494 case 'CustomRendered':
495 switch ( $val ) {
496 case 0:
497 case 1:
498 $val = $this->exifMsg( $tag, $val );
499 break;
500 default:
501 /* If not recognized, display as is. */
502 break;
503 }
504 break;
505
506 case 'ExposureMode':
507 switch ( $val ) {
508 case 0:
509 case 1:
510 case 2:
511 $val = $this->exifMsg( $tag, $val );
512 break;
513 default:
514 /* If not recognized, display as is. */
515 break;
516 }
517 break;
518
519 case 'WhiteBalance':
520 switch ( $val ) {
521 case 0:
522 case 1:
523 $val = $this->exifMsg( $tag, $val );
524 break;
525 default:
526 /* If not recognized, display as is. */
527 break;
528 }
529 break;
530
531 case 'SceneCaptureType':
532 switch ( $val ) {
533 case 0:
534 case 1:
535 case 2:
536 case 3:
537 $val = $this->exifMsg( $tag, $val );
538 break;
539 default:
540 /* If not recognized, display as is. */
541 break;
542 }
543 break;
544
545 case 'GainControl':
546 switch ( $val ) {
547 case 0:
548 case 1:
549 case 2:
550 case 3:
551 case 4:
552 $val = $this->exifMsg( $tag, $val );
553 break;
554 default:
555 /* If not recognized, display as is. */
556 break;
557 }
558 break;
559
560 case 'Contrast':
561 switch ( $val ) {
562 case 0:
563 case 1:
564 case 2:
565 $val = $this->exifMsg( $tag, $val );
566 break;
567 default:
568 /* If not recognized, display as is. */
569 break;
570 }
571 break;
572
573 case 'Saturation':
574 switch ( $val ) {
575 case 0:
576 case 1:
577 case 2:
578 $val = $this->exifMsg( $tag, $val );
579 break;
580 default:
581 /* If not recognized, display as is. */
582 break;
583 }
584 break;
585
586 case 'Sharpness':
587 switch ( $val ) {
588 case 0:
589 case 1:
590 case 2:
591 $val = $this->exifMsg( $tag, $val );
592 break;
593 default:
594 /* If not recognized, display as is. */
595 break;
596 }
597 break;
598
599 case 'SubjectDistanceRange':
600 switch ( $val ) {
601 case 0:
602 case 1:
603 case 2:
604 case 3:
605 $val = $this->exifMsg( $tag, $val );
606 break;
607 default:
608 /* If not recognized, display as is. */
609 break;
610 }
611 break;
612
613 // The GPS...Ref values are kept for compatibility, probably won't be reached.
614 case 'GPSLatitudeRef':
615 case 'GPSDestLatitudeRef':
616 switch ( $val ) {
617 case 'N':
618 case 'S':
619 $val = $this->exifMsg( 'GPSLatitude', $val );
620 break;
621 default:
622 /* If not recognized, display as is. */
623 break;
624 }
625 break;
626
627 case 'GPSLongitudeRef':
628 case 'GPSDestLongitudeRef':
629 switch ( $val ) {
630 case 'E':
631 case 'W':
632 $val = $this->exifMsg( 'GPSLongitude', $val );
633 break;
634 default:
635 /* If not recognized, display as is. */
636 break;
637 }
638 break;
639
640 case 'GPSAltitude':
641 if ( $val < 0 ) {
642 $val = $this->exifMsg( 'GPSAltitude', 'below-sealevel', $this->formatNum( -$val, 3 ) );
643 } else {
644 $val = $this->exifMsg( 'GPSAltitude', 'above-sealevel', $this->formatNum( $val, 3 ) );
645 }
646 break;
647
648 case 'GPSStatus':
649 switch ( $val ) {
650 case 'A':
651 case 'V':
652 $val = $this->exifMsg( $tag, $val );
653 break;
654 default:
655 /* If not recognized, display as is. */
656 break;
657 }
658 break;
659
660 case 'GPSMeasureMode':
661 switch ( $val ) {
662 case 2:
663 case 3:
664 $val = $this->exifMsg( $tag, $val );
665 break;
666 default:
667 /* If not recognized, display as is. */
668 break;
669 }
670 break;
671
672 case 'GPSTrackRef':
673 case 'GPSImgDirectionRef':
674 case 'GPSDestBearingRef':
675 switch ( $val ) {
676 case 'T':
677 case 'M':
678 $val = $this->exifMsg( 'GPSDirection', $val );
679 break;
680 default:
681 /* If not recognized, display as is. */
682 break;
683 }
684 break;
685
686 case 'GPSLatitude':
687 case 'GPSDestLatitude':
688 $val = $this->formatCoords( $val, 'latitude' );
689 break;
690 case 'GPSLongitude':
691 case 'GPSDestLongitude':
692 $val = $this->formatCoords( $val, 'longitude' );
693 break;
694
695 case 'GPSSpeedRef':
696 switch ( $val ) {
697 case 'K':
698 case 'M':
699 case 'N':
700 $val = $this->exifMsg( 'GPSSpeed', $val );
701 break;
702 default:
703 /* If not recognized, display as is. */
704 break;
705 }
706 break;
707
708 case 'GPSDestDistanceRef':
709 switch ( $val ) {
710 case 'K':
711 case 'M':
712 case 'N':
713 $val = $this->exifMsg( 'GPSDestDistance', $val );
714 break;
715 default:
716 /* If not recognized, display as is. */
717 break;
718 }
719 break;
720
721 case 'GPSDOP':
722 // See https://en.wikipedia.org/wiki/Dilution_of_precision_(GPS)
723 if ( $val <= 2 ) {
724 $val = $this->exifMsg( $tag, 'excellent', $this->formatNum( $val ) );
725 } elseif ( $val <= 5 ) {
726 $val = $this->exifMsg( $tag, 'good', $this->formatNum( $val ) );
727 } elseif ( $val <= 10 ) {
728 $val = $this->exifMsg( $tag, 'moderate', $this->formatNum( $val ) );
729 } elseif ( $val <= 20 ) {
730 $val = $this->exifMsg( $tag, 'fair', $this->formatNum( $val ) );
731 } else {
732 $val = $this->exifMsg( $tag, 'poor', $this->formatNum( $val ) );
733 }
734 break;
735
736 // This is not in the Exif standard, just a special
737 // case for our purposes which enables wikis to wikify
738 // the make, model and software name to link to their articles.
739 case 'Make':
740 case 'Model':
741 $val = $this->exifMsg( $tag, '', $val );
742 break;
743
744 case 'Software':
745 if ( is_array( $val ) ) {
746 // if its a software, version array.
747 $val = $this->msg( 'exif-software-version-value', $val[0], $val[1] )->text();
748 } else {
749 $val = $this->exifMsg( $tag, '', $val );
750 }
751 break;
752
753 case 'ExposureTime':
754 // Show the pretty fraction as well as decimal version
755 $val = $this->msg( 'exif-exposuretime-format',
756 $this->formatFraction( $val ), $this->formatNum( $val ) )->text();
757 break;
758 case 'ISOSpeedRatings':
759 // If its = 65535 that means its at the
760 // limit of the size of Exif::short and
761 // is really higher.
762 if ( $val == '65535' ) {
763 $val = $this->exifMsg( $tag, 'overflow' );
764 } else {
765 $val = $this->formatNum( $val );
766 }
767 break;
768 case 'FNumber':
769 $val = $this->msg( 'exif-fnumber-format',
770 $this->formatNum( $val ) )->text();
771 break;
772
773 case 'FocalLength':
774 case 'FocalLengthIn35mmFilm':
775 $val = $this->msg( 'exif-focallength-format',
776 $this->formatNum( $val ) )->text();
777 break;
778
779 case 'MaxApertureValue':
780 if ( strpos( $val, '/' ) !== false ) {
781 // need to expand this earlier to calculate fNumber
782 list( $n, $d ) = explode( '/', $val );
783 if ( is_numeric( $n ) && is_numeric( $d ) ) {
784 $val = $n / $d;
785 }
786 }
787 if ( is_numeric( $val ) ) {
788 $fNumber = pow( 2, $val / 2 );
789 if ( $fNumber !== false ) {
790 $val = $this->msg( 'exif-maxaperturevalue-value',
791 $this->formatNum( $val ),
792 $this->formatNum( $fNumber, 2 )
793 )->text();
794 }
795 }
796 break;
797
798 case 'iimCategory':
799 switch ( strtolower( $val ) ) {
800 // See pg 29 of IPTC photo
801 // metadata standard.
802 case 'ace':
803 case 'clj':
804 case 'dis':
805 case 'fin':
806 case 'edu':
807 case 'evn':
808 case 'hth':
809 case 'hum':
810 case 'lab':
811 case 'lif':
812 case 'pol':
813 case 'rel':
814 case 'sci':
815 case 'soi':
816 case 'spo':
817 case 'war':
818 case 'wea':
819 $val = $this->exifMsg(
820 'iimcategory',
821 $val
822 );
823 }
824 break;
825 case 'SubjectNewsCode':
826 // Essentially like iimCategory.
827 // 8 (numeric) digit hierarchical
828 // classification. We decode the
829 // first 2 digits, which provide
830 // a broad category.
831 $val = $this->convertNewsCode( $val );
832 break;
833 case 'Urgency':
834 // 1-8 with 1 being highest, 5 normal
835 // 0 is reserved, and 9 is 'user-defined'.
836 $urgency = '';
837 if ( $val == 0 || $val == 9 ) {
838 $urgency = 'other';
839 } elseif ( $val < 5 && $val > 1 ) {
840 $urgency = 'high';
841 } elseif ( $val == 5 ) {
842 $urgency = 'normal';
843 } elseif ( $val <= 8 && $val > 5 ) {
844 $urgency = 'low';
845 }
846
847 if ( $urgency !== '' ) {
848 $val = $this->exifMsg( 'urgency',
849 $urgency, $val
850 );
851 }
852 break;
853
854 // Things that have a unit of pixels.
855 case 'OriginalImageHeight':
856 case 'OriginalImageWidth':
857 case 'PixelXDimension':
858 case 'PixelYDimension':
859 case 'ImageWidth':
860 case 'ImageLength':
861 $val = $this->formatNum( $val ) . ' ' . $this->msg( 'unit-pixel' )->text();
862 break;
863
864 // Do not transform fields with pure text.
865 // For some languages the formatNum()
866 // conversion results to wrong output like
867 // foo,bar@example,com or foo٫bar@example٫com.
868 // Also some 'numeric' things like Scene codes
869 // are included here as we really don't want
870 // commas inserted.
871 case 'ImageDescription':
872 case 'UserComment':
873 case 'Artist':
874 case 'Copyright':
875 case 'RelatedSoundFile':
876 case 'ImageUniqueID':
877 case 'SpectralSensitivity':
878 case 'GPSSatellites':
879 case 'GPSVersionID':
880 case 'GPSMapDatum':
881 case 'Keywords':
882 case 'WorldRegionDest':
883 case 'CountryDest':
884 case 'CountryCodeDest':
885 case 'ProvinceOrStateDest':
886 case 'CityDest':
887 case 'SublocationDest':
888 case 'WorldRegionCreated':
889 case 'CountryCreated':
890 case 'CountryCodeCreated':
891 case 'ProvinceOrStateCreated':
892 case 'CityCreated':
893 case 'SublocationCreated':
894 case 'ObjectName':
895 case 'SpecialInstructions':
896 case 'Headline':
897 case 'Credit':
898 case 'Source':
899 case 'EditStatus':
900 case 'FixtureIdentifier':
901 case 'LocationDest':
902 case 'LocationDestCode':
903 case 'Writer':
904 case 'JPEGFileComment':
905 case 'iimSupplementalCategory':
906 case 'OriginalTransmissionRef':
907 case 'Identifier':
908 case 'dc-contributor':
909 case 'dc-coverage':
910 case 'dc-publisher':
911 case 'dc-relation':
912 case 'dc-rights':
913 case 'dc-source':
914 case 'dc-type':
915 case 'Lens':
916 case 'SerialNumber':
917 case 'CameraOwnerName':
918 case 'Label':
919 case 'Nickname':
920 case 'RightsCertificate':
921 case 'CopyrightOwner':
922 case 'UsageTerms':
923 case 'WebStatement':
924 case 'OriginalDocumentID':
925 case 'LicenseUrl':
926 case 'MorePermissionsUrl':
927 case 'AttributionUrl':
928 case 'PreferredAttributionName':
929 case 'PNGFileComment':
930 case 'Disclaimer':
931 case 'ContentWarning':
932 case 'GIFFileComment':
933 case 'SceneCode':
934 case 'IntellectualGenre':
935 case 'Event':
936 case 'OrginisationInImage':
937 case 'PersonInImage':
938
939 $val = htmlspecialchars( $val );
940 break;
941
942 case 'ObjectCycle':
943 switch ( $val ) {
944 case 'a':
945 case 'p':
946 case 'b':
947 $val = $this->exifMsg( $tag, $val );
948 break;
949 default:
950 $val = htmlspecialchars( $val );
951 break;
952 }
953 break;
954 case 'Copyrighted':
955 switch ( $val ) {
956 case 'True':
957 case 'False':
958 $val = $this->exifMsg( $tag, $val );
959 break;
960 }
961 break;
962 case 'Rating':
963 if ( $val == '-1' ) {
964 $val = $this->exifMsg( $tag, 'rejected' );
965 } else {
966 $val = $this->formatNum( $val );
967 }
968 break;
969
970 case 'LanguageCode':
971 $lang = Language::fetchLanguageName( strtolower( $val ), $this->getLanguage()->getCode() );
972 if ( $lang ) {
973 $val = htmlspecialchars( $lang );
974 } else {
975 $val = htmlspecialchars( $val );
976 }
977 break;
978
979 default:
980 $val = $this->formatNum( $val );
981 break;
982 }
983 }
984 // End formatting values, start flattening arrays.
985 $vals = $this->flattenArrayReal( $vals, $type );
986 }
987
988 return $tags;
989 }
990
991 /**
992 * Flatten an array, using the content language for any messages.
993 *
994 * @param array $vals Array of values
995 * @param string $type Type of array (either lang, ul, ol).
996 * lang = language assoc array with keys being the lang code
997 * ul = unordered list, ol = ordered list
998 * type can also come from the '_type' member of $vals.
999 * @param bool $noHtml If to avoid returning anything resembling HTML.
1000 * (Ugly hack for backwards compatibility with old MediaWiki).
1001 * @param bool|IContextSource $context
1002 * @return string Single value (in wiki-syntax).
1003 * @since 1.23
1004 */
1005 public static function flattenArrayContentLang( $vals, $type = 'ul',
1006 $noHtml = false, $context = false
1007 ) {
1008 global $wgContLang;
1009 $obj = new FormatMetadata;
1010 if ( $context ) {
1011 $obj->setContext( $context );
1012 }
1013 $context = new DerivativeContext( $obj->getContext() );
1014 $context->setLanguage( $wgContLang );
1015 $obj->setContext( $context );
1016
1017 return $obj->flattenArrayReal( $vals, $type, $noHtml );
1018 }
1019
1020 /**
1021 * A function to collapse multivalued tags into a single value.
1022 * This turns an array of (for example) authors into a bulleted list.
1023 *
1024 * This is public on the basis it might be useful outside of this class.
1025 *
1026 * @param array $vals Array of values
1027 * @param string $type Type of array (either lang, ul, ol).
1028 * lang = language assoc array with keys being the lang code
1029 * ul = unordered list, ol = ordered list
1030 * type can also come from the '_type' member of $vals.
1031 * @param bool $noHtml If to avoid returning anything resembling HTML.
1032 * (Ugly hack for backwards compatibility with old mediawiki).
1033 * @return string Single value (in wiki-syntax).
1034 * @since 1.23
1035 */
1036 public function flattenArrayReal( $vals, $type = 'ul', $noHtml = false ) {
1037 if ( !is_array( $vals ) ) {
1038 return $vals; // do nothing if not an array;
1039 }
1040
1041 if ( isset( $vals['_type'] ) ) {
1042 $type = $vals['_type'];
1043 unset( $vals['_type'] );
1044 }
1045
1046 if ( !is_array( $vals ) ) {
1047 return $vals; // do nothing if not an array;
1048 } elseif ( count( $vals ) === 1 && $type !== 'lang' ) {
1049 return $vals[0];
1050 } elseif ( count( $vals ) === 0 ) {
1051 wfDebug( __METHOD__ . " metadata array with 0 elements!\n" );
1052
1053 return ""; // paranoia. This should never happen
1054 } else {
1055 /* @todo FIXME: This should hide some of the list entries if there are
1056 * say more than four. Especially if a field is translated into 20
1057 * languages, we don't want to show them all by default
1058 */
1059 switch ( $type ) {
1060 case 'lang':
1061 // Display default, followed by ContLang,
1062 // followed by the rest in no particular
1063 // order.
1064
1065 // Todo: hide some items if really long list.
1066
1067 $content = '';
1068
1069 $priorityLanguages = $this->getPriorityLanguages();
1070 $defaultItem = false;
1071 $defaultLang = false;
1072
1073 // If default is set, save it for later,
1074 // as we don't know if it's equal to
1075 // one of the lang codes. (In xmp
1076 // you specify the language for a
1077 // default property by having both
1078 // a default prop, and one in the language
1079 // that are identical)
1080 if ( isset( $vals['x-default'] ) ) {
1081 $defaultItem = $vals['x-default'];
1082 unset( $vals['x-default'] );
1083 }
1084 foreach ( $priorityLanguages as $pLang ) {
1085 if ( isset( $vals[$pLang] ) ) {
1086 $isDefault = false;
1087 if ( $vals[$pLang] === $defaultItem ) {
1088 $defaultItem = false;
1089 $isDefault = true;
1090 }
1091 $content .= $this->langItem(
1092 $vals[$pLang], $pLang,
1093 $isDefault, $noHtml );
1094
1095 unset( $vals[$pLang] );
1096
1097 if ( $this->singleLang ) {
1098 return Html::rawElement( 'span',
1099 [ 'lang' => $pLang ], $vals[$pLang] );
1100 }
1101 }
1102 }
1103
1104 // Now do the rest.
1105 foreach ( $vals as $lang => $item ) {
1106 if ( $item === $defaultItem ) {
1107 $defaultLang = $lang;
1108 continue;
1109 }
1110 $content .= $this->langItem( $item,
1111 $lang, false, $noHtml );
1112 if ( $this->singleLang ) {
1113 return Html::rawElement( 'span',
1114 [ 'lang' => $lang ], $item );
1115 }
1116 }
1117 if ( $defaultItem !== false ) {
1118 $content = $this->langItem( $defaultItem,
1119 $defaultLang, true, $noHtml ) .
1120 $content;
1121 if ( $this->singleLang ) {
1122 return $defaultItem;
1123 }
1124 }
1125 if ( $noHtml ) {
1126 return $content;
1127 }
1128
1129 return '<ul class="metadata-langlist">' .
1130 $content .
1131 '</ul>';
1132 case 'ol':
1133 if ( $noHtml ) {
1134 return "\n#" . implode( "\n#", $vals );
1135 }
1136
1137 return "<ol><li>" . implode( "</li>\n<li>", $vals ) . '</li></ol>';
1138 case 'ul':
1139 default:
1140 if ( $noHtml ) {
1141 return "\n*" . implode( "\n*", $vals );
1142 }
1143
1144 return "<ul><li>" . implode( "</li>\n<li>", $vals ) . '</li></ul>';
1145 }
1146 }
1147 }
1148
1149 /** Helper function for creating lists of translations.
1150 *
1151 * @param string $value Value (this is not escaped)
1152 * @param string $lang Lang code of item or false
1153 * @param bool $default If it is default value.
1154 * @param bool $noHtml If to avoid html (for back-compat)
1155 * @throws MWException
1156 * @return string Language item (Note: despite how this looks, this is
1157 * treated as wikitext, not as HTML).
1158 */
1159 private function langItem( $value, $lang, $default = false, $noHtml = false ) {
1160 if ( $lang === false && $default === false ) {
1161 throw new MWException( '$lang and $default cannot both '
1162 . 'be false.' );
1163 }
1164
1165 if ( $noHtml ) {
1166 $wrappedValue = $value;
1167 } else {
1168 $wrappedValue = '<span class="mw-metadata-lang-value">'
1169 . $value . '</span>';
1170 }
1171
1172 if ( $lang === false ) {
1173 $msg = $this->msg( 'metadata-langitem-default', $wrappedValue );
1174 if ( $noHtml ) {
1175 return $msg->text() . "\n\n";
1176 } /* else */
1177
1178 return '<li class="mw-metadata-lang-default">'
1179 . $msg->text()
1180 . "</li>\n";
1181 }
1182
1183 $lowLang = strtolower( $lang );
1184 $langName = Language::fetchLanguageName( $lowLang );
1185 if ( $langName === '' ) {
1186 // try just the base language name. (aka en-US -> en ).
1187 list( $langPrefix ) = explode( '-', $lowLang, 2 );
1188 $langName = Language::fetchLanguageName( $langPrefix );
1189 if ( $langName === '' ) {
1190 // give up.
1191 $langName = $lang;
1192 }
1193 }
1194 // else we have a language specified
1195
1196 $msg = $this->msg( 'metadata-langitem', $wrappedValue, $langName, $lang );
1197 if ( $noHtml ) {
1198 return '*' . $msg->text();
1199 } /* else: */
1200
1201 $item = '<li class="mw-metadata-lang-code-'
1202 . $lang;
1203 if ( $default ) {
1204 $item .= ' mw-metadata-lang-default';
1205 }
1206 $item .= '" lang="' . $lang . '">';
1207 $item .= $msg->text();
1208 $item .= "</li>\n";
1209
1210 return $item;
1211 }
1212
1213 /**
1214 * Convenience function for getFormattedData()
1215 *
1216 * @param string $tag The tag name to pass on
1217 * @param string $val The value of the tag
1218 * @param string $arg An argument to pass ($1)
1219 * @param string $arg2 A 2nd argument to pass ($2)
1220 * @return string The text content of "exif-$tag-$val" message in lower case
1221 */
1222 private function exifMsg( $tag, $val, $arg = null, $arg2 = null ) {
1223 global $wgContLang;
1224
1225 if ( $val === '' ) {
1226 $val = 'value';
1227 }
1228
1229 return $this->msg( $wgContLang->lc( "exif-$tag-$val" ), $arg, $arg2 )->text();
1230 }
1231
1232 /**
1233 * Format a number, convert numbers from fractions into floating point
1234 * numbers, joins arrays of numbers with commas.
1235 *
1236 * @param mixed $num The value to format
1237 * @param float|int|bool $round Digits to round to or false.
1238 * @return mixed A floating point number or whatever we were fed
1239 */
1240 private function formatNum( $num, $round = false ) {
1241 $m = [];
1242 if ( is_array( $num ) ) {
1243 $out = [];
1244 foreach ( $num as $number ) {
1245 $out[] = $this->formatNum( $number );
1246 }
1247
1248 return $this->getLanguage()->commaList( $out );
1249 }
1250 if ( preg_match( '/^(-?\d+)\/(\d+)$/', $num, $m ) ) {
1251 if ( $m[2] != 0 ) {
1252 $newNum = $m[1] / $m[2];
1253 if ( $round !== false ) {
1254 $newNum = round( $newNum, $round );
1255 }
1256 } else {
1257 $newNum = $num;
1258 }
1259
1260 return $this->getLanguage()->formatNum( $newNum );
1261 } else {
1262 if ( is_numeric( $num ) && $round !== false ) {
1263 $num = round( $num, $round );
1264 }
1265
1266 return $this->getLanguage()->formatNum( $num );
1267 }
1268 }
1269
1270 /**
1271 * Format a rational number, reducing fractions
1272 *
1273 * @param mixed $num The value to format
1274 * @return mixed A floating point number or whatever we were fed
1275 */
1276 private function formatFraction( $num ) {
1277 $m = [];
1278 if ( preg_match( '/^(-?\d+)\/(\d+)$/', $num, $m ) ) {
1279 $numerator = intval( $m[1] );
1280 $denominator = intval( $m[2] );
1281 $gcd = $this->gcd( abs( $numerator ), $denominator );
1282 if ( $gcd != 0 ) {
1283 // 0 shouldn't happen! ;)
1284 return $this->formatNum( $numerator / $gcd ) . '/' . $this->formatNum( $denominator / $gcd );
1285 }
1286 }
1287
1288 return $this->formatNum( $num );
1289 }
1290
1291 /**
1292 * Calculate the greatest common divisor of two integers.
1293 *
1294 * @param int $a Numerator
1295 * @param int $b Denominator
1296 * @return int
1297 */
1298 private function gcd( $a, $b ) {
1299 /*
1300 // https://en.wikipedia.org/wiki/Euclidean_algorithm
1301 // Recursive form would be:
1302 if( $b == 0 )
1303 return $a;
1304 else
1305 return gcd( $b, $a % $b );
1306 */
1307 while ( $b != 0 ) {
1308 $remainder = $a % $b;
1309
1310 // tail recursion...
1311 $a = $b;
1312 $b = $remainder;
1313 }
1314
1315 return $a;
1316 }
1317
1318 /**
1319 * Fetch the human readable version of a news code.
1320 * A news code is an 8 digit code. The first two
1321 * digits are a general classification, so we just
1322 * translate that.
1323 *
1324 * Note, leading 0's are significant, so this is
1325 * a string, not an int.
1326 *
1327 * @param string $val The 8 digit news code.
1328 * @return string The human readable form
1329 */
1330 private function convertNewsCode( $val ) {
1331 if ( !preg_match( '/^\d{8}$/D', $val ) ) {
1332 // Not a valid news code.
1333 return $val;
1334 }
1335 $cat = '';
1336 switch ( substr( $val, 0, 2 ) ) {
1337 case '01':
1338 $cat = 'ace';
1339 break;
1340 case '02':
1341 $cat = 'clj';
1342 break;
1343 case '03':
1344 $cat = 'dis';
1345 break;
1346 case '04':
1347 $cat = 'fin';
1348 break;
1349 case '05':
1350 $cat = 'edu';
1351 break;
1352 case '06':
1353 $cat = 'evn';
1354 break;
1355 case '07':
1356 $cat = 'hth';
1357 break;
1358 case '08':
1359 $cat = 'hum';
1360 break;
1361 case '09':
1362 $cat = 'lab';
1363 break;
1364 case '10':
1365 $cat = 'lif';
1366 break;
1367 case '11':
1368 $cat = 'pol';
1369 break;
1370 case '12':
1371 $cat = 'rel';
1372 break;
1373 case '13':
1374 $cat = 'sci';
1375 break;
1376 case '14':
1377 $cat = 'soi';
1378 break;
1379 case '15':
1380 $cat = 'spo';
1381 break;
1382 case '16':
1383 $cat = 'war';
1384 break;
1385 case '17':
1386 $cat = 'wea';
1387 break;
1388 }
1389 if ( $cat !== '' ) {
1390 $catMsg = $this->exifMsg( 'iimcategory', $cat );
1391 $val = $this->exifMsg( 'subjectnewscode', '', $val, $catMsg );
1392 }
1393
1394 return $val;
1395 }
1396
1397 /**
1398 * Format a coordinate value, convert numbers from floating point
1399 * into degree minute second representation.
1400 *
1401 * @param int $coord Degrees, minutes and seconds
1402 * @param string $type Latitude or longitude (for if its a NWS or E)
1403 * @return mixed A floating point number or whatever we were fed
1404 */
1405 private function formatCoords( $coord, $type ) {
1406 $ref = '';
1407 if ( $coord < 0 ) {
1408 $nCoord = -$coord;
1409 if ( $type === 'latitude' ) {
1410 $ref = 'S';
1411 } elseif ( $type === 'longitude' ) {
1412 $ref = 'W';
1413 }
1414 } else {
1415 $nCoord = $coord;
1416 if ( $type === 'latitude' ) {
1417 $ref = 'N';
1418 } elseif ( $type === 'longitude' ) {
1419 $ref = 'E';
1420 }
1421 }
1422
1423 $deg = floor( $nCoord );
1424 $min = floor( ( $nCoord - $deg ) * 60.0 );
1425 $sec = round( ( ( $nCoord - $deg ) - $min / 60 ) * 3600, 2 );
1426
1427 $deg = $this->formatNum( $deg );
1428 $min = $this->formatNum( $min );
1429 $sec = $this->formatNum( $sec );
1430
1431 return $this->msg( 'exif-coordinate-format', $deg, $min, $sec, $ref, $coord )->text();
1432 }
1433
1434 /**
1435 * Format the contact info field into a single value.
1436 *
1437 * This function might be called from
1438 * JpegHandler::convertMetadataVersion which is why it is
1439 * public.
1440 *
1441 * @param array $vals Array with fields of the ContactInfo
1442 * struct defined in the IPTC4XMP spec. Or potentially
1443 * an array with one element that is a free form text
1444 * value from the older iptc iim 1:118 prop.
1445 * @return string HTML-ish looking wikitext
1446 * @since 1.23 no longer static
1447 */
1448 public function collapseContactInfo( $vals ) {
1449 if ( !( isset( $vals['CiAdrExtadr'] )
1450 || isset( $vals['CiAdrCity'] )
1451 || isset( $vals['CiAdrCtry'] )
1452 || isset( $vals['CiEmailWork'] )
1453 || isset( $vals['CiTelWork'] )
1454 || isset( $vals['CiAdrPcode'] )
1455 || isset( $vals['CiAdrRegion'] )
1456 || isset( $vals['CiUrlWork'] )
1457 ) ) {
1458 // We don't have any sub-properties
1459 // This could happen if its using old
1460 // iptc that just had this as a free-form
1461 // text value.
1462 // Note: We run this through htmlspecialchars
1463 // partially to be consistent, and partially
1464 // because people often insert >, etc into
1465 // the metadata which should not be interpreted
1466 // but we still want to auto-link urls.
1467 foreach ( $vals as &$val ) {
1468 $val = htmlspecialchars( $val );
1469 }
1470
1471 return $this->flattenArrayReal( $vals );
1472 } else {
1473 // We have a real ContactInfo field.
1474 // Its unclear if all these fields have to be
1475 // set, so assume they do not.
1476 $url = $tel = $street = $city = $country = '';
1477 $email = $postal = $region = '';
1478
1479 // Also note, some of the class names this uses
1480 // are similar to those used by hCard. This is
1481 // mostly because they're sensible names. This
1482 // does not (and does not attempt to) output
1483 // stuff in the hCard microformat. However it
1484 // might output in the adr microformat.
1485
1486 if ( isset( $vals['CiAdrExtadr'] ) ) {
1487 // Todo: This can potentially be multi-line.
1488 // Need to check how that works in XMP.
1489 $street = '<span class="extended-address">'
1490 . htmlspecialchars(
1491 $vals['CiAdrExtadr'] )
1492 . '</span>';
1493 }
1494 if ( isset( $vals['CiAdrCity'] ) ) {
1495 $city = '<span class="locality">'
1496 . htmlspecialchars( $vals['CiAdrCity'] )
1497 . '</span>';
1498 }
1499 if ( isset( $vals['CiAdrCtry'] ) ) {
1500 $country = '<span class="country-name">'
1501 . htmlspecialchars( $vals['CiAdrCtry'] )
1502 . '</span>';
1503 }
1504 if ( isset( $vals['CiEmailWork'] ) ) {
1505 $emails = [];
1506 // Have to split multiple emails at commas/new lines.
1507 $splitEmails = explode( "\n", $vals['CiEmailWork'] );
1508 foreach ( $splitEmails as $e1 ) {
1509 // Also split on comma
1510 foreach ( explode( ',', $e1 ) as $e2 ) {
1511 $finalEmail = trim( $e2 );
1512 if ( $finalEmail == ',' || $finalEmail == '' ) {
1513 continue;
1514 }
1515 if ( strpos( $finalEmail, '<' ) !== false ) {
1516 // Don't do fancy formatting to
1517 // "My name" <foo@bar.com> style stuff
1518 $emails[] = $finalEmail;
1519 } else {
1520 $emails[] = '[mailto:'
1521 . $finalEmail
1522 . ' <span class="email">'
1523 . $finalEmail
1524 . '</span>]';
1525 }
1526 }
1527 }
1528 $email = implode( ', ', $emails );
1529 }
1530 if ( isset( $vals['CiTelWork'] ) ) {
1531 $tel = '<span class="tel">'
1532 . htmlspecialchars( $vals['CiTelWork'] )
1533 . '</span>';
1534 }
1535 if ( isset( $vals['CiAdrPcode'] ) ) {
1536 $postal = '<span class="postal-code">'
1537 . htmlspecialchars(
1538 $vals['CiAdrPcode'] )
1539 . '</span>';
1540 }
1541 if ( isset( $vals['CiAdrRegion'] ) ) {
1542 // Note this is province/state.
1543 $region = '<span class="region">'
1544 . htmlspecialchars(
1545 $vals['CiAdrRegion'] )
1546 . '</span>';
1547 }
1548 if ( isset( $vals['CiUrlWork'] ) ) {
1549 $url = '<span class="url">'
1550 . htmlspecialchars( $vals['CiUrlWork'] )
1551 . '</span>';
1552 }
1553
1554 return $this->msg( 'exif-contact-value', $email, $url,
1555 $street, $city, $region, $postal, $country,
1556 $tel )->text();
1557 }
1558 }
1559
1560 /**
1561 * Get a list of fields that are visible by default.
1562 *
1563 * @return array
1564 * @since 1.23
1565 */
1566 public static function getVisibleFields() {
1567 $fields = [];
1568 $lines = explode( "\n", wfMessage( 'metadata-fields' )->inContentLanguage()->text() );
1569 foreach ( $lines as $line ) {
1570 $matches = [];
1571 if ( preg_match( '/^\\*\s*(.*?)\s*$/', $line, $matches ) ) {
1572 $fields[] = $matches[1];
1573 }
1574 }
1575 $fields = array_map( 'strtolower', $fields );
1576
1577 return $fields;
1578 }
1579
1580 /**
1581 * Get an array of extended metadata. (See the imageinfo API for format.)
1582 *
1583 * @param File $file File to use
1584 * @return array [<property name> => ['value' => <value>]], or [] on error
1585 * @since 1.23
1586 */
1587 public function fetchExtendedMetadata( File $file ) {
1588 $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
1589
1590 // If revision deleted, exit immediately
1591 if ( $file->isDeleted( File::DELETED_FILE ) ) {
1592 return [];
1593 }
1594
1595 $cacheKey = $cache->makeKey(
1596 'getExtendedMetadata',
1597 $this->getLanguage()->getCode(),
1598 (int)$this->singleLang,
1599 $file->getSha1()
1600 );
1601
1602 $cachedValue = $cache->get( $cacheKey );
1603 if (
1604 $cachedValue
1605 && Hooks::run( 'ValidateExtendedMetadataCache', [ $cachedValue['timestamp'], $file ] )
1606 ) {
1607 $extendedMetadata = $cachedValue['data'];
1608 } else {
1609 $maxCacheTime = ( $file instanceof ForeignAPIFile ) ? 60 * 60 * 12 : 60 * 60 * 24 * 30;
1610 $fileMetadata = $this->getExtendedMetadataFromFile( $file );
1611 $extendedMetadata = $this->getExtendedMetadataFromHook( $file, $fileMetadata, $maxCacheTime );
1612 if ( $this->singleLang ) {
1613 $this->resolveMultilangMetadata( $extendedMetadata );
1614 }
1615 $this->discardMultipleValues( $extendedMetadata );
1616 // Make sure the metadata won't break the API when an XML format is used.
1617 // This is an API-specific function so it would be cleaner to call it from
1618 // outside fetchExtendedMetadata, but this way we don't need to redo the
1619 // computation on a cache hit.
1620 $this->sanitizeArrayForAPI( $extendedMetadata );
1621 $valueToCache = [ 'data' => $extendedMetadata, 'timestamp' => wfTimestampNow() ];
1622 $cache->set( $cacheKey, $valueToCache, $maxCacheTime );
1623 }
1624
1625 return $extendedMetadata;
1626 }
1627
1628 /**
1629 * Get file-based metadata in standardized format.
1630 *
1631 * Note that for a remote file, this might return metadata supplied by extensions.
1632 *
1633 * @param File $file File to use
1634 * @return array [<property name> => ['value' => <value>]], or [] on error
1635 * @since 1.23
1636 */
1637 protected function getExtendedMetadataFromFile( File $file ) {
1638 // If this is a remote file accessed via an API request, we already
1639 // have remote metadata so we just ignore any local one
1640 if ( $file instanceof ForeignAPIFile ) {
1641 // In case of error we pretend no metadata - this will get cached.
1642 // Might or might not be a good idea.
1643 return $file->getExtendedMetadata() ?: [];
1644 }
1645
1646 $uploadDate = wfTimestamp( TS_ISO_8601, $file->getTimestamp() );
1647
1648 $fileMetadata = [
1649 // This is modification time, which is close to "upload" time.
1650 'DateTime' => [
1651 'value' => $uploadDate,
1652 'source' => 'mediawiki-metadata',
1653 ],
1654 ];
1655
1656 $title = $file->getTitle();
1657 if ( $title ) {
1658 $text = $title->getText();
1659 $pos = strrpos( $text, '.' );
1660
1661 if ( $pos ) {
1662 $name = substr( $text, 0, $pos );
1663 } else {
1664 $name = $text;
1665 }
1666
1667 $fileMetadata['ObjectName'] = [
1668 'value' => $name,
1669 'source' => 'mediawiki-metadata',
1670 ];
1671 }
1672
1673 return $fileMetadata;
1674 }
1675
1676 /**
1677 * Get additional metadata from hooks in standardized format.
1678 *
1679 * @param File $file File to use
1680 * @param array $extendedMetadata
1681 * @param int $maxCacheTime Hook handlers might use this parameter to override cache time
1682 *
1683 * @return array [<property name> => ['value' => <value>]], or [] on error
1684 * @since 1.23
1685 */
1686 protected function getExtendedMetadataFromHook( File $file, array $extendedMetadata,
1687 &$maxCacheTime
1688 ) {
1689 Hooks::run( 'GetExtendedMetadata', [
1690 &$extendedMetadata,
1691 $file,
1692 $this->getContext(),
1693 $this->singleLang,
1694 &$maxCacheTime
1695 ] );
1696
1697 $visible = array_flip( self::getVisibleFields() );
1698 foreach ( $extendedMetadata as $key => $value ) {
1699 if ( !isset( $visible[strtolower( $key )] ) ) {
1700 $extendedMetadata[$key]['hidden'] = '';
1701 }
1702 }
1703
1704 return $extendedMetadata;
1705 }
1706
1707 /**
1708 * Turns an XMP-style multilang array into a single value.
1709 * If the value is not a multilang array, it is returned unchanged.
1710 * See mediawiki.org/wiki/Manual:File_metadata_handling#Multi-language_array_format
1711 * @param mixed $value
1712 * @return mixed Value in best language, null if there were no languages at all
1713 * @since 1.23
1714 */
1715 protected function resolveMultilangValue( $value ) {
1716 if (
1717 !is_array( $value )
1718 || !isset( $value['_type'] )
1719 || $value['_type'] != 'lang'
1720 ) {
1721 return $value; // do nothing if not a multilang array
1722 }
1723
1724 // choose the language best matching user or site settings
1725 $priorityLanguages = $this->getPriorityLanguages();
1726 foreach ( $priorityLanguages as $lang ) {
1727 if ( isset( $value[$lang] ) ) {
1728 return $value[$lang];
1729 }
1730 }
1731
1732 // otherwise go with the default language, if set
1733 if ( isset( $value['x-default'] ) ) {
1734 return $value['x-default'];
1735 }
1736
1737 // otherwise just return any one language
1738 unset( $value['_type'] );
1739 if ( !empty( $value ) ) {
1740 return reset( $value );
1741 }
1742
1743 // this should not happen; signal error
1744 return null;
1745 }
1746
1747 /**
1748 * Turns an XMP-style multivalue array into a single value by dropping all but the first
1749 * value. If the value is not a multivalue array (or a multivalue array inside a multilang
1750 * array), it is returned unchanged.
1751 * See mediawiki.org/wiki/Manual:File_metadata_handling#Multi-language_array_format
1752 * @param mixed $value
1753 * @return mixed The value, or the first value if there were multiple ones
1754 * @since 1.25
1755 */
1756 protected function resolveMultivalueValue( $value ) {
1757 if ( !is_array( $value ) ) {
1758 return $value;
1759 } elseif ( isset( $value['_type'] ) && $value['_type'] === 'lang' ) {
1760 // if this is a multilang array, process fields separately
1761 $newValue = [];
1762 foreach ( $value as $k => $v ) {
1763 $newValue[$k] = $this->resolveMultivalueValue( $v );
1764 }
1765 return $newValue;
1766 } else { // _type is 'ul' or 'ol' or missing in which case it defaults to 'ul'
1767 list( $k, $v ) = each( $value );
1768 if ( $k === '_type' ) {
1769 $v = current( $value );
1770 }
1771 return $v;
1772 }
1773 }
1774
1775 /**
1776 * Takes an array returned by the getExtendedMetadata* functions,
1777 * and resolves multi-language values in it.
1778 * @param array $metadata
1779 * @since 1.23
1780 */
1781 protected function resolveMultilangMetadata( &$metadata ) {
1782 if ( !is_array( $metadata ) ) {
1783 return;
1784 }
1785 foreach ( $metadata as &$field ) {
1786 if ( isset( $field['value'] ) ) {
1787 $field['value'] = $this->resolveMultilangValue( $field['value'] );
1788 }
1789 }
1790 }
1791
1792 /**
1793 * Takes an array returned by the getExtendedMetadata* functions,
1794 * and turns all fields into single-valued ones by dropping extra values.
1795 * @param array $metadata
1796 * @since 1.25
1797 */
1798 protected function discardMultipleValues( &$metadata ) {
1799 if ( !is_array( $metadata ) ) {
1800 return;
1801 }
1802 foreach ( $metadata as $key => &$field ) {
1803 if ( $key === 'Software' || $key === 'Contact' ) {
1804 // we skip some fields which have composite values. They are not particularly interesting
1805 // and you can get them via the metadata / commonmetadata APIs anyway.
1806 continue;
1807 }
1808 if ( isset( $field['value'] ) ) {
1809 $field['value'] = $this->resolveMultivalueValue( $field['value'] );
1810 }
1811 }
1812 }
1813
1814 /**
1815 * Makes sure the given array is a valid API response fragment
1816 * @param array $arr
1817 */
1818 protected function sanitizeArrayForAPI( &$arr ) {
1819 if ( !is_array( $arr ) ) {
1820 return;
1821 }
1822
1823 $counter = 1;
1824 foreach ( $arr as $key => &$value ) {
1825 $sanitizedKey = $this->sanitizeKeyForAPI( $key );
1826 if ( $sanitizedKey !== $key ) {
1827 if ( isset( $arr[$sanitizedKey] ) ) {
1828 // Make the sanitized keys hopefully unique.
1829 // To make it definitely unique would be too much effort, given that
1830 // sanitizing is only needed for misformatted metadata anyway, but
1831 // this at least covers the case when $arr is numeric.
1832 $sanitizedKey .= $counter;
1833 ++$counter;
1834 }
1835 $arr[$sanitizedKey] = $arr[$key];
1836 unset( $arr[$key] );
1837 }
1838 if ( is_array( $value ) ) {
1839 $this->sanitizeArrayForAPI( $value );
1840 }
1841 }
1842
1843 // Handle API metadata keys (particularly "_type")
1844 $keys = array_filter( array_keys( $arr ), 'ApiResult::isMetadataKey' );
1845 if ( $keys ) {
1846 ApiResult::setPreserveKeysList( $arr, $keys );
1847 }
1848 }
1849
1850 /**
1851 * Turns a string into a valid API identifier.
1852 * @param string $key
1853 * @return string
1854 * @since 1.23
1855 */
1856 protected function sanitizeKeyForAPI( $key ) {
1857 // drop all characters which are not valid in an XML tag name
1858 // a bunch of non-ASCII letters would be valid but probably won't
1859 // be used so we take the easy way
1860 $key = preg_replace( '/[^a-zA-z0-9_:.-]/', '', $key );
1861 // drop characters which are invalid at the first position
1862 $key = preg_replace( '/^[\d-.]+/', '', $key );
1863
1864 if ( $key == '' ) {
1865 $key = '_';
1866 }
1867
1868 // special case for an internal keyword
1869 if ( $key == '_element' ) {
1870 $key = 'element';
1871 }
1872
1873 return $key;
1874 }
1875
1876 /**
1877 * Returns a list of languages (first is best) to use when formatting multilang fields,
1878 * based on user and site preferences.
1879 * @return array
1880 * @since 1.23
1881 */
1882 protected function getPriorityLanguages() {
1883 $priorityLanguages =
1884 Language::getFallbacksIncludingSiteLanguage( $this->getLanguage()->getCode() );
1885 $priorityLanguages = array_merge(
1886 (array)$this->getLanguage()->getCode(),
1887 $priorityLanguages[0],
1888 $priorityLanguages[1]
1889 );
1890
1891 return $priorityLanguages;
1892 }
1893 }