|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* File containing the Image converter. |
|
5
|
|
|
* |
|
6
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
|
7
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter; |
|
10
|
|
|
|
|
11
|
|
|
use eZ\Publish\Core\IO\IOServiceInterface; |
|
12
|
|
|
use eZ\Publish\Core\IO\UrlRedecoratorInterface; |
|
13
|
|
|
use eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue; |
|
14
|
|
|
use eZ\Publish\SPI\Persistence\Content\FieldValue; |
|
15
|
|
|
|
|
16
|
|
|
class ImageConverter extends BinaryFileConverter |
|
17
|
|
|
{ |
|
18
|
|
|
/** @var \eZ\Publish\Core\IO\IOServiceInterface */ |
|
19
|
|
|
private $imageIoService; |
|
20
|
|
|
|
|
21
|
|
|
/** @var \eZ\Publish\Core\IO\UrlRedecoratorInterface */ |
|
22
|
|
|
private $urlRedecorator; |
|
23
|
|
|
|
|
24
|
|
|
public function __construct(IOServiceInterface $imageIoService, UrlRedecoratorInterface $urlRedecorator) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->imageIoService = $imageIoService; |
|
27
|
|
|
$this->urlRedecorator = $urlRedecorator; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Converts data from $value to $storageFieldValue. |
|
32
|
|
|
* |
|
33
|
|
|
* @param \eZ\Publish\SPI\Persistence\Content\FieldValue $value |
|
34
|
|
|
* @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue $storageFieldValue |
|
35
|
|
|
*/ |
|
36
|
|
|
public function toStorageValue(FieldValue $value, StorageFieldValue $storageFieldValue) |
|
37
|
|
|
{ |
|
38
|
|
|
if (isset($value->data)) { |
|
39
|
|
|
// Determine what needs to be stored |
|
40
|
|
|
if (isset($value->data['width']) && isset($value->data['fieldId'])) { |
|
41
|
|
|
// width + field id set means that something really needs to be stored |
|
42
|
|
|
$storageFieldValue->dataText = $this->createLegacyXml($value->data); |
|
43
|
|
|
} elseif (isset($value->data['fieldId'])) { |
|
44
|
|
|
// $fieldId without width mleans an empty field |
|
45
|
|
|
$storageFieldValue->dataText = $this->createEmptyLegacyXml($value->data); |
|
46
|
|
|
} |
|
47
|
|
|
// otherwise the image is unprocessed and the DB field stays empty |
|
48
|
|
|
// there will be a subsequent call to this method, after the image |
|
49
|
|
|
// has been stored |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Creates an XML considered "empty" by the legacy storage. |
|
55
|
|
|
* |
|
56
|
|
|
* @param array $contentMetaData |
|
57
|
|
|
* |
|
58
|
|
|
* @return string |
|
59
|
|
|
*/ |
|
60
|
|
|
protected function createEmptyLegacyXml($contentMetaData) |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->fillXml( |
|
63
|
|
|
array_merge( |
|
64
|
|
|
array( |
|
65
|
|
|
'uri' => '', |
|
66
|
|
|
'path' => '', |
|
67
|
|
|
'width' => '', |
|
68
|
|
|
'height' => '', |
|
69
|
|
|
'mime' => '', |
|
70
|
|
|
'alternativeText' => '', |
|
71
|
|
|
), |
|
72
|
|
|
$contentMetaData |
|
73
|
|
|
), |
|
74
|
|
|
array( |
|
75
|
|
|
'basename' => '', |
|
76
|
|
|
'extension' => '', |
|
77
|
|
|
'dirname' => '', |
|
78
|
|
|
'filename' => '', |
|
79
|
|
|
), |
|
80
|
|
|
time() |
|
81
|
|
|
); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Returns the XML required by the legacy database. |
|
86
|
|
|
* |
|
87
|
|
|
* @param array $data |
|
88
|
|
|
* |
|
89
|
|
|
* @return string |
|
90
|
|
|
*/ |
|
91
|
|
|
protected function createLegacyXml(array $data) |
|
92
|
|
|
{ |
|
93
|
|
|
$data['uri'] = $this->urlRedecorator->redecorateFromSource($data['uri']); |
|
94
|
|
|
$pathInfo = pathinfo($data['uri']); |
|
95
|
|
|
|
|
96
|
|
|
return $this->fillXml($data, $pathInfo, time()); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Fill the XML template with the data provided. |
|
101
|
|
|
* |
|
102
|
|
|
* @param array $imageData |
|
103
|
|
|
* @param array $pathInfo |
|
104
|
|
|
* @param int $timestamp |
|
105
|
|
|
* |
|
106
|
|
|
* @return string |
|
107
|
|
|
*/ |
|
108
|
|
|
protected function fillXml($imageData, $pathInfo, $timestamp) |
|
109
|
|
|
{ |
|
110
|
|
|
// <?xml version="1.0" encoding="utf-8" |
|
111
|
|
|
// <ezimage serial_number="1" is_valid="1" filename="River-Boat.jpg" suffix="jpg" basename="River-Boat" dirpath="var/ezdemo_site/storage/images/travel/peruvian-amazon/river-boat/322-1-eng-US" url="var/ezdemo_site/storage/images/travel/peruvian-amazon/river-boat/322-1-eng-US/River-Boat.jpg" original_filename="bbbbc2fe.jpg" mime_type="image/jpeg" width="770" height="512" alternative_text="Old River Boat" alias_key="1293033771" timestamp="1342530101"> |
|
112
|
|
|
// <original attribute_id="322" attribute_version="1" attribute_language="eng-US"/> |
|
113
|
|
|
// <information Height="512" Width="770" IsColor="1"/> |
|
114
|
|
|
// </ezimage> |
|
115
|
|
|
$xml = <<<EOT |
|
116
|
|
|
<?xml version="1.0" encoding="utf-8"?> |
|
117
|
|
|
<ezimage serial_number="1" is_valid="%s" filename="%s" |
|
118
|
|
|
suffix="%s" basename="%s" dirpath="%s" url="%s" |
|
119
|
|
|
original_filename="%s" mime_type="%s" width="%s" |
|
120
|
|
|
height="%s" alternative_text="%s" alias_key="%s" timestamp="%s"> |
|
121
|
|
|
<original attribute_id="%s" attribute_version="%s" attribute_language="%s"/> |
|
122
|
|
|
<information Height="%s" Width="%s" IsColor="%s"/> |
|
123
|
|
|
</ezimage> |
|
124
|
|
|
EOT; |
|
125
|
|
|
|
|
126
|
|
|
return sprintf( |
|
127
|
|
|
$xml, |
|
128
|
|
|
// <ezimage> |
|
129
|
|
|
($pathInfo['basename'] !== '' ? '1' : ''), // is_valid="%s" |
|
130
|
|
|
htmlspecialchars($pathInfo['basename']), // filename="%s" |
|
131
|
|
|
htmlspecialchars($pathInfo['extension']), // suffix="%s" |
|
132
|
|
|
htmlspecialchars($pathInfo['filename']), // basename="%s" |
|
133
|
|
|
htmlspecialchars($pathInfo['dirname']), // dirpath |
|
134
|
|
|
htmlspecialchars($imageData['uri']), // url |
|
135
|
|
|
htmlspecialchars($pathInfo['basename']), // @todo: Needs original file name, for whatever reason? |
|
136
|
|
|
htmlspecialchars($imageData['mime']), // mime_type |
|
137
|
|
|
htmlspecialchars($imageData['width']), // width |
|
138
|
|
|
htmlspecialchars($imageData['height']), // height |
|
139
|
|
|
htmlspecialchars($imageData['alternativeText']), // alternative_text |
|
140
|
|
|
htmlspecialchars(1293033771), // alias_key, fixed for the original image |
|
141
|
|
|
htmlspecialchars($timestamp), // timestamp |
|
142
|
|
|
// <original> |
|
143
|
|
|
$imageData['fieldId'], |
|
144
|
|
|
$imageData['versionNo'], |
|
145
|
|
|
$imageData['languageCode'], |
|
146
|
|
|
// <information> |
|
147
|
|
|
$imageData['height'], // Height |
|
148
|
|
|
$imageData['width'], // Width |
|
149
|
|
|
1 // IsColor @todo Do we need to fix that here? |
|
150
|
|
|
); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Converts data from $value to $fieldValue. |
|
155
|
|
|
* |
|
156
|
|
|
* @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue $value |
|
157
|
|
|
* @param \eZ\Publish\SPI\Persistence\Content\FieldValue $fieldValue |
|
158
|
|
|
*/ |
|
159
|
|
|
public function toFieldValue(StorageFieldValue $value, FieldValue $fieldValue) |
|
160
|
|
|
{ |
|
161
|
|
|
if (empty($value->dataText)) { |
|
162
|
|
|
// Special case for anonymous user |
|
163
|
|
|
return; |
|
164
|
|
|
} |
|
165
|
|
|
$fieldValue->data = $this->parseLegacyXml($value->dataText); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Parses the XML from the legacy database. |
|
170
|
|
|
* |
|
171
|
|
|
* Returns only the data required by the FieldType, nothing more. |
|
172
|
|
|
* |
|
173
|
|
|
* @param string $xml |
|
174
|
|
|
* |
|
175
|
|
|
* @return array |
|
176
|
|
|
*/ |
|
177
|
|
|
protected function parseLegacyXml($xml) |
|
178
|
|
|
{ |
|
179
|
|
|
$extractedData = array(); |
|
180
|
|
|
|
|
181
|
|
|
$dom = new \DOMDocument(); |
|
182
|
|
|
$dom->loadXml($xml); |
|
183
|
|
|
|
|
184
|
|
|
$ezimageTag = $dom->documentElement; |
|
185
|
|
|
|
|
186
|
|
|
if (!$ezimageTag->hasAttribute('url')) { |
|
187
|
|
|
throw new \RuntimeException('Missing attribute "url" in <ezimage/> tag.'); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
if (($legacyUrl = $ezimageTag->getAttribute('url')) === '') { |
|
191
|
|
|
// Detected XML considered "empty" by the legacy storage |
|
192
|
|
|
return null; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
$url = $this->urlRedecorator->redecorateFromTarget($legacyUrl); |
|
196
|
|
|
$extractedData['id'] = $this->imageIoService->loadBinaryFileByUri($url)->id; |
|
197
|
|
|
|
|
198
|
|
|
if (!$ezimageTag->hasAttribute('filename')) { |
|
199
|
|
|
throw new \RuntimeException('Missing attribute "filename" in <ezimage/> tag.'); |
|
200
|
|
|
} |
|
201
|
|
|
$extractedData['fileName'] = $ezimageTag->getAttribute('filename'); |
|
202
|
|
|
$extractedData['width'] = $ezimageTag->getAttribute('width'); |
|
203
|
|
|
$extractedData['height'] = $ezimageTag->getAttribute('height'); |
|
204
|
|
|
$extractedData['mime'] = $ezimageTag->getAttribute('mime_type'); |
|
205
|
|
|
|
|
206
|
|
|
if (!$ezimageTag->hasAttribute('alternative_text')) { |
|
207
|
|
|
throw new \RuntimeException('Missing attribute "alternative_text" in <ezimage/> tag.'); |
|
208
|
|
|
} |
|
209
|
|
|
$extractedData['alternativeText'] = $ezimageTag->getAttribute('alternative_text'); |
|
210
|
|
|
|
|
211
|
|
|
return $extractedData; |
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
|