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