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