Use consistent notation for "@todo FIXME". Should update http://svn.wikimedia.org...
[lhc/web/wiklou.git] / includes / media / XMP.php
1 <?php
2 /**
3 * Class for reading xmp data containing properties relevant to
4 * images, and spitting out an array that FormatExif accepts.
5 *
6 * Note, this is not meant to recognize every possible thing you can
7 * encode in XMP. It should recognize all the properties we want.
8 * For example it doesn't have support for structures with multiple
9 * nesting levels, as none of the properties we're supporting use that
10 * feature. If it comes across properties it doesn't recognize, it should
11 * ignore them.
12 *
13 * The public methods one would call in this class are
14 * - parse( $content )
15 * Reads in xmp content.
16 * Can potentially be called multiple times with partial data each time.
17 * - parseExtended( $content )
18 * Reads XMPExtended blocks (jpeg files only).
19 * - getResults
20 * Outputs a results array.
21 *
22 * Note XMP kind of looks like rdf. They are not the same thing - XMP is
23 * encoded as a specific subset of rdf. This class can read XMP. It cannot
24 * read rdf.
25 *
26 */
27 class XMPReader {
28
29 private $curItem = array(); // array to hold the current element (and previous element, and so on)
30 private $ancestorStruct = false; // the structure name when processing nested structures.
31 private $charContent = false; // temporary holder for character data that appears in xmp doc.
32 private $mode = array(); // stores the state the xmpreader is in (see MODE_FOO constants)
33 private $results = array(); // array to hold results
34 private $processingArray = false; // if we're doing a seq or bag.
35 private $itemLang = false; // used for lang alts only
36
37 private $xmlParser;
38 private $charset = false;
39 private $extendedXMPOffset = 0;
40
41 protected $items;
42
43 /*
44 * These are various mode constants.
45 * they are used to figure out what to do
46 * with an element when its encountered.
47 *
48 * For example, MODE_IGNORE is used when processing
49 * a property we're not interested in. So if a new
50 * element pops up when we're in that mode, we ignore it.
51 */
52 const MODE_INITIAL = 0;
53 const MODE_IGNORE = 1;
54 const MODE_LI = 2;
55 const MODE_LI_LANG = 3;
56 const MODE_QDESC = 4;
57
58 // The following MODE constants are also used in the
59 // $items array to denote what type of property the item is.
60 const MODE_SIMPLE = 10;
61 const MODE_STRUCT = 11; // structure (associative array)
62 const MODE_SEQ = 12; // ordered list
63 const MODE_BAG = 13; // unordered list
64 const MODE_LANG = 14;
65 const MODE_ALT = 15; // non-language alt. Currently not implemented, and not needed atm.
66 const MODE_BAGSTRUCT = 16; // A BAG of Structs.
67
68 const NS_RDF = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
69 const NS_XML = 'http://www.w3.org/XML/1998/namespace';
70
71
72 /**
73 * Constructor.
74 *
75 * Primary job is to initialize the XMLParser
76 */
77 function __construct() {
78
79 if ( !function_exists( 'xml_parser_create_ns' ) ) {
80 // this should already be checked by this point
81 throw new MWException( 'XMP support requires XML Parser' );
82 }
83
84 $this->items = XMPInfo::getItems();
85
86 $this->resetXMLParser();
87
88 }
89 /**
90 * Main use is if a single item has multiple xmp documents describing it.
91 * For example in jpeg's with extendedXMP
92 */
93 private function resetXMLParser() {
94
95 if ($this->xmlParser) {
96 //is this needed?
97 xml_parser_free( $this->xmlParser );
98 }
99
100 $this->xmlParser = xml_parser_create_ns( 'UTF-8', ' ' );
101 xml_parser_set_option( $this->xmlParser, XML_OPTION_CASE_FOLDING, 0 );
102 xml_parser_set_option( $this->xmlParser, XML_OPTION_SKIP_WHITE, 1 );
103
104 xml_set_element_handler( $this->xmlParser,
105 array( $this, 'startElement' ),
106 array( $this, 'endElement' ) );
107
108 xml_set_character_data_handler( $this->xmlParser, array( $this, 'char' ) );
109 }
110
111 /** Destroy the xml parser
112 *
113 * Not sure if this is actually needed.
114 */
115 function __destruct() {
116 // not sure if this is needed.
117 xml_parser_free( $this->xmlParser );
118 }
119
120 /** Get the result array. Do some post-processing before returning
121 * the array, and transform any metadata that is special-cased.
122 *
123 * @return Array array of results as an array of arrays suitable for
124 * FormatMetadata::getFormattedData().
125 */
126 public function getResults() {
127 // xmp-special is for metadata that affects how stuff
128 // is extracted. For example xmpNote:HasExtendedXMP.
129
130 // It is also used to handle photoshop:AuthorsPosition
131 // which is weird and really part of another property,
132 // see 2:85 in IPTC. See also pg 21 of IPTC4XMP standard.
133 // The location fields also use it.
134
135 $data = $this->results;
136
137 wfRunHooks('XMPGetResults', Array(&$data));
138
139 if ( isset( $data['xmp-special']['AuthorsPosition'] )
140 && is_string( $data['xmp-special']['AuthorsPosition'] )
141 && isset( $data['xmp-general']['Artist'][0] )
142 ) {
143 // Note, if there is more than one creator,
144 // this only applies to first. This also will
145 // only apply to the dc:Creator prop, not the
146 // exif:Artist prop.
147
148 $data['xmp-general']['Artist'][0] =
149 $data['xmp-special']['AuthorsPosition'] . ', '
150 . $data['xmp-general']['Artist'][0];
151 }
152
153 // Go through the LocationShown and LocationCreated
154 // changing it to the non-hierarchal form used by
155 // the other location fields.
156
157 if ( isset( $data['xmp-special']['LocationShown'][0] )
158 && is_array( $data['xmp-special']['LocationShown'][0] )
159 ) {
160 // the is_array is just paranoia. It should always
161 // be an array.
162 foreach( $data['xmp-special']['LocationShown'] as $loc ) {
163 if ( !is_array( $loc ) ) {
164 // To avoid copying over the _type meta-fields.
165 continue;
166 }
167 foreach( $loc as $field => $val ) {
168 $data['xmp-general'][$field . 'Dest'][] = $val;
169 }
170 }
171 }
172 if ( isset( $data['xmp-special']['LocationCreated'][0] )
173 && is_array( $data['xmp-special']['LocationCreated'][0] )
174 ) {
175 // the is_array is just paranoia. It should always
176 // be an array.
177 foreach( $data['xmp-special']['LocationCreated'] as $loc ) {
178 if ( !is_array( $loc ) ) {
179 // To avoid copying over the _type meta-fields.
180 continue;
181 }
182 foreach( $loc as $field => $val ) {
183 $data['xmp-general'][$field . 'Created'][] = $val;
184 }
185 }
186 }
187
188
189 // We don't want to return the special values, since they're
190 // special and not info to be stored about the file.
191 unset( $data['xmp-special'] );
192
193 // Convert GPSAltitude to negative if below sea level.
194 if ( isset( $data['xmp-exif']['GPSAltitudeRef'] ) ) {
195 if ( $data['xmp-exif']['GPSAltitudeRef'] == '1'
196 && isset( $data['xmp-exif']['GPSAltitude'] )
197 ) {
198 $data['xmp-exif']['GPSAltitude'] *= -1;
199 }
200 unset( $data['xmp-exif']['GPSAltitudeRef'] );
201 }
202
203 return $data;
204 }
205
206 /**
207 * Main function to call to parse XMP. Use getResults to
208 * get results.
209 *
210 * Also catches any errors during processing, writes them to
211 * debug log, blanks result array and returns false.
212 *
213 * @param String: $content XMP data
214 * @param Boolean: $allOfIt If this is all the data (true) or if its split up (false). Default true
215 * @param Boolean: $reset - does xml parser need to be reset. Default false
216 * @return Boolean success.
217 */
218 public function parse( $content, $allOfIt = true, $reset = false ) {
219 if ( $reset ) {
220 $this->resetXMLParser();
221 }
222 try {
223
224 // detect encoding by looking for BOM which is supposed to be in processing instruction.
225 // see page 12 of http://www.adobe.com/devnet/xmp/pdfs/XMPSpecificationPart3.pdf
226 if ( !$this->charset ) {
227 $bom = array();
228 if ( preg_match( '/\xEF\xBB\xBF|\xFE\xFF|\x00\x00\xFE\xFF|\xFF\xFE\x00\x00|\xFF\xFE/',
229 $content, $bom )
230 ) {
231 switch ( $bom[0] ) {
232 case "\xFE\xFF":
233 $this->charset = 'UTF-16BE';
234 break;
235 case "\xFF\xFE":
236 $this->charset = 'UTF-16LE';
237 break;
238 case "\x00\x00\xFE\xFF":
239 $this->charset = 'UTF-32BE';
240 break;
241 case "\xFF\xFE\x00\x00":
242 $this->charset = 'UTF-32LE';
243 break;
244 case "\xEF\xBB\xBF":
245 $this->charset = 'UTF-8';
246 break;
247 default:
248 //this should be impossible to get to
249 throw new MWException("Invalid BOM");
250 break;
251
252 }
253
254 } else {
255 // standard specifically says, if no bom assume utf-8
256 $this->charset = 'UTF-8';
257 }
258 }
259 if ( $this->charset !== 'UTF-8' ) {
260 //don't convert if already utf-8
261 wfSuppressWarnings();
262 $content = iconv( $this->charset, 'UTF-8//IGNORE', $content );
263 wfRestoreWarnings();
264 }
265
266 $ok = xml_parse( $this->xmlParser, $content, $allOfIt );
267 if ( !$ok ) {
268 $error = xml_error_string( xml_get_error_code( $this->xmlParser ) );
269 $where = 'line: ' . xml_get_current_line_number( $this->xmlParser )
270 . ' column: ' . xml_get_current_column_number( $this->xmlParser )
271 . ' byte offset: ' . xml_get_current_byte_index( $this->xmlParser );
272
273 wfDebugLog( 'XMP', "XMPReader::parse : Error reading XMP content: $error ($where)" );
274 $this->results = array(); // blank if error.
275 return false;
276 }
277 } catch ( MWException $e ) {
278 wfDebugLog( 'XMP', 'XMP parse error: ' . $e );
279 $this->results = array();
280 return false;
281 }
282 return true;
283 }
284
285 /** Entry point for XMPExtended blocks in jpeg files
286 *
287 * @todo In serious need of testing
288 * @see http://www.adobe.ge/devnet/xmp/pdfs/XMPSpecificationPart3.pdf XMP spec part 3 page 20
289 * @param String $content XMPExtended block minus the namespace signature
290 * @return Boolean If it succeeded.
291 */
292 public function parseExtended( $content ) {
293 // @todo FIXME: This is untested. Hard to find example files
294 // or programs that make such files..
295 $guid = substr( $content, 0, 32 );
296 if ( !isset( $this->results['xmp-special']['HasExtendedXMP'] )
297 || $this->results['xmp-special']['HasExtendedXMP'] !== $guid ) {
298 wfDebugLog('XMP', __METHOD__ . " Ignoring XMPExtended block due to wrong guid (guid= '$guid' )");
299 return false;
300 }
301 $len = unpack( 'Nlength/Noffset', substr( $content, 32, 8 ) );
302
303 if (!$len || $len['length'] < 4 || $len['offset'] < 0 || $len['offset'] > $len['length'] ) {
304 wfDebugLog('XMP', __METHOD__ . 'Error reading extended XMP block, invalid length or offset.');
305 return false;
306 }
307
308
309 // we're not very robust here. we should accept it in the wrong order. To quote
310 // the xmp standard:
311 // "A JPEG writer should write the ExtendedXMP marker segments in order, immediately following the
312 // StandardXMP. However, the JPEG standard does not require preservation of marker segment order. A
313 // robust JPEG reader should tolerate the marker segments in any order."
314 //
315 // otoh the probability that an image will have more than 128k of metadata is rather low...
316 // so the probability that it will have > 128k, and be in the wrong order is very low...
317
318 if ( $len['offset'] !== $this->extendedXMPOffset ) {
319 wfDebugLog('XMP', __METHOD__ . 'Ignoring XMPExtended block due to wrong order. (Offset was '
320 . $len['offset'] . ' but expected ' . $this->extendedXMPOffset . ')');
321 return false;
322 }
323
324 if ( $len['offset'] === 0 ) {
325 // if we're starting the extended block, we've probably already
326 // done the XMPStandard block, so reset.
327 $this->resetXMLParser();
328 }
329
330 $this->extendedXMPOffset += $len['length'];
331
332 $actualContent = substr( $content, 40 );
333
334 if ( $this->extendedXMPOffset === strlen( $actualContent ) ) {
335 $atEnd = true;
336 } else {
337 $atEnd = false;
338 }
339
340 wfDebugLog('XMP', __METHOD__ . 'Parsing a XMPExtended block');
341 return $this->parse( $actualContent, $atEnd );
342 }
343
344 /**
345 * Character data handler
346 * Called whenever character data is found in the xmp document.
347 *
348 * does nothing if we're in MODE_IGNORE or if the data is whitespace
349 * throws an error if we're not in MODE_SIMPLE (as we're not allowed to have character
350 * data in the other modes).
351 *
352 * As an example, this happens when we encounter XMP like:
353 * <exif:DigitalZoomRatio>0/10</exif:DigitalZoomRatio>
354 * and are processing the 0/10 bit.
355 *
356 * @param $parser XMLParser reference to the xml parser
357 * @param $data String Character data
358 * @throws MWException on invalid data
359 */
360 function char( $parser, $data ) {
361
362 $data = trim( $data );
363 if ( trim( $data ) === "" ) {
364 return;
365 }
366
367 if ( !isset( $this->mode[0] ) ) {
368 throw new MWException( 'Unexpected character data before first rdf:Description element' );
369 }
370
371 if ( $this->mode[0] === self::MODE_IGNORE ) return;
372
373 if ( $this->mode[0] !== self::MODE_SIMPLE
374 && $this->mode[0] !== self::MODE_QDESC
375 ) {
376 throw new MWException( 'character data where not expected. (mode ' . $this->mode[0] . ')' );
377 }
378
379 // to check, how does this handle w.s.
380 if ( $this->charContent === false ) {
381 $this->charContent = $data;
382 } else {
383 $this->charContent .= $data;
384 }
385
386 }
387
388 /** When we hit a closing element in MODE_IGNORE
389 * Check to see if this is the element we started to ignore,
390 * in which case we get out of MODE_IGNORE
391 *
392 * @param $elm String Namespace of element followed by a space and then tag name of element.
393 */
394 private function endElementModeIgnore ( $elm ) {
395
396 if ( $this->curItem[0] === $elm ) {
397 array_shift( $this->curItem );
398 array_shift( $this->mode );
399 }
400 return;
401
402 }
403
404 /**
405 * Hit a closing element when in MODE_SIMPLE.
406 * This generally means that we finished processing a
407 * property value, and now have to save the result to the
408 * results array
409 *
410 * For example, when processing:
411 * <exif:DigitalZoomRatio>0/10</exif:DigitalZoomRatio>
412 * this deals with when we hit </exif:DigitalZoomRatio>.
413 *
414 * Or it could be if we hit the end element of a property
415 * of a compound data structure (like a member of an array).
416 *
417 * @param $elm String namespace, space, and tag name.
418 */
419 private function endElementModeSimple ( $elm ) {
420 if ( $this->charContent !== false ) {
421 if ( $this->processingArray ) {
422 // if we're processing an array, use the original element
423 // name instead of rdf:li.
424 list( $ns, $tag ) = explode( ' ', $this->curItem[0], 2 );
425 } else {
426 list( $ns, $tag ) = explode( ' ', $elm, 2 );
427 }
428 $this->saveValue( $ns, $tag, $this->charContent );
429
430 $this->charContent = false; // reset
431 }
432 array_shift( $this->curItem );
433 array_shift( $this->mode );
434
435 }
436
437 /**
438 * Hit a closing element in MODE_STRUCT, MODE_SEQ, MODE_BAG
439 * generally means we've finished processing a nested structure.
440 * resets some internal variables to indicate that.
441 *
442 * Note this means we hit the </closing element> not the </rdf:Seq>.
443 *
444 * For example, when processing:
445 * <exif:ISOSpeedRatings> <rdf:Seq> <rdf:li>64</rdf:li>
446 * </rdf:Seq> </exif:ISOSpeedRatings>
447 *
448 * This method is called when we hit the </exif:ISOSpeedRatings> tag.
449 *
450 * @param $elm String namespace . space . tag name.
451 */
452 private function endElementNested( $elm ) {
453
454 /* cur item must be the same as $elm, unless if in MODE_STRUCT
455 in which case it could also be rdf:Description */
456 if ( $this->curItem[0] !== $elm
457 && !( $elm === self::NS_RDF . ' Description'
458 && $this->mode[0] === self::MODE_STRUCT )
459 ) {
460 throw new MWException( "nesting mismatch. got a </$elm> but expected a </" . $this->curItem[0] . '>' );
461 }
462
463 // Validate structures.
464 list( $ns, $tag ) = explode( ' ', $elm, 2 );
465 if ( isset( $this->items[$ns][$tag]['validate'] ) ) {
466
467 $info =& $this->items[$ns][$tag];
468 $finalName = isset( $info['map_name'] )
469 ? $info['map_name'] : $tag;
470
471 $validate = is_array( $info['validate'] ) ? $info['validate']
472 : array( 'XMPValidate', $info['validate'] );
473
474 if ( !isset( $this->results['xmp-' . $info['map_group']][$finalName] ) ) {
475 // This can happen if all the members of the struct failed validation.
476 wfDebugLog( 'XMP', __METHOD__ . " <$ns:$tag> has no valid members." );
477
478 } elseif ( is_callable( $validate ) ) {
479 $val =& $this->results['xmp-' . $info['map_group']][$finalName];
480 call_user_func_array( $validate, array( $info, &$val, false ) );
481 if ( is_null( $val ) ) {
482 // the idea being the validation function will unset the variable if
483 // its invalid.
484 wfDebugLog( 'XMP', __METHOD__ . " <$ns:$tag> failed validation." );
485 unset( $this->results['xmp-' . $info['map_group']][$finalName] );
486 }
487 } else {
488 wfDebugLog( 'XMP', __METHOD__ . " Validation function for $finalName ("
489 . $validate[0] . '::' . $validate[1] . '()) is not callable.' );
490 }
491 }
492
493 array_shift( $this->curItem );
494 array_shift( $this->mode );
495 $this->ancestorStruct = false;
496 $this->processingArray = false;
497 $this->itemLang = false;
498 }
499
500 /**
501 * Hit a closing element in MODE_LI (either rdf:Seq, or rdf:Bag )
502 * Add information about what type of element this is.
503 *
504 * Note we still have to hit the outer </property>
505 *
506 * For example, when processing:
507 * <exif:ISOSpeedRatings> <rdf:Seq> <rdf:li>64</rdf:li>
508 * </rdf:Seq> </exif:ISOSpeedRatings>
509 *
510 * This method is called when we hit the </rdf:Seq>.
511 * (For comparison, we call endElementModeSimple when we
512 * hit the </rdf:li>)
513 *
514 * @param $elm String namespace . ' ' . element name
515 */
516 private function endElementModeLi( $elm ) {
517
518 list( $ns, $tag ) = explode( ' ', $this->curItem[0], 2 );
519 $info = $this->items[$ns][$tag];
520 $finalName = isset( $info['map_name'] )
521 ? $info['map_name'] : $tag;
522
523 array_shift( $this->mode );
524
525 if ( !isset( $this->results['xmp-' . $info['map_group']][$finalName] ) ) {
526 wfDebugLog( 'XMP', __METHOD__ . " Empty compund element $finalName." );
527 return;
528 }
529
530 if ( $elm === self::NS_RDF . ' Seq' ) {
531 $this->results['xmp-' . $info['map_group']][$finalName]['_type'] = 'ol';
532 } elseif ( $elm === self::NS_RDF . ' Bag' ) {
533 $this->results['xmp-' . $info['map_group']][$finalName]['_type'] = 'ul';
534 } elseif ( $elm === self::NS_RDF . ' Alt' ) {
535 // extra if needed as you could theoretically have a non-language alt.
536 if ( $info['mode'] === self::MODE_LANG ) {
537 $this->results['xmp-' . $info['map_group']][$finalName]['_type'] = 'lang';
538 }
539
540 } else {
541 throw new MWException( __METHOD__ . " expected </rdf:seq> or </rdf:bag> but instead got $elm." );
542 }
543 }
544
545 /**
546 * End element while in MODE_QDESC
547 * mostly when ending an element when we have a simple value
548 * that has qualifiers.
549 *
550 * Qualifiers aren't all that common, and we don't do anything
551 * with them.
552 *
553 * @param $elm String namespace and element
554 */
555 private function endElementModeQDesc( $elm ) {
556
557 if ( $elm === self::NS_RDF . ' value' ) {
558 list( $ns, $tag ) = explode( ' ', $this->curItem[0], 2 );
559 $this->saveValue( $ns, $tag, $this->charContent );
560 return;
561 } else {
562 array_shift( $this->mode );
563 array_shift( $this->curItem );
564 }
565
566
567 }
568
569 /**
570 * Handler for hitting a closing element.
571 *
572 * generally just calls a helper function depending on what
573 * mode we're in.
574 *
575 * Ignores the outer wrapping elements that are optional in
576 * xmp and have no meaning.
577 *
578 * @param $parser XMLParser
579 * @param $elm String namespace . ' ' . element name
580 */
581 function endElement( $parser, $elm ) {
582 if ( $elm === ( self::NS_RDF . ' RDF' )
583 || $elm === 'adobe:ns:meta/ xmpmeta'
584 || $elm === 'adobe:ns:meta/ xapmeta' )
585 {
586 // ignore these.
587 return;
588 }
589
590 if ( $elm === self::NS_RDF . ' type' ) {
591 // these aren't really supported properly yet.
592 // However, it appears they almost never used.
593 wfDebugLog( 'XMP', __METHOD__ . ' encountered <rdf:type>' );
594 }
595
596 if ( strpos( $elm, ' ' ) === false ) {
597 // This probably shouldn't happen.
598 // However, there is a bug in an adobe product
599 // that forgets the namespace on some things.
600 // (Luckily they are unimportant things).
601 wfDebugLog( 'XMP', __METHOD__ . " Encountered </$elm> which has no namespace. Skipping." );
602 return;
603 }
604
605 if ( count( $this->mode[0] ) === 0 ) {
606 // This should never ever happen and means
607 // there is a pretty major bug in this class.
608 throw new MWException( 'Encountered end element with no mode' );
609 }
610
611 if ( count( $this->curItem ) == 0 && $this->mode[0] !== self::MODE_INITIAL ) {
612 // just to be paranoid. Should always have a curItem, except for initially
613 // (aka during MODE_INITAL).
614 throw new MWException( "Hit end element </$elm> but no curItem" );
615 }
616
617 switch( $this->mode[0] ) {
618 case self::MODE_IGNORE:
619 $this->endElementModeIgnore( $elm );
620 break;
621 case self::MODE_SIMPLE:
622 $this->endElementModeSimple( $elm );
623 break;
624 case self::MODE_STRUCT:
625 case self::MODE_SEQ:
626 case self::MODE_BAG:
627 case self::MODE_LANG:
628 case self::MODE_BAGSTRUCT:
629 $this->endElementNested( $elm );
630 break;
631 case self::MODE_INITIAL:
632 if ( $elm === self::NS_RDF . ' Description' ) {
633 array_shift( $this->mode );
634 } else {
635 throw new MWException( 'Element ended unexpectedly while in MODE_INITIAL' );
636 }
637 break;
638 case self::MODE_LI:
639 case self::MODE_LI_LANG:
640 $this->endElementModeLi( $elm );
641 break;
642 case self::MODE_QDESC:
643 $this->endElementModeQDesc( $elm );
644 break;
645 default:
646 wfDebugLog( 'XMP', __METHOD__ . " no mode (elm = $elm)" );
647 break;
648 }
649 }
650
651 /**
652 * Hit an opening element while in MODE_IGNORE
653 *
654 * XMP is extensible, so ignore any tag we don't understand.
655 *
656 * Mostly ignores, unless we encounter the element that we are ignoring.
657 * in which case we add it to the item stack, so we can ignore things
658 * that are nested, correctly.
659 *
660 * @param $elm String namespace . ' ' . tag name
661 */
662 private function startElementModeIgnore( $elm ) {
663 if ( $elm === $this->curItem[0] ) {
664 array_unshift( $this->curItem, $elm );
665 array_unshift( $this->mode, self::MODE_IGNORE );
666 }
667 }
668
669 /**
670 * Start element in MODE_BAG (unordered array)
671 * this should always be <rdf:Bag>
672 *
673 * @param $elm String namespace . ' ' . tag
674 * @throws MWException if we have an element that's not <rdf:Bag>
675 */
676 private function startElementModeBag( $elm ) {
677 if ( $elm === self::NS_RDF . ' Bag' ) {
678 array_unshift( $this->mode, self::MODE_LI );
679 } else {
680 throw new MWException( "Expected <rdf:Bag> but got $elm." );
681 }
682
683 }
684
685 /**
686 * Start element in MODE_SEQ (ordered array)
687 * this should always be <rdf:Seq>
688 *
689 * @param $elm String namespace . ' ' . tag
690 * @throws MWException if we have an element that's not <rdf:Seq>
691 */
692 private function startElementModeSeq( $elm ) {
693 if ( $elm === self::NS_RDF . ' Seq' ) {
694 array_unshift( $this->mode, self::MODE_LI );
695 } else if ( $elm === self::NS_RDF . ' Bag' ) {
696 # bug 27105
697 wfDebugLog( 'XMP', __METHOD__ . ' Expected an rdf:Seq, but got an rdf:Bag. Pretending'
698 . ' it is a Seq, since some buggy software is known to screw this up.' );
699 array_unshift( $this->mode, self::MODE_LI );
700 } else {
701 throw new MWException( "Expected <rdf:Seq> but got $elm." );
702 }
703
704 }
705
706 /**
707 * Start element in MODE_LANG (language alternative)
708 * this should always be <rdf:Alt>
709 *
710 * This tag tends to be used for metadata like describe this
711 * picture, which can be translated into multiple languages.
712 *
713 * XMP supports non-linguistic alternative selections,
714 * which are really only used for thumbnails, which
715 * we don't care about.
716 *
717 * @param $elm String namespace . ' ' . tag
718 * @throws MWException if we have an element that's not <rdf:Alt>
719 */
720 private function startElementModeLang( $elm ) {
721 if ( $elm === self::NS_RDF . ' Alt' ) {
722 array_unshift( $this->mode, self::MODE_LI_LANG );
723 } else {
724 throw new MWException( "Expected <rdf:Seq> but got $elm." );
725 }
726
727 }
728
729 /**
730 * Handle an opening element when in MODE_SIMPLE
731 *
732 * This should not happen often. This is for if a simple element
733 * already opened has a child element. Could happen for a
734 * qualified element.
735 *
736 * For example:
737 * <exif:DigitalZoomRatio><rdf:Description><rdf:value>0/10</rdf:value>
738 * <foo:someQualifier>Bar</foo:someQualifier> </rdf:Description>
739 * </exif:DigitalZoomRatio>
740 *
741 * This method is called when processing the <rdf:Description> element
742 *
743 * @param $elm String namespace and tag names separated by space.
744 * @param $attribs Array Attributes of the element.
745 */
746 private function startElementModeSimple( $elm, $attribs ) {
747 if ( $elm === self::NS_RDF . ' Description' ) {
748 // If this value has qualifiers
749 array_unshift( $this->mode, self::MODE_QDESC );
750 array_unshift( $this->curItem, $this->curItem[0] );
751
752 if ( isset( $attribs[self::NS_RDF . ' value'] ) ) {
753 list( $ns, $tag ) = explode( ' ', $this->curItem[0], 2 );
754 $this->saveValue( $ns, $tag, $attribs[self::NS_RDF . ' value'] );
755 }
756 } elseif ( $elm === self::NS_RDF . ' value' ) {
757 // This should not be here.
758 throw new MWException( __METHOD__ . ' Encountered <rdf:value> where it was unexpected.' );
759
760 } else {
761 // something else we don't recognize, like a qualifier maybe.
762 wfDebugLog( 'XMP', __METHOD__ . " Encountered element <$elm> where only expecting character data as value of " . $this->curItem[0] );
763 array_unshift( $this->mode, self::MODE_IGNORE );
764 array_unshift( $this->curItem, $elm );
765
766 }
767
768 }
769
770 /**
771 * Start an element when in MODE_QDESC.
772 * This generally happens when a simple element has an inner
773 * rdf:Description to hold qualifier elements.
774 *
775 * For example in:
776 * <exif:DigitalZoomRatio><rdf:Description><rdf:value>0/10</rdf:value>
777 * <foo:someQualifier>Bar</foo:someQualifier> </rdf:Description>
778 * </exif:DigitalZoomRatio>
779 * Called when processing the <rdf:value> or <foo:someQualifier>.
780 *
781 * @param $elm String namespace and tag name separated by a space.
782 *
783 */
784 private function startElementModeQDesc( $elm ) {
785 if ( $elm === self::NS_RDF . ' value' ) {
786 return; // do nothing
787 } else {
788 // otherwise its a qualifier, which we ignore
789 array_unshift( $this->mode, self::MODE_IGNORE );
790 array_unshift( $this->curItem, $elm );
791 }
792 }
793
794 /**
795 * Starting an element when in MODE_INITIAL
796 * This usually happens when we hit an element inside
797 * the outer rdf:Description
798 *
799 * This is generally where most properties start.
800 *
801 * @param $ns String Namespace
802 * @param $tag String tag name (without namespace prefix)
803 * @param $attribs Array array of attributes
804 */
805 private function startElementModeInitial( $ns, $tag, $attribs ) {
806 if ( $ns !== self::NS_RDF ) {
807
808 if ( isset( $this->items[$ns][$tag] ) ) {
809 if ( isset( $this->items[$ns][$tag]['structPart'] ) ) {
810 // If this element is supposed to appear only as
811 // a child of a structure, but appears here (not as
812 // a child of a struct), then something weird is
813 // happening, so ignore this element and its children.
814
815 wfDebugLog( 'XMP', "Encountered <$ns:$tag> outside"
816 . " of its expected parent. Ignoring." );
817
818 array_unshift( $this->mode, self::MODE_IGNORE );
819 array_unshift( $this->curItem, $ns . ' ' . $tag );
820 return;
821 }
822 $mode = $this->items[$ns][$tag]['mode'];
823 array_unshift( $this->mode, $mode );
824 array_unshift( $this->curItem, $ns . ' ' . $tag );
825 if ( $mode === self::MODE_STRUCT ) {
826 $this->ancestorStruct = isset( $this->items[$ns][$tag]['map_name'] )
827 ? $this->items[$ns][$tag]['map_name'] : $tag;
828 }
829 if ( $this->charContent !== false ) {
830 // Something weird.
831 // Should not happen in valid XMP.
832 throw new MWException( 'tag nested in non-whitespace characters.' );
833 }
834 } else {
835 // This element is not on our list of allowed elements so ignore.
836 wfDebugLog( 'XMP', __METHOD__ . " Ignoring unrecognized element <$ns:$tag>." );
837 array_unshift( $this->mode, self::MODE_IGNORE );
838 array_unshift( $this->curItem, $ns . ' ' . $tag );
839 return;
840 }
841
842 }
843 // process attributes
844 $this->doAttribs( $attribs );
845 }
846
847 /**
848 * Hit an opening element when in a Struct (MODE_STRUCT)
849 * This is generally for fields of a compound property.
850 *
851 * Example of a struct (abbreviated; flash has more properties):
852 *
853 * <exif:Flash> <rdf:Description> <exif:Fired>True</exif:Fired>
854 * <exif:Mode>1</exif:Mode></rdf:Description></exif:Flash>
855 *
856 * or:
857 *
858 * <exif:Flash rdf:parseType='Resource'> <exif:Fired>True</exif:Fired>
859 * <exif:Mode>1</exif:Mode></exif:Flash>
860 *
861 * @param $ns String namespace
862 * @param $tag String tag name (no ns)
863 * @param $attribs Array array of attribs w/ values.
864 */
865 private function startElementModeStruct( $ns, $tag, $attribs ) {
866 if ( $ns !== self::NS_RDF ) {
867
868 if ( isset( $this->items[$ns][$tag] ) ) {
869 if ( isset( $this->items[$ns][$this->ancestorStruct]['children'] )
870 && !isset( $this->items[$ns][$this->ancestorStruct]['children'][$tag] ) )
871 {
872 // This assumes that we don't have inter-namespace nesting
873 // which we don't in all the properties we're interested in.
874 throw new MWException( " <$tag> appeared nested in <" . $this->ancestorStruct
875 . "> where it is not allowed." );
876 }
877 array_unshift( $this->mode, $this->items[$ns][$tag]['mode'] );
878 array_unshift( $this->curItem, $ns . ' ' . $tag );
879 if ( $this->charContent !== false ) {
880 // Something weird.
881 // Should not happen in valid XMP.
882 throw new MWException( "tag <$tag> nested in non-whitespace characters (" . $this->charContent . ")." );
883 }
884 } else {
885 array_unshift( $this->mode, self::MODE_IGNORE );
886 array_unshift( $this->curItem, $elm );
887 return;
888 }
889
890 }
891
892 if ( $ns === self::NS_RDF && $tag === 'Description' ) {
893 $this->doAttribs( $attribs );
894 array_unshift( $this->mode, self::MODE_STRUCT );
895 array_unshift( $this->curItem, $this->curItem[0] );
896 }
897 }
898
899 /**
900 * opening element in MODE_LI
901 * process elements of arrays.
902 *
903 * Example:
904 * <exif:ISOSpeedRatings> <rdf:Seq> <rdf:li>64</rdf:li>
905 * </rdf:Seq> </exif:ISOSpeedRatings>
906 * This method is called when we hit the <rdf:li> element.
907 *
908 * @param $elm String: namespace . ' ' . tagname
909 * @param $attribs Array: Attributes. (needed for BAGSTRUCTS)
910 * @throws MWException if gets a tag other than <rdf:li>
911 */
912 private function startElementModeLi( $elm, $attribs ) {
913 if ( ( $elm ) !== self::NS_RDF . ' li' ) {
914 throw new MWException( "<rdf:li> expected but got $elm." );
915 }
916
917 if ( !isset( $this->mode[1] ) ) {
918 // This should never ever ever happen. Checking for it
919 // to be paranoid.
920 throw new MWException( 'In mode Li, but no 2xPrevious mode!' );
921 }
922
923 if ( $this->mode[1] === self::MODE_BAGSTRUCT ) {
924 // This list item contains a compound (STRUCT) value.
925 array_unshift( $this->mode, self::MODE_STRUCT );
926 array_unshift( $this->curItem, $elm );
927 $this->processingArray = true;
928
929 if ( !isset( $this->curItem[1] ) ) {
930 // be paranoid.
931 throw new MWException( 'Can not find parent of BAGSTRUCT.' );
932 }
933 list( $curNS, $curTag ) = explode( ' ', $this->curItem[1] );
934 $this->ancestorStruct = isset( $this->items[$curNS][$curTag]['map_name'] )
935 ? $this->items[$curNS][$curTag]['map_name'] : $curTag;
936
937 $this->doAttribs( $attribs );
938
939 } else {
940 // Normal BAG or SEQ containing simple values.
941 array_unshift( $this->mode, self::MODE_SIMPLE );
942 // need to add curItem[0] on again since one is for the specific item
943 // and one is for the entire group.
944 array_unshift( $this->curItem, $this->curItem[0] );
945 $this->processingArray = true;
946 }
947
948 }
949
950 /**
951 * Opening element in MODE_LI_LANG.
952 * process elements of language alternatives
953 *
954 * Example:
955 * <dc:title> <rdf:Alt> <rdf:li xml:lang="x-default">My house
956 * </rdf:li> </rdf:Alt> </dc:title>
957 *
958 * This method is called when we hit the <rdf:li> element.
959 *
960 * @param $elm String namespace . ' ' . tag
961 * @param $attribs array array of elements (most importantly xml:lang)
962 * @throws MWException if gets a tag other than <rdf:li> or if no xml:lang
963 */
964 private function startElementModeLiLang( $elm, $attribs ) {
965 if ( $elm !== self::NS_RDF . ' li' ) {
966 throw new MWException( __METHOD__ . " <rdf:li> expected but got $elm." );
967 }
968 if ( !isset( $attribs[ self::NS_XML . ' lang'] )
969 || !preg_match( '/^[-A-Za-z0-9]{2,}$/D', $attribs[ self::NS_XML . ' lang' ] ) )
970 {
971 throw new MWException( __METHOD__
972 . " <rdf:li> did not contain, or has invalid xml:lang attribute in lang alternative" );
973 }
974
975 // Lang is case-insensitive.
976 $this->itemLang = strtolower( $attribs[ self::NS_XML . ' lang' ] );
977
978 // need to add curItem[0] on again since one is for the specific item
979 // and one is for the entire group.
980 array_unshift( $this->curItem, $this->curItem[0] );
981 array_unshift( $this->mode, self::MODE_SIMPLE );
982 $this->processingArray = true;
983 }
984
985 /**
986 * Hits an opening element.
987 * Generally just calls a helper based on what MODE we're in.
988 * Also does some initial set up for the wrapper element
989 *
990 * @param $parser XMLParser
991 * @param $elm String namespace <space> element
992 * @param $attribs Array attribute name => value
993 */
994 function startElement( $parser, $elm, $attribs ) {
995
996 if ( $elm === self::NS_RDF . ' RDF'
997 || $elm === 'adobe:ns:meta/ xmpmeta'
998 || $elm === 'adobe:ns:meta/ xapmeta')
999 {
1000 /* ignore. */
1001 return;
1002 } elseif ( $elm === self::NS_RDF . ' Description' ) {
1003 if ( count( $this->mode ) === 0 ) {
1004 // outer rdf:desc
1005 array_unshift( $this->mode, self::MODE_INITIAL );
1006 }
1007 } elseif ( $elm === self::NS_RDF . ' type' ) {
1008 // This doesn't support rdf:type properly.
1009 // In practise I have yet to see a file that
1010 // uses this element, however it is mentioned
1011 // on page 25 of part 1 of the xmp standard.
1012 //
1013 // also it seems as if exiv2 and exiftool do not support
1014 // this either (That or I misunderstand the standard)
1015 wfDebugLog( 'XMP', __METHOD__ . ' Encountered <rdf:type> which isn\'t currently supported' );
1016 }
1017
1018 if ( strpos( $elm, ' ' ) === false ) {
1019 // This probably shouldn't happen.
1020 wfDebugLog( 'XMP', __METHOD__ . " Encountered <$elm> which has no namespace. Skipping." );
1021 return;
1022 }
1023
1024 list( $ns, $tag ) = explode( ' ', $elm, 2 );
1025
1026 if ( count( $this->mode ) === 0 ) {
1027 // This should not happen.
1028 throw new MWException('Error extracting XMP, '
1029 . "encountered <$elm> with no mode" );
1030 }
1031
1032 switch( $this->mode[0] ) {
1033 case self::MODE_IGNORE:
1034 $this->startElementModeIgnore( $elm );
1035 break;
1036 case self::MODE_SIMPLE:
1037 $this->startElementModeSimple( $elm, $attribs );
1038 break;
1039 case self::MODE_INITIAL:
1040 $this->startElementModeInitial( $ns, $tag, $attribs );
1041 break;
1042 case self::MODE_STRUCT:
1043 $this->startElementModeStruct( $ns, $tag, $attribs );
1044 break;
1045 case self::MODE_BAG:
1046 case self::MODE_BAGSTRUCT:
1047 $this->startElementModeBag( $elm );
1048 break;
1049 case self::MODE_SEQ:
1050 $this->startElementModeSeq( $elm );
1051 break;
1052 case self::MODE_LANG:
1053 $this->startElementModeLang( $elm );
1054 break;
1055 case self::MODE_LI_LANG:
1056 $this->startElementModeLiLang( $elm, $attribs );
1057 break;
1058 case self::MODE_LI:
1059 $this->startElementModeLi( $elm, $attribs );
1060 break;
1061 case self::MODE_QDESC:
1062 $this->startElementModeQDesc( $elm );
1063 break;
1064 default:
1065 throw new MWException( 'StartElement in unknown mode: ' . $this->mode[0] );
1066 break;
1067 }
1068 }
1069
1070 /**
1071 * Process attributes.
1072 * Simple values can be stored as either a tag or attribute
1073 *
1074 * Often the initial <rdf:Description> tag just has all the simple
1075 * properties as attributes.
1076 *
1077 * Example:
1078 * <rdf:Description rdf:about="" xmlns:exif="http://ns.adobe.com/exif/1.0/" exif:DigitalZoomRatio="0/10">
1079 *
1080 * @param $attribs Array attribute=>value array.
1081 */
1082 private function doAttribs( $attribs ) {
1083
1084 // first check for rdf:parseType attribute, as that can change
1085 // how the attributes are interperted.
1086
1087 if ( isset( $attribs[self::NS_RDF . ' parseType'] )
1088 && $attribs[self::NS_RDF . ' parseType'] === 'Resource'
1089 && $this->mode[0] === self::MODE_SIMPLE )
1090 {
1091 // this is equivalent to having an inner rdf:Description
1092 $this->mode[0] = self::MODE_QDESC;
1093 }
1094 foreach ( $attribs as $name => $val ) {
1095
1096
1097 if ( strpos( $name, ' ' ) === false ) {
1098 // This shouldn't happen, but so far some old software forgets namespace
1099 // on rdf:about.
1100 wfDebugLog( 'XMP', __METHOD__ . ' Encountered non-namespaced attribute: '
1101 . " $name=\"$val\". Skipping. " );
1102 continue;
1103 }
1104 list( $ns, $tag ) = explode( ' ', $name, 2 );
1105 if ( $ns === self::NS_RDF ) {
1106 if ( $tag === 'value' || $tag === 'resource' ) {
1107 // resource is for url.
1108 // value attribute is a weird way of just putting the contents.
1109 $this->char( $this->xmlParser, $val );
1110 }
1111 } elseif ( isset( $this->items[$ns][$tag] ) ) {
1112 if ( $this->mode[0] === self::MODE_SIMPLE ) {
1113 throw new MWException( __METHOD__
1114 . " $ns:$tag found as attribute where not allowed" );
1115 }
1116 $this->saveValue( $ns, $tag, $val );
1117 } else {
1118 wfDebugLog( 'XMP', __METHOD__ . " Ignoring unrecognized element <$ns:$tag>." );
1119 }
1120 }
1121 }
1122
1123 /**
1124 * Given an extracted value, save it to results array
1125 *
1126 * note also uses $this->ancestorStruct and
1127 * $this->processingArray to determine what name to
1128 * save the value under. (in addition to $tag).
1129 *
1130 * @param $ns String namespace of tag this is for
1131 * @param $tag String tag name
1132 * @param $val String value to save
1133 */
1134 private function saveValue( $ns, $tag, $val ) {
1135
1136 $info =& $this->items[$ns][$tag];
1137 $finalName = isset( $info['map_name'] )
1138 ? $info['map_name'] : $tag;
1139 if ( isset( $info['validate'] ) ) {
1140 $validate = is_array( $info['validate'] ) ? $info['validate']
1141 : array( 'XMPValidate', $info['validate'] );
1142
1143 if ( is_callable( $validate ) ) {
1144 call_user_func_array( $validate, array( $info, &$val, true ) );
1145 // the reasoning behind using &$val instead of using the return value
1146 // is to be consistent between here and validating structures.
1147 if ( is_null( $val ) ) {
1148 wfDebugLog( 'XMP', __METHOD__ . " <$ns:$tag> failed validation." );
1149 return;
1150 }
1151 } else {
1152 wfDebugLog( 'XMP', __METHOD__ . " Validation function for $finalName ("
1153 . $validate[0] . '::' . $validate[1] . '()) is not callable.' );
1154 }
1155 }
1156
1157 if ( $this->ancestorStruct && $this->processingArray ) {
1158 // Aka both an array and a struct. ( self::MODE_BAGSTRUCT )
1159 $this->results['xmp-' . $info['map_group']][$this->ancestorStruct][][$finalName] = $val;
1160 } elseif ( $this->ancestorStruct ) {
1161 $this->results['xmp-' . $info['map_group']][$this->ancestorStruct][$finalName] = $val;
1162 } elseif ( $this->processingArray ) {
1163 if ( $this->itemLang === false ) {
1164 // normal array
1165 $this->results['xmp-' . $info['map_group']][$finalName][] = $val;
1166 } else {
1167 // lang array.
1168 $this->results['xmp-' . $info['map_group']][$finalName][$this->itemLang] = $val;
1169 }
1170 } else {
1171 $this->results['xmp-' . $info['map_group']][$finalName] = $val;
1172 }
1173 }
1174 }