MediaWiki REL1_39
FormatMetadata.php
Go to the documentation of this file.
1<?php
28use MediaWiki\HookContainer\ProtectedHookAccessorTrait;
31use Wikimedia\Timestamp\TimestampException;
32
55 use ProtectedHookAccessorTrait;
56
62 protected $singleLang = false;
63
70 public function setSingleLanguage( $val ) {
71 $this->singleLang = $val;
72 }
73
87 public static function getFormattedData( $tags, $context = false ) {
88 $obj = new FormatMetadata;
89 if ( $context ) {
90 $obj->setContext( $context );
91 }
92
93 return $obj->makeFormattedData( $tags );
94 }
95
107 public function makeFormattedData( $tags ) {
108 $resolutionunit = !isset( $tags['ResolutionUnit'] ) || $tags['ResolutionUnit'] == 2 ? 2 : 3;
109 unset( $tags['ResolutionUnit'] );
110
111 // Ignore these complex values
112 unset( $tags['HasExtendedXMP'] );
113 unset( $tags['AuthorsPosition'] );
114 unset( $tags['LocationCreated'] );
115 unset( $tags['LocationShown'] );
116 unset( $tags['GPSAltitudeRef'] );
117
118 foreach ( $tags as $tag => &$vals ) {
119 // This seems ugly to wrap non-array's in an array just to unwrap again,
120 // especially when most of the time it is not an array
121 if ( !is_array( $vals ) ) {
122 $vals = [ $vals ];
123 }
124
125 // _type is a special value to say what array type
126 if ( isset( $vals['_type'] ) ) {
127 $type = $vals['_type'];
128 unset( $vals['_type'] );
129 } else {
130 $type = 'ul'; // default unordered list.
131 }
132
133 // _formatted is a special value to indicate the subclass
134 // already handled & formatted this tag as wikitext
135 if ( isset( $tags[$tag]['_formatted'] ) ) {
136 $tags[$tag] = $this->flattenArrayReal(
137 $tags[$tag]['_formatted'], $type
138 );
139 continue;
140 }
141
142 // This is done differently as the tag is an array.
143 if ( $tag == 'GPSTimeStamp' && count( $vals ) === 3 ) {
144 // hour min sec array
145
146 $h = explode( '/', $vals[0], 2 );
147 $m = explode( '/', $vals[1], 2 );
148 $s = explode( '/', $vals[2], 2 );
149
150 // this should already be validated
151 // when loaded from file, but it could
152 // come from a foreign repo, so be
153 // paranoid.
154 if ( !isset( $h[1] )
155 || !isset( $m[1] )
156 || !isset( $s[1] )
157 || $h[1] == 0
158 || $m[1] == 0
159 || $s[1] == 0
160 ) {
161 continue;
162 }
163 $vals = str_pad( (string)( (int)$h[0] / (int)$h[1] ), 2, '0', STR_PAD_LEFT )
164 . ':' . str_pad( (string)( (int)$m[0] / (int)$m[1] ), 2, '0', STR_PAD_LEFT )
165 . ':' . str_pad( (string)( (int)$s[0] / (int)$s[1] ), 2, '0', STR_PAD_LEFT );
166
167 try {
168 $time = wfTimestamp( TS_MW, '1971:01:01 ' . $vals );
169 // the 1971:01:01 is just a placeholder, and not shown to user.
170 if ( $time && intval( $time ) > 0 ) {
171 $vals = $this->getLanguage()->time( $time );
172 }
173 } catch ( TimestampException $e ) {
174 // This shouldn't happen, but we've seen bad formats
175 // such as 4-digit seconds in the wild.
176 // leave $vals as-is
177 }
178 continue;
179 }
180
181 // The contact info is a multi-valued field
182 // instead of the other props which are single
183 // valued (mostly) so handle as a special case.
184 if ( $tag === 'Contact' || $tag === 'CreatorContactInfo' ) {
185 $vals = $this->collapseContactInfo( $vals );
186 continue;
187 }
188
189 foreach ( $vals as &$val ) {
190 switch ( $tag ) {
191 case 'Compression':
192 switch ( $val ) {
193 case 1:
194 case 2:
195 case 3:
196 case 4:
197 case 5:
198 case 6:
199 case 7:
200 case 8:
201 case 32773:
202 case 32946:
203 case 34712:
204 $val = $this->exifMsg( $tag, $val );
205 break;
206 default:
207 /* If not recognized, display as is. */
208 $val = $this->literal( $val );
209 break;
210 }
211 break;
212
213 case 'PhotometricInterpretation':
214 switch ( $val ) {
215 case 0:
216 case 1:
217 case 2:
218 case 3:
219 case 4:
220 case 5:
221 case 6:
222 case 8:
223 case 9:
224 case 10:
225 case 32803:
226 case 34892:
227 $val = $this->exifMsg( $tag, $val );
228 break;
229 default:
230 /* If not recognized, display as is. */
231 $val = $this->literal( $val );
232 break;
233 }
234 break;
235
236 case 'Orientation':
237 switch ( $val ) {
238 case 1:
239 case 2:
240 case 3:
241 case 4:
242 case 5:
243 case 6:
244 case 7:
245 case 8:
246 $val = $this->exifMsg( $tag, $val );
247 break;
248 default:
249 /* If not recognized, display as is. */
250 $val = $this->literal( $val );
251 break;
252 }
253 break;
254
255 case 'PlanarConfiguration':
256 switch ( $val ) {
257 case 1:
258 case 2:
259 $val = $this->exifMsg( $tag, $val );
260 break;
261 default:
262 /* If not recognized, display as is. */
263 $val = $this->literal( $val );
264 break;
265 }
266 break;
267
268 // TODO: YCbCrSubSampling
269 case 'YCbCrPositioning':
270 switch ( $val ) {
271 case 1:
272 case 2:
273 $val = $this->exifMsg( $tag, $val );
274 break;
275 default:
276 /* If not recognized, display as is. */
277 $val = $this->literal( $val );
278 break;
279 }
280 break;
281
282 case 'XResolution':
283 case 'YResolution':
284 switch ( $resolutionunit ) {
285 case 2:
286 $val = $this->exifMsg( 'XYResolution', 'i', $this->formatNum( $val ) );
287 break;
288 case 3:
289 $val = $this->exifMsg( 'XYResolution', 'c', $this->formatNum( $val ) );
290 break;
291 default:
292 /* If not recognized, display as is. */
293 $val = $this->literal( $val );
294 break;
295 }
296 break;
297
298 // TODO: YCbCrCoefficients #p27 (see annex E)
299 case 'ExifVersion':
300 // PHP likes to be the odd one out with casing of FlashPixVersion;
301 // https://www.exif.org/Exif2-2.PDF#page=32 and
302 // https://www.digitalgalen.net/Documents/External/XMP/XMPSpecificationPart2.pdf#page=51
303 // both use FlashpixVersion. However, since at least 2002, PHP has used FlashPixVersion at
304 // https://github.com/php/php-src/blame/master/ext/exif/exif.c#L725
305 case 'FlashPixVersion':
306 // But we can still get the correct casing from
307 // Wikimedia\XMPReader on PDFs
308 case 'FlashpixVersion':
309 $val = $this->literal( (int)$val / 100 );
310 break;
311
312 case 'ColorSpace':
313 switch ( $val ) {
314 case 1:
315 case 65535:
316 $val = $this->exifMsg( $tag, $val );
317 break;
318 default:
319 /* If not recognized, display as is. */
320 $val = $this->literal( $val );
321 break;
322 }
323 break;
324
325 case 'ComponentsConfiguration':
326 switch ( $val ) {
327 case 0:
328 case 1:
329 case 2:
330 case 3:
331 case 4:
332 case 5:
333 case 6:
334 $val = $this->exifMsg( $tag, $val );
335 break;
336 default:
337 /* If not recognized, display as is. */
338 $val = $this->literal( $val );
339 break;
340 }
341 break;
342
343 case 'DateTime':
344 case 'DateTimeOriginal':
345 case 'DateTimeDigitized':
346 case 'DateTimeReleased':
347 case 'DateTimeExpires':
348 case 'GPSDateStamp':
349 case 'dc-date':
350 case 'DateTimeMetadata':
351 case 'FirstPhotoDate':
352 case 'LastPhotoDate':
353 if ( $val === null ) {
354 // T384879 - we don't need to call literal to turn this into a string, but
355 // we might as well call it for consistency and future proofing of the default value
356 $val = $this->literal( $val );
357 break;
358 }
359
360 if ( $val == '0000:00:00 00:00:00' || $val == ' : : : : ' ) {
361 $val = $this->msg( 'exif-unknowndate' )->text();
362 break;
363 } elseif ( preg_match(
364 '/^(?:\d{4}):(?:\d\d):(?:\d\d) (?:\d\d):(?:\d\d):(?:\d\d)$/D',
365 $val
366 ) ) {
367 // Full date.
368 $time = wfTimestamp( TS_MW, $val );
369 if ( $time && intval( $time ) > 0 ) {
370 $val = $this->getLanguage()->timeanddate( $time );
371 break;
372 }
373 } elseif ( preg_match( '/^(?:\d{4}):(?:\d\d):(?:\d\d) (?:\d\d):(?:\d\d)$/D', $val ) ) {
374 // No second field. Still format the same
375 // since timeanddate doesn't include seconds anyways,
376 // but second still available in api
377 $time = wfTimestamp( TS_MW, $val . ':00' );
378 if ( $time && intval( $time ) > 0 ) {
379 $val = $this->getLanguage()->timeanddate( $time );
380 break;
381 }
382 } elseif ( preg_match( '/^(?:\d{4}):(?:\d\d):(?:\d\d)$/D', $val ) ) {
383 // If only the date but not the time is filled in.
384 $time = wfTimestamp( TS_MW, substr( $val, 0, 4 )
385 . substr( $val, 5, 2 )
386 . substr( $val, 8, 2 )
387 . '000000' );
388 if ( $time && intval( $time ) > 0 ) {
389 $val = $this->getLanguage()->date( $time );
390 break;
391 }
392 }
393 // else it will just output $val without formatting it.
394 $val = $this->literal( $val );
395 break;
396
397 case 'ExposureProgram':
398 switch ( $val ) {
399 case 0:
400 case 1:
401 case 2:
402 case 3:
403 case 4:
404 case 5:
405 case 6:
406 case 7:
407 case 8:
408 $val = $this->exifMsg( $tag, $val );
409 break;
410 default:
411 /* If not recognized, display as is. */
412 $val = $this->literal( $val );
413 break;
414 }
415 break;
416
417 case 'SubjectDistance':
418 $val = $this->exifMsg( $tag, '', $this->formatNum( $val ) );
419 break;
420
421 case 'MeteringMode':
422 switch ( $val ) {
423 case 0:
424 case 1:
425 case 2:
426 case 3:
427 case 4:
428 case 5:
429 case 6:
430 case 7:
431 case 255:
432 $val = $this->exifMsg( $tag, $val );
433 break;
434 default:
435 /* If not recognized, display as is. */
436 $val = $this->literal( $val );
437 break;
438 }
439 break;
440
441 case 'LightSource':
442 switch ( $val ) {
443 case 0:
444 case 1:
445 case 2:
446 case 3:
447 case 4:
448 case 9:
449 case 10:
450 case 11:
451 case 12:
452 case 13:
453 case 14:
454 case 15:
455 case 17:
456 case 18:
457 case 19:
458 case 20:
459 case 21:
460 case 22:
461 case 23:
462 case 24:
463 case 255:
464 $val = $this->exifMsg( $tag, $val );
465 break;
466 default:
467 /* If not recognized, display as is. */
468 $val = $this->literal( $val );
469 break;
470 }
471 break;
472
473 case 'Flash':
474 $flashDecode = [
475 'fired' => $val & 0b00000001,
476 'return' => ( $val & 0b00000110 ) >> 1,
477 'mode' => ( $val & 0b00011000 ) >> 3,
478 'function' => ( $val & 0b00100000 ) >> 5,
479 'redeye' => ( $val & 0b01000000 ) >> 6,
480 // 'reserved' => ( $val & 0b10000000 ) >> 7,
481 ];
482 $flashMsgs = [];
483 # We do not need to handle unknown values since all are used.
484 foreach ( $flashDecode as $subTag => $subValue ) {
485 # We do not need any message for zeroed values.
486 if ( $subTag != 'fired' && $subValue == 0 ) {
487 continue;
488 }
489 $fullTag = $tag . '-' . $subTag;
490 $flashMsgs[] = $this->exifMsg( $fullTag, $subValue );
491 }
492 $val = $this->getLanguage()->commaList( $flashMsgs );
493 break;
494
495 case 'FocalPlaneResolutionUnit':
496 switch ( $val ) {
497 case 2:
498 $val = $this->exifMsg( $tag, $val );
499 break;
500 default:
501 /* If not recognized, display as is. */
502 $val = $this->literal( $val );
503 break;
504 }
505 break;
506
507 case 'SensingMethod':
508 switch ( $val ) {
509 case 1:
510 case 2:
511 case 3:
512 case 4:
513 case 5:
514 case 7:
515 case 8:
516 $val = $this->exifMsg( $tag, $val );
517 break;
518 default:
519 /* If not recognized, display as is. */
520 $val = $this->literal( $val );
521 break;
522 }
523 break;
524
525 case 'FileSource':
526 switch ( $val ) {
527 case 3:
528 $val = $this->exifMsg( $tag, $val );
529 break;
530 default:
531 /* If not recognized, display as is. */
532 $val = $this->literal( $val );
533 break;
534 }
535 break;
536
537 case 'SceneType':
538 switch ( $val ) {
539 case 1:
540 $val = $this->exifMsg( $tag, $val );
541 break;
542 default:
543 /* If not recognized, display as is. */
544 $val = $this->literal( $val );
545 break;
546 }
547 break;
548
549 case 'CustomRendered':
550 switch ( $val ) {
551 case 0: /* normal */
552 case 1: /* custom */
553 /* The following are unofficial Apple additions */
554 case 2: /* HDR (no original saved) */
555 case 3: /* HDR (original saved) */
556 case 4: /* Original (for HDR) */
557 /* Yes 5 is not present ;) */
558 case 6: /* Panorama */
559 case 7: /* Portrait HDR */
560 case 8: /* Portrait */
561 $val = $this->exifMsg( $tag, $val );
562 break;
563 default:
564 /* If not recognized, display as is. */
565 $val = $this->literal( $val );
566 break;
567 }
568 break;
569
570 case 'ExposureMode':
571 switch ( $val ) {
572 case 0:
573 case 1:
574 case 2:
575 $val = $this->exifMsg( $tag, $val );
576 break;
577 default:
578 /* If not recognized, display as is. */
579 break;
580 }
581 break;
582
583 case 'WhiteBalance':
584 switch ( $val ) {
585 case 0:
586 case 1:
587 $val = $this->exifMsg( $tag, $val );
588 break;
589 default:
590 /* If not recognized, display as is. */
591 $val = $this->literal( $val );
592 break;
593 }
594 break;
595
596 case 'SceneCaptureType':
597 switch ( $val ) {
598 case 0:
599 case 1:
600 case 2:
601 case 3:
602 $val = $this->exifMsg( $tag, $val );
603 break;
604 default:
605 /* If not recognized, display as is. */
606 $val = $this->literal( $val );
607 break;
608 }
609 break;
610
611 case 'GainControl':
612 switch ( $val ) {
613 case 0:
614 case 1:
615 case 2:
616 case 3:
617 case 4:
618 $val = $this->exifMsg( $tag, $val );
619 break;
620 default:
621 /* If not recognized, display as is. */
622 $val = $this->literal( $val );
623 break;
624 }
625 break;
626
627 case 'Contrast':
628 switch ( $val ) {
629 case 0:
630 case 1:
631 case 2:
632 $val = $this->exifMsg( $tag, $val );
633 break;
634 default:
635 /* If not recognized, display as is. */
636 $val = $this->literal( $val );
637 break;
638 }
639 break;
640
641 case 'Saturation':
642 switch ( $val ) {
643 case 0:
644 case 1:
645 case 2:
646 $val = $this->exifMsg( $tag, $val );
647 break;
648 default:
649 /* If not recognized, display as is. */
650 $val = $this->literal( $val );
651 break;
652 }
653 break;
654
655 case 'Sharpness':
656 switch ( $val ) {
657 case 0:
658 case 1:
659 case 2:
660 $val = $this->exifMsg( $tag, $val );
661 break;
662 default:
663 /* If not recognized, display as is. */
664 $val = $this->literal( $val );
665 break;
666 }
667 break;
668
669 case 'SubjectDistanceRange':
670 switch ( $val ) {
671 case 0:
672 case 1:
673 case 2:
674 case 3:
675 $val = $this->exifMsg( $tag, $val );
676 break;
677 default:
678 /* If not recognized, display as is. */
679 $val = $this->literal( $val );
680 break;
681 }
682 break;
683
684 // The GPS...Ref values are kept for compatibility, probably won't be reached.
685 case 'GPSLatitudeRef':
686 case 'GPSDestLatitudeRef':
687 switch ( $val ) {
688 case 'N':
689 case 'S':
690 $val = $this->exifMsg( 'GPSLatitude', $val );
691 break;
692 default:
693 /* If not recognized, display as is. */
694 $val = $this->literal( $val );
695 break;
696 }
697 break;
698
699 case 'GPSLongitudeRef':
700 case 'GPSDestLongitudeRef':
701 switch ( $val ) {
702 case 'E':
703 case 'W':
704 $val = $this->exifMsg( 'GPSLongitude', $val );
705 break;
706 default:
707 /* If not recognized, display as is. */
708 $val = $this->literal( $val );
709 break;
710 }
711 break;
712
713 case 'GPSAltitude':
714 if ( $val < 0 ) {
715 $val = $this->exifMsg( 'GPSAltitude', 'below-sealevel', $this->formatNum( -$val, 3 ) );
716 } else {
717 $val = $this->exifMsg( 'GPSAltitude', 'above-sealevel', $this->formatNum( $val, 3 ) );
718 }
719 break;
720
721 case 'GPSStatus':
722 switch ( $val ) {
723 case 'A':
724 case 'V':
725 $val = $this->exifMsg( $tag, $val );
726 break;
727 default:
728 /* If not recognized, display as is. */
729 $val = $this->literal( $val );
730 break;
731 }
732 break;
733
734 case 'GPSMeasureMode':
735 switch ( $val ) {
736 case 2:
737 case 3:
738 $val = $this->exifMsg( $tag, $val );
739 break;
740 default:
741 /* If not recognized, display as is. */
742 $val = $this->literal( $val );
743 break;
744 }
745 break;
746
747 case 'GPSTrackRef':
748 case 'GPSImgDirectionRef':
749 case 'GPSDestBearingRef':
750 switch ( $val ) {
751 case 'T':
752 case 'M':
753 $val = $this->exifMsg( 'GPSDirection', $val );
754 break;
755 default:
756 /* If not recognized, display as is. */
757 $val = $this->literal( $val );
758 break;
759 }
760 break;
761
762 case 'GPSLatitude':
763 case 'GPSDestLatitude':
764 $val = $this->formatCoords( $val, 'latitude' );
765 break;
766 case 'GPSLongitude':
767 case 'GPSDestLongitude':
768 $val = $this->formatCoords( $val, 'longitude' );
769 break;
770
771 case 'GPSSpeedRef':
772 switch ( $val ) {
773 case 'K':
774 case 'M':
775 case 'N':
776 $val = $this->exifMsg( 'GPSSpeed', $val );
777 break;
778 default:
779 /* If not recognized, display as is. */
780 $val = $this->literal( $val );
781 break;
782 }
783 break;
784
785 case 'GPSDestDistanceRef':
786 switch ( $val ) {
787 case 'K':
788 case 'M':
789 case 'N':
790 $val = $this->exifMsg( 'GPSDestDistance', $val );
791 break;
792 default:
793 /* If not recognized, display as is. */
794 $val = $this->literal( $val );
795 break;
796 }
797 break;
798
799 case 'GPSDOP':
800 // See https://en.wikipedia.org/wiki/Dilution_of_precision_(GPS)
801 if ( $val <= 2 ) {
802 $val = $this->exifMsg( $tag, 'excellent', $this->formatNum( $val ) );
803 } elseif ( $val <= 5 ) {
804 $val = $this->exifMsg( $tag, 'good', $this->formatNum( $val ) );
805 } elseif ( $val <= 10 ) {
806 $val = $this->exifMsg( $tag, 'moderate', $this->formatNum( $val ) );
807 } elseif ( $val <= 20 ) {
808 $val = $this->exifMsg( $tag, 'fair', $this->formatNum( $val ) );
809 } else {
810 $val = $this->exifMsg( $tag, 'poor', $this->formatNum( $val ) );
811 }
812 break;
813
814 // This is not in the Exif standard, just a special
815 // case for our purposes which enables wikis to wikify
816 // the make, model and software name to link to their articles.
817 case 'Make':
818 case 'Model':
819 $val = $this->exifMsg( $tag, '', $this->literal( $val ) );
820 break;
821
822 case 'Software':
823 if ( is_array( $val ) ) {
824 if ( count( $val ) > 1 ) {
825 // if its a software, version array.
826 $val = $this->msg(
827 'exif-software-version-value',
828 $this->literal( $val[0] ),
829 $this->literal( $val[1] )
830 )->text();
831 } else {
832 // https://phabricator.wikimedia.org/T178130
833 $val = $this->exifMsg( $tag, '', $this->literal( $val[0] ) );
834 }
835 } else {
836 $val = $this->exifMsg( $tag, '', $this->literal( $val ) );
837 }
838 break;
839
840 case 'ExposureTime':
841 // Show the pretty fraction as well as decimal version
842 $val = $this->msg( 'exif-exposuretime-format',
843 $this->formatFraction( $val ), $this->formatNum( $val ) )->text();
844 break;
845 case 'ISOSpeedRatings':
846 // If its = 65535 that means its at the
847 // limit of the size of Exif::short and
848 // is really higher.
849 if ( $val == '65535' ) {
850 $val = $this->exifMsg( $tag, 'overflow' );
851 } else {
852 $val = $this->formatNum( $val );
853 }
854 break;
855 case 'FNumber':
856 $val = $this->msg( 'exif-fnumber-format',
857 $this->formatNum( $val ) )->text();
858 break;
859
860 case 'FocalLength':
861 case 'FocalLengthIn35mmFilm':
862 $val = $this->msg( 'exif-focallength-format',
863 $this->formatNum( $val ) )->text();
864 break;
865
866 case 'MaxApertureValue':
867 if ( strpos( $val, '/' ) !== false ) {
868 // need to expand this earlier to calculate fNumber
869 list( $n, $d ) = explode( '/', $val, 2 );
870 if ( is_numeric( $n ) && is_numeric( $d ) ) {
871 $val = (int)$n / (int)$d;
872 }
873 }
874 if ( is_numeric( $val ) ) {
875 $fNumber = 2 ** ( $val / 2 );
876 if ( is_finite( $fNumber ) ) {
877 $val = $this->msg( 'exif-maxaperturevalue-value',
878 $this->formatNum( $val ),
879 $this->formatNum( $fNumber, 2 )
880 )->text();
881 break;
882 }
883 }
884 $val = $this->literal( $val );
885 break;
886
887 case 'iimCategory':
888 switch ( strtolower( $val ) ) {
889 // See pg 29 of IPTC photo
890 // metadata standard.
891 case 'ace':
892 case 'clj':
893 case 'dis':
894 case 'fin':
895 case 'edu':
896 case 'evn':
897 case 'hth':
898 case 'hum':
899 case 'lab':
900 case 'lif':
901 case 'pol':
902 case 'rel':
903 case 'sci':
904 case 'soi':
905 case 'spo':
906 case 'war':
907 case 'wea':
908 $val = $this->exifMsg(
909 'iimcategory',
910 $val
911 );
912 break;
913 default:
914 $val = $this->literal( $val );
915 }
916 break;
917 case 'SubjectNewsCode':
918 // Essentially like iimCategory.
919 // 8 (numeric) digit hierarchical
920 // classification. We decode the
921 // first 2 digits, which provide
922 // a broad category.
923 $val = $this->convertNewsCode( $val );
924 break;
925 case 'Urgency':
926 // 1-8 with 1 being highest, 5 normal
927 // 0 is reserved, and 9 is 'user-defined'.
928 $urgency = '';
929 if ( $val == 0 || $val == 9 ) {
930 $urgency = 'other';
931 } elseif ( $val < 5 && $val > 1 ) {
932 $urgency = 'high';
933 } elseif ( $val == 5 ) {
934 $urgency = 'normal';
935 } elseif ( $val <= 8 && $val > 5 ) {
936 $urgency = 'low';
937 }
938
939 if ( $urgency !== '' ) {
940 $val = $this->exifMsg( 'urgency',
941 $urgency, $this->literal( $val )
942 );
943 } else {
944 $val = $this->literal( $val );
945 }
946 break;
947
948 // Things that have a unit of pixels.
949 case 'OriginalImageHeight':
950 case 'OriginalImageWidth':
951 case 'PixelXDimension':
952 case 'PixelYDimension':
953 case 'ImageWidth':
954 case 'ImageLength':
955 $val = $this->formatNum( $val ) . ' ' . $this->msg( 'unit-pixel' )->text();
956 break;
957
958 // Do not transform fields with pure text.
959 // For some languages the formatNum()
960 // conversion results to wrong output like
961 // foo,bar@example,com or fooÙ«bar@exampleÙ«com.
962 // Also some 'numeric' things like Scene codes
963 // are included here as we really don't want
964 // commas inserted.
965 case 'ImageDescription':
966 case 'UserComment':
967 case 'Artist':
968 case 'Copyright':
969 case 'RelatedSoundFile':
970 case 'ImageUniqueID':
971 case 'SpectralSensitivity':
972 case 'GPSSatellites':
973 case 'GPSVersionID':
974 case 'GPSMapDatum':
975 case 'Keywords':
976 case 'WorldRegionDest':
977 case 'CountryDest':
978 case 'CountryCodeDest':
979 case 'ProvinceOrStateDest':
980 case 'CityDest':
981 case 'SublocationDest':
982 case 'WorldRegionCreated':
983 case 'CountryCreated':
984 case 'CountryCodeCreated':
985 case 'ProvinceOrStateCreated':
986 case 'CityCreated':
987 case 'SublocationCreated':
988 case 'ObjectName':
989 case 'SpecialInstructions':
990 case 'Headline':
991 case 'Credit':
992 case 'Source':
993 case 'EditStatus':
994 case 'FixtureIdentifier':
995 case 'LocationDest':
996 case 'LocationDestCode':
997 case 'Writer':
998 case 'JPEGFileComment':
999 case 'iimSupplementalCategory':
1000 case 'OriginalTransmissionRef':
1001 case 'Identifier':
1002 case 'dc-contributor':
1003 case 'dc-coverage':
1004 case 'dc-publisher':
1005 case 'dc-relation':
1006 case 'dc-rights':
1007 case 'dc-source':
1008 case 'dc-type':
1009 case 'Lens':
1010 case 'SerialNumber':
1011 case 'CameraOwnerName':
1012 case 'Label':
1013 case 'Nickname':
1014 case 'RightsCertificate':
1015 case 'CopyrightOwner':
1016 case 'UsageTerms':
1017 case 'WebStatement':
1018 case 'OriginalDocumentID':
1019 case 'LicenseUrl':
1020 case 'MorePermissionsUrl':
1021 case 'AttributionUrl':
1022 case 'PreferredAttributionName':
1023 case 'PNGFileComment':
1024 case 'Disclaimer':
1025 case 'ContentWarning':
1026 case 'GIFFileComment':
1027 case 'SceneCode':
1028 case 'IntellectualGenre':
1029 case 'Event':
1030 case 'OrganisationInImage':
1031 case 'PersonInImage':
1032 case 'CaptureSoftware':
1033 case 'GPSAreaInformation':
1034 case 'GPSProcessingMethod':
1035 case 'StitchingSoftware':
1036 case 'SubSecTime':
1037 case 'SubSecTimeOriginal':
1038 case 'SubSecTimeDigitized':
1039 $val = $this->literal( $val );
1040 break;
1041
1042 case 'ProjectionType':
1043 switch ( $val ) {
1044 case 'equirectangular':
1045 $val = $this->exifMsg( $tag, $val );
1046 break;
1047 default:
1048 $val = $this->literal( $val );
1049 break;
1050 }
1051 break;
1052 case 'ObjectCycle':
1053 switch ( $val ) {
1054 case 'a':
1055 case 'p':
1056 case 'b':
1057 $val = $this->exifMsg( $tag, $val );
1058 break;
1059 default:
1060 $val = $this->literal( $val );
1061 break;
1062 }
1063 break;
1064 case 'Copyrighted':
1065 case 'UsePanoramaViewer':
1066 case 'ExposureLockUsed':
1067 switch ( $val ) {
1068 case 'True':
1069 case 'False':
1070 $val = $this->exifMsg( $tag, $val );
1071 break;
1072 default:
1073 $val = $this->literal( $val );
1074 break;
1075 }
1076 break;
1077 case 'Rating':
1078 if ( $val == '-1' ) {
1079 $val = $this->exifMsg( $tag, 'rejected' );
1080 } else {
1081 $val = $this->formatNum( $val );
1082 }
1083 break;
1084
1085 case 'LanguageCode':
1086 $lang = MediaWikiServices::getInstance()
1087 ->getLanguageNameUtils()
1088 ->getLanguageName( strtolower( $val ), $this->getLanguage()->getCode() );
1089 $val = $this->literal( $lang ?: $val );
1090 break;
1091
1092 default:
1093 $val = $this->formatNum( $val, false, $tag );
1094 break;
1095 }
1096 }
1097 // End formatting values, start flattening arrays.
1098 $vals = $this->flattenArrayReal( $vals, $type );
1099 }
1100
1101 return $tags;
1102 }
1103
1122 public static function flattenArrayContentLang( $vals, $type = 'ul',
1123 $noHtml = false, $context = false
1124 ) {
1125 wfDeprecated( __METHOD__, '1.36' );
1126 // Allow $noHtml to be omitted.
1127 if ( $noHtml instanceof IContextSource ) {
1128 $context = $noHtml;
1129 $noHtml = false;
1130 }
1131 $obj = new FormatMetadata;
1132 if ( $context ) {
1133 $obj->setContext( $context );
1134 }
1135 $context = new DerivativeContext( $obj->getContext() );
1136 $context->setLanguage( MediaWikiServices::getInstance()->getContentLanguage() );
1137 $obj->setContext( $context );
1138
1139 return $obj->flattenArrayReal( $vals, $type, $noHtml );
1140 }
1141
1159 public function flattenArrayReal( $vals, $type = 'ul', $noHtml = false ) {
1160 if ( !is_array( $vals ) ) {
1161 return $vals; // do nothing if not an array;
1162 }
1163
1164 if ( isset( $vals['_type'] ) ) {
1165 $type = $vals['_type'];
1166 unset( $vals['_type'] );
1167 }
1168
1169 if ( count( $vals ) === 1 && $type !== 'lang' && isset( $vals[0] ) ) {
1170 return $vals[0];
1171 } elseif ( count( $vals ) === 0 ) {
1172 wfDebug( __METHOD__ . " metadata array with 0 elements!" );
1173
1174 return ""; // paranoia. This should never happen
1175 } else {
1176 // Check if $vals contains nested arrays
1177 $containsNestedArrays = in_array( true, array_map( 'is_array', $vals ), true );
1178 if ( $containsNestedArrays ) {
1179 wfLogWarning( __METHOD__ . ': Invalid $vals, contains nested arrays: ' . json_encode( $vals ) );
1180 }
1181
1182 /* @todo FIXME: This should hide some of the list entries if there are
1183 * say more than four. Especially if a field is translated into 20
1184 * languages, we don't want to show them all by default
1185 */
1186 switch ( $type ) {
1187 case 'lang':
1188 // Display default, followed by ContentLanguage,
1189 // followed by the rest in no particular order.
1190
1191 // Todo: hide some items if really long list.
1192
1193 $content = '';
1194
1195 $priorityLanguages = $this->getPriorityLanguages();
1196 $defaultItem = false;
1197 $defaultLang = false;
1198
1199 // If default is set, save it for later,
1200 // as we don't know if it's equal to one of the lang codes.
1201 // (In xmp you specify the language for a default property by having
1202 // both a default prop, and one in the language that are identical)
1203 if ( isset( $vals['x-default'] ) ) {
1204 $defaultItem = $vals['x-default'];
1205 unset( $vals['x-default'] );
1206 }
1207 foreach ( $priorityLanguages as $pLang ) {
1208 if ( isset( $vals[$pLang] ) ) {
1209 $isDefault = false;
1210 if ( $vals[$pLang] === $defaultItem ) {
1211 $defaultItem = false;
1212 $isDefault = true;
1213 }
1214 $content .= $this->langItem( $vals[$pLang], $pLang, $isDefault, $noHtml );
1215
1216 unset( $vals[$pLang] );
1217
1218 if ( $this->singleLang ) {
1219 return Html::rawElement( 'span', [ 'lang' => $pLang ], $vals[$pLang] );
1220 }
1221 }
1222 }
1223
1224 // Now do the rest.
1225 foreach ( $vals as $lang => $item ) {
1226 if ( $item === $defaultItem ) {
1227 $defaultLang = $lang;
1228 continue;
1229 }
1230 $content .= $this->langItem( $item, $lang, false, $noHtml );
1231 if ( $this->singleLang ) {
1232 return Html::rawElement( 'span', [ 'lang' => $lang ], $item );
1233 }
1234 }
1235 if ( $defaultItem !== false ) {
1236 $content = $this->langItem( $defaultItem, $defaultLang, true, $noHtml ) . $content;
1237 if ( $this->singleLang ) {
1238 return $defaultItem;
1239 }
1240 }
1241 if ( $noHtml ) {
1242 return $content;
1243 }
1244
1245 return '<ul class="metadata-langlist">' . $content . '</ul>';
1246 case 'ol':
1247 if ( $noHtml ) {
1248 return "\n#" . implode( "\n#", $vals );
1249 }
1250
1251 return "<ol><li>" . implode( "</li>\n<li>", $vals ) . '</li></ol>';
1252 case 'ul':
1253 default:
1254 if ( $noHtml ) {
1255 return "\n*" . implode( "\n*", $vals );
1256 }
1257
1258 return "<ul><li>" . implode( "</li>\n<li>", $vals ) . '</li></ul>';
1259 }
1260 }
1261 }
1262
1273 private function langItem( $value, $lang, $default = false, $noHtml = false ) {
1274 if ( $lang === false && $default === false ) {
1275 throw new MWException( '$lang and $default cannot both be false.' );
1276 }
1277
1278 if ( $noHtml ) {
1279 $wrappedValue = $this->literal( $value );
1280 } else {
1281 $wrappedValue = '<span class="mw-metadata-lang-value">' . $this->literal( $value ) . '</span>';
1282 }
1283
1284 if ( $lang === false ) {
1285 $msg = $this->msg( 'metadata-langitem-default', $wrappedValue );
1286 if ( $noHtml ) {
1287 return $msg->text() . "\n\n";
1288 } /* else */
1289
1290 return '<li class="mw-metadata-lang-default">' . $msg->text() . "</li>\n";
1291 }
1292
1293 $lowLang = strtolower( $lang );
1294 $languageNameUtils = MediaWikiServices::getInstance()->getLanguageNameUtils();
1295 $langName = $languageNameUtils->getLanguageName( $lowLang );
1296 if ( $langName === '' ) {
1297 // try just the base language name. (aka en-US -> en ).
1298 $langPrefix = explode( '-', $lowLang, 2 )[0];
1299 $langName = $languageNameUtils->getLanguageName( $langPrefix );
1300 if ( $langName === '' ) {
1301 // give up.
1302 $langName = $lang;
1303 }
1304 }
1305 // else we have a language specified
1306
1307 $msg = $this->msg( 'metadata-langitem', $wrappedValue, $langName, $lang );
1308 if ( $noHtml ) {
1309 return '*' . $msg->text();
1310 } /* else: */
1311
1312 $item = '<li class="mw-metadata-lang-code-' . $lang;
1313 if ( $default ) {
1314 $item .= ' mw-metadata-lang-default';
1315 }
1316 $item .= '" lang="' . $lang . '">';
1317 $item .= $msg->text();
1318 $item .= "</li>\n";
1319
1320 return $item;
1321 }
1322
1330 protected function literal( $val ): string {
1331 if ( $val === null ) {
1332 return '';
1333 }
1334 // T266707: historically this has used htmlspecialchars to protect
1335 // the string contents, but it should probably be changed to use
1336 // wfEscapeWikitext() instead -- however, "we still want to auto-link
1337 // urls" so wfEscapeWikitext isn't *quite* right...
1338 return htmlspecialchars( $val );
1339 }
1340
1350 private function exifMsg( $tag, $val, $arg = null, $arg2 = null ) {
1351 if ( $val === '' ) {
1352 $val = 'value';
1353 }
1354
1355 return $this->msg(
1356 MediaWikiServices::getInstance()->getContentLanguage()->lc( "exif-$tag-$val" ),
1357 $arg,
1358 $arg2
1359 )->text();
1360 }
1361
1371 private function formatNum( $num, $round = false, $tagName = null ) {
1372 $m = [];
1373 if ( is_array( $num ) ) {
1374 $out = [];
1375 foreach ( $num as $number ) {
1376 $out[] = $this->formatNum( $number, $round, $tagName );
1377 }
1378
1379 return $this->getLanguage()->commaList( $out );
1380 }
1381 if ( is_numeric( $num ) ) {
1382 if ( $round !== false ) {
1383 $num = round( $num, $round );
1384 }
1385 return $this->getLanguage()->formatNum( $num );
1386 }
1387 $num ??= '';
1388 if ( preg_match( '/^(-?\d+)\/(\d+)$/', $num, $m ) ) {
1389 if ( $m[2] != 0 ) {
1390 $newNum = (int)$m[1] / (int)$m[2];
1391 if ( $round !== false ) {
1392 $newNum = round( $newNum, $round );
1393 }
1394 } else {
1395 $newNum = $num;
1396 }
1397
1398 return $this->getLanguage()->formatNum( $newNum );
1399 }
1400 # T267370: there are a lot of strange EXIF tags floating around.
1401 LoggerFactory::getInstance( 'formatnum' )->warning(
1402 'FormatMetadata::formatNum with non-numeric value',
1403 [
1404 'tag' => $tagName,
1405 'value' => $num,
1406 ]
1407 );
1408 return $this->literal( $num );
1409 }
1410
1417 private function formatFraction( $num ) {
1418 $m = [];
1419 if ( preg_match( '/^(-?\d+)\/(\d+)$/', $num, $m ) ) {
1420 $numerator = intval( $m[1] );
1421 $denominator = intval( $m[2] );
1422 $gcd = $this->gcd( abs( $numerator ), $denominator );
1423 if ( $gcd != 0 ) {
1424 // 0 shouldn't happen! ;)
1425 return $this->formatNum( $numerator / $gcd ) . '/' . $this->formatNum( $denominator / $gcd );
1426 }
1427 }
1428
1429 return $this->formatNum( $num );
1430 }
1431
1439 private function gcd( $a, $b ) {
1440 /*
1441 // https://en.wikipedia.org/wiki/Euclidean_algorithm
1442 // Recursive form would be:
1443 if ( $b == 0 )
1444 return $a;
1445 else
1446 return gcd( $b, $a % $b );
1447 */
1448 while ( $b != 0 ) {
1449 $remainder = $a % $b;
1450
1451 // tail recursion...
1452 $a = $b;
1453 $b = $remainder;
1454 }
1455
1456 return $a;
1457 }
1458
1471 private function convertNewsCode( $val ) {
1472 if ( !preg_match( '/^\d{8}$/D', $val ) ) {
1473 // Not a valid news code.
1474 return $val;
1475 }
1476 $cat = '';
1477 switch ( substr( $val, 0, 2 ) ) {
1478 case '01':
1479 $cat = 'ace';
1480 break;
1481 case '02':
1482 $cat = 'clj';
1483 break;
1484 case '03':
1485 $cat = 'dis';
1486 break;
1487 case '04':
1488 $cat = 'fin';
1489 break;
1490 case '05':
1491 $cat = 'edu';
1492 break;
1493 case '06':
1494 $cat = 'evn';
1495 break;
1496 case '07':
1497 $cat = 'hth';
1498 break;
1499 case '08':
1500 $cat = 'hum';
1501 break;
1502 case '09':
1503 $cat = 'lab';
1504 break;
1505 case '10':
1506 $cat = 'lif';
1507 break;
1508 case '11':
1509 $cat = 'pol';
1510 break;
1511 case '12':
1512 $cat = 'rel';
1513 break;
1514 case '13':
1515 $cat = 'sci';
1516 break;
1517 case '14':
1518 $cat = 'soi';
1519 break;
1520 case '15':
1521 $cat = 'spo';
1522 break;
1523 case '16':
1524 $cat = 'war';
1525 break;
1526 case '17':
1527 $cat = 'wea';
1528 break;
1529 }
1530 if ( $cat !== '' ) {
1531 $catMsg = $this->exifMsg( 'iimcategory', $cat );
1532 $val = $this->exifMsg( 'subjectnewscode', '', $this->literal( $val ), $catMsg );
1533 }
1534
1535 return $val;
1536 }
1537
1546 private function formatCoords( $coord, string $type ) {
1547 if ( !is_numeric( $coord ) ) {
1548 wfDebugLog( 'exif', __METHOD__ . ": \"$coord\" is not a number" );
1549 return $this->literal( (string)$coord );
1550 }
1551
1552 $ref = '';
1553 if ( $coord < 0 ) {
1554 $nCoord = -$coord;
1555 if ( $type === 'latitude' ) {
1556 $ref = 'S';
1557 } elseif ( $type === 'longitude' ) {
1558 $ref = 'W';
1559 }
1560 } else {
1561 $nCoord = (float)$coord;
1562 if ( $type === 'latitude' ) {
1563 $ref = 'N';
1564 } elseif ( $type === 'longitude' ) {
1565 $ref = 'E';
1566 }
1567 }
1568
1569 $deg = floor( $nCoord );
1570 $min = floor( ( $nCoord - $deg ) * 60 );
1571 $sec = round( ( ( $nCoord - $deg ) * 60 - $min ) * 60, 2 );
1572
1573 $deg = $this->formatNum( $deg );
1574 $min = $this->formatNum( $min );
1575 $sec = $this->formatNum( $sec );
1576
1577 // Note the default message "$1° $2′ $3″ $4" ignores the 5th parameter
1578 return $this->msg( 'exif-coordinate-format', $deg, $min, $sec, $ref, $this->literal( $coord ) )->text();
1579 }
1580
1595 public function collapseContactInfo( array $vals ) {
1596 if ( !( isset( $vals['CiAdrExtadr'] )
1597 || isset( $vals['CiAdrCity'] )
1598 || isset( $vals['CiAdrCtry'] )
1599 || isset( $vals['CiEmailWork'] )
1600 || isset( $vals['CiTelWork'] )
1601 || isset( $vals['CiAdrPcode'] )
1602 || isset( $vals['CiAdrRegion'] )
1603 || isset( $vals['CiUrlWork'] )
1604 ) ) {
1605 // We don't have any sub-properties
1606 // This could happen if its using old
1607 // iptc that just had this as a free-form
1608 // text value.
1609 // Note: people often insert >, etc into
1610 // the metadata which should not be interpreted
1611 // but we still want to auto-link urls.
1612 foreach ( $vals as &$val ) {
1613 $val = $this->literal( $val );
1614 }
1615
1616 return $this->flattenArrayReal( $vals );
1617 } else {
1618 // We have a real ContactInfo field.
1619 // Its unclear if all these fields have to be
1620 // set, so assume they do not.
1621 $url = $tel = $street = $city = $country = '';
1622 $email = $postal = $region = '';
1623
1624 // Also note, some of the class names this uses
1625 // are similar to those used by hCard. This is
1626 // mostly because they're sensible names. This
1627 // does not (and does not attempt to) output
1628 // stuff in the hCard microformat. However it
1629 // might output in the adr microformat.
1630
1631 if ( isset( $vals['CiAdrExtadr'] ) ) {
1632 // Todo: This can potentially be multi-line.
1633 // Need to check how that works in XMP.
1634 $street = '<span class="extended-address">'
1635 . $this->literal(
1636 $vals['CiAdrExtadr'] )
1637 . '</span>';
1638 }
1639 if ( isset( $vals['CiAdrCity'] ) ) {
1640 $city = '<span class="locality">'
1641 . $this->literal( $vals['CiAdrCity'] )
1642 . '</span>';
1643 }
1644 if ( isset( $vals['CiAdrCtry'] ) ) {
1645 $country = '<span class="country-name">'
1646 . $this->literal( $vals['CiAdrCtry'] )
1647 . '</span>';
1648 }
1649 if ( isset( $vals['CiEmailWork'] ) ) {
1650 $emails = [];
1651 // Have to split multiple emails at commas/new lines.
1652 $splitEmails = explode( "\n", $vals['CiEmailWork'] );
1653 foreach ( $splitEmails as $e1 ) {
1654 // Also split on comma
1655 foreach ( explode( ',', $e1 ) as $e2 ) {
1656 $finalEmail = trim( $e2 );
1657 if ( $finalEmail == ',' || $finalEmail == '' ) {
1658 continue;
1659 }
1660 if ( strpos( $finalEmail, '<' ) !== false ) {
1661 // Don't do fancy formatting to
1662 // "My name" <foo@bar.com> style stuff
1663 $emails[] = $this->literal( $finalEmail );
1664 } else {
1665 $emails[] = '[mailto:'
1666 . $finalEmail
1667 . ' <span class="email">'
1668 . $this->literal( $finalEmail )
1669 . '</span>]';
1670 }
1671 }
1672 }
1673 $email = implode( ', ', $emails );
1674 }
1675 if ( isset( $vals['CiTelWork'] ) ) {
1676 $tel = '<span class="tel">'
1677 . $this->literal( $vals['CiTelWork'] )
1678 . '</span>';
1679 }
1680 if ( isset( $vals['CiAdrPcode'] ) ) {
1681 $postal = '<span class="postal-code">'
1682 . $this->literal( $vals['CiAdrPcode'] )
1683 . '</span>';
1684 }
1685 if ( isset( $vals['CiAdrRegion'] ) ) {
1686 // Note this is province/state.
1687 $region = '<span class="region">'
1688 . $this->literal( $vals['CiAdrRegion'] )
1689 . '</span>';
1690 }
1691 if ( isset( $vals['CiUrlWork'] ) ) {
1692 $url = '<span class="url">'
1693 . $this->literal( $vals['CiUrlWork'] )
1694 . '</span>';
1695 }
1696
1697 return $this->msg( 'exif-contact-value', $email, $url,
1698 $street, $city, $region, $postal, $country, $tel )->text();
1699 }
1700 }
1701
1708 public static function getVisibleFields() {
1709 $fields = [];
1710 $lines = explode( "\n", wfMessage( 'metadata-fields' )->inContentLanguage()->text() );
1711 foreach ( $lines as $line ) {
1712 $matches = [];
1713 if ( preg_match( '/^\\*\s*(.*?)\s*$/', $line, $matches ) ) {
1714 $fields[] = $matches[1];
1715 }
1716 }
1717 $fields = array_map( 'strtolower', $fields );
1718
1719 return $fields;
1720 }
1721
1729 public function fetchExtendedMetadata( File $file ) {
1730 $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
1731
1732 // If revision deleted, exit immediately
1733 if ( $file->isDeleted( File::DELETED_FILE ) ) {
1734 return [];
1735 }
1736
1737 $cacheKey = $cache->makeKey(
1738 'getExtendedMetadata',
1739 $this->getLanguage()->getCode(),
1740 (int)$this->singleLang,
1741 $file->getSha1()
1742 );
1743
1744 $cachedValue = $cache->get( $cacheKey );
1745 if (
1746 $cachedValue
1747 && $this->getHookRunner()->onValidateExtendedMetadataCache( $cachedValue['timestamp'], $file )
1748 ) {
1749 $extendedMetadata = $cachedValue['data'];
1750 } else {
1751 $maxCacheTime = ( $file instanceof ForeignAPIFile ) ? 60 * 60 * 12 : 60 * 60 * 24 * 30;
1752 $fileMetadata = $this->getExtendedMetadataFromFile( $file );
1753 $extendedMetadata = $this->getExtendedMetadataFromHook( $file, $fileMetadata, $maxCacheTime );
1754 if ( $this->singleLang ) {
1755 $this->resolveMultilangMetadata( $extendedMetadata );
1756 }
1757 $this->discardMultipleValues( $extendedMetadata );
1758 // Make sure the metadata won't break the API when an XML format is used.
1759 // This is an API-specific function so it would be cleaner to call it from
1760 // outside fetchExtendedMetadata, but this way we don't need to redo the
1761 // computation on a cache hit.
1762 $this->sanitizeArrayForAPI( $extendedMetadata );
1763 $valueToCache = [ 'data' => $extendedMetadata, 'timestamp' => wfTimestampNow() ];
1764 $cache->set( $cacheKey, $valueToCache, $maxCacheTime );
1765 }
1766
1767 return $extendedMetadata;
1768 }
1769
1780 // If this is a remote file accessed via an API request, we already
1781 // have remote metadata so we just ignore any local one
1782 if ( $file instanceof ForeignAPIFile ) {
1783 // In case of error we pretend no metadata - this will get cached.
1784 // Might or might not be a good idea.
1785 return $file->getExtendedMetadata() ?: [];
1786 }
1787
1788 $uploadDate = wfTimestamp( TS_ISO_8601, $file->getTimestamp() );
1789
1790 $fileMetadata = [
1791 // This is modification time, which is close to "upload" time.
1792 'DateTime' => [
1793 'value' => $uploadDate,
1794 'source' => 'mediawiki-metadata',
1795 ],
1796 ];
1797
1798 $title = $file->getTitle();
1799 if ( $title ) {
1800 $text = $title->getText();
1801 $pos = strrpos( $text, '.' );
1802
1803 if ( $pos ) {
1804 $name = substr( $text, 0, $pos );
1805 } else {
1806 $name = $text;
1807 }
1808
1809 $fileMetadata['ObjectName'] = [
1810 'value' => $name,
1811 'source' => 'mediawiki-metadata',
1812 ];
1813 }
1814
1815 return $fileMetadata;
1816 }
1817
1828 protected function getExtendedMetadataFromHook( File $file, array $extendedMetadata,
1829 &$maxCacheTime
1830 ) {
1831 $this->getHookRunner()->onGetExtendedMetadata(
1832 $extendedMetadata,
1833 $file,
1834 $this->getContext(),
1835 $this->singleLang,
1836 $maxCacheTime
1837 );
1838
1839 $visible = array_fill_keys( self::getVisibleFields(), true );
1840 foreach ( $extendedMetadata as $key => $value ) {
1841 if ( !isset( $visible[strtolower( $key )] ) ) {
1842 $extendedMetadata[$key]['hidden'] = '';
1843 }
1844 }
1845
1846 return $extendedMetadata;
1847 }
1848
1857 protected function resolveMultilangValue( $value ) {
1858 if (
1859 !is_array( $value )
1860 || !isset( $value['_type'] )
1861 || $value['_type'] != 'lang'
1862 ) {
1863 return $value; // do nothing if not a multilang array
1864 }
1865
1866 // choose the language best matching user or site settings
1867 $priorityLanguages = $this->getPriorityLanguages();
1868 foreach ( $priorityLanguages as $lang ) {
1869 if ( isset( $value[$lang] ) ) {
1870 return $value[$lang];
1871 }
1872 }
1873
1874 // otherwise go with the default language, if set
1875 if ( isset( $value['x-default'] ) ) {
1876 return $value['x-default'];
1877 }
1878
1879 // otherwise just return any one language
1880 unset( $value['_type'] );
1881 if ( !empty( $value ) ) {
1882 return reset( $value );
1883 }
1884
1885 // this should not happen; signal error
1886 return null;
1887 }
1888
1898 protected function resolveMultivalueValue( $value ) {
1899 if ( !is_array( $value ) ) {
1900 return $value;
1901 } elseif ( isset( $value['_type'] ) && $value['_type'] === 'lang' ) {
1902 // if this is a multilang array, process fields separately
1903 $newValue = [];
1904 foreach ( $value as $k => $v ) {
1905 $newValue[$k] = $this->resolveMultivalueValue( $v );
1906 }
1907 return $newValue;
1908 } else { // _type is 'ul' or 'ol' or missing in which case it defaults to 'ul'
1909 $v = reset( $value );
1910 if ( key( $value ) === '_type' ) {
1911 $v = next( $value );
1912 }
1913 return $v;
1914 }
1915 }
1916
1923 protected function resolveMultilangMetadata( &$metadata ) {
1924 if ( !is_array( $metadata ) ) {
1925 return;
1926 }
1927 foreach ( $metadata as &$field ) {
1928 if ( isset( $field['value'] ) ) {
1929 $field['value'] = $this->resolveMultilangValue( $field['value'] );
1930 }
1931 }
1932 }
1933
1940 protected function discardMultipleValues( &$metadata ) {
1941 if ( !is_array( $metadata ) ) {
1942 return;
1943 }
1944 foreach ( $metadata as $key => &$field ) {
1945 if ( $key === 'Software' || $key === 'Contact' ) {
1946 // we skip some fields which have composite values. They are not particularly interesting
1947 // and you can get them via the metadata / commonmetadata APIs anyway.
1948 continue;
1949 }
1950 if ( isset( $field['value'] ) ) {
1951 $field['value'] = $this->resolveMultivalueValue( $field['value'] );
1952 }
1953 }
1954 }
1955
1960 protected function sanitizeArrayForAPI( &$arr ) {
1961 if ( !is_array( $arr ) ) {
1962 return;
1963 }
1964
1965 $counter = 1;
1966 foreach ( $arr as $key => &$value ) {
1967 $sanitizedKey = $this->sanitizeKeyForAPI( $key );
1968 if ( $sanitizedKey !== $key ) {
1969 if ( isset( $arr[$sanitizedKey] ) ) {
1970 // Make the sanitized keys hopefully unique.
1971 // To make it definitely unique would be too much effort, given that
1972 // sanitizing is only needed for misformatted metadata anyway, but
1973 // this at least covers the case when $arr is numeric.
1974 $sanitizedKey .= $counter;
1975 ++$counter;
1976 }
1977 $arr[$sanitizedKey] = $arr[$key];
1978 unset( $arr[$key] );
1979 }
1980 if ( is_array( $value ) ) {
1981 $this->sanitizeArrayForAPI( $value );
1982 }
1983 }
1984
1985 // Handle API metadata keys (particularly "_type")
1986 $keys = array_filter( array_keys( $arr ), [ ApiResult::class, 'isMetadataKey' ] );
1987 if ( $keys ) {
1988 ApiResult::setPreserveKeysList( $arr, $keys );
1989 }
1990 }
1991
1998 protected function sanitizeKeyForAPI( $key ) {
1999 // drop all characters which are not valid in an XML tag name
2000 // a bunch of non-ASCII letters would be valid but probably won't
2001 // be used so we take the easy way
2002 $key = preg_replace( '/[^a-zA-Z0-9_:.\-]/', '', $key );
2003 // drop characters which are invalid at the first position
2004 $key = preg_replace( '/^[\d\-.]+/', '', $key );
2005
2006 if ( $key === '' ) {
2007 $key = '_';
2008 // special case for an internal keyword
2009 } elseif ( $key === '_element' ) {
2010 $key = 'element';
2011 }
2012
2013 return $key;
2014 }
2015
2022 protected function getPriorityLanguages() {
2023 $priorityLanguages = MediaWikiServices::getInstance()
2024 ->getLanguageFallback()
2025 ->getAllIncludingSiteLanguage( $this->getLanguage()->getCode() );
2026 $priorityLanguages = array_merge(
2027 (array)$this->getLanguage()->getCode(),
2028 $priorityLanguages[0],
2029 $priorityLanguages[1]
2030 );
2031
2032 return $priorityLanguages;
2033 }
2034}
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
wfLogWarning( $msg, $callerOffset=1, $level=E_USER_WARNING)
Send a warning as a PHP error and the debug log.
wfDebugLog( $logGroup, $text, $dest='all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not.
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
getContext()
if(!defined('MW_SETUP_CALLBACK'))
The persistent session ID (if any) loaded at startup.
Definition WebStart.php:82
The simplest way of implementing IContextSource is to hold a RequestContext as a member variable and ...
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
setContext(IContextSource $context)
An IContextSource implementation which will inherit context from another source but allow individual ...
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:67
Foreign file accessible through api.php requests.
Format Image metadata values into a human readable form.
resolveMultilangValue( $value)
Turns an XMP-style multilang array into a single value.
getPriorityLanguages()
Returns a list of languages (first is best) to use when formatting multilang fields,...
flattenArrayReal( $vals, $type='ul', $noHtml=false)
A function to collapse multivalued tags into a single value.
literal( $val)
Convenience function for getFormattedData()
getExtendedMetadataFromFile(File $file)
Get file-based metadata in standardized format.
collapseContactInfo(array $vals)
Format the contact info field into a single value.
fetchExtendedMetadata(File $file)
Get an array of extended metadata.
setSingleLanguage( $val)
Trigger only outputting single language for multilanguage fields.
sanitizeArrayForAPI(&$arr)
Makes sure the given array is a valid API response fragment.
static flattenArrayContentLang( $vals, $type='ul', $noHtml=false, $context=false)
Flatten an array, using the content language for any messages.
discardMultipleValues(&$metadata)
Takes an array returned by the getExtendedMetadata* functions, and turns all fields into single-value...
makeFormattedData( $tags)
Numbers given by Exif user agents are often magical, that is they should be replaced by a detailed ex...
resolveMultivalueValue( $value)
Turns an XMP-style multivalue array into a single value by dropping all but the first value.
static getVisibleFields()
Get a list of fields that are visible by default.
getExtendedMetadataFromHook(File $file, array $extendedMetadata, &$maxCacheTime)
Get additional metadata from hooks in standardized format.
resolveMultilangMetadata(&$metadata)
Takes an array returned by the getExtendedMetadata* functions, and resolves multi-language values in ...
static getFormattedData( $tags, $context=false)
Numbers given by Exif user agents are often magical, that is they should be replaced by a detailed ex...
sanitizeKeyForAPI( $key)
Turns a string into a valid API identifier.
bool $singleLang
Only output a single language for multi-language fields.
MediaWiki exception.
PSR-3 logger instance factory.
Service locator for MediaWiki core services.
Interface for objects which can provide a MediaWiki context on request.
$line
Definition mcc.php:119
$cache
Definition mcc.php:33
foreach( $mmfl['setupFiles'] as $fileName) if($queue) if(empty( $mmfl['quiet'])) $s
$content
Definition router.php:76
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42
if(!isset( $args[0])) $lang
if(!file_exists( $CREDITS)) $lines