1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SESP\Annotator; |
4
|
|
|
|
5
|
|
|
use SESP\PropertyRegistry; |
6
|
|
|
|
7
|
|
|
use SMW\SemanticData; |
8
|
|
|
use SMW\DIProperty; |
9
|
|
|
use SMW\Subobject; |
10
|
|
|
|
11
|
|
|
use SMWDataItem as DataItem; |
12
|
|
|
use SMWDITime as DITime; |
13
|
|
|
use SMWDIBlob as DIBlob; |
14
|
|
|
use SMWDINumber as DINumber; |
15
|
|
|
|
16
|
|
|
use FormatMetadata; |
17
|
|
|
use Title; |
18
|
|
|
use File; |
19
|
|
|
|
20
|
|
|
use RuntimeException; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @ingroup SESP |
24
|
|
|
* |
25
|
|
|
* @license GNU GPL v2+ |
26
|
|
|
* @since 1.0 |
27
|
|
|
* |
28
|
|
|
* @author mwjames |
29
|
|
|
* @author rotsee |
30
|
|
|
* @author Stephan Gambke |
31
|
|
|
*/ |
32
|
|
|
class ExifDataAnnotator { |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var SemanticData |
36
|
|
|
*/ |
37
|
|
|
private $semanticData = null; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var File |
41
|
|
|
*/ |
42
|
|
|
private $file = null; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var Subobject |
46
|
|
|
*/ |
47
|
|
|
private $subobject = null; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @since 1.0 |
51
|
|
|
* |
52
|
|
|
* @param SemanticData $semanticData |
53
|
|
|
* @param File $file |
54
|
|
|
*/ |
55
|
11 |
|
public function __construct( SemanticData $semanticData, File $file ) { |
56
|
11 |
|
$this->semanticData = $semanticData; |
57
|
11 |
|
$this->file = $file; |
58
|
11 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @since 1.0 |
62
|
|
|
* |
63
|
|
|
* @return SemanticData |
64
|
|
|
*/ |
65
|
7 |
|
public function getSemanticData() { |
66
|
7 |
|
return $this->semanticData; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @since 1.0 |
71
|
|
|
* |
72
|
|
|
* @return boolean |
73
|
|
|
*/ |
74
|
10 |
|
public function addAnnotation() { |
75
|
|
|
|
76
|
10 |
|
if ( !$this->file->exists() ) { |
77
|
1 |
|
return false; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// #66 |
81
|
9 |
|
$meta = $this->file->getMetadata(); |
82
|
|
|
|
83
|
9 |
|
if ( !$meta ) { |
84
|
1 |
|
return false; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
// Guard against "Error at offset 0 of 1 bytes" |
88
|
8 |
|
$exif = @unserialize( $meta ); |
89
|
|
|
|
90
|
8 |
|
if ( !is_array( $exif ) || count( $exif ) === 0 ) { |
91
|
1 |
|
return false; |
92
|
|
|
} |
93
|
|
|
|
94
|
7 |
|
$exif[ 'ImageWidth' ] = $this->file->getWidth(); |
95
|
7 |
|
$exif[ 'ImageLength' ] = $this->file->getHeight(); |
96
|
|
|
|
97
|
7 |
|
return $this->processExifData( $exif ); |
98
|
|
|
} |
99
|
|
|
|
100
|
7 |
|
protected function processExifData( $rawExif ) { |
101
|
|
|
|
102
|
7 |
|
$this->subobject = new Subobject( $this->getSemanticData()->getSubject()->getTitle() ); |
|
|
|
|
103
|
7 |
|
$this->subobject->setSemanticData( '_EXIFDATA' ); |
|
|
|
|
104
|
|
|
|
105
|
7 |
|
$this->addPropertyValuesFromExifData( $rawExif ); |
106
|
|
|
|
107
|
7 |
|
if ( $this->subobject->getSemanticData()->isEmpty() ) { |
108
|
2 |
|
return true; |
109
|
|
|
} |
110
|
|
|
|
111
|
5 |
|
$this->getSemanticData()->addPropertyObjectValue( |
112
|
5 |
|
new DIProperty( PropertyRegistry::getInstance()->getPropertyId( '_EXIFDATA' ) ), |
113
|
5 |
|
$this->subobject->getContainer() |
114
|
5 |
|
); |
115
|
|
|
|
116
|
5 |
|
return true; |
117
|
|
|
} |
118
|
|
|
|
119
|
7 |
|
protected function addPropertyValuesFromExifData( $rawExif ) { |
120
|
|
|
|
121
|
7 |
|
$formattedExif = FormatMetadata::getFormattedData( $rawExif ); |
122
|
|
|
|
123
|
7 |
|
foreach ( $formattedExif as $key => $value ) { |
124
|
|
|
|
125
|
7 |
|
$dataItem = null; |
126
|
7 |
|
$propertyId = PropertyRegistry::getInstance()->getPropertyId( $key ); |
127
|
|
|
|
128
|
7 |
|
if ( $propertyId === null ) { |
129
|
2 |
|
continue; |
130
|
|
|
} |
131
|
|
|
|
132
|
7 |
|
$dataItemType = PropertyRegistry::getInstance()->getPropertyType( $key ); |
133
|
|
|
|
134
|
|
|
switch ( $dataItemType ) { |
135
|
7 |
|
case DataItem::TYPE_NUMBER : |
136
|
7 |
|
$dataItem = is_numeric( $rawExif[$key] ) ? new DINumber( $rawExif[$key] ) : null; |
137
|
7 |
|
break; |
138
|
5 |
|
case DataItem::TYPE_BLOB : |
139
|
1 |
|
$dataItem = new DIBlob( $value ); |
140
|
1 |
|
break; |
141
|
4 |
|
case DataItem::TYPE_TIME : |
142
|
4 |
|
$dataItem = $this->makeDataItemTime( $rawExif[$key] ); |
143
|
4 |
|
} |
144
|
|
|
|
145
|
7 |
|
if ( $dataItem !== null ) { |
146
|
5 |
|
$this->subobject->getSemanticData()->addPropertyObjectValue( |
147
|
5 |
|
new DIProperty( $propertyId ), |
148
|
|
|
$dataItem |
149
|
5 |
|
); |
150
|
5 |
|
} |
151
|
|
|
|
152
|
7 |
|
} |
153
|
7 |
|
} |
154
|
|
|
|
155
|
4 |
|
protected function makeDataItemTime( $exifValue ) { |
156
|
4 |
|
$datetime = $this->convertExifDate( $exifValue ); |
157
|
|
|
|
158
|
4 |
|
if ( $datetime ) { |
159
|
3 |
|
return new DITime( |
160
|
3 |
|
DITime::CM_GREGORIAN, |
161
|
3 |
|
$datetime->format('Y'), |
162
|
3 |
|
$datetime->format('n'), |
|
|
|
|
163
|
3 |
|
$datetime->format('j'), |
|
|
|
|
164
|
3 |
|
$datetime->format('G'), |
|
|
|
|
165
|
3 |
|
$datetime->format('i') |
|
|
|
|
166
|
3 |
|
); |
167
|
|
|
} |
168
|
1 |
|
} |
169
|
|
|
|
170
|
4 |
|
protected function convertExifDate( $exifString ) { |
171
|
|
|
|
172
|
|
|
// Unknown date |
173
|
4 |
|
if ( $exifString == '0000:00:00 00:00:00' || $exifString == ' : : : : ' ) { |
174
|
1 |
|
return false; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
// Full date |
178
|
3 |
|
if ( preg_match( '/^(?:\d{4}):(?:\d\d):(?:\d\d) (?:\d\d):(?:\d\d):(?:\d\d)$/D', $exifString ) ) { |
179
|
1 |
|
return new \DateTime( $exifString ); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
// No second field, timeanddate doesn't include seconds but second still available in api |
183
|
2 |
|
if ( preg_match( '/^(?:\d{4}):(?:\d\d):(?:\d\d) (?:\d\d):(?:\d\d)$/D', $exifString ) ) { |
184
|
1 |
|
return new \DateTime( $exifString . ':00' ); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
// Only the date but not the time |
188
|
1 |
|
if ( preg_match( '/^(?:\d{4}):(?:\d\d):(?:\d\d)$/D', $exifString ) ) { |
189
|
1 |
|
return new \DateTime( |
190
|
1 |
|
substr( $exifString, 0, 4 ) . ':' . |
191
|
1 |
|
substr( $exifString, 5, 2 ) . ':' . |
192
|
1 |
|
substr( $exifString, 8, 2 ) . ' 00:00:00' |
193
|
1 |
|
); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
return false; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
} |
200
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: