* @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;
+ }
+
/**
* Main use is if a single item has multiple xmp documents describing it.
* For example in jpeg's with extendedXMP
. ' 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.
return false;
}
} catch ( Exception $e ) {
- wfDebugLog( 'XMP', 'XMP parse error: ' . $e );
+ $this->logger->info( 'XMP parse error: ' . $e );
$this->results = array();
return false;
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 );
}
$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.' );
}
}
* @ingroup Media
*/
+use Psr\Log\LoggerInterface;
+use Psr\Log\LoggerAwareInterface;
+
/**
* This contains some static methods for
* validating XMP properties. See XMPInfo and XMPReader classes.
* @see http://www.adobe.com/devnet/xmp/pdfs/XMPSpecificationPart1.pdf starting at pg 28
* @see http://www.adobe.com/devnet/xmp/pdfs/XMPSpecificationPart2.pdf starting at pg 11
*/
-class XMPValidate {
+class XMPValidate implements LoggerAwareInterface {
+
+ /**
+ * @var LoggerInterface
+ */
+ private $logger;
+
+ public function __construct( LoggerInterface $logger ) {
+ $this->setLogger( $logger );
+ }
+
+ public function setLogger( LoggerInterface $logger ) {
+ $this->logger = $logger;
+ }
/**
* Function to validate boolean properties ( True or False )
*
* @param mixed &$val Current value to validate
* @param bool $standalone If this is a simple property or array
*/
- public static function validateBoolean( $info, &$val, $standalone ) {
+ public function validateBoolean( $info, &$val, $standalone ) {
if ( !$standalone ) {
// this only validates standalone properties, not arrays, etc
return;
}
if ( $val !== 'True' && $val !== 'False' ) {
- wfDebugLog( 'XMP', __METHOD__ . " Expected True or False but got $val" );
+ $this->debug->info( __METHOD__ . " Expected True or False but got $val" );
$val = null;
}
}
* @param mixed &$val Current value to validate
* @param bool $standalone If this is a simple property or array
*/
- public static function validateRational( $info, &$val, $standalone ) {
+ public function validateRational( $info, &$val, $standalone ) {
if ( !$standalone ) {
// this only validates standalone properties, not arrays, etc
return;
}
if ( !preg_match( '/^(?:-?\d+)\/(?:\d+[1-9]|[1-9]\d*)$/D', $val ) ) {
- wfDebugLog( 'XMP', __METHOD__ . " Expected rational but got $val" );
+ $this->logger->info( __METHOD__ . " Expected rational but got $val" );
$val = null;
}
}
* @param mixed &$val Current value to validate
* @param bool $standalone If this is a simple property or array
*/
- public static function validateRating( $info, &$val, $standalone ) {
+ public function validateRating( $info, &$val, $standalone ) {
if ( !$standalone ) {
// this only validates standalone properties, not arrays, etc
return;
if ( !preg_match( '/^[-+]?\d*(?:\.?\d*)$/D', $val )
|| !is_numeric( $val )
) {
- wfDebugLog( 'XMP', __METHOD__ . " Expected rating but got $val" );
+ $this->logger->info( __METHOD__ . " Expected rating but got $val" );
$val = null;
return;
// We do < 0 here instead of < -1 here, since
// the values between 0 and -1 are also illegal
// as -1 is meant as a special reject rating.
- wfDebugLog( 'XMP', __METHOD__ . " Rating too low, setting to -1 (Rejected)" );
+ $this->logger->info( __METHOD__ . " Rating too low, setting to -1 (Rejected)" );
$val = '-1';
return;
}
if ( $nVal > 5 ) {
- wfDebugLog( 'XMP', __METHOD__ . " Rating too high, setting to 5" );
+ $this->logger->info( __METHOD__ . " Rating too high, setting to 5" );
$val = '5';
return;
* @param mixed &$val Current value to validate
* @param bool $standalone If this is a simple property or array
*/
- public static function validateInteger( $info, &$val, $standalone ) {
+ public function validateInteger( $info, &$val, $standalone ) {
if ( !$standalone ) {
// this only validates standalone properties, not arrays, etc
return;
}
if ( !preg_match( '/^[-+]?\d+$/D', $val ) ) {
- wfDebugLog( 'XMP', __METHOD__ . " Expected integer but got $val" );
+ $this->logger->info( __METHOD__ . " Expected integer but got $val" );
$val = null;
}
}
* @param mixed &$val Current value to validate
* @param bool $standalone If this is a simple property or array
*/
- public static function validateClosed( $info, &$val, $standalone ) {
+ public function validateClosed( $info, &$val, $standalone ) {
if ( !$standalone ) {
// this only validates standalone properties, not arrays, etc
return;
}
if ( !isset( $info['choices'][$val] ) && !$inRange ) {
- wfDebugLog( 'XMP', __METHOD__ . " Expected closed choice, but got $val" );
+ $this->logger->info( __METHOD__ . " Expected closed choice, but got $val" );
$val = null;
}
}
* @param mixed &$val Current value to validate
* @param bool $standalone If this is a simple property or array
*/
- public static function validateFlash( $info, &$val, $standalone ) {
+ public function validateFlash( $info, &$val, $standalone ) {
if ( $standalone ) {
// this only validates flash structs, not individual properties
return;
&& isset( $val['RedEyeMode'] )
&& isset( $val['Return'] )
) ) {
- wfDebugLog( 'XMP', __METHOD__ . " Flash structure did not have all the required components" );
+ $this->logger->info( __METHOD__ . " Flash structure did not have all the required components" );
$val = null;
} else {
$val = ( "\0" | ( $val['Fired'] === 'True' )
* @param mixed &$val Current value to validate
* @param bool $standalone If this is a simple property or array
*/
- public static function validateLangCode( $info, &$val, $standalone ) {
+ public function validateLangCode( $info, &$val, $standalone ) {
if ( !$standalone ) {
// this only validates standalone properties, not arrays, etc
return;
}
if ( !preg_match( '/^[-A-Za-z0-9]{2,}$/D', $val ) ) {
//this is a rather naive check.
- wfDebugLog( 'XMP', __METHOD__ . " Expected Lang code but got $val" );
+ $this->logger->info( __METHOD__ . " Expected Lang code but got $val" );
$val = null;
}
}
* 2011:04.
* @param bool $standalone If this is a simple property or array
*/
- public static function validateDate( $info, &$val, $standalone ) {
+ public function validateDate( $info, &$val, $standalone ) {
if ( !$standalone ) {
// this only validates standalone properties, not arrays, etc
return;
) {
// @codingStandardsIgnoreEnd
- wfDebugLog( 'XMP', __METHOD__ . " Expected date but got $val" );
+ $this->logger->info( __METHOD__ . " Expected date but got $val" );
$val = null;
} else {
/*
* some programs convert between metadata formats.
*/
if ( $res[1] === '0000' ) {
- wfDebugLog( 'XMP', __METHOD__ . " Invalid date (year 0): $val" );
+ $this->logger->info( __METHOD__ . " Invalid date (year 0): $val" );
$val = null;
return;
* or DDD,MM.mmk form
* @param bool $standalone If its a simple prop (should always be true)
*/
- public static function validateGPS( $info, &$val, $standalone ) {
+ public function validateGPS( $info, &$val, $standalone ) {
if ( !$standalone ) {
return;
}
return;
} else {
- wfDebugLog( 'XMP', __METHOD__
+ $this->logger->info( __METHOD__
. " Expected GPSCoordinate, but got $val." );
$val = null;