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