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