MediaWiki  master
FormatMetadata.php
Go to the documentation of this file.
1 <?php
28 use MediaWiki\HookContainer\ProtectedHookAccessorTrait;
31 use Wikimedia\Timestamp\TimestampException;
32 
55  use ProtectedHookAccessorTrait;
56 
62  protected $singleLang = false;
63 
70  public function setSingleLanguage( $val ) {
71  $this->singleLang = $val;
72  }
73 
87  public static function getFormattedData( $tags, $context = false ) {
88  $obj = new FormatMetadata;
89  if ( $context ) {
90  $obj->setContext( $context );
91  }
92 
93  return $obj->makeFormattedData( $tags );
94  }
95 
107  public function makeFormattedData( $tags ) {
108  $resolutionunit = !isset( $tags['ResolutionUnit'] ) || $tags['ResolutionUnit'] == 2 ? 2 : 3;
109  unset( $tags['ResolutionUnit'] );
110 
111  // Ignore these complex values
112  unset( $tags['HasExtendedXMP'] );
113  unset( $tags['AuthorsPosition'] );
114  unset( $tags['LocationCreated'] );
115  unset( $tags['LocationShown'] );
116  unset( $tags['GPSAltitudeRef'] );
117 
118  foreach ( $tags as $tag => &$vals ) {
119  // This seems ugly to wrap non-array's in an array just to unwrap again,
120  // especially when most of the time it is not an array
121  if ( !is_array( $vals ) ) {
122  $vals = [ $vals ];
123  }
124 
125  // _type is a special value to say what array type
126  if ( isset( $vals['_type'] ) ) {
127  $type = $vals['_type'];
128  unset( $vals['_type'] );
129  } else {
130  $type = 'ul'; // default unordered list.
131  }
132 
133  // _formatted is a special value to indicate the subclass
134  // already handled & formatted this tag as wikitext
135  if ( isset( $tags[$tag]['_formatted'] ) ) {
136  $tags[$tag] = $this->flattenArrayReal(
137  $tags[$tag]['_formatted'], $type
138  );
139  continue;
140  }
141 
142  // This is done differently as the tag is an array.
143  if ( $tag == 'GPSTimeStamp' && count( $vals ) === 3 ) {
144  // hour min sec array
145 
146  $h = explode( '/', $vals[0], 2 );
147  $m = explode( '/', $vals[1], 2 );
148  $s = explode( '/', $vals[2], 2 );
149 
150  // this should already be validated
151  // when loaded from file, but it could
152  // come from a foreign repo, so be
153  // paranoid.
154  if ( !isset( $h[1] )
155  || !isset( $m[1] )
156  || !isset( $s[1] )
157  || $h[1] == 0
158  || $m[1] == 0
159  || $s[1] == 0
160  ) {
161  continue;
162  }
163  $vals = str_pad( (string)( (int)$h[0] / (int)$h[1] ), 2, '0', STR_PAD_LEFT )
164  . ':' . str_pad( (string)( (int)$m[0] / (int)$m[1] ), 2, '0', STR_PAD_LEFT )
165  . ':' . str_pad( (string)( (int)$s[0] / (int)$s[1] ), 2, '0', STR_PAD_LEFT );
166 
167  try {
168  $time = wfTimestamp( TS_MW, '1971:01:01 ' . $vals );
169  // the 1971:01:01 is just a placeholder, and not shown to user.
170  if ( $time && intval( $time ) > 0 ) {
171  $vals = $this->getLanguage()->time( $time );
172  }
173  } catch ( TimestampException $e ) {
174  // This shouldn't happen, but we've seen bad formats
175  // such as 4-digit seconds in the wild.
176  // leave $vals as-is
177  }
178  continue;
179  }
180 
181  // The contact info is a multi-valued field
182  // instead of the other props which are single
183  // valued (mostly) so handle as a special case.
184  if ( $tag === 'Contact' || $tag === 'CreatorContactInfo' ) {
185  $vals = $this->collapseContactInfo( $vals );
186  continue;
187  }
188 
189  foreach ( $vals as &$val ) {
190  switch ( $tag ) {
191  case 'Compression':
192  switch ( $val ) {
193  case 1:
194  case 2:
195  case 3:
196  case 4:
197  case 5:
198  case 6:
199  case 7:
200  case 8:
201  case 32773:
202  case 32946:
203  case 34712:
204  $val = $this->exifMsg( $tag, $val );
205  break;
206  default:
207  /* If not recognized, display as is. */
208  $val = $this->literal( $val );
209  break;
210  }
211  break;
212 
213  case 'PhotometricInterpretation':
214  switch ( $val ) {
215  case 0:
216  case 1:
217  case 2:
218  case 3:
219  case 4:
220  case 5:
221  case 6:
222  case 8:
223  case 9:
224  case 10:
225  case 32803:
226  case 34892:
227  $val = $this->exifMsg( $tag, $val );
228  break;
229  default:
230  /* If not recognized, display as is. */
231  $val = $this->literal( $val );
232  break;
233  }
234  break;
235 
236  case 'Orientation':
237  switch ( $val ) {
238  case 1:
239  case 2:
240  case 3:
241  case 4:
242  case 5:
243  case 6:
244  case 7:
245  case 8:
246  $val = $this->exifMsg( $tag, $val );
247  break;
248  default:
249  /* If not recognized, display as is. */
250  $val = $this->literal( $val );
251  break;
252  }
253  break;
254 
255  case 'PlanarConfiguration':
256  switch ( $val ) {
257  case 1:
258  case 2:
259  $val = $this->exifMsg( $tag, $val );
260  break;
261  default:
262  /* If not recognized, display as is. */
263  $val = $this->literal( $val );
264  break;
265  }
266  break;
267 
268  // TODO: YCbCrSubSampling
269  case 'YCbCrPositioning':
270  switch ( $val ) {
271  case 1:
272  case 2:
273  $val = $this->exifMsg( $tag, $val );
274  break;
275  default:
276  /* If not recognized, display as is. */
277  $val = $this->literal( $val );
278  break;
279  }
280  break;
281 
282  case 'XResolution':
283  case 'YResolution':
284  switch ( $resolutionunit ) {
285  case 2:
286  $val = $this->exifMsg( 'XYResolution', 'i', $this->formatNum( $val ) );
287  break;
288  case 3:
289  $val = $this->exifMsg( 'XYResolution', 'c', $this->formatNum( $val ) );
290  break;
291  default:
292  /* If not recognized, display as is. */
293  $val = $this->literal( $val );
294  break;
295  }
296  break;
297 
298  // TODO: YCbCrCoefficients #p27 (see annex E)
299  case 'ExifVersion':
300  // PHP likes to be the odd one out with casing of FlashPixVersion;
301  // https://www.exif.org/Exif2-2.PDF#page=32 and
302  // https://www.digitalgalen.net/Documents/External/XMP/XMPSpecificationPart2.pdf#page=51
303  // both use FlashpixVersion. However, since at least 2002, PHP has used FlashPixVersion at
304  // https://github.com/php/php-src/blame/master/ext/exif/exif.c#L725
305  case 'FlashPixVersion':
306  // But we can still get the correct casing from
307  // Wikimedia\XMPReader on PDFs
308  case 'FlashpixVersion':
309  $val = $this->literal( (int)$val / 100 );
310  break;
311 
312  case 'ColorSpace':
313  switch ( $val ) {
314  case 1:
315  case 65535:
316  $val = $this->exifMsg( $tag, $val );
317  break;
318  default:
319  /* If not recognized, display as is. */
320  $val = $this->literal( $val );
321  break;
322  }
323  break;
324 
325  case 'ComponentsConfiguration':
326  switch ( $val ) {
327  case 0:
328  case 1:
329  case 2:
330  case 3:
331  case 4:
332  case 5:
333  case 6:
334  $val = $this->exifMsg( $tag, $val );
335  break;
336  default:
337  /* If not recognized, display as is. */
338  $val = $this->literal( $val );
339  break;
340  }
341  break;
342 
343  case 'DateTime':
344  case 'DateTimeOriginal':
345  case 'DateTimeDigitized':
346  case 'DateTimeReleased':
347  case 'DateTimeExpires':
348  case 'GPSDateStamp':
349  case 'dc-date':
350  case 'DateTimeMetadata':
351  case 'FirstPhotoDate':
352  case 'LastPhotoDate':
353  if ( $val == '0000:00:00 00:00:00' || $val == ' : : : : ' ) {
354  $val = $this->msg( 'exif-unknowndate' )->text();
355  break;
356  } elseif ( preg_match(
357  '/^(?:\d{4}):(?:\d\d):(?:\d\d) (?:\d\d):(?:\d\d):(?:\d\d)$/D',
358  $val
359  ) ) {
360  // Full date.
361  $time = wfTimestamp( TS_MW, $val );
362  if ( $time && intval( $time ) > 0 ) {
363  $val = $this->getLanguage()->timeanddate( $time );
364  break;
365  }
366  } elseif ( preg_match( '/^(?:\d{4}):(?:\d\d):(?:\d\d) (?:\d\d):(?:\d\d)$/D', $val ) ) {
367  // No second field. Still format the same
368  // since timeanddate doesn't include seconds anyways,
369  // but second still available in api
370  $time = wfTimestamp( TS_MW, $val . ':00' );
371  if ( $time && intval( $time ) > 0 ) {
372  $val = $this->getLanguage()->timeanddate( $time );
373  break;
374  }
375  } elseif ( preg_match( '/^(?:\d{4}):(?:\d\d):(?:\d\d)$/D', $val ) ) {
376  // If only the date but not the time is filled in.
377  $time = wfTimestamp( TS_MW, substr( $val, 0, 4 )
378  . substr( $val, 5, 2 )
379  . substr( $val, 8, 2 )
380  . '000000' );
381  if ( $time && intval( $time ) > 0 ) {
382  $val = $this->getLanguage()->date( $time );
383  break;
384  }
385  }
386  // else it will just output $val without formatting it.
387  $val = $this->literal( $val );
388  break;
389 
390  case 'ExposureProgram':
391  switch ( $val ) {
392  case 0:
393  case 1:
394  case 2:
395  case 3:
396  case 4:
397  case 5:
398  case 6:
399  case 7:
400  case 8:
401  $val = $this->exifMsg( $tag, $val );
402  break;
403  default:
404  /* If not recognized, display as is. */
405  $val = $this->literal( $val );
406  break;
407  }
408  break;
409 
410  case 'SubjectDistance':
411  $val = $this->exifMsg( $tag, '', $this->formatNum( $val ) );
412  break;
413 
414  case 'MeteringMode':
415  switch ( $val ) {
416  case 0:
417  case 1:
418  case 2:
419  case 3:
420  case 4:
421  case 5:
422  case 6:
423  case 7:
424  case 255:
425  $val = $this->exifMsg( $tag, $val );
426  break;
427  default:
428  /* If not recognized, display as is. */
429  $val = $this->literal( $val );
430  break;
431  }
432  break;
433 
434  case 'LightSource':
435  switch ( $val ) {
436  case 0:
437  case 1:
438  case 2:
439  case 3:
440  case 4:
441  case 9:
442  case 10:
443  case 11:
444  case 12:
445  case 13:
446  case 14:
447  case 15:
448  case 17:
449  case 18:
450  case 19:
451  case 20:
452  case 21:
453  case 22:
454  case 23:
455  case 24:
456  case 255:
457  $val = $this->exifMsg( $tag, $val );
458  break;
459  default:
460  /* If not recognized, display as is. */
461  $val = $this->literal( $val );
462  break;
463  }
464  break;
465 
466  case 'Flash':
467  $flashDecode = [
468  'fired' => $val & 0b00000001,
469  'return' => ( $val & 0b00000110 ) >> 1,
470  'mode' => ( $val & 0b00011000 ) >> 3,
471  'function' => ( $val & 0b00100000 ) >> 5,
472  'redeye' => ( $val & 0b01000000 ) >> 6,
473  // 'reserved' => ( $val & 0b10000000 ) >> 7,
474  ];
475  $flashMsgs = [];
476  # We do not need to handle unknown values since all are used.
477  foreach ( $flashDecode as $subTag => $subValue ) {
478  # We do not need any message for zeroed values.
479  if ( $subTag != 'fired' && $subValue == 0 ) {
480  continue;
481  }
482  $fullTag = $tag . '-' . $subTag;
483  $flashMsgs[] = $this->exifMsg( $fullTag, $subValue );
484  }
485  $val = $this->getLanguage()->commaList( $flashMsgs );
486  break;
487 
488  case 'FocalPlaneResolutionUnit':
489  switch ( $val ) {
490  case 2:
491  $val = $this->exifMsg( $tag, $val );
492  break;
493  default:
494  /* If not recognized, display as is. */
495  $val = $this->literal( $val );
496  break;
497  }
498  break;
499 
500  case 'SensingMethod':
501  switch ( $val ) {
502  case 1:
503  case 2:
504  case 3:
505  case 4:
506  case 5:
507  case 7:
508  case 8:
509  $val = $this->exifMsg( $tag, $val );
510  break;
511  default:
512  /* If not recognized, display as is. */
513  $val = $this->literal( $val );
514  break;
515  }
516  break;
517 
518  case 'FileSource':
519  switch ( $val ) {
520  case 3:
521  $val = $this->exifMsg( $tag, $val );
522  break;
523  default:
524  /* If not recognized, display as is. */
525  $val = $this->literal( $val );
526  break;
527  }
528  break;
529 
530  case 'SceneType':
531  switch ( $val ) {
532  case 1:
533  $val = $this->exifMsg( $tag, $val );
534  break;
535  default:
536  /* If not recognized, display as is. */
537  $val = $this->literal( $val );
538  break;
539  }
540  break;
541 
542  case 'CustomRendered':
543  switch ( $val ) {
544  case 0: /* normal */
545  case 1: /* custom */
546  /* The following are unofficial Apple additions */
547  case 2: /* HDR (no original saved) */
548  case 3: /* HDR (original saved) */
549  case 4: /* Original (for HDR) */
550  /* Yes 5 is not present ;) */
551  case 6: /* Panorama */
552  case 7: /* Portrait HDR */
553  case 8: /* Portrait */
554  $val = $this->exifMsg( $tag, $val );
555  break;
556  default:
557  /* If not recognized, display as is. */
558  $val = $this->literal( $val );
559  break;
560  }
561  break;
562 
563  case 'ExposureMode':
564  switch ( $val ) {
565  case 0:
566  case 1:
567  case 2:
568  $val = $this->exifMsg( $tag, $val );
569  break;
570  default:
571  /* If not recognized, display as is. */
572  break;
573  }
574  break;
575 
576  case 'WhiteBalance':
577  switch ( $val ) {
578  case 0:
579  case 1:
580  $val = $this->exifMsg( $tag, $val );
581  break;
582  default:
583  /* If not recognized, display as is. */
584  $val = $this->literal( $val );
585  break;
586  }
587  break;
588 
589  case 'SceneCaptureType':
590  switch ( $val ) {
591  case 0:
592  case 1:
593  case 2:
594  case 3:
595  $val = $this->exifMsg( $tag, $val );
596  break;
597  default:
598  /* If not recognized, display as is. */
599  $val = $this->literal( $val );
600  break;
601  }
602  break;
603 
604  case 'GainControl':
605  switch ( $val ) {
606  case 0:
607  case 1:
608  case 2:
609  case 3:
610  case 4:
611  $val = $this->exifMsg( $tag, $val );
612  break;
613  default:
614  /* If not recognized, display as is. */
615  $val = $this->literal( $val );
616  break;
617  }
618  break;
619 
620  case 'Contrast':
621  switch ( $val ) {
622  case 0:
623  case 1:
624  case 2:
625  $val = $this->exifMsg( $tag, $val );
626  break;
627  default:
628  /* If not recognized, display as is. */
629  $val = $this->literal( $val );
630  break;
631  }
632  break;
633 
634  case 'Saturation':
635  switch ( $val ) {
636  case 0:
637  case 1:
638  case 2:
639  $val = $this->exifMsg( $tag, $val );
640  break;
641  default:
642  /* If not recognized, display as is. */
643  $val = $this->literal( $val );
644  break;
645  }
646  break;
647 
648  case 'Sharpness':
649  switch ( $val ) {
650  case 0:
651  case 1:
652  case 2:
653  $val = $this->exifMsg( $tag, $val );
654  break;
655  default:
656  /* If not recognized, display as is. */
657  $val = $this->literal( $val );
658  break;
659  }
660  break;
661 
662  case 'SubjectDistanceRange':
663  switch ( $val ) {
664  case 0:
665  case 1:
666  case 2:
667  case 3:
668  $val = $this->exifMsg( $tag, $val );
669  break;
670  default:
671  /* If not recognized, display as is. */
672  $val = $this->literal( $val );
673  break;
674  }
675  break;
676 
677  // The GPS...Ref values are kept for compatibility, probably won't be reached.
678  case 'GPSLatitudeRef':
679  case 'GPSDestLatitudeRef':
680  switch ( $val ) {
681  case 'N':
682  case 'S':
683  $val = $this->exifMsg( 'GPSLatitude', $val );
684  break;
685  default:
686  /* If not recognized, display as is. */
687  $val = $this->literal( $val );
688  break;
689  }
690  break;
691 
692  case 'GPSLongitudeRef':
693  case 'GPSDestLongitudeRef':
694  switch ( $val ) {
695  case 'E':
696  case 'W':
697  $val = $this->exifMsg( 'GPSLongitude', $val );
698  break;
699  default:
700  /* If not recognized, display as is. */
701  $val = $this->literal( $val );
702  break;
703  }
704  break;
705 
706  case 'GPSAltitude':
707  if ( $val < 0 ) {
708  $val = $this->exifMsg( 'GPSAltitude', 'below-sealevel', $this->formatNum( -$val, 3 ) );
709  } else {
710  $val = $this->exifMsg( 'GPSAltitude', 'above-sealevel', $this->formatNum( $val, 3 ) );
711  }
712  break;
713 
714  case 'GPSStatus':
715  switch ( $val ) {
716  case 'A':
717  case 'V':
718  $val = $this->exifMsg( $tag, $val );
719  break;
720  default:
721  /* If not recognized, display as is. */
722  $val = $this->literal( $val );
723  break;
724  }
725  break;
726 
727  case 'GPSMeasureMode':
728  switch ( $val ) {
729  case 2:
730  case 3:
731  $val = $this->exifMsg( $tag, $val );
732  break;
733  default:
734  /* If not recognized, display as is. */
735  $val = $this->literal( $val );
736  break;
737  }
738  break;
739 
740  case 'GPSTrackRef':
741  case 'GPSImgDirectionRef':
742  case 'GPSDestBearingRef':
743  switch ( $val ) {
744  case 'T':
745  case 'M':
746  $val = $this->exifMsg( 'GPSDirection', $val );
747  break;
748  default:
749  /* If not recognized, display as is. */
750  $val = $this->literal( $val );
751  break;
752  }
753  break;
754 
755  case 'GPSLatitude':
756  case 'GPSDestLatitude':
757  $val = $this->formatCoords( $val, 'latitude' );
758  break;
759  case 'GPSLongitude':
760  case 'GPSDestLongitude':
761  $val = $this->formatCoords( $val, 'longitude' );
762  break;
763 
764  case 'GPSSpeedRef':
765  switch ( $val ) {
766  case 'K':
767  case 'M':
768  case 'N':
769  $val = $this->exifMsg( 'GPSSpeed', $val );
770  break;
771  default:
772  /* If not recognized, display as is. */
773  $val = $this->literal( $val );
774  break;
775  }
776  break;
777 
778  case 'GPSDestDistanceRef':
779  switch ( $val ) {
780  case 'K':
781  case 'M':
782  case 'N':
783  $val = $this->exifMsg( 'GPSDestDistance', $val );
784  break;
785  default:
786  /* If not recognized, display as is. */
787  $val = $this->literal( $val );
788  break;
789  }
790  break;
791 
792  case 'GPSDOP':
793  // See https://en.wikipedia.org/wiki/Dilution_of_precision_(GPS)
794  if ( $val <= 2 ) {
795  $val = $this->exifMsg( $tag, 'excellent', $this->formatNum( $val ) );
796  } elseif ( $val <= 5 ) {
797  $val = $this->exifMsg( $tag, 'good', $this->formatNum( $val ) );
798  } elseif ( $val <= 10 ) {
799  $val = $this->exifMsg( $tag, 'moderate', $this->formatNum( $val ) );
800  } elseif ( $val <= 20 ) {
801  $val = $this->exifMsg( $tag, 'fair', $this->formatNum( $val ) );
802  } else {
803  $val = $this->exifMsg( $tag, 'poor', $this->formatNum( $val ) );
804  }
805  break;
806 
807  // This is not in the Exif standard, just a special
808  // case for our purposes which enables wikis to wikify
809  // the make, model and software name to link to their articles.
810  case 'Make':
811  case 'Model':
812  $val = $this->exifMsg( $tag, '', $this->literal( $val ) );
813  break;
814 
815  case 'Software':
816  if ( is_array( $val ) ) {
817  if ( count( $val ) > 1 ) {
818  // if its a software, version array.
819  $val = $this->msg(
820  'exif-software-version-value',
821  $this->literal( $val[0] ),
822  $this->literal( $val[1] )
823  )->text();
824  } else {
825  // https://phabricator.wikimedia.org/T178130
826  $val = $this->exifMsg( $tag, '', $this->literal( $val[0] ) );
827  }
828  } else {
829  $val = $this->exifMsg( $tag, '', $this->literal( $val ) );
830  }
831  break;
832 
833  case 'ExposureTime':
834  // Show the pretty fraction as well as decimal version
835  $val = $this->msg( 'exif-exposuretime-format',
836  $this->formatFraction( $val ), $this->formatNum( $val ) )->text();
837  break;
838  case 'ISOSpeedRatings':
839  // If its = 65535 that means its at the
840  // limit of the size of Exif::short and
841  // is really higher.
842  if ( $val == '65535' ) {
843  $val = $this->exifMsg( $tag, 'overflow' );
844  } else {
845  $val = $this->formatNum( $val );
846  }
847  break;
848  case 'FNumber':
849  $val = $this->msg( 'exif-fnumber-format',
850  $this->formatNum( $val ) )->text();
851  break;
852 
853  case 'FocalLength':
854  case 'FocalLengthIn35mmFilm':
855  $val = $this->msg( 'exif-focallength-format',
856  $this->formatNum( $val ) )->text();
857  break;
858 
859  case 'MaxApertureValue':
860  if ( strpos( $val, '/' ) !== false ) {
861  // need to expand this earlier to calculate fNumber
862  [ $n, $d ] = explode( '/', $val, 2 );
863  if ( is_numeric( $n ) && is_numeric( $d ) ) {
864  $val = (int)$n / (int)$d;
865  }
866  }
867  if ( is_numeric( $val ) ) {
868  $fNumber = 2 ** ( $val / 2 );
869  if ( is_finite( $fNumber ) ) {
870  $val = $this->msg( 'exif-maxaperturevalue-value',
871  $this->formatNum( $val ),
872  $this->formatNum( $fNumber, 2 )
873  )->text();
874  break;
875  }
876  }
877  $val = $this->literal( $val );
878  break;
879 
880  case 'iimCategory':
881  switch ( strtolower( $val ) ) {
882  // See pg 29 of IPTC photo
883  // metadata standard.
884  case 'ace':
885  case 'clj':
886  case 'dis':
887  case 'fin':
888  case 'edu':
889  case 'evn':
890  case 'hth':
891  case 'hum':
892  case 'lab':
893  case 'lif':
894  case 'pol':
895  case 'rel':
896  case 'sci':
897  case 'soi':
898  case 'spo':
899  case 'war':
900  case 'wea':
901  $val = $this->exifMsg(
902  'iimcategory',
903  $val
904  );
905  break;
906  default:
907  $val = $this->literal( $val );
908  }
909  break;
910  case 'SubjectNewsCode':
911  // Essentially like iimCategory.
912  // 8 (numeric) digit hierarchical
913  // classification. We decode the
914  // first 2 digits, which provide
915  // a broad category.
916  $val = $this->convertNewsCode( $val );
917  break;
918  case 'Urgency':
919  // 1-8 with 1 being highest, 5 normal
920  // 0 is reserved, and 9 is 'user-defined'.
921  $urgency = '';
922  if ( $val == 0 || $val == 9 ) {
923  $urgency = 'other';
924  } elseif ( $val < 5 && $val > 1 ) {
925  $urgency = 'high';
926  } elseif ( $val == 5 ) {
927  $urgency = 'normal';
928  } elseif ( $val <= 8 && $val > 5 ) {
929  $urgency = 'low';
930  }
931 
932  if ( $urgency !== '' ) {
933  $val = $this->exifMsg( 'urgency',
934  $urgency, $this->literal( $val )
935  );
936  } else {
937  $val = $this->literal( $val );
938  }
939  break;
940 
941  // Things that have a unit of pixels.
942  case 'OriginalImageHeight':
943  case 'OriginalImageWidth':
944  case 'PixelXDimension':
945  case 'PixelYDimension':
946  case 'ImageWidth':
947  case 'ImageLength':
948  $val = $this->formatNum( $val ) . ' ' . $this->msg( 'unit-pixel' )->text();
949  break;
950 
951  // Do not transform fields with pure text.
952  // For some languages the formatNum()
953  // conversion results to wrong output like
954  // foo,bar@example,com or foo٫bar@example٫com.
955  // Also some 'numeric' things like Scene codes
956  // are included here as we really don't want
957  // commas inserted.
958  case 'ImageDescription':
959  case 'UserComment':
960  case 'Artist':
961  case 'Copyright':
962  case 'RelatedSoundFile':
963  case 'ImageUniqueID':
964  case 'SpectralSensitivity':
965  case 'GPSSatellites':
966  case 'GPSVersionID':
967  case 'GPSMapDatum':
968  case 'Keywords':
969  case 'WorldRegionDest':
970  case 'CountryDest':
971  case 'CountryCodeDest':
972  case 'ProvinceOrStateDest':
973  case 'CityDest':
974  case 'SublocationDest':
975  case 'WorldRegionCreated':
976  case 'CountryCreated':
977  case 'CountryCodeCreated':
978  case 'ProvinceOrStateCreated':
979  case 'CityCreated':
980  case 'SublocationCreated':
981  case 'ObjectName':
982  case 'SpecialInstructions':
983  case 'Headline':
984  case 'Credit':
985  case 'Source':
986  case 'EditStatus':
987  case 'FixtureIdentifier':
988  case 'LocationDest':
989  case 'LocationDestCode':
990  case 'Writer':
991  case 'JPEGFileComment':
992  case 'iimSupplementalCategory':
993  case 'OriginalTransmissionRef':
994  case 'Identifier':
995  case 'dc-contributor':
996  case 'dc-coverage':
997  case 'dc-publisher':
998  case 'dc-relation':
999  case 'dc-rights':
1000  case 'dc-source':
1001  case 'dc-type':
1002  case 'Lens':
1003  case 'SerialNumber':
1004  case 'CameraOwnerName':
1005  case 'Label':
1006  case 'Nickname':
1007  case 'RightsCertificate':
1008  case 'CopyrightOwner':
1009  case 'UsageTerms':
1010  case 'WebStatement':
1011  case 'OriginalDocumentID':
1012  case 'LicenseUrl':
1013  case 'MorePermissionsUrl':
1014  case 'AttributionUrl':
1015  case 'PreferredAttributionName':
1016  case 'PNGFileComment':
1017  case 'Disclaimer':
1018  case 'ContentWarning':
1019  case 'GIFFileComment':
1020  case 'SceneCode':
1021  case 'IntellectualGenre':
1022  case 'Event':
1023  case 'OrganisationInImage':
1024  case 'PersonInImage':
1025  case 'CaptureSoftware':
1026  case 'GPSAreaInformation':
1027  case 'GPSProcessingMethod':
1028  case 'StitchingSoftware':
1029  case 'SubSecTime':
1030  case 'SubSecTimeOriginal':
1031  case 'SubSecTimeDigitized':
1032  $val = $this->literal( $val );
1033  break;
1034 
1035  case 'ProjectionType':
1036  switch ( $val ) {
1037  case 'equirectangular':
1038  $val = $this->exifMsg( $tag, $val );
1039  break;
1040  default:
1041  $val = $this->literal( $val );
1042  break;
1043  }
1044  break;
1045  case 'ObjectCycle':
1046  switch ( $val ) {
1047  case 'a':
1048  case 'p':
1049  case 'b':
1050  $val = $this->exifMsg( $tag, $val );
1051  break;
1052  default:
1053  $val = $this->literal( $val );
1054  break;
1055  }
1056  break;
1057  case 'Copyrighted':
1058  case 'UsePanoramaViewer':
1059  case 'ExposureLockUsed':
1060  switch ( $val ) {
1061  case 'True':
1062  case 'False':
1063  $val = $this->exifMsg( $tag, $val );
1064  break;
1065  default:
1066  $val = $this->literal( $val );
1067  break;
1068  }
1069  break;
1070  case 'Rating':
1071  if ( $val == '-1' ) {
1072  $val = $this->exifMsg( $tag, 'rejected' );
1073  } else {
1074  $val = $this->formatNum( $val );
1075  }
1076  break;
1077 
1078  case 'LanguageCode':
1079  $lang = MediaWikiServices::getInstance()
1080  ->getLanguageNameUtils()
1081  ->getLanguageName( strtolower( $val ), $this->getLanguage()->getCode() );
1082  $val = $this->literal( $lang ?: $val );
1083  break;
1084 
1085  default:
1086  $val = $this->formatNum( $val, false, $tag );
1087  break;
1088  }
1089  }
1090  // End formatting values, start flattening arrays.
1091  $vals = $this->flattenArrayReal( $vals, $type );
1092  }
1093 
1094  return $tags;
1095  }
1096 
1114  public function flattenArrayReal( $vals, $type = 'ul', $noHtml = false ) {
1115  if ( !is_array( $vals ) ) {
1116  return $vals; // do nothing if not an array;
1117  }
1118 
1119  if ( isset( $vals['_type'] ) ) {
1120  $type = $vals['_type'];
1121  unset( $vals['_type'] );
1122  }
1123 
1124  if ( count( $vals ) === 1 && $type !== 'lang' && isset( $vals[0] ) ) {
1125  return $vals[0];
1126  } elseif ( count( $vals ) === 0 ) {
1127  wfDebug( __METHOD__ . " metadata array with 0 elements!" );
1128 
1129  return ""; // paranoia. This should never happen
1130  } else {
1131  // Check if $vals contains nested arrays
1132  $containsNestedArrays = in_array( true, array_map( 'is_array', $vals ), true );
1133  if ( $containsNestedArrays ) {
1134  wfLogWarning( __METHOD__ . ': Invalid $vals, contains nested arrays: ' . json_encode( $vals ) );
1135  }
1136 
1137  /* @todo FIXME: This should hide some of the list entries if there are
1138  * say more than four. Especially if a field is translated into 20
1139  * languages, we don't want to show them all by default
1140  */
1141  switch ( $type ) {
1142  case 'lang':
1143  // Display default, followed by ContentLanguage,
1144  // followed by the rest in no particular order.
1145 
1146  // Todo: hide some items if really long list.
1147 
1148  $content = '';
1149 
1150  $priorityLanguages = $this->getPriorityLanguages();
1151  $defaultItem = false;
1152  $defaultLang = false;
1153 
1154  // If default is set, save it for later,
1155  // as we don't know if it's equal to one of the lang codes.
1156  // (In xmp you specify the language for a default property by having
1157  // both a default prop, and one in the language that are identical)
1158  if ( isset( $vals['x-default'] ) ) {
1159  $defaultItem = $vals['x-default'];
1160  unset( $vals['x-default'] );
1161  }
1162  foreach ( $priorityLanguages as $pLang ) {
1163  if ( isset( $vals[$pLang] ) ) {
1164  $isDefault = false;
1165  if ( $vals[$pLang] === $defaultItem ) {
1166  $defaultItem = false;
1167  $isDefault = true;
1168  }
1169  $content .= $this->langItem( $vals[$pLang], $pLang, $isDefault, $noHtml );
1170 
1171  unset( $vals[$pLang] );
1172 
1173  if ( $this->singleLang ) {
1174  return Html::rawElement( 'span', [ 'lang' => $pLang ], $vals[$pLang] );
1175  }
1176  }
1177  }
1178 
1179  // Now do the rest.
1180  foreach ( $vals as $lang => $item ) {
1181  if ( $item === $defaultItem ) {
1182  $defaultLang = $lang;
1183  continue;
1184  }
1185  $content .= $this->langItem( $item, $lang, false, $noHtml );
1186  if ( $this->singleLang ) {
1187  return Html::rawElement( 'span', [ 'lang' => $lang ], $item );
1188  }
1189  }
1190  if ( $defaultItem !== false ) {
1191  $content = $this->langItem( $defaultItem, $defaultLang, true, $noHtml ) . $content;
1192  if ( $this->singleLang ) {
1193  return $defaultItem;
1194  }
1195  }
1196  if ( $noHtml ) {
1197  return $content;
1198  }
1199 
1200  return '<ul class="metadata-langlist">' . $content . '</ul>';
1201  case 'ol':
1202  if ( $noHtml ) {
1203  return "\n#" . implode( "\n#", $vals );
1204  }
1205 
1206  return "<ol><li>" . implode( "</li>\n<li>", $vals ) . '</li></ol>';
1207  case 'ul':
1208  default:
1209  if ( $noHtml ) {
1210  return "\n*" . implode( "\n*", $vals );
1211  }
1212 
1213  return "<ul><li>" . implode( "</li>\n<li>", $vals ) . '</li></ul>';
1214  }
1215  }
1216  }
1217 
1228  private function langItem( $value, $lang, $default = false, $noHtml = false ) {
1229  if ( $lang === false && $default === false ) {
1230  throw new MWException( '$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 = intval( $m[1] );
1376  $denominator = intval( $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  } else {
1573  // We have a real ContactInfo field.
1574  // Its unclear if all these fields have to be
1575  // set, so assume they do not.
1576  $url = $tel = $street = $city = $country = '';
1577  $email = $postal = $region = '';
1578 
1579  // Also note, some of the class names this uses
1580  // are similar to those used by hCard. This is
1581  // mostly because they're sensible names. This
1582  // does not (and does not attempt to) output
1583  // stuff in the hCard microformat. However it
1584  // might output in the adr microformat.
1585 
1586  if ( isset( $vals['CiAdrExtadr'] ) ) {
1587  // Todo: This can potentially be multi-line.
1588  // Need to check how that works in XMP.
1589  $street = '<span class="extended-address">'
1590  . $this->literal(
1591  $vals['CiAdrExtadr'] )
1592  . '</span>';
1593  }
1594  if ( isset( $vals['CiAdrCity'] ) ) {
1595  $city = '<span class="locality">'
1596  . $this->literal( $vals['CiAdrCity'] )
1597  . '</span>';
1598  }
1599  if ( isset( $vals['CiAdrCtry'] ) ) {
1600  $country = '<span class="country-name">'
1601  . $this->literal( $vals['CiAdrCtry'] )
1602  . '</span>';
1603  }
1604  if ( isset( $vals['CiEmailWork'] ) ) {
1605  $emails = [];
1606  // Have to split multiple emails at commas/new lines.
1607  $splitEmails = explode( "\n", $vals['CiEmailWork'] );
1608  foreach ( $splitEmails as $e1 ) {
1609  // Also split on comma
1610  foreach ( explode( ',', $e1 ) as $e2 ) {
1611  $finalEmail = trim( $e2 );
1612  if ( $finalEmail == ',' || $finalEmail == '' ) {
1613  continue;
1614  }
1615  if ( strpos( $finalEmail, '<' ) !== false ) {
1616  // Don't do fancy formatting to
1617  // "My name" <foo@bar.com> style stuff
1618  $emails[] = $this->literal( $finalEmail );
1619  } else {
1620  $emails[] = '[mailto:'
1621  . $finalEmail
1622  . ' <span class="email">'
1623  . $this->literal( $finalEmail )
1624  . '</span>]';
1625  }
1626  }
1627  }
1628  $email = implode( ', ', $emails );
1629  }
1630  if ( isset( $vals['CiTelWork'] ) ) {
1631  $tel = '<span class="tel">'
1632  . $this->literal( $vals['CiTelWork'] )
1633  . '</span>';
1634  }
1635  if ( isset( $vals['CiAdrPcode'] ) ) {
1636  $postal = '<span class="postal-code">'
1637  . $this->literal( $vals['CiAdrPcode'] )
1638  . '</span>';
1639  }
1640  if ( isset( $vals['CiAdrRegion'] ) ) {
1641  // Note this is province/state.
1642  $region = '<span class="region">'
1643  . $this->literal( $vals['CiAdrRegion'] )
1644  . '</span>';
1645  }
1646  if ( isset( $vals['CiUrlWork'] ) ) {
1647  $url = '<span class="url">'
1648  . $this->literal( $vals['CiUrlWork'] )
1649  . '</span>';
1650  }
1651 
1652  return $this->msg( 'exif-contact-value', $email, $url,
1653  $street, $city, $region, $postal, $country, $tel )->text();
1654  }
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 ( !empty( $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  } elseif ( isset( $value['_type'] ) && $value['_type'] === 'lang' ) {
1857  // if this is a multilang array, process fields separately
1858  $newValue = [];
1859  foreach ( $value as $k => $v ) {
1860  $newValue[$k] = $this->resolveMultivalueValue( $v );
1861  }
1862  return $newValue;
1863  } else { // _type is 'ul' or 'ol' or missing in which case it defaults to 'ul'
1864  $v = reset( $value );
1865  if ( key( $value ) === '_type' ) {
1866  $v = next( $value );
1867  }
1868  return $v;
1869  }
1870  }
1871 
1878  protected function resolveMultilangMetadata( &$metadata ) {
1879  if ( !is_array( $metadata ) ) {
1880  return;
1881  }
1882  foreach ( $metadata as &$field ) {
1883  if ( isset( $field['value'] ) ) {
1884  $field['value'] = $this->resolveMultilangValue( $field['value'] );
1885  }
1886  }
1887  }
1888 
1895  protected function discardMultipleValues( &$metadata ) {
1896  if ( !is_array( $metadata ) ) {
1897  return;
1898  }
1899  foreach ( $metadata as $key => &$field ) {
1900  if ( $key === 'Software' || $key === 'Contact' ) {
1901  // we skip some fields which have composite values. They are not particularly interesting
1902  // and you can get them via the metadata / commonmetadata APIs anyway.
1903  continue;
1904  }
1905  if ( isset( $field['value'] ) ) {
1906  $field['value'] = $this->resolveMultivalueValue( $field['value'] );
1907  }
1908  }
1909  }
1910 
1915  protected function sanitizeArrayForAPI( &$arr ) {
1916  if ( !is_array( $arr ) ) {
1917  return;
1918  }
1919 
1920  $counter = 1;
1921  foreach ( $arr as $key => &$value ) {
1922  $sanitizedKey = $this->sanitizeKeyForAPI( $key );
1923  if ( $sanitizedKey !== $key ) {
1924  if ( isset( $arr[$sanitizedKey] ) ) {
1925  // Make the sanitized keys hopefully unique.
1926  // To make it definitely unique would be too much effort, given that
1927  // sanitizing is only needed for misformatted metadata anyway, but
1928  // this at least covers the case when $arr is numeric.
1929  $sanitizedKey .= $counter;
1930  ++$counter;
1931  }
1932  $arr[$sanitizedKey] = $arr[$key];
1933  unset( $arr[$key] );
1934  }
1935  if ( is_array( $value ) ) {
1936  $this->sanitizeArrayForAPI( $value );
1937  }
1938  }
1939 
1940  // Handle API metadata keys (particularly "_type")
1941  $keys = array_filter( array_keys( $arr ), [ ApiResult::class, 'isMetadataKey' ] );
1942  if ( $keys ) {
1944  }
1945  }
1946 
1953  protected function sanitizeKeyForAPI( $key ) {
1954  // drop all characters which are not valid in an XML tag name
1955  // a bunch of non-ASCII letters would be valid but probably won't
1956  // be used so we take the easy way
1957  $key = preg_replace( '/[^a-zA-z0-9_:.\-]/', '', $key );
1958  // drop characters which are invalid at the first position
1959  $key = preg_replace( '/^[\d\-.]+/', '', $key );
1960 
1961  if ( $key === '' ) {
1962  $key = '_';
1963  // special case for an internal keyword
1964  } elseif ( $key === '_element' ) {
1965  $key = 'element';
1966  }
1967 
1968  return $key;
1969  }
1970 
1977  protected function getPriorityLanguages() {
1978  $priorityLanguages = MediaWikiServices::getInstance()
1979  ->getLanguageFallback()
1980  ->getAllIncludingSiteLanguage( $this->getLanguage()->getCode() );
1981  $priorityLanguages = array_merge(
1982  (array)$this->getLanguage()->getCode(),
1983  $priorityLanguages[0],
1984  $priorityLanguages[1]
1985  );
1986 
1987  return $priorityLanguages;
1988  }
1989 }
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.
$matches
getContext()
if(!defined('MW_SETUP_CALLBACK'))
The persistent session ID (if any) loaded at startup.
Definition: WebStart.php:82
static setPreserveKeysList(array &$arr, $names)
Preserve specified keys.
Definition: ApiResult.php:663
The simplest way of implementing IContextSource is to hold a RequestContext as a member variable and ...
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
setContext(IContextSource $context)
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:67
const DELETED_FILE
Definition: File.php:71
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.
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:214
MediaWiki exception.
Definition: MWException.php:30
PSR-3 logger instance factory.
Service locator for MediaWiki core services.
foreach( $mmfl['setupFiles'] as $fileName) if( $queue) if(empty( $mmfl['quiet'])) $s
$content
Definition: router.php:76
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
if(!isset( $args[0])) $lang
if(!file_exists( $CREDITS)) $lines