24 use Psr\Log\LoggerAwareInterface;
25 use Psr\Log\LoggerInterface;
26 use Psr\Log\NullLogger;
27 use Wikimedia\ScopedCallback;
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';
139 if ( !function_exists(
'xml_parser_create_ns' ) ) {
141 throw new RuntimeException(
'XMP support requires XML Parser' );
166 if ( $this->xmlParser ) {
167 xml_parser_free( $this->xmlParser );
168 $this->xmlParser =
null;
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 );
184 xml_set_element_handler( $this->xmlParser,
185 [ $this,
'startElement' ],
186 [ $this,
'endElement' ] );
188 xml_set_character_data_handler( $this->xmlParser, [ $this,
'char' ] );
191 $this->xmlParsableBuffer =
'';
198 return function_exists(
'xml_parser_create_ns' ) && class_exists(
'XMLReader' );
218 if ( isset( $data[
'xmp-special'][
'AuthorsPosition'] )
219 && is_string( $data[
'xmp-special'][
'AuthorsPosition'] )
220 && isset( $data[
'xmp-general'][
'Artist'][0] )
227 $data[
'xmp-general'][
'Artist'][0] =
228 $data[
'xmp-special'][
'AuthorsPosition'] .
', '
229 . $data[
'xmp-general'][
'Artist'][0];
236 if ( isset( $data[
'xmp-special'][
'LocationShown'][0] )
237 && is_array( $data[
'xmp-special'][
'LocationShown'][0] )
241 foreach ( $data[
'xmp-special'][
'LocationShown']
as $loc ) {
242 if ( !is_array( $loc ) ) {
246 foreach ( $loc
as $field => $val ) {
247 $data[
'xmp-general'][$field .
'Dest'][] = $val;
251 if ( isset( $data[
'xmp-special'][
'LocationCreated'][0] )
252 && is_array( $data[
'xmp-special'][
'LocationCreated'][0] )
256 foreach ( $data[
'xmp-special'][
'LocationCreated']
as $loc ) {
257 if ( !is_array( $loc ) ) {
261 foreach ( $loc
as $field => $val ) {
262 $data[
'xmp-general'][$field .
'Created'][] = $val;
269 unset( $data[
'xmp-special'] );
272 if ( isset( $data[
'xmp-exif'][
'GPSAltitudeRef'] )
273 && isset( $data[
'xmp-exif'][
'GPSAltitude'] )
278 list( $nom, $denom ) = explode(
'/', $data[
'xmp-exif'][
'GPSAltitude'] );
279 $data[
'xmp-exif'][
'GPSAltitude'] = $nom / $denom;
281 if ( $data[
'xmp-exif'][
'GPSAltitudeRef'] ==
'1' ) {
282 $data[
'xmp-exif'][
'GPSAltitude'] *= -1;
284 unset( $data[
'xmp-exif'][
'GPSAltitudeRef'] );
303 if ( !$this->xmlParser ) {
310 if ( !$this->charset ) {
312 if ( preg_match(
'/\xEF\xBB\xBF|\xFE\xFF|\x00\x00\xFE\xFF|\xFF\xFE\x00\x00|\xFF\xFE/',
317 $this->charset =
'UTF-16BE';
320 $this->charset =
'UTF-16LE';
322 case "\x00\x00\xFE\xFF":
323 $this->charset =
'UTF-32BE';
325 case "\xFF\xFE\x00\x00":
326 $this->charset =
'UTF-32LE';
329 $this->charset =
'UTF-8';
333 throw new RuntimeException(
"Invalid BOM" );
337 $this->charset =
'UTF-8';
340 if ( $this->charset !==
'UTF-8' ) {
342 MediaWiki\suppressWarnings();
344 MediaWiki\restoreWarnings();
349 if ( $this->parsable !== self::PARSABLE_OK ) {
350 if ( $this->parsable === self::PARSABLE_NO ) {
351 throw new RuntimeException(
'Unsafe doctype declaration in XML.' );
356 if ( !$allOfIt && $this->parsable !== self::PARSABLE_NO ) {
362 'Unsafe doctype declaration in XML.' :
363 'No root element found in XML.';
364 throw new RuntimeException( $msg );
368 $ok = xml_parse( $this->xmlParser,
$content, $allOfIt );
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 );
376 $this->logger->warning(
377 '{method} : Error reading XMP content: {error} ' .
378 '(line: {line} column: {column} byte offset: {offset})',
380 'method' => __METHOD__,
381 'error_code' =>
$code,
392 }
catch ( Exception
$e ) {
393 $this->logger->warning(
394 '{method} Exception caught while parsing: ' .
$e->getMessage(),
396 'method' => __METHOD__,
422 if ( !isset( $this->results[
'xmp-special'][
'HasExtendedXMP'] )
423 || $this->results[
'xmp-special'][
'HasExtendedXMP'] !== $guid
425 $this->logger->info( __METHOD__ .
426 " Ignoring XMPExtended block due to wrong guid (guid= '$guid')" );
430 $len = unpack(
'Nlength/Noffset', substr(
$content, 32, 8 ) );
433 $len[
'length'] < 4 ||
434 $len[
'offset'] < 0 ||
435 $len[
'offset'] > $len[
'length']
438 __METHOD__ .
'Error reading extended XMP block, invalid length or offset.'
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 .
')' );
461 if ( $len[
'offset'] === 0 ) {
467 $this->extendedXMPOffset += $len[
'length'];
469 $actualContent = substr(
$content, 40 );
471 if ( $this->extendedXMPOffset === strlen( $actualContent ) ) {
477 $this->logger->debug( __METHOD__ .
'Parsing a XMPExtended block' );
479 return $this->
parse( $actualContent, $atEnd );
500 $data = trim( $data );
501 if ( trim( $data ) ===
"" ) {
505 if ( !isset( $this->
mode[0] ) ) {
506 throw new RuntimeException(
'Unexpected character data before first rdf:Description element' );
509 if ( $this->
mode[0] === self::MODE_IGNORE ) {
513 if ( $this->
mode[0] !== self::MODE_SIMPLE
514 && $this->
mode[0] !== self::MODE_QDESC
516 throw new RuntimeException(
'character data where not expected. (mode ' . $this->
mode[0] .
')' );
520 if ( $this->charContent ===
false ) {
521 $this->charContent = $data;
523 $this->charContent .= $data;
536 $reader =
new XMLReader();
542 'data://text/plain,' . urlencode(
$content ),
544 LIBXML_NOERROR | LIBXML_NOWARNING | LIBXML_NONET
547 $oldDisable = libxml_disable_entity_loader(
true );
549 $reset =
new ScopedCallback(
550 'libxml_disable_entity_loader',
553 $reader->setParserProperty( XMLReader::SUBST_ENTITIES,
false );
557 MediaWiki\suppressWarnings();
558 while ( $reader->read() ) {
559 if ( $reader->nodeType === XMLReader::ELEMENT ) {
565 if ( $reader->nodeType === XMLReader::DOC_TYPE ) {
571 MediaWiki\restoreWarnings();
580 $this->xmlParsableBuffer =
$content;
591 if ( $this->curItem[0] === $elm ) {
592 array_shift( $this->curItem );
593 array_shift( $this->
mode );
613 if ( $this->charContent !==
false ) {
614 if ( $this->processingArray ) {
617 list( $ns,
$tag ) = explode(
' ', $this->curItem[0], 2 );
619 list( $ns,
$tag ) = explode(
' ', $elm, 2 );
623 $this->charContent =
false;
625 array_shift( $this->curItem );
626 array_shift( $this->
mode );
651 if ( $this->curItem[0] !== $elm
652 && !( $elm === self::NS_RDF .
' Description'
653 && $this->
mode[0] === self::MODE_STRUCT )
655 throw new RuntimeException(
"nesting mismatch. got a </$elm> but expected a </" .
656 $this->curItem[0] .
'>' );
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;
666 if ( is_array( $info[
'validate'] ) ) {
667 $validate = $info[
'validate'];
670 $validate = [ $validator, $info[
'validate'] ];
673 if ( !isset( $this->results[
'xmp-' . $info[
'map_group']][$finalName] ) ) {
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 ) ) {
682 $this->logger->info( __METHOD__ .
" <$ns:$tag> failed validation." );
683 unset( $this->results[
'xmp-' . $info[
'map_group']][$finalName] );
686 $this->logger->warning( __METHOD__ .
" Validation function for $finalName ("
687 . $validate[0] .
'::' . $validate[1] .
'()) is not callable.' );
691 array_shift( $this->curItem );
692 array_shift( $this->
mode );
693 $this->ancestorStruct =
false;
694 $this->processingArray =
false;
695 $this->itemLang =
false;
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;
723 array_shift( $this->
mode );
725 if ( !isset( $this->results[
'xmp-' . $info[
'map_group']][$finalName] ) ) {
726 $this->logger->debug( __METHOD__ .
" Empty compund element $finalName." );
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' ) {
737 if ( $info[
'mode'] === self::MODE_LANG ) {
738 $this->results[
'xmp-' . $info[
'map_group']][$finalName][
'_type'] =
'lang';
741 throw new RuntimeException(
742 __METHOD__ .
" expected </rdf:seq> or </rdf:bag> but instead got $elm."
759 if ( $elm === self::NS_RDF .
' value' ) {
760 list( $ns,
$tag ) = explode(
' ', $this->curItem[0], 2 );
765 array_shift( $this->
mode );
766 array_shift( $this->curItem );
784 if ( $elm === ( self::NS_RDF .
' RDF' )
785 || $elm ===
'adobe:ns:meta/ xmpmeta'
786 || $elm ===
'adobe:ns:meta/ xapmeta'
792 if ( $elm === self::NS_RDF .
' type' ) {
795 $this->logger->info( __METHOD__ .
' encountered <rdf:type>' );
798 if ( strpos( $elm,
' ' ) ===
false ) {
803 $this->logger->info( __METHOD__ .
" Encountered </$elm> which has no namespace. Skipping." );
811 throw new RuntimeException(
'Encountered end element with no mode' );
814 if (
count( $this->curItem ) == 0 && $this->
mode[0] !== self::MODE_INITIAL ) {
817 throw new RuntimeException(
"Hit end element </$elm> but no curItem" );
820 switch ( $this->
mode[0] ) {
835 if ( $elm === self::NS_RDF .
' Description' ) {
836 array_shift( $this->
mode );
838 throw new RuntimeException(
'Element ended unexpectedly while in MODE_INITIAL' );
849 $this->logger->warning( __METHOD__ .
" no mode (elm = $elm)" );
866 if ( $elm === $this->curItem[0] ) {
867 array_unshift( $this->curItem, $elm );
868 array_unshift( $this->
mode, self::MODE_IGNORE );
880 if ( $elm === self::NS_RDF .
' Bag' ) {
881 array_unshift( $this->
mode, self::MODE_LI );
883 throw new RuntimeException(
"Expected <rdf:Bag> but got $elm." );
895 if ( $elm === self::NS_RDF .
' Seq' ) {
896 array_unshift( $this->
mode, self::MODE_LI );
897 } elseif ( $elm === self::NS_RDF .
' Bag' ) {
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 );
903 throw new RuntimeException(
"Expected <rdf:Seq> but got $elm." );
922 if ( $elm === self::NS_RDF .
' Alt' ) {
923 array_unshift( $this->
mode, self::MODE_LI_LANG );
925 throw new RuntimeException(
"Expected <rdf:Seq> but got $elm." );
948 if ( $elm === self::NS_RDF .
' Description' ) {
950 array_unshift( $this->
mode, self::MODE_QDESC );
951 array_unshift( $this->curItem, $this->curItem[0] );
953 if ( isset(
$attribs[self::NS_RDF .
' value'] ) ) {
954 list( $ns,
$tag ) = explode(
' ', $this->curItem[0], 2 );
957 } elseif ( $elm === self::NS_RDF .
' value' ) {
959 throw new RuntimeException( __METHOD__ .
' Encountered <rdf:value> where it was unexpected.' );
962 $this->logger->info( __METHOD__ .
963 " Encountered element <$elm> where only expecting character data as value of " .
965 array_unshift( $this->
mode, self::MODE_IGNORE );
966 array_unshift( $this->curItem, $elm );
984 if ( $elm === self::NS_RDF .
' value' ) {
988 array_unshift( $this->
mode, self::MODE_IGNORE );
989 array_unshift( $this->curItem, $elm );
1006 if ( $ns !== self::NS_RDF ) {
1008 if ( isset( $this->items[$ns][
$tag] ) ) {
1009 if ( isset( $this->items[$ns][
$tag][
'structPart'] ) ) {
1015 $this->logger->warning(
"Encountered <$ns:$tag> outside"
1016 .
" of its expected parent. Ignoring." );
1018 array_unshift( $this->
mode, self::MODE_IGNORE );
1019 array_unshift( $this->curItem, $ns .
' ' .
$tag );
1023 $mode = $this->items[$ns][
$tag][
'mode'];
1025 array_unshift( $this->curItem, $ns .
' ' .
$tag );
1026 if (
$mode === self::MODE_STRUCT ) {
1027 $this->ancestorStruct = isset( $this->items[$ns][
$tag][
'map_name'] )
1028 ? $this->items[$ns][
$tag][
'map_name'] :
$tag;
1030 if ( $this->charContent !==
false ) {
1033 throw new RuntimeException(
'tag nested in non-whitespace characters.' );
1037 $this->logger->debug( __METHOD__ .
" Ignoring unrecognized element <$ns:$tag>." );
1038 array_unshift( $this->
mode, self::MODE_IGNORE );
1039 array_unshift( $this->curItem, $ns .
' ' .
$tag );
1068 if ( $ns !== self::NS_RDF ) {
1070 if ( isset( $this->items[$ns][
$tag] ) ) {
1071 if ( isset( $this->items[$ns][$this->ancestorStruct][
'children'] )
1072 && !isset( $this->items[$ns][$this->ancestorStruct][
'children'][
$tag] )
1076 throw new RuntimeException(
" <$tag> appeared nested in <" . $this->ancestorStruct
1077 .
"> where it is not allowed." );
1079 array_unshift( $this->
mode, $this->items[$ns][
$tag][
'mode'] );
1080 array_unshift( $this->curItem, $ns .
' ' .
$tag );
1081 if ( $this->charContent !==
false ) {
1084 throw new RuntimeException(
"tag <$tag> nested in non-whitespace characters (" .
1085 $this->charContent .
")." );
1088 array_unshift( $this->
mode, self::MODE_IGNORE );
1089 array_unshift( $this->curItem, $ns .
' ' .
$tag );
1095 if ( $ns === self::NS_RDF &&
$tag ===
'Description' ) {
1097 array_unshift( $this->
mode, self::MODE_STRUCT );
1098 array_unshift( $this->curItem, $this->curItem[0] );
1116 if ( ( $elm ) !== self::NS_RDF .
' li' ) {
1117 throw new RuntimeException(
"<rdf:li> expected but got $elm." );
1120 if ( !isset( $this->
mode[1] ) ) {
1123 throw new RuntimeException(
'In mode Li, but no 2xPrevious mode!' );
1126 if ( $this->
mode[1] === self::MODE_BAGSTRUCT ) {
1128 array_unshift( $this->
mode, self::MODE_STRUCT );
1129 array_unshift( $this->curItem, $elm );
1130 $this->processingArray =
true;
1132 if ( !isset( $this->curItem[1] ) ) {
1134 throw new RuntimeException(
'Can not find parent of BAGSTRUCT.' );
1136 list( $curNS, $curTag ) = explode(
' ', $this->curItem[1] );
1137 $this->ancestorStruct = isset( $this->items[$curNS][$curTag][
'map_name'] )
1138 ? $this->items[$curNS][$curTag][
'map_name'] : $curTag;
1143 array_unshift( $this->
mode, self::MODE_SIMPLE );
1146 array_unshift( $this->curItem, $this->curItem[0] );
1147 $this->processingArray =
true;
1166 if ( $elm !== self::NS_RDF .
' li' ) {
1167 throw new RuntimeException( __METHOD__ .
" <rdf:li> expected but got $elm." );
1169 if ( !isset(
$attribs[self::NS_XML .
' lang'] )
1170 || !preg_match(
'/^[-A-Za-z0-9]{2,}$/D',
$attribs[self::NS_XML .
' lang'] )
1172 throw new RuntimeException( __METHOD__
1173 .
" <rdf:li> did not contain, or has invalid xml:lang attribute in lang alternative" );
1177 $this->itemLang = strtolower(
$attribs[self::NS_XML .
' lang'] );
1181 array_unshift( $this->curItem, $this->curItem[0] );
1182 array_unshift( $this->
mode, self::MODE_SIMPLE );
1183 $this->processingArray =
true;
1198 if ( $elm === self::NS_RDF .
' RDF'
1199 || $elm ===
'adobe:ns:meta/ xmpmeta'
1200 || $elm ===
'adobe:ns:meta/ xapmeta'
1204 } elseif ( $elm === self::NS_RDF .
' Description' ) {
1207 array_unshift( $this->
mode, self::MODE_INITIAL );
1209 } elseif ( $elm === self::NS_RDF .
' type' ) {
1216 $this->logger->info( __METHOD__ .
' Encountered <rdf:type> which isn\'t currently supported' );
1219 if ( strpos( $elm,
' ' ) ===
false ) {
1221 $this->logger->info( __METHOD__ .
" Encountered <$elm> which has no namespace. Skipping." );
1226 list( $ns,
$tag ) = explode(
' ', $elm, 2 );
1230 throw new RuntimeException(
'Error extracting XMP, '
1231 .
"encountered <$elm> with no mode" );
1234 switch ( $this->
mode[0] ) {
1267 throw new RuntimeException(
'StartElement in unknown mode: ' . $this->
mode[0] );
1292 if ( isset(
$attribs[self::NS_RDF .
' parseType'] )
1293 &&
$attribs[self::NS_RDF .
' parseType'] ===
'Resource'
1294 && $this->
mode[0] === self::MODE_SIMPLE
1300 if ( strpos(
$name,
' ' ) ===
false ) {
1303 $this->logger->info( __METHOD__ .
' Encountered non-namespaced attribute: '
1304 .
" $name=\"$val\". Skipping. " );
1308 if ( $ns === self::NS_RDF ) {
1309 if (
$tag ===
'value' ||
$tag ===
'resource' ) {
1312 $this->
char( $this->xmlParser, $val );
1314 } elseif ( isset( $this->items[$ns][
$tag] ) ) {
1315 if ( $this->
mode[0] === self::MODE_SIMPLE ) {
1316 throw new RuntimeException( __METHOD__
1317 .
" $ns:$tag found as attribute where not allowed" );
1321 $this->logger->debug( __METHOD__ .
" Ignoring unrecognized element <$ns:$tag>." );
1339 $info =& $this->items[$ns][
$tag];
1340 $finalName = isset( $info[
'map_name'] )
1341 ? $info[
'map_name'] :
$tag;
1342 if ( isset( $info[
'validate'] ) ) {
1343 if ( is_array( $info[
'validate'] ) ) {
1344 $validate = $info[
'validate'];
1347 $validate = [ $validator, $info[
'validate'] ];
1350 if ( is_callable( $validate ) ) {
1351 call_user_func_array( $validate, [ $info, &$val,
true ] );
1354 if ( is_null( $val ) ) {
1355 $this->logger->info( __METHOD__ .
" <$ns:$tag> failed validation." );
1360 $this->logger->warning( __METHOD__ .
" Validation function for $finalName ("
1361 . $validate[0] .
'::' . $validate[1] .
'()) is not callable.' );
1365 if ( $this->ancestorStruct && $this->processingArray ) {
1368 } elseif ( $this->ancestorStruct ) {
1370 } elseif ( $this->processingArray ) {
1371 if ( $this->itemLang ===
false ) {
1373 $this->results[
'xmp-' . $info[
'map_group']][$finalName][] = $val;
1376 $this->results[
'xmp-' . $info[
'map_group']][$finalName][
$this->itemLang] = $val;
1379 $this->results[
'xmp-' . $info[
'map_group']][$finalName] = $val;