MediaWiki
REL1_24
|
00001 <?php 00049 class XMPReader { 00051 protected $items; 00052 00054 private $curItem = array(); 00055 00057 private $ancestorStruct = false; 00058 00060 private $charContent = false; 00061 00063 private $mode = array(); 00064 00066 private $results = array(); 00067 00069 private $processingArray = false; 00070 00072 private $itemLang = false; 00073 00075 private $xmlParser; 00076 00078 private $charset = false; 00079 00081 private $extendedXMPOffset = 0; 00082 00092 const MODE_INITIAL = 0; 00093 const MODE_IGNORE = 1; 00094 const MODE_LI = 2; 00095 const MODE_LI_LANG = 3; 00096 const MODE_QDESC = 4; 00097 00098 // The following MODE constants are also used in the 00099 // $items array to denote what type of property the item is. 00100 const MODE_SIMPLE = 10; 00101 const MODE_STRUCT = 11; // structure (associative array) 00102 const MODE_SEQ = 12; // ordered list 00103 const MODE_BAG = 13; // unordered list 00104 const MODE_LANG = 14; 00105 const MODE_ALT = 15; // non-language alt. Currently not implemented, and not needed atm. 00106 const MODE_BAGSTRUCT = 16; // A BAG of Structs. 00107 00108 const NS_RDF = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'; 00109 const NS_XML = 'http://www.w3.org/XML/1998/namespace'; 00110 00116 function __construct() { 00117 00118 if ( !function_exists( 'xml_parser_create_ns' ) ) { 00119 // this should already be checked by this point 00120 throw new MWException( 'XMP support requires XML Parser' ); 00121 } 00122 00123 $this->items = XMPInfo::getItems(); 00124 00125 $this->resetXMLParser(); 00126 } 00127 00132 private function resetXMLParser() { 00133 00134 if ( $this->xmlParser ) { 00135 //is this needed? 00136 xml_parser_free( $this->xmlParser ); 00137 } 00138 00139 $this->xmlParser = xml_parser_create_ns( 'UTF-8', ' ' ); 00140 xml_parser_set_option( $this->xmlParser, XML_OPTION_CASE_FOLDING, 0 ); 00141 xml_parser_set_option( $this->xmlParser, XML_OPTION_SKIP_WHITE, 1 ); 00142 00143 xml_set_element_handler( $this->xmlParser, 00144 array( $this, 'startElement' ), 00145 array( $this, 'endElement' ) ); 00146 00147 xml_set_character_data_handler( $this->xmlParser, array( $this, 'char' ) ); 00148 } 00149 00154 function __destruct() { 00155 // not sure if this is needed. 00156 xml_parser_free( $this->xmlParser ); 00157 } 00158 00165 public function getResults() { 00166 // xmp-special is for metadata that affects how stuff 00167 // is extracted. For example xmpNote:HasExtendedXMP. 00168 00169 // It is also used to handle photoshop:AuthorsPosition 00170 // which is weird and really part of another property, 00171 // see 2:85 in IPTC. See also pg 21 of IPTC4XMP standard. 00172 // The location fields also use it. 00173 00174 $data = $this->results; 00175 00176 wfRunHooks( 'XMPGetResults', array( &$data ) ); 00177 00178 if ( isset( $data['xmp-special']['AuthorsPosition'] ) 00179 && is_string( $data['xmp-special']['AuthorsPosition'] ) 00180 && isset( $data['xmp-general']['Artist'][0] ) 00181 ) { 00182 // Note, if there is more than one creator, 00183 // this only applies to first. This also will 00184 // only apply to the dc:Creator prop, not the 00185 // exif:Artist prop. 00186 00187 $data['xmp-general']['Artist'][0] = 00188 $data['xmp-special']['AuthorsPosition'] . ', ' 00189 . $data['xmp-general']['Artist'][0]; 00190 } 00191 00192 // Go through the LocationShown and LocationCreated 00193 // changing it to the non-hierarchal form used by 00194 // the other location fields. 00195 00196 if ( isset( $data['xmp-special']['LocationShown'][0] ) 00197 && is_array( $data['xmp-special']['LocationShown'][0] ) 00198 ) { 00199 // the is_array is just paranoia. It should always 00200 // be an array. 00201 foreach ( $data['xmp-special']['LocationShown'] as $loc ) { 00202 if ( !is_array( $loc ) ) { 00203 // To avoid copying over the _type meta-fields. 00204 continue; 00205 } 00206 foreach ( $loc as $field => $val ) { 00207 $data['xmp-general'][$field . 'Dest'][] = $val; 00208 } 00209 } 00210 } 00211 if ( isset( $data['xmp-special']['LocationCreated'][0] ) 00212 && is_array( $data['xmp-special']['LocationCreated'][0] ) 00213 ) { 00214 // the is_array is just paranoia. It should always 00215 // be an array. 00216 foreach ( $data['xmp-special']['LocationCreated'] as $loc ) { 00217 if ( !is_array( $loc ) ) { 00218 // To avoid copying over the _type meta-fields. 00219 continue; 00220 } 00221 foreach ( $loc as $field => $val ) { 00222 $data['xmp-general'][$field . 'Created'][] = $val; 00223 } 00224 } 00225 } 00226 00227 // We don't want to return the special values, since they're 00228 // special and not info to be stored about the file. 00229 unset( $data['xmp-special'] ); 00230 00231 // Convert GPSAltitude to negative if below sea level. 00232 if ( isset( $data['xmp-exif']['GPSAltitudeRef'] ) 00233 && isset( $data['xmp-exif']['GPSAltitude'] ) 00234 ) { 00235 00236 // Must convert to a real before multiplying by -1 00237 // XMPValidate guarantees there will always be a '/' in this value. 00238 list( $nom, $denom ) = explode( '/', $data['xmp-exif']['GPSAltitude'] ); 00239 $data['xmp-exif']['GPSAltitude'] = $nom / $denom; 00240 00241 if ( $data['xmp-exif']['GPSAltitudeRef'] == '1' ) { 00242 $data['xmp-exif']['GPSAltitude'] *= -1; 00243 } 00244 unset( $data['xmp-exif']['GPSAltitudeRef'] ); 00245 } 00246 00247 return $data; 00248 } 00249 00263 public function parse( $content, $allOfIt = true, $reset = false ) { 00264 if ( $reset ) { 00265 $this->resetXMLParser(); 00266 } 00267 try { 00268 00269 // detect encoding by looking for BOM which is supposed to be in processing instruction. 00270 // see page 12 of http://www.adobe.com/devnet/xmp/pdfs/XMPSpecificationPart3.pdf 00271 if ( !$this->charset ) { 00272 $bom = array(); 00273 if ( preg_match( '/\xEF\xBB\xBF|\xFE\xFF|\x00\x00\xFE\xFF|\xFF\xFE\x00\x00|\xFF\xFE/', 00274 $content, $bom ) 00275 ) { 00276 switch ( $bom[0] ) { 00277 case "\xFE\xFF": 00278 $this->charset = 'UTF-16BE'; 00279 break; 00280 case "\xFF\xFE": 00281 $this->charset = 'UTF-16LE'; 00282 break; 00283 case "\x00\x00\xFE\xFF": 00284 $this->charset = 'UTF-32BE'; 00285 break; 00286 case "\xFF\xFE\x00\x00": 00287 $this->charset = 'UTF-32LE'; 00288 break; 00289 case "\xEF\xBB\xBF": 00290 $this->charset = 'UTF-8'; 00291 break; 00292 default: 00293 //this should be impossible to get to 00294 throw new MWException( "Invalid BOM" ); 00295 } 00296 } else { 00297 // standard specifically says, if no bom assume utf-8 00298 $this->charset = 'UTF-8'; 00299 } 00300 } 00301 if ( $this->charset !== 'UTF-8' ) { 00302 //don't convert if already utf-8 00303 wfSuppressWarnings(); 00304 $content = iconv( $this->charset, 'UTF-8//IGNORE', $content ); 00305 wfRestoreWarnings(); 00306 } 00307 00308 $ok = xml_parse( $this->xmlParser, $content, $allOfIt ); 00309 if ( !$ok ) { 00310 $error = xml_error_string( xml_get_error_code( $this->xmlParser ) ); 00311 $where = 'line: ' . xml_get_current_line_number( $this->xmlParser ) 00312 . ' column: ' . xml_get_current_column_number( $this->xmlParser ) 00313 . ' byte offset: ' . xml_get_current_byte_index( $this->xmlParser ); 00314 00315 wfDebugLog( 'XMP', "XMPReader::parse : Error reading XMP content: $error ($where)" ); 00316 $this->results = array(); // blank if error. 00317 return false; 00318 } 00319 } catch ( MWException $e ) { 00320 wfDebugLog( 'XMP', 'XMP parse error: ' . $e ); 00321 $this->results = array(); 00322 00323 return false; 00324 } 00325 00326 return true; 00327 } 00328 00336 public function parseExtended( $content ) { 00337 // @todo FIXME: This is untested. Hard to find example files 00338 // or programs that make such files.. 00339 $guid = substr( $content, 0, 32 ); 00340 if ( !isset( $this->results['xmp-special']['HasExtendedXMP'] ) 00341 || $this->results['xmp-special']['HasExtendedXMP'] !== $guid 00342 ) { 00343 wfDebugLog( 'XMP', __METHOD__ . 00344 " Ignoring XMPExtended block due to wrong guid (guid= '$guid')" ); 00345 00346 return false; 00347 } 00348 $len = unpack( 'Nlength/Noffset', substr( $content, 32, 8 ) ); 00349 00350 if ( !$len || $len['length'] < 4 || $len['offset'] < 0 || $len['offset'] > $len['length'] ) { 00351 wfDebugLog( 'XMP', __METHOD__ . 'Error reading extended XMP block, invalid length or offset.' ); 00352 00353 return false; 00354 } 00355 00356 // we're not very robust here. we should accept it in the wrong order. 00357 // To quote the XMP standard: 00358 // "A JPEG writer should write the ExtendedXMP marker segments in order, 00359 // immediately following the StandardXMP. However, the JPEG standard 00360 // does not require preservation of marker segment order. A robust JPEG 00361 // reader should tolerate the marker segments in any order." 00362 // 00363 // otoh the probability that an image will have more than 128k of 00364 // metadata is rather low... so the probability that it will have 00365 // > 128k, and be in the wrong order is very low... 00366 00367 if ( $len['offset'] !== $this->extendedXMPOffset ) { 00368 wfDebugLog( 'XMP', __METHOD__ . 'Ignoring XMPExtended block due to wrong order. (Offset was ' 00369 . $len['offset'] . ' but expected ' . $this->extendedXMPOffset . ')' ); 00370 00371 return false; 00372 } 00373 00374 if ( $len['offset'] === 0 ) { 00375 // if we're starting the extended block, we've probably already 00376 // done the XMPStandard block, so reset. 00377 $this->resetXMLParser(); 00378 } 00379 00380 $this->extendedXMPOffset += $len['length']; 00381 00382 $actualContent = substr( $content, 40 ); 00383 00384 if ( $this->extendedXMPOffset === strlen( $actualContent ) ) { 00385 $atEnd = true; 00386 } else { 00387 $atEnd = false; 00388 } 00389 00390 wfDebugLog( 'XMP', __METHOD__ . 'Parsing a XMPExtended block' ); 00391 00392 return $this->parse( $actualContent, $atEnd ); 00393 } 00394 00411 function char( $parser, $data ) { 00412 00413 $data = trim( $data ); 00414 if ( trim( $data ) === "" ) { 00415 return; 00416 } 00417 00418 if ( !isset( $this->mode[0] ) ) { 00419 throw new MWException( 'Unexpected character data before first rdf:Description element' ); 00420 } 00421 00422 if ( $this->mode[0] === self::MODE_IGNORE ) { 00423 return; 00424 } 00425 00426 if ( $this->mode[0] !== self::MODE_SIMPLE 00427 && $this->mode[0] !== self::MODE_QDESC 00428 ) { 00429 throw new MWException( 'character data where not expected. (mode ' . $this->mode[0] . ')' ); 00430 } 00431 00432 // to check, how does this handle w.s. 00433 if ( $this->charContent === false ) { 00434 $this->charContent = $data; 00435 } else { 00436 $this->charContent .= $data; 00437 } 00438 } 00439 00446 private function endElementModeIgnore( $elm ) { 00447 if ( $this->curItem[0] === $elm ) { 00448 array_shift( $this->curItem ); 00449 array_shift( $this->mode ); 00450 } 00451 } 00452 00468 private function endElementModeSimple( $elm ) { 00469 if ( $this->charContent !== false ) { 00470 if ( $this->processingArray ) { 00471 // if we're processing an array, use the original element 00472 // name instead of rdf:li. 00473 list( $ns, $tag ) = explode( ' ', $this->curItem[0], 2 ); 00474 } else { 00475 list( $ns, $tag ) = explode( ' ', $elm, 2 ); 00476 } 00477 $this->saveValue( $ns, $tag, $this->charContent ); 00478 00479 $this->charContent = false; // reset 00480 } 00481 array_shift( $this->curItem ); 00482 array_shift( $this->mode ); 00483 } 00484 00503 private function endElementNested( $elm ) { 00504 00505 /* cur item must be the same as $elm, unless if in MODE_STRUCT 00506 in which case it could also be rdf:Description */ 00507 if ( $this->curItem[0] !== $elm 00508 && !( $elm === self::NS_RDF . ' Description' 00509 && $this->mode[0] === self::MODE_STRUCT ) 00510 ) { 00511 throw new MWException( "nesting mismatch. got a </$elm> but expected a </" . 00512 $this->curItem[0] . '>' ); 00513 } 00514 00515 // Validate structures. 00516 list( $ns, $tag ) = explode( ' ', $elm, 2 ); 00517 if ( isset( $this->items[$ns][$tag]['validate'] ) ) { 00518 00519 $info =& $this->items[$ns][$tag]; 00520 $finalName = isset( $info['map_name'] ) 00521 ? $info['map_name'] : $tag; 00522 00523 $validate = is_array( $info['validate'] ) ? $info['validate'] 00524 : array( 'XMPValidate', $info['validate'] ); 00525 00526 if ( !isset( $this->results['xmp-' . $info['map_group']][$finalName] ) ) { 00527 // This can happen if all the members of the struct failed validation. 00528 wfDebugLog( 'XMP', __METHOD__ . " <$ns:$tag> has no valid members." ); 00529 } elseif ( is_callable( $validate ) ) { 00530 $val =& $this->results['xmp-' . $info['map_group']][$finalName]; 00531 call_user_func_array( $validate, array( $info, &$val, false ) ); 00532 if ( is_null( $val ) ) { 00533 // the idea being the validation function will unset the variable if 00534 // its invalid. 00535 wfDebugLog( 'XMP', __METHOD__ . " <$ns:$tag> failed validation." ); 00536 unset( $this->results['xmp-' . $info['map_group']][$finalName] ); 00537 } 00538 } else { 00539 wfDebugLog( 'XMP', __METHOD__ . " Validation function for $finalName (" 00540 . $validate[0] . '::' . $validate[1] . '()) is not callable.' ); 00541 } 00542 } 00543 00544 array_shift( $this->curItem ); 00545 array_shift( $this->mode ); 00546 $this->ancestorStruct = false; 00547 $this->processingArray = false; 00548 $this->itemLang = false; 00549 } 00550 00570 private function endElementModeLi( $elm ) { 00571 00572 list( $ns, $tag ) = explode( ' ', $this->curItem[0], 2 ); 00573 $info = $this->items[$ns][$tag]; 00574 $finalName = isset( $info['map_name'] ) 00575 ? $info['map_name'] : $tag; 00576 00577 array_shift( $this->mode ); 00578 00579 if ( !isset( $this->results['xmp-' . $info['map_group']][$finalName] ) ) { 00580 wfDebugLog( 'XMP', __METHOD__ . " Empty compund element $finalName." ); 00581 00582 return; 00583 } 00584 00585 if ( $elm === self::NS_RDF . ' Seq' ) { 00586 $this->results['xmp-' . $info['map_group']][$finalName]['_type'] = 'ol'; 00587 } elseif ( $elm === self::NS_RDF . ' Bag' ) { 00588 $this->results['xmp-' . $info['map_group']][$finalName]['_type'] = 'ul'; 00589 } elseif ( $elm === self::NS_RDF . ' Alt' ) { 00590 // extra if needed as you could theoretically have a non-language alt. 00591 if ( $info['mode'] === self::MODE_LANG ) { 00592 $this->results['xmp-' . $info['map_group']][$finalName]['_type'] = 'lang'; 00593 } 00594 } else { 00595 throw new MWException( __METHOD__ . " expected </rdf:seq> or </rdf:bag> but instead got $elm." ); 00596 } 00597 } 00598 00609 private function endElementModeQDesc( $elm ) { 00610 00611 if ( $elm === self::NS_RDF . ' value' ) { 00612 list( $ns, $tag ) = explode( ' ', $this->curItem[0], 2 ); 00613 $this->saveValue( $ns, $tag, $this->charContent ); 00614 00615 return; 00616 } else { 00617 array_shift( $this->mode ); 00618 array_shift( $this->curItem ); 00619 } 00620 } 00621 00635 function endElement( $parser, $elm ) { 00636 if ( $elm === ( self::NS_RDF . ' RDF' ) 00637 || $elm === 'adobe:ns:meta/ xmpmeta' 00638 || $elm === 'adobe:ns:meta/ xapmeta' 00639 ) { 00640 // ignore these. 00641 return; 00642 } 00643 00644 if ( $elm === self::NS_RDF . ' type' ) { 00645 // these aren't really supported properly yet. 00646 // However, it appears they almost never used. 00647 wfDebugLog( 'XMP', __METHOD__ . ' encountered <rdf:type>' ); 00648 } 00649 00650 if ( strpos( $elm, ' ' ) === false ) { 00651 // This probably shouldn't happen. 00652 // However, there is a bug in an adobe product 00653 // that forgets the namespace on some things. 00654 // (Luckily they are unimportant things). 00655 wfDebugLog( 'XMP', __METHOD__ . " Encountered </$elm> which has no namespace. Skipping." ); 00656 00657 return; 00658 } 00659 00660 if ( count( $this->mode[0] ) === 0 ) { 00661 // This should never ever happen and means 00662 // there is a pretty major bug in this class. 00663 throw new MWException( 'Encountered end element with no mode' ); 00664 } 00665 00666 if ( count( $this->curItem ) == 0 && $this->mode[0] !== self::MODE_INITIAL ) { 00667 // just to be paranoid. Should always have a curItem, except for initially 00668 // (aka during MODE_INITAL). 00669 throw new MWException( "Hit end element </$elm> but no curItem" ); 00670 } 00671 00672 switch ( $this->mode[0] ) { 00673 case self::MODE_IGNORE: 00674 $this->endElementModeIgnore( $elm ); 00675 break; 00676 case self::MODE_SIMPLE: 00677 $this->endElementModeSimple( $elm ); 00678 break; 00679 case self::MODE_STRUCT: 00680 case self::MODE_SEQ: 00681 case self::MODE_BAG: 00682 case self::MODE_LANG: 00683 case self::MODE_BAGSTRUCT: 00684 $this->endElementNested( $elm ); 00685 break; 00686 case self::MODE_INITIAL: 00687 if ( $elm === self::NS_RDF . ' Description' ) { 00688 array_shift( $this->mode ); 00689 } else { 00690 throw new MWException( 'Element ended unexpectedly while in MODE_INITIAL' ); 00691 } 00692 break; 00693 case self::MODE_LI: 00694 case self::MODE_LI_LANG: 00695 $this->endElementModeLi( $elm ); 00696 break; 00697 case self::MODE_QDESC: 00698 $this->endElementModeQDesc( $elm ); 00699 break; 00700 default: 00701 wfDebugLog( 'XMP', __METHOD__ . " no mode (elm = $elm)" ); 00702 break; 00703 } 00704 } 00705 00717 private function startElementModeIgnore( $elm ) { 00718 if ( $elm === $this->curItem[0] ) { 00719 array_unshift( $this->curItem, $elm ); 00720 array_unshift( $this->mode, self::MODE_IGNORE ); 00721 } 00722 } 00723 00731 private function startElementModeBag( $elm ) { 00732 if ( $elm === self::NS_RDF . ' Bag' ) { 00733 array_unshift( $this->mode, self::MODE_LI ); 00734 } else { 00735 throw new MWException( "Expected <rdf:Bag> but got $elm." ); 00736 } 00737 } 00738 00746 private function startElementModeSeq( $elm ) { 00747 if ( $elm === self::NS_RDF . ' Seq' ) { 00748 array_unshift( $this->mode, self::MODE_LI ); 00749 } elseif ( $elm === self::NS_RDF . ' Bag' ) { 00750 # bug 27105 00751 wfDebugLog( 'XMP', __METHOD__ . ' Expected an rdf:Seq, but got an rdf:Bag. Pretending' 00752 . ' it is a Seq, since some buggy software is known to screw this up.' ); 00753 array_unshift( $this->mode, self::MODE_LI ); 00754 } else { 00755 throw new MWException( "Expected <rdf:Seq> but got $elm." ); 00756 } 00757 } 00758 00773 private function startElementModeLang( $elm ) { 00774 if ( $elm === self::NS_RDF . ' Alt' ) { 00775 array_unshift( $this->mode, self::MODE_LI_LANG ); 00776 } else { 00777 throw new MWException( "Expected <rdf:Seq> but got $elm." ); 00778 } 00779 } 00780 00799 private function startElementModeSimple( $elm, $attribs ) { 00800 if ( $elm === self::NS_RDF . ' Description' ) { 00801 // If this value has qualifiers 00802 array_unshift( $this->mode, self::MODE_QDESC ); 00803 array_unshift( $this->curItem, $this->curItem[0] ); 00804 00805 if ( isset( $attribs[self::NS_RDF . ' value'] ) ) { 00806 list( $ns, $tag ) = explode( ' ', $this->curItem[0], 2 ); 00807 $this->saveValue( $ns, $tag, $attribs[self::NS_RDF . ' value'] ); 00808 } 00809 } elseif ( $elm === self::NS_RDF . ' value' ) { 00810 // This should not be here. 00811 throw new MWException( __METHOD__ . ' Encountered <rdf:value> where it was unexpected.' ); 00812 } else { 00813 // something else we don't recognize, like a qualifier maybe. 00814 wfDebugLog( 'XMP', __METHOD__ . 00815 " Encountered element <$elm> where only expecting character data as value of " . 00816 $this->curItem[0] ); 00817 array_unshift( $this->mode, self::MODE_IGNORE ); 00818 array_unshift( $this->curItem, $elm ); 00819 } 00820 } 00821 00836 private function startElementModeQDesc( $elm ) { 00837 if ( $elm === self::NS_RDF . ' value' ) { 00838 return; // do nothing 00839 } else { 00840 // otherwise its a qualifier, which we ignore 00841 array_unshift( $this->mode, self::MODE_IGNORE ); 00842 array_unshift( $this->curItem, $elm ); 00843 } 00844 } 00845 00858 private function startElementModeInitial( $ns, $tag, $attribs ) { 00859 if ( $ns !== self::NS_RDF ) { 00860 00861 if ( isset( $this->items[$ns][$tag] ) ) { 00862 if ( isset( $this->items[$ns][$tag]['structPart'] ) ) { 00863 // If this element is supposed to appear only as 00864 // a child of a structure, but appears here (not as 00865 // a child of a struct), then something weird is 00866 // happening, so ignore this element and its children. 00867 00868 wfDebugLog( 'XMP', "Encountered <$ns:$tag> outside" 00869 . " of its expected parent. Ignoring." ); 00870 00871 array_unshift( $this->mode, self::MODE_IGNORE ); 00872 array_unshift( $this->curItem, $ns . ' ' . $tag ); 00873 00874 return; 00875 } 00876 $mode = $this->items[$ns][$tag]['mode']; 00877 array_unshift( $this->mode, $mode ); 00878 array_unshift( $this->curItem, $ns . ' ' . $tag ); 00879 if ( $mode === self::MODE_STRUCT ) { 00880 $this->ancestorStruct = isset( $this->items[$ns][$tag]['map_name'] ) 00881 ? $this->items[$ns][$tag]['map_name'] : $tag; 00882 } 00883 if ( $this->charContent !== false ) { 00884 // Something weird. 00885 // Should not happen in valid XMP. 00886 throw new MWException( 'tag nested in non-whitespace characters.' ); 00887 } 00888 } else { 00889 // This element is not on our list of allowed elements so ignore. 00890 wfDebugLog( 'XMP', __METHOD__ . " Ignoring unrecognized element <$ns:$tag>." ); 00891 array_unshift( $this->mode, self::MODE_IGNORE ); 00892 array_unshift( $this->curItem, $ns . ' ' . $tag ); 00893 00894 return; 00895 } 00896 } 00897 // process attributes 00898 $this->doAttribs( $attribs ); 00899 } 00900 00920 private function startElementModeStruct( $ns, $tag, $attribs ) { 00921 if ( $ns !== self::NS_RDF ) { 00922 00923 if ( isset( $this->items[$ns][$tag] ) ) { 00924 if ( isset( $this->items[$ns][$this->ancestorStruct]['children'] ) 00925 && !isset( $this->items[$ns][$this->ancestorStruct]['children'][$tag] ) 00926 ) { 00927 // This assumes that we don't have inter-namespace nesting 00928 // which we don't in all the properties we're interested in. 00929 throw new MWException( " <$tag> appeared nested in <" . $this->ancestorStruct 00930 . "> where it is not allowed." ); 00931 } 00932 array_unshift( $this->mode, $this->items[$ns][$tag]['mode'] ); 00933 array_unshift( $this->curItem, $ns . ' ' . $tag ); 00934 if ( $this->charContent !== false ) { 00935 // Something weird. 00936 // Should not happen in valid XMP. 00937 throw new MWException( "tag <$tag> nested in non-whitespace characters (" . 00938 $this->charContent . ")." ); 00939 } 00940 } else { 00941 array_unshift( $this->mode, self::MODE_IGNORE ); 00942 array_unshift( $this->curItem, $elm ); 00943 00944 return; 00945 } 00946 } 00947 00948 if ( $ns === self::NS_RDF && $tag === 'Description' ) { 00949 $this->doAttribs( $attribs ); 00950 array_unshift( $this->mode, self::MODE_STRUCT ); 00951 array_unshift( $this->curItem, $this->curItem[0] ); 00952 } 00953 } 00954 00968 private function startElementModeLi( $elm, $attribs ) { 00969 if ( ( $elm ) !== self::NS_RDF . ' li' ) { 00970 throw new MWException( "<rdf:li> expected but got $elm." ); 00971 } 00972 00973 if ( !isset( $this->mode[1] ) ) { 00974 // This should never ever ever happen. Checking for it 00975 // to be paranoid. 00976 throw new MWException( 'In mode Li, but no 2xPrevious mode!' ); 00977 } 00978 00979 if ( $this->mode[1] === self::MODE_BAGSTRUCT ) { 00980 // This list item contains a compound (STRUCT) value. 00981 array_unshift( $this->mode, self::MODE_STRUCT ); 00982 array_unshift( $this->curItem, $elm ); 00983 $this->processingArray = true; 00984 00985 if ( !isset( $this->curItem[1] ) ) { 00986 // be paranoid. 00987 throw new MWException( 'Can not find parent of BAGSTRUCT.' ); 00988 } 00989 list( $curNS, $curTag ) = explode( ' ', $this->curItem[1] ); 00990 $this->ancestorStruct = isset( $this->items[$curNS][$curTag]['map_name'] ) 00991 ? $this->items[$curNS][$curTag]['map_name'] : $curTag; 00992 00993 $this->doAttribs( $attribs ); 00994 } else { 00995 // Normal BAG or SEQ containing simple values. 00996 array_unshift( $this->mode, self::MODE_SIMPLE ); 00997 // need to add curItem[0] on again since one is for the specific item 00998 // and one is for the entire group. 00999 array_unshift( $this->curItem, $this->curItem[0] ); 01000 $this->processingArray = true; 01001 } 01002 } 01003 01018 private function startElementModeLiLang( $elm, $attribs ) { 01019 if ( $elm !== self::NS_RDF . ' li' ) { 01020 throw new MWException( __METHOD__ . " <rdf:li> expected but got $elm." ); 01021 } 01022 if ( !isset( $attribs[self::NS_XML . ' lang'] ) 01023 || !preg_match( '/^[-A-Za-z0-9]{2,}$/D', $attribs[self::NS_XML . ' lang'] ) 01024 ) { 01025 throw new MWException( __METHOD__ 01026 . " <rdf:li> did not contain, or has invalid xml:lang attribute in lang alternative" ); 01027 } 01028 01029 // Lang is case-insensitive. 01030 $this->itemLang = strtolower( $attribs[self::NS_XML . ' lang'] ); 01031 01032 // need to add curItem[0] on again since one is for the specific item 01033 // and one is for the entire group. 01034 array_unshift( $this->curItem, $this->curItem[0] ); 01035 array_unshift( $this->mode, self::MODE_SIMPLE ); 01036 $this->processingArray = true; 01037 } 01038 01049 function startElement( $parser, $elm, $attribs ) { 01050 01051 if ( $elm === self::NS_RDF . ' RDF' 01052 || $elm === 'adobe:ns:meta/ xmpmeta' 01053 || $elm === 'adobe:ns:meta/ xapmeta' 01054 ) { 01055 /* ignore. */ 01056 return; 01057 } elseif ( $elm === self::NS_RDF . ' Description' ) { 01058 if ( count( $this->mode ) === 0 ) { 01059 // outer rdf:desc 01060 array_unshift( $this->mode, self::MODE_INITIAL ); 01061 } 01062 } elseif ( $elm === self::NS_RDF . ' type' ) { 01063 // This doesn't support rdf:type properly. 01064 // In practise I have yet to see a file that 01065 // uses this element, however it is mentioned 01066 // on page 25 of part 1 of the xmp standard. 01067 // 01068 // also it seems as if exiv2 and exiftool do not support 01069 // this either (That or I misunderstand the standard) 01070 wfDebugLog( 'XMP', __METHOD__ . ' Encountered <rdf:type> which isn\'t currently supported' ); 01071 } 01072 01073 if ( strpos( $elm, ' ' ) === false ) { 01074 // This probably shouldn't happen. 01075 wfDebugLog( 'XMP', __METHOD__ . " Encountered <$elm> which has no namespace. Skipping." ); 01076 01077 return; 01078 } 01079 01080 list( $ns, $tag ) = explode( ' ', $elm, 2 ); 01081 01082 if ( count( $this->mode ) === 0 ) { 01083 // This should not happen. 01084 throw new MWException( 'Error extracting XMP, ' 01085 . "encountered <$elm> with no mode" ); 01086 } 01087 01088 switch ( $this->mode[0] ) { 01089 case self::MODE_IGNORE: 01090 $this->startElementModeIgnore( $elm ); 01091 break; 01092 case self::MODE_SIMPLE: 01093 $this->startElementModeSimple( $elm, $attribs ); 01094 break; 01095 case self::MODE_INITIAL: 01096 $this->startElementModeInitial( $ns, $tag, $attribs ); 01097 break; 01098 case self::MODE_STRUCT: 01099 $this->startElementModeStruct( $ns, $tag, $attribs ); 01100 break; 01101 case self::MODE_BAG: 01102 case self::MODE_BAGSTRUCT: 01103 $this->startElementModeBag( $elm ); 01104 break; 01105 case self::MODE_SEQ: 01106 $this->startElementModeSeq( $elm ); 01107 break; 01108 case self::MODE_LANG: 01109 $this->startElementModeLang( $elm ); 01110 break; 01111 case self::MODE_LI_LANG: 01112 $this->startElementModeLiLang( $elm, $attribs ); 01113 break; 01114 case self::MODE_LI: 01115 $this->startElementModeLi( $elm, $attribs ); 01116 break; 01117 case self::MODE_QDESC: 01118 $this->startElementModeQDesc( $elm ); 01119 break; 01120 default: 01121 throw new MWException( 'StartElement in unknown mode: ' . $this->mode[0] ); 01122 } 01123 } 01124 01142 private function doAttribs( $attribs ) { 01143 // first check for rdf:parseType attribute, as that can change 01144 // how the attributes are interperted. 01145 01146 if ( isset( $attribs[self::NS_RDF . ' parseType'] ) 01147 && $attribs[self::NS_RDF . ' parseType'] === 'Resource' 01148 && $this->mode[0] === self::MODE_SIMPLE 01149 ) { 01150 // this is equivalent to having an inner rdf:Description 01151 $this->mode[0] = self::MODE_QDESC; 01152 } 01153 foreach ( $attribs as $name => $val ) { 01154 if ( strpos( $name, ' ' ) === false ) { 01155 // This shouldn't happen, but so far some old software forgets namespace 01156 // on rdf:about. 01157 wfDebugLog( 'XMP', __METHOD__ . ' Encountered non-namespaced attribute: ' 01158 . " $name=\"$val\". Skipping. " ); 01159 continue; 01160 } 01161 list( $ns, $tag ) = explode( ' ', $name, 2 ); 01162 if ( $ns === self::NS_RDF ) { 01163 if ( $tag === 'value' || $tag === 'resource' ) { 01164 // resource is for url. 01165 // value attribute is a weird way of just putting the contents. 01166 $this->char( $this->xmlParser, $val ); 01167 } 01168 } elseif ( isset( $this->items[$ns][$tag] ) ) { 01169 if ( $this->mode[0] === self::MODE_SIMPLE ) { 01170 throw new MWException( __METHOD__ 01171 . " $ns:$tag found as attribute where not allowed" ); 01172 } 01173 $this->saveValue( $ns, $tag, $val ); 01174 } else { 01175 wfDebugLog( 'XMP', __METHOD__ . " Ignoring unrecognized element <$ns:$tag>." ); 01176 } 01177 } 01178 } 01179 01191 private function saveValue( $ns, $tag, $val ) { 01192 01193 $info =& $this->items[$ns][$tag]; 01194 $finalName = isset( $info['map_name'] ) 01195 ? $info['map_name'] : $tag; 01196 if ( isset( $info['validate'] ) ) { 01197 $validate = is_array( $info['validate'] ) ? $info['validate'] 01198 : array( 'XMPValidate', $info['validate'] ); 01199 01200 if ( is_callable( $validate ) ) { 01201 call_user_func_array( $validate, array( $info, &$val, true ) ); 01202 // the reasoning behind using &$val instead of using the return value 01203 // is to be consistent between here and validating structures. 01204 if ( is_null( $val ) ) { 01205 wfDebugLog( 'XMP', __METHOD__ . " <$ns:$tag> failed validation." ); 01206 01207 return; 01208 } 01209 } else { 01210 wfDebugLog( 'XMP', __METHOD__ . " Validation function for $finalName (" 01211 . $validate[0] . '::' . $validate[1] . '()) is not callable.' ); 01212 } 01213 } 01214 01215 if ( $this->ancestorStruct && $this->processingArray ) { 01216 // Aka both an array and a struct. ( self::MODE_BAGSTRUCT ) 01217 $this->results['xmp-' . $info['map_group']][$this->ancestorStruct][][$finalName] = $val; 01218 } elseif ( $this->ancestorStruct ) { 01219 $this->results['xmp-' . $info['map_group']][$this->ancestorStruct][$finalName] = $val; 01220 } elseif ( $this->processingArray ) { 01221 if ( $this->itemLang === false ) { 01222 // normal array 01223 $this->results['xmp-' . $info['map_group']][$finalName][] = $val; 01224 } else { 01225 // lang array. 01226 $this->results['xmp-' . $info['map_group']][$finalName][$this->itemLang] = $val; 01227 } 01228 } else { 01229 $this->results['xmp-' . $info['map_group']][$finalName] = $val; 01230 } 01231 } 01232 }