* @ingroup Media
*/
+use Psr\Log\LoggerAwareInterface;
+use Psr\Log\LoggerInterface;
+use Psr\Log\NullLogger;
+
/**
* Class for reading xmp data containing properties relevant to
* images, and spitting out an array that FormatMetadata accepts.
* read rdf.
*
*/
-class XMPReader {
+class XMPReader implements LoggerAwareInterface {
/** @var array XMP item configuration array */
protected $items;
const PARSABLE_BUFFERING = 2;
const PARSABLE_NO = 3;
+ /**
+ * @var LoggerInterface
+ */
+ private $logger;
+
/**
* Constructor.
*
* Primary job is to initialize the XMLParser
*/
- function __construct() {
+ function __construct( LoggerInterface $logger = null ) {
if ( !function_exists( 'xml_parser_create_ns' ) ) {
// this should already be checked by this point
throw new RuntimeException( 'XMP support requires XML Parser' );
}
+ if ( $logger ) {
+ $this->setLogger( $logger );
+ } else {
+ $this->setLogger( new NullLogger() );
+ }
$this->items = XMPInfo::getItems();
$this->resetXMLParser();
}
+ public function setLogger( LoggerInterface $logger ) {
+ $this->logger = $logger;
+ }
+
+ /**
+ * free the XML parser.
+ *
+ * @note It is unclear to me if we really need to do this ourselves
+ * or if php garbage collection will automatically free the xmlParser
+ * when it is no longer needed.
+ */
+ private function destroyXMLParser() {
+ if ( $this->xmlParser ) {
+ xml_parser_free( $this->xmlParser );
+ $this->xmlParser = null;
+ }
+ }
+
/**
* Main use is if a single item has multiple xmp documents describing it.
* For example in jpeg's with extendedXMP
*/
private function resetXMLParser() {
- if ( $this->xmlParser ) {
- //is this needed?
- xml_parser_free( $this->xmlParser );
- }
+ $this->destroyXMLParser();
$this->xmlParser = xml_parser_create_ns( 'UTF-8', ' ' );
xml_parser_set_option( $this->xmlParser, XML_OPTION_CASE_FOLDING, 0 );
$this->xmlParsableBuffer = '';
}
- /** Destroy the xml parser
- *
- * Not sure if this is actually needed.
- */
- function __destruct() {
- // not sure if this is needed.
- xml_parser_free( $this->xmlParser );
- }
-
/**
* Check if this instance supports using this class
*/
*
* @param string $content XMP data
* @param bool $allOfIt If this is all the data (true) or if its split up (false). Default true
- * @param bool $reset Does xml parser need to be reset. Default false
* @throws RuntimeException
* @return bool Success.
*/
- public function parse( $content, $allOfIt = true, $reset = false ) {
- if ( $reset ) {
+ public function parse( $content, $allOfIt = true ) {
+ if ( !$this->xmlParser ) {
$this->resetXMLParser();
}
try {
}
if ( $this->charset !== 'UTF-8' ) {
//don't convert if already utf-8
- wfSuppressWarnings();
+ MediaWiki\suppressWarnings();
$content = iconv( $this->charset, 'UTF-8//IGNORE', $content );
- wfRestoreWarnings();
+ MediaWiki\restoreWarnings();
}
// Ensure the XMP block does not have an xml doctype declaration, which
. ' column: ' . xml_get_current_column_number( $this->xmlParser )
. ' byte offset: ' . xml_get_current_byte_index( $this->xmlParser );
- wfDebugLog( 'XMP', "XMPReader::parse : Error reading XMP content: $error ($where)" );
+ $this->logger->info( "XMPReader::parse : Error reading XMP content: $error ($where)" );
$this->results = array(); // blank if error.
+ $this->destroyXMLParser();
return false;
}
} catch ( Exception $e ) {
- wfDebugLog( 'XMP', 'XMP parse error: ' . $e );
+ $this->logger->info( 'XMP parse error: ' . $e );
$this->results = array();
+ if ( $allOfIt ) {
+ $this->destroyXMLParser();
+ }
return false;
}
+ if ( $allOfIt ) {
+ $this->destroyXMLParser();
+ }
return true;
}
if ( !isset( $this->results['xmp-special']['HasExtendedXMP'] )
|| $this->results['xmp-special']['HasExtendedXMP'] !== $guid
) {
- wfDebugLog( 'XMP', __METHOD__ .
+ $this->logger->info( __METHOD__ .
" Ignoring XMPExtended block due to wrong guid (guid= '$guid')" );
return false;
$len = unpack( 'Nlength/Noffset', substr( $content, 32, 8 ) );
if ( !$len || $len['length'] < 4 || $len['offset'] < 0 || $len['offset'] > $len['length'] ) {
- wfDebugLog( 'XMP', __METHOD__ . 'Error reading extended XMP block, invalid length or offset.' );
+ $this->logger->info( __METHOD__ . 'Error reading extended XMP block, invalid length or offset.' );
return false;
}
// > 128k, and be in the wrong order is very low...
if ( $len['offset'] !== $this->extendedXMPOffset ) {
- wfDebugLog( 'XMP', __METHOD__ . 'Ignoring XMPExtended block due to wrong order. (Offset was '
+ $this->logger->info( __METHOD__ . 'Ignoring XMPExtended block due to wrong order. (Offset was '
. $len['offset'] . ' but expected ' . $this->extendedXMPOffset . ')' );
return false;
$atEnd = false;
}
- wfDebugLog( 'XMP', __METHOD__ . 'Parsing a XMPExtended block' );
+ $this->logger->debug( __METHOD__ . 'Parsing a XMPExtended block' );
return $this->parse( $actualContent, $atEnd );
}
// Even with LIBXML_NOWARNING set, XMLReader::read gives a warning
// when parsing truncated XML, which causes unit tests to fail.
- wfSuppressWarnings();
+ MediaWiki\suppressWarnings();
while ( $reader->read() ) {
if ( $reader->nodeType === XMLReader::ELEMENT ) {
// Reached the first element without hitting a doctype declaration
break;
}
}
- wfRestoreWarnings();
+ MediaWiki\restoreWarnings();
if ( !is_null( $result ) ) {
return $result;
// Validate structures.
list( $ns, $tag ) = explode( ' ', $elm, 2 );
if ( isset( $this->items[$ns][$tag]['validate'] ) ) {
-
$info =& $this->items[$ns][$tag];
$finalName = isset( $info['map_name'] )
? $info['map_name'] : $tag;
- $validate = is_array( $info['validate'] ) ? $info['validate']
- : array( 'XMPValidate', $info['validate'] );
+ if ( is_array( $info['validate'] ) ) {
+ $validate = $info['validate'];
+ } else {
+ $validator = new XMPValidate( $this->logger );
+ $validate = array( $validator, $info['validate'] );
+ }
if ( !isset( $this->results['xmp-' . $info['map_group']][$finalName] ) ) {
// This can happen if all the members of the struct failed validation.
- wfDebugLog( 'XMP', __METHOD__ . " <$ns:$tag> has no valid members." );
+ $this->logger->debug( __METHOD__ . " <$ns:$tag> has no valid members." );
} elseif ( is_callable( $validate ) ) {
$val =& $this->results['xmp-' . $info['map_group']][$finalName];
call_user_func_array( $validate, array( $info, &$val, false ) );
if ( is_null( $val ) ) {
// the idea being the validation function will unset the variable if
// its invalid.
- wfDebugLog( 'XMP', __METHOD__ . " <$ns:$tag> failed validation." );
+ $this->logger->info( __METHOD__ . " <$ns:$tag> failed validation." );
unset( $this->results['xmp-' . $info['map_group']][$finalName] );
}
} else {
- wfDebugLog( 'XMP', __METHOD__ . " Validation function for $finalName ("
+ $this->logger->warning( __METHOD__ . " Validation function for $finalName ("
. $validate[0] . '::' . $validate[1] . '()) is not callable.' );
}
}
array_shift( $this->mode );
if ( !isset( $this->results['xmp-' . $info['map_group']][$finalName] ) ) {
- wfDebugLog( 'XMP', __METHOD__ . " Empty compund element $finalName." );
+ $this->logger->debug( __METHOD__ . " Empty compund element $finalName." );
return;
}
if ( $elm === self::NS_RDF . ' type' ) {
// these aren't really supported properly yet.
// However, it appears they almost never used.
- wfDebugLog( 'XMP', __METHOD__ . ' encountered <rdf:type>' );
+ $this->logger->info( __METHOD__ . ' encountered <rdf:type>' );
}
if ( strpos( $elm, ' ' ) === false ) {
// However, there is a bug in an adobe product
// that forgets the namespace on some things.
// (Luckily they are unimportant things).
- wfDebugLog( 'XMP', __METHOD__ . " Encountered </$elm> which has no namespace. Skipping." );
+ $this->logger->info( __METHOD__ . " Encountered </$elm> which has no namespace. Skipping." );
return;
}
$this->endElementModeQDesc( $elm );
break;
default:
- wfDebugLog( 'XMP', __METHOD__ . " no mode (elm = $elm)" );
+ $this->logger->warning( __METHOD__ . " no mode (elm = $elm)" );
break;
}
}
array_unshift( $this->mode, self::MODE_LI );
} elseif ( $elm === self::NS_RDF . ' Bag' ) {
# bug 27105
- wfDebugLog( 'XMP', __METHOD__ . ' Expected an rdf:Seq, but got an rdf:Bag. Pretending'
+ $this->logger->info( __METHOD__ . ' Expected an rdf:Seq, but got an rdf:Bag. Pretending'
. ' it is a Seq, since some buggy software is known to screw this up.' );
array_unshift( $this->mode, self::MODE_LI );
} else {
throw new RuntimeException( __METHOD__ . ' Encountered <rdf:value> where it was unexpected.' );
} else {
// something else we don't recognize, like a qualifier maybe.
- wfDebugLog( 'XMP', __METHOD__ .
+ $this->logger->info( __METHOD__ .
" Encountered element <$elm> where only expecting character data as value of " .
$this->curItem[0] );
array_unshift( $this->mode, self::MODE_IGNORE );
// a child of a struct), then something weird is
// happening, so ignore this element and its children.
- wfDebugLog( 'XMP', "Encountered <$ns:$tag> outside"
+ $this->logger->warning( "Encountered <$ns:$tag> outside"
. " of its expected parent. Ignoring." );
array_unshift( $this->mode, self::MODE_IGNORE );
}
} else {
// This element is not on our list of allowed elements so ignore.
- wfDebugLog( 'XMP', __METHOD__ . " Ignoring unrecognized element <$ns:$tag>." );
+ $this->logger->debug( __METHOD__ . " Ignoring unrecognized element <$ns:$tag>." );
array_unshift( $this->mode, self::MODE_IGNORE );
array_unshift( $this->curItem, $ns . ' ' . $tag );
//
// also it seems as if exiv2 and exiftool do not support
// this either (That or I misunderstand the standard)
- wfDebugLog( 'XMP', __METHOD__ . ' Encountered <rdf:type> which isn\'t currently supported' );
+ $this->logger->info( __METHOD__ . ' Encountered <rdf:type> which isn\'t currently supported' );
}
if ( strpos( $elm, ' ' ) === false ) {
// This probably shouldn't happen.
- wfDebugLog( 'XMP', __METHOD__ . " Encountered <$elm> which has no namespace. Skipping." );
+ $this->logger->info( __METHOD__ . " Encountered <$elm> which has no namespace. Skipping." );
return;
}
if ( strpos( $name, ' ' ) === false ) {
// This shouldn't happen, but so far some old software forgets namespace
// on rdf:about.
- wfDebugLog( 'XMP', __METHOD__ . ' Encountered non-namespaced attribute: '
+ $this->logger->info( __METHOD__ . ' Encountered non-namespaced attribute: '
. " $name=\"$val\". Skipping. " );
continue;
}
}
$this->saveValue( $ns, $tag, $val );
} else {
- wfDebugLog( 'XMP', __METHOD__ . " Ignoring unrecognized element <$ns:$tag>." );
+ $this->logger->debug( __METHOD__ . " Ignoring unrecognized element <$ns:$tag>." );
}
}
}
$finalName = isset( $info['map_name'] )
? $info['map_name'] : $tag;
if ( isset( $info['validate'] ) ) {
- $validate = is_array( $info['validate'] ) ? $info['validate']
- : array( 'XMPValidate', $info['validate'] );
+ if ( is_array( $info['validate'] ) ) {
+ $validate = $info['validate'];
+ } else {
+ $validator = new XMPValidate( $this->logger );
+ $validate = array( $validator, $info['validate'] );
+ }
if ( is_callable( $validate ) ) {
call_user_func_array( $validate, array( $info, &$val, true ) );
// the reasoning behind using &$val instead of using the return value
// is to be consistent between here and validating structures.
if ( is_null( $val ) ) {
- wfDebugLog( 'XMP', __METHOD__ . " <$ns:$tag> failed validation." );
+ $this->logger->info( __METHOD__ . " <$ns:$tag> failed validation." );
return;
}
} else {
- wfDebugLog( 'XMP', __METHOD__ . " Validation function for $finalName ("
+ $this->logger->warning( __METHOD__ . " Validation function for $finalName ("
. $validate[0] . '::' . $validate[1] . '()) is not callable.' );
}
}