114 const NS_RDF =
'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
115 const NS_XML =
'http://www.w3.org/XML/1998/namespace';
130 if ( !function_exists(
'xml_parser_create_ns' ) ) {
132 throw new MWException(
'XMP support requires XML Parser' );
146 if ( $this->xmlParser ) {
148 xml_parser_free( $this->xmlParser );
151 $this->xmlParser = xml_parser_create_ns(
'UTF-8',
' ' );
152 xml_parser_set_option( $this->xmlParser, XML_OPTION_CASE_FOLDING, 0 );
153 xml_parser_set_option( $this->xmlParser, XML_OPTION_SKIP_WHITE, 1 );
155 xml_set_element_handler( $this->xmlParser,
157 array( $this,
'endElement' ) );
159 xml_set_character_data_handler( $this->xmlParser,
array( $this,
'char' ) );
162 $this->xmlParsableBuffer =
'';
171 xml_parser_free( $this->xmlParser );
178 return function_exists(
'xml_parser_create_ns' ) && class_exists(
'XMLReader' );
200 if ( isset( $data[
'xmp-special'][
'AuthorsPosition'] )
201 && is_string( $data[
'xmp-special'][
'AuthorsPosition'] )
202 && isset( $data[
'xmp-general'][
'Artist'][0] )
209 $data[
'xmp-general'][
'Artist'][0] =
210 $data[
'xmp-special'][
'AuthorsPosition'] .
', '
211 . $data[
'xmp-general'][
'Artist'][0];
218 if ( isset( $data[
'xmp-special'][
'LocationShown'][0] )
219 && is_array( $data[
'xmp-special'][
'LocationShown'][0] )
223 foreach ( $data[
'xmp-special'][
'LocationShown']
as $loc ) {
224 if ( !is_array( $loc ) ) {
228 foreach ( $loc
as $field => $val ) {
229 $data[
'xmp-general'][$field .
'Dest'][] = $val;
233 if ( isset( $data[
'xmp-special'][
'LocationCreated'][0] )
234 && is_array( $data[
'xmp-special'][
'LocationCreated'][0] )
238 foreach ( $data[
'xmp-special'][
'LocationCreated']
as $loc ) {
239 if ( !is_array( $loc ) ) {
243 foreach ( $loc
as $field => $val ) {
244 $data[
'xmp-general'][$field .
'Created'][] = $val;
251 unset( $data[
'xmp-special'] );
254 if ( isset( $data[
'xmp-exif'][
'GPSAltitudeRef'] )
255 && isset( $data[
'xmp-exif'][
'GPSAltitude'] )
260 list( $nom, $denom ) = explode(
'/', $data[
'xmp-exif'][
'GPSAltitude'] );
261 $data[
'xmp-exif'][
'GPSAltitude'] = $nom / $denom;
263 if ( $data[
'xmp-exif'][
'GPSAltitudeRef'] ==
'1' ) {
264 $data[
'xmp-exif'][
'GPSAltitude'] *= -1;
266 unset( $data[
'xmp-exif'][
'GPSAltitudeRef'] );
285 public function parse( $content, $allOfIt =
true, $reset =
false ) {
293 if ( !$this->charset ) {
295 if ( preg_match(
'/\xEF\xBB\xBF|\xFE\xFF|\x00\x00\xFE\xFF|\xFF\xFE\x00\x00|\xFF\xFE/',
300 $this->charset =
'UTF-16BE';
303 $this->charset =
'UTF-16LE';
305 case "\x00\x00\xFE\xFF":
306 $this->charset =
'UTF-32BE';
308 case "\xFF\xFE\x00\x00":
309 $this->charset =
'UTF-32LE';
312 $this->charset =
'UTF-8';
320 $this->charset =
'UTF-8';
323 if ( $this->charset !==
'UTF-8' ) {
326 $content = iconv( $this->charset,
'UTF-8//IGNORE', $content );
332 if ( $this->parsable !== self::PARSABLE_OK ) {
333 if ( $this->parsable === self::PARSABLE_NO ) {
334 throw new MWException(
'Unsafe doctype declaration in XML.' );
337 $content = $this->xmlParsableBuffer . $content;
339 if ( !$allOfIt && $this->parsable !== self::PARSABLE_NO ) {
345 'Unsafe doctype declaration in XML.' :
346 'No root element found in XML.';
351 $ok = xml_parse( $this->xmlParser, $content, $allOfIt );
353 $error = xml_error_string( xml_get_error_code( $this->xmlParser ) );
354 $where =
'line: ' . xml_get_current_line_number( $this->xmlParser )
355 .
' column: ' . xml_get_current_column_number( $this->xmlParser )
356 .
' byte offset: ' . xml_get_current_byte_index( $this->xmlParser );
358 wfDebugLog(
'XMP',
"XMPReader::parse : Error reading XMP content: $error ($where)" );
359 $this->results =
array();
364 $this->results =
array();
382 $guid = substr( $content, 0, 32 );
383 if ( !isset( $this->results[
'xmp-special'][
'HasExtendedXMP'] )
384 || $this->results[
'xmp-special'][
'HasExtendedXMP'] !== $guid
387 " Ignoring XMPExtended block due to wrong guid (guid= '$guid')" );
391 $len = unpack(
'Nlength/Noffset', substr( $content, 32, 8 ) );
393 if ( !$len || $len[
'length'] < 4 || $len[
'offset'] < 0 || $len[
'offset'] > $len[
'length'] ) {
394 wfDebugLog(
'XMP', __METHOD__ .
'Error reading extended XMP block, invalid length or offset.' );
410 if ( $len[
'offset'] !== $this->extendedXMPOffset ) {
411 wfDebugLog(
'XMP', __METHOD__ .
'Ignoring XMPExtended block due to wrong order. (Offset was '
412 . $len[
'offset'] .
' but expected ' . $this->extendedXMPOffset .
')' );
417 if ( $len[
'offset'] === 0 ) {
423 $this->extendedXMPOffset += $len[
'length'];
425 $actualContent = substr( $content, 40 );
427 if ( $this->extendedXMPOffset === strlen( $actualContent ) ) {
433 wfDebugLog(
'XMP', __METHOD__ .
'Parsing a XMPExtended block' );
435 return $this->
parse( $actualContent, $atEnd );
456 $data = trim( $data );
457 if ( trim( $data ) ===
"" ) {
461 if ( !isset( $this->mode[0] ) ) {
462 throw new MWException(
'Unexpected character data before first rdf:Description element' );
465 if ( $this->mode[0] === self::MODE_IGNORE ) {
469 if ( $this->mode[0] !== self::MODE_SIMPLE
470 && $this->mode[0] !== self::MODE_QDESC
472 throw new MWException(
'character data where not expected. (mode ' . $this->mode[0] .
')' );
476 if ( $this->charContent ===
false ) {
477 $this->charContent = $data;
479 $this->charContent .= $data;
492 $reader =
new XMLReader();
498 'data://text/plain,' . urlencode( $content ),
500 LIBXML_NOERROR | LIBXML_NOWARNING | LIBXML_NONET
503 $oldDisable = libxml_disable_entity_loader(
true );
504 $reader->setParserProperty( XMLReader::SUBST_ENTITIES,
false );
509 while ( $reader->read() ) {
510 if ( $reader->nodeType === XMLReader::ELEMENT ) {
516 if ( $reader->nodeType === XMLReader::DOC_TYPE ) {
523 libxml_disable_entity_loader( $oldDisable );
532 $this->xmlParsableBuffer = $content;
543 if ( $this->curItem[0] === $elm ) {
544 array_shift( $this->curItem );
545 array_shift( $this->mode );
565 if ( $this->charContent !==
false ) {
566 if ( $this->processingArray ) {
569 list( $ns, $tag ) = explode(
' ', $this->curItem[0], 2 );
571 list( $ns, $tag ) = explode(
' ', $elm, 2 );
573 $this->
saveValue( $ns, $tag, $this->charContent );
575 $this->charContent =
false;
577 array_shift( $this->curItem );
578 array_shift( $this->mode );
603 if ( $this->curItem[0] !== $elm
604 && !( $elm === self::NS_RDF .
' Description'
605 && $this->mode[0] === self::MODE_STRUCT )
607 throw new MWException(
"nesting mismatch. got a </$elm> but expected a </" .
608 $this->curItem[0] .
'>' );
612 list( $ns, $tag ) = explode(
' ', $elm, 2 );
613 if ( isset( $this->items[$ns][$tag][
'validate'] ) ) {
615 $info =& $this->items[$ns][$tag];
616 $finalName = isset( $info[
'map_name'] )
617 ? $info[
'map_name'] : $tag;
619 $validate = is_array( $info[
'validate'] ) ? $info[
'validate']
620 :
array(
'XMPValidate', $info[
'validate'] );
622 if ( !isset( $this->results[
'xmp-' . $info[
'map_group']][$finalName] ) ) {
624 wfDebugLog(
'XMP', __METHOD__ .
" <$ns:$tag> has no valid members." );
625 } elseif ( is_callable( $validate ) ) {
626 $val =& $this->results[
'xmp-' . $info[
'map_group']][$finalName];
627 call_user_func_array( $validate,
array( $info, &$val,
false ) );
628 if ( is_null( $val ) ) {
631 wfDebugLog(
'XMP', __METHOD__ .
" <$ns:$tag> failed validation." );
632 unset( $this->results[
'xmp-' . $info[
'map_group']][$finalName] );
635 wfDebugLog(
'XMP', __METHOD__ .
" Validation function for $finalName ("
636 . $validate[0] .
'::' . $validate[1] .
'()) is not callable.' );
640 array_shift( $this->curItem );
641 array_shift( $this->mode );
642 $this->ancestorStruct =
false;
643 $this->processingArray =
false;
644 $this->itemLang =
false;
668 list( $ns, $tag ) = explode(
' ', $this->curItem[0], 2 );
669 $info = $this->items[$ns][$tag];
670 $finalName = isset( $info[
'map_name'] )
671 ? $info[
'map_name'] : $tag;
673 array_shift( $this->mode );
675 if ( !isset( $this->results[
'xmp-' . $info[
'map_group']][$finalName] ) ) {
676 wfDebugLog(
'XMP', __METHOD__ .
" Empty compund element $finalName." );
681 if ( $elm === self::NS_RDF .
' Seq' ) {
682 $this->results[
'xmp-' . $info[
'map_group']][$finalName][
'_type'] =
'ol';
683 } elseif ( $elm === self::NS_RDF .
' Bag' ) {
684 $this->results[
'xmp-' . $info[
'map_group']][$finalName][
'_type'] =
'ul';
685 } elseif ( $elm === self::NS_RDF .
' Alt' ) {
687 if ( $info[
'mode'] === self::MODE_LANG ) {
688 $this->results[
'xmp-' . $info[
'map_group']][$finalName][
'_type'] =
'lang';
691 throw new MWException( __METHOD__ .
" expected </rdf:seq> or </rdf:bag> but instead got $elm." );
707 if ( $elm === self::NS_RDF .
' value' ) {
708 list( $ns, $tag ) = explode(
' ', $this->curItem[0], 2 );
709 $this->
saveValue( $ns, $tag, $this->charContent );
713 array_shift( $this->mode );
714 array_shift( $this->curItem );
732 if ( $elm === ( self::NS_RDF .
' RDF' )
733 || $elm ===
'adobe:ns:meta/ xmpmeta'
734 || $elm ===
'adobe:ns:meta/ xapmeta'
740 if ( $elm === self::NS_RDF .
' type' ) {
743 wfDebugLog(
'XMP', __METHOD__ .
' encountered <rdf:type>' );
746 if ( strpos( $elm,
' ' ) ===
false ) {
751 wfDebugLog(
'XMP', __METHOD__ .
" Encountered </$elm> which has no namespace. Skipping." );
756 if ( count( $this->mode[0] ) === 0 ) {
759 throw new MWException(
'Encountered end element with no mode' );
762 if ( count( $this->curItem ) == 0 && $this->mode[0] !== self::MODE_INITIAL ) {
765 throw new MWException(
"Hit end element </$elm> but no curItem" );
768 switch ( $this->mode[0] ) {
783 if ( $elm === self::NS_RDF .
' Description' ) {
784 array_shift( $this->mode );
786 throw new MWException(
'Element ended unexpectedly while in MODE_INITIAL' );
797 wfDebugLog(
'XMP', __METHOD__ .
" no mode (elm = $elm)" );
814 if ( $elm === $this->curItem[0] ) {
815 array_unshift( $this->curItem, $elm );
816 array_unshift( $this->mode, self::MODE_IGNORE );
828 if ( $elm === self::NS_RDF .
' Bag' ) {
829 array_unshift( $this->mode, self::MODE_LI );
831 throw new MWException(
"Expected <rdf:Bag> but got $elm." );
843 if ( $elm === self::NS_RDF .
' Seq' ) {
844 array_unshift( $this->mode, self::MODE_LI );
845 } elseif ( $elm === self::NS_RDF .
' Bag' ) {
847 wfDebugLog(
'XMP', __METHOD__ .
' Expected an rdf:Seq, but got an rdf:Bag. Pretending'
848 .
' it is a Seq, since some buggy software is known to screw this up.' );
849 array_unshift( $this->mode, self::MODE_LI );
851 throw new MWException(
"Expected <rdf:Seq> but got $elm." );
870 if ( $elm === self::NS_RDF .
' Alt' ) {
871 array_unshift( $this->mode, self::MODE_LI_LANG );
873 throw new MWException(
"Expected <rdf:Seq> but got $elm." );
896 if ( $elm === self::NS_RDF .
' Description' ) {
898 array_unshift( $this->mode, self::MODE_QDESC );
899 array_unshift( $this->curItem, $this->curItem[0] );
901 if ( isset(
$attribs[self::NS_RDF .
' value'] ) ) {
902 list( $ns, $tag ) = explode(
' ', $this->curItem[0], 2 );
905 } elseif ( $elm === self::NS_RDF .
' value' ) {
907 throw new MWException( __METHOD__ .
' Encountered <rdf:value> where it was unexpected.' );
911 " Encountered element <$elm> where only expecting character data as value of " .
913 array_unshift( $this->mode, self::MODE_IGNORE );
914 array_unshift( $this->curItem, $elm );
933 if ( $elm === self::NS_RDF .
' value' ) {
937 array_unshift( $this->mode, self::MODE_IGNORE );
938 array_unshift( $this->curItem, $elm );
955 if ( $ns !== self::NS_RDF ) {
957 if ( isset( $this->items[$ns][$tag] ) ) {
958 if ( isset( $this->items[$ns][$tag][
'structPart'] ) ) {
964 wfDebugLog(
'XMP',
"Encountered <$ns:$tag> outside"
965 .
" of its expected parent. Ignoring." );
967 array_unshift( $this->mode, self::MODE_IGNORE );
968 array_unshift( $this->curItem, $ns .
' ' . $tag );
972 $mode = $this->items[$ns][$tag][
'mode'];
973 array_unshift( $this->mode,
$mode );
974 array_unshift( $this->curItem, $ns .
' ' . $tag );
975 if (
$mode === self::MODE_STRUCT ) {
976 $this->ancestorStruct = isset( $this->items[$ns][$tag][
'map_name'] )
977 ? $this->items[$ns][$tag][
'map_name'] : $tag;
979 if ( $this->charContent !==
false ) {
982 throw new MWException(
'tag nested in non-whitespace characters.' );
986 wfDebugLog(
'XMP', __METHOD__ .
" Ignoring unrecognized element <$ns:$tag>." );
987 array_unshift( $this->mode, self::MODE_IGNORE );
988 array_unshift( $this->curItem, $ns .
' ' . $tag );
1017 if ( $ns !== self::NS_RDF ) {
1019 if ( isset( $this->items[$ns][$tag] ) ) {
1020 if ( isset( $this->items[$ns][$this->ancestorStruct][
'children'] )
1021 && !isset( $this->items[$ns][$this->ancestorStruct][
'children'][$tag] )
1025 throw new MWException(
" <$tag> appeared nested in <" . $this->ancestorStruct
1026 .
"> where it is not allowed." );
1028 array_unshift( $this->mode, $this->items[$ns][$tag][
'mode'] );
1029 array_unshift( $this->curItem, $ns .
' ' . $tag );
1030 if ( $this->charContent !==
false ) {
1033 throw new MWException(
"tag <$tag> nested in non-whitespace characters (" .
1034 $this->charContent .
")." );
1037 array_unshift( $this->mode, self::MODE_IGNORE );
1038 array_unshift( $this->curItem, $elm );
1044 if ( $ns === self::NS_RDF && $tag ===
'Description' ) {
1046 array_unshift( $this->mode, self::MODE_STRUCT );
1047 array_unshift( $this->curItem, $this->curItem[0] );
1065 if ( ( $elm ) !== self::NS_RDF .
' li' ) {
1066 throw new MWException(
"<rdf:li> expected but got $elm." );
1069 if ( !isset( $this->mode[1] ) ) {
1072 throw new MWException(
'In mode Li, but no 2xPrevious mode!' );
1075 if ( $this->mode[1] === self::MODE_BAGSTRUCT ) {
1077 array_unshift( $this->mode, self::MODE_STRUCT );
1078 array_unshift( $this->curItem, $elm );
1079 $this->processingArray =
true;
1081 if ( !isset( $this->curItem[1] ) ) {
1083 throw new MWException(
'Can not find parent of BAGSTRUCT.' );
1085 list( $curNS, $curTag ) = explode(
' ', $this->curItem[1] );
1086 $this->ancestorStruct = isset( $this->items[$curNS][$curTag][
'map_name'] )
1087 ? $this->items[$curNS][$curTag][
'map_name'] : $curTag;
1092 array_unshift( $this->mode, self::MODE_SIMPLE );
1095 array_unshift( $this->curItem, $this->curItem[0] );
1096 $this->processingArray =
true;
1115 if ( $elm !== self::NS_RDF .
' li' ) {
1116 throw new MWException( __METHOD__ .
" <rdf:li> expected but got $elm." );
1118 if ( !isset(
$attribs[self::NS_XML .
' lang'] )
1119 || !preg_match(
'/^[-A-Za-z0-9]{2,}$/D',
$attribs[self::NS_XML .
' lang'] )
1122 .
" <rdf:li> did not contain, or has invalid xml:lang attribute in lang alternative" );
1126 $this->itemLang = strtolower(
$attribs[self::NS_XML .
' lang'] );
1130 array_unshift( $this->curItem, $this->curItem[0] );
1131 array_unshift( $this->mode, self::MODE_SIMPLE );
1132 $this->processingArray =
true;
1147 if ( $elm === self::NS_RDF .
' RDF'
1148 || $elm ===
'adobe:ns:meta/ xmpmeta'
1149 || $elm ===
'adobe:ns:meta/ xapmeta'
1153 } elseif ( $elm === self::NS_RDF .
' Description' ) {
1154 if ( count( $this->mode ) === 0 ) {
1156 array_unshift( $this->mode, self::MODE_INITIAL );
1158 } elseif ( $elm === self::NS_RDF .
' type' ) {
1166 wfDebugLog(
'XMP', __METHOD__ .
' Encountered <rdf:type> which isn\'t currently supported' );
1169 if ( strpos( $elm,
' ' ) ===
false ) {
1171 wfDebugLog(
'XMP', __METHOD__ .
" Encountered <$elm> which has no namespace. Skipping." );
1176 list( $ns, $tag ) = explode(
' ', $elm, 2 );
1178 if ( count( $this->mode ) === 0 ) {
1181 .
"encountered <$elm> with no mode" );
1184 switch ( $this->mode[0] ) {
1217 throw new MWException(
'StartElement in unknown mode: ' . $this->mode[0] );
1242 if ( isset(
$attribs[self::NS_RDF .
' parseType'] )
1243 &&
$attribs[self::NS_RDF .
' parseType'] ===
'Resource'
1244 && $this->mode[0] === self::MODE_SIMPLE
1250 if ( strpos(
$name,
' ' ) ===
false ) {
1253 wfDebugLog(
'XMP', __METHOD__ .
' Encountered non-namespaced attribute: '
1254 .
" $name=\"$val\". Skipping. " );
1257 list( $ns, $tag ) = explode(
' ',
$name, 2 );
1258 if ( $ns === self::NS_RDF ) {
1259 if ( $tag ===
'value' || $tag ===
'resource' ) {
1262 $this->
char( $this->xmlParser, $val );
1264 } elseif ( isset( $this->items[$ns][$tag] ) ) {
1265 if ( $this->mode[0] === self::MODE_SIMPLE ) {
1267 .
" $ns:$tag found as attribute where not allowed" );
1271 wfDebugLog(
'XMP', __METHOD__ .
" Ignoring unrecognized element <$ns:$tag>." );
1287 private function saveValue( $ns, $tag, $val ) {
1289 $info =& $this->items[$ns][$tag];
1290 $finalName = isset( $info[
'map_name'] )
1291 ? $info[
'map_name'] : $tag;
1292 if ( isset( $info[
'validate'] ) ) {
1293 $validate = is_array( $info[
'validate'] ) ? $info[
'validate']
1294 :
array(
'XMPValidate', $info[
'validate'] );
1296 if ( is_callable( $validate ) ) {
1297 call_user_func_array( $validate,
array( $info, &$val,
true ) );
1300 if ( is_null( $val ) ) {
1301 wfDebugLog(
'XMP', __METHOD__ .
" <$ns:$tag> failed validation." );
1306 wfDebugLog(
'XMP', __METHOD__ .
" Validation function for $finalName ("
1307 . $validate[0] .
'::' . $validate[1] .
'()) is not callable.' );
1311 if ( $this->ancestorStruct && $this->processingArray ) {
1314 } elseif ( $this->ancestorStruct ) {
1316 } elseif ( $this->processingArray ) {
1317 if ( $this->itemLang ===
false ) {
1319 $this->results[
'xmp-' . $info[
'map_group']][$finalName][] = $val;
1322 $this->results[
'xmp-' . $info[
'map_group']][$finalName][
$this->itemLang] = $val;
1325 $this->results[
'xmp-' . $info[
'map_group']][$finalName] = $val;