Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

Image/ImageStorage/Gateway/LegacyStorage.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\FieldType\Image\ImageStorage\Gateway;
8
9 use eZ\Publish\API\Repository\Exceptions\NotImplementedException;
10 use eZ\Publish\Core\IO\UrlRedecoratorInterface;
11 use eZ\Publish\Core\Persistence\Database\DatabaseHandler;
12 use eZ\Publish\SPI\Persistence\Content\VersionInfo;
13 use eZ\Publish\Core\FieldType\Image\ImageStorage\Gateway;
14
18 class LegacyStorage extends Gateway
19 {
21 protected $dbHandler;
22
28 protected $fieldNameMap = [
29 'id' => 'fieldId',
30 'version' => 'versionNo',
31 'language_code' => 'languageCode',
32 'path_identification_string' => 'nodePathString',
33 'data_string' => 'xml',
34 ];
35
37 private $redecorator;
38
39 public function __construct(UrlRedecoratorInterface $redecorator, DatabaseHandler $dbHandler)
40 {
41 @trigger_error(
42 sprintf('%s is deprecated, use %s instead', self::class, DoctrineStorage::class),
43 E_USER_DEPRECATED
44 );
45 $this->redecorator = $redecorator;
46 $this->dbHandler = $dbHandler;
47 }
48
54 protected function getConnection()
55 {
56 return $this->dbHandler;
57 }
58
66 public function getNodePathString(VersionInfo $versionInfo)
67 {
68 $connection = $this->getConnection();
69
70 $selectQuery = $connection->createSelectQuery();
71 $selectQuery->select('path_identification_string')
72 ->from($connection->quoteTable('ezcontentobject_tree'))
73 ->where(
74 $selectQuery->expr->lAnd(
75 $selectQuery->expr->eq(
76 $connection->quoteColumn('contentobject_id'),
77 $selectQuery->bindValue($versionInfo->contentInfo->id)
78 ),
79 $selectQuery->expr->eq(
80 $connection->quoteColumn('contentobject_version'),
81 $selectQuery->bindValue($versionInfo->versionNo)
82 ),
83 $selectQuery->expr->eq(
84 $connection->quoteColumn('node_id'),
85 $connection->quoteColumn('main_node_id')
86 )
87 )
88 );
89 $statement = $selectQuery->prepare();
90 $statement->execute();
91
92 return $statement->fetchColumn();
93 }
94
101 public function storeImageReference($uri, $fieldId)
102 {
103 // legacy stores the path to the image without a leading /
104 $path = $this->redecorator->redecorateFromSource($uri);
105
106 $connection = $this->getConnection();
107
108 $insertQuery = $connection->createInsertQuery();
109 $insertQuery->insertInto($connection->quoteTable('ezimagefile'))
110 ->set(
111 $connection->quoteColumn('contentobject_attribute_id'),
112 $insertQuery->bindValue($fieldId, null, \PDO::PARAM_INT)
113 )->set(
114 $connection->quoteColumn('filepath'),
115 $insertQuery->bindValue($path)
116 );
117
118 $statement = $insertQuery->prepare();
119 $statement->execute();
120 }
121
130 public function getXmlForImages($versionNo, array $fieldIds)
131 {
132 $connection = $this->getConnection();
133
134 $selectQuery = $connection->createSelectQuery();
135 $selectQuery->select(
136 $connection->quoteColumn('id', 'ezcontentobject_attribute'),
137 $connection->quoteColumn('data_text', 'ezcontentobject_attribute')
138 )->from(
139 $connection->quoteTable('ezcontentobject_attribute')
140 )->where(
141 $selectQuery->expr->lAnd(
142 $selectQuery->expr->eq(
143 $connection->quoteColumn('version', 'ezcontentobject_attribute'),
144 $selectQuery->bindValue($versionNo, null, \PDO::PARAM_INT)
145 ),
146 $selectQuery->expr->in(
147 $connection->quoteColumn('id', 'ezcontentobject_attribute'),
148 $fieldIds
149 )
150 )
151 );
152
153 $statement = $selectQuery->prepare();
154 $statement->execute();
155
156 $fieldLookup = [];
157 foreach ($statement->fetchAll(\PDO::FETCH_ASSOC) as $row) {
158 $fieldLookup[$row['id']] = $row['data_text'];
159 }
160
161 return $fieldLookup;
162 }
163
171 public function removeImageReferences($uri, $versionNo, $fieldId)
172 {
173 $path = $this->redecorator->redecorateFromSource($uri);
174
175 if (!$this->canRemoveImageReference($path, $versionNo, $fieldId)) {
176 return;
177 }
178
179 $connection = $this->getConnection();
180
181 $deleteQuery = $connection->createDeleteQuery();
182 $deleteQuery->deleteFrom(
183 $connection->quoteTable('ezimagefile')
184 )->where(
185 $deleteQuery->expr->lAnd(
186 $deleteQuery->expr->eq(
187 $connection->quoteColumn('contentobject_attribute_id'),
188 $deleteQuery->bindValue($fieldId, null, \PDO::PARAM_INT)
189 ),
190 $deleteQuery->expr->like(
191 $connection->quoteColumn('filepath'),
192 $deleteQuery->bindValue($path . '%')
193 )
194 )
195 );
196
197 $statement = $deleteQuery->prepare();
198 $statement->execute();
199 }
200
208 public function countImageReferences($uri)
209 {
210 $path = $this->redecorator->redecorateFromSource($uri);
211
212 $connection = $this->getConnection();
213
214 $selectQuery = $connection->createSelectQuery();
215 $selectQuery->select(
216 $selectQuery->expr->count(
217 $connection->quoteColumn('id')
218 )
219 )->from(
220 $connection->quoteTable('ezimagefile')
221 )->where(
222 $selectQuery->expr->eq(
223 $connection->quoteColumn('filepath'),
224 $selectQuery->bindValue($path)
225 )
226 );
227
228 $statement = $selectQuery->prepare();
229 $statement->execute();
230
231 return (int)$statement->fetchColumn();
232 }
233
234 public function isImageReferenced(string $uri): bool
235 {
236 $path = $this->redecorator->redecorateFromSource($uri);
237
238 $connection = $this->getConnection();
239
240 $selectQuery = $connection->createSelectQuery();
241 $selectQuery
242 ->select(1)
243 ->from($connection->quoteTable('ezimagefile'))
244 ->where(
245 $selectQuery->expr->eq(
246 $connection->quoteColumn('filepath'),
247 $selectQuery->bindValue($path)
248 )
249 );
250
251 $statement = $selectQuery->prepare();
252 $statement->execute();
253
254 return (bool)$statement->fetchColumn();
255 }
256
266 protected function canRemoveImageReference($path, $versionNo, $fieldId)
267 {
268 $connection = $this->getConnection();
269
270 $selectQuery = $connection->createSelectQuery();
271 $selectQuery->select(
272 $selectQuery->expr->count(
273 $connection->quoteColumn('id', 'ezcontentobject_attribute')
274 )
275 )->from(
276 $connection->quoteTable('ezcontentobject_attribute')
277 )->innerJoin(
278 $connection->quoteTable('ezimagefile'),
279 $selectQuery->expr->eq(
280 $connection->quoteColumn('contentobject_attribute_id', 'ezimagefile'),
281 $connection->quoteColumn('id', 'ezcontentobject_attribute')
282 )
283 )->where(
284 $selectQuery->expr->lAnd(
285 $selectQuery->expr->eq(
286 $connection->quoteColumn('contentobject_attribute_id'),
287 $selectQuery->bindValue($fieldId, null, \PDO::PARAM_INT)
288 ),
289 $selectQuery->expr->neq(
290 $connection->quoteColumn('version'),
291 $selectQuery->bindValue($versionNo, null, \PDO::PARAM_INT)
292 ),
293 $selectQuery->expr->like(
294 $connection->quoteColumn('filepath'),
295 $selectQuery->bindValue($path . '%')
296 )
297 )
298 );
299
300 $statement = $selectQuery->prepare();
301 $statement->execute();
302
303 return (int)$statement->fetchColumn() === 0;
304 }
305
306 public function extractFilesFromXml($xml)
307 {
308 if (empty($xml)) {
309 // Empty image value
310 return null;
311 }
312
313 $files = [];
314
315 $dom = new \DOMDocument();
316 $dom->loadXml($xml);
317 if ($dom->documentElement->hasAttribute('dirpath')) {
318 $url = $dom->documentElement->getAttribute('url');
319 if (empty($url)) {
320 return null;
321 }
322
323 $files['original'] = $this->redecorator->redecorateFromTarget($url);
325 foreach ($dom->documentElement->childNodes as $childNode) {
326 if ($childNode->nodeName != 'alias') {
327 continue;
328 }
329
330 $files[$childNode->getAttribute('name')] = $this->redecorator->redecorateFromTarget(
331 $childNode->getAttribute('url')
332 );
333 }
334
335 return $files;
336 }
337
338 return null;
339 }
340
344 public function getAllVersionsImageXmlForFieldId(int $fieldId): array
345 {
346 throw new NotImplementedException('getAllVersionsImageXmlForFieldId is not supported with LegacyStorage gateway, inject DoctrineStorage gateway instead to use it');
347 }
348
352 public function updateImageData(int $fieldId, int $versionNo, string $xml): void
353 {
354 throw new NotImplementedException('updateImageData is not supported with LegacyStorage gateway, inject DoctrineStorage gateway instead to use it');
355 }
356
360 public function getImagesData(int $offset, int $limit): array
361 {
362 throw new NotImplementedException('getImagesData is not supported with LegacyStorage gateway, inject DoctrineStorage gateway instead to use it');
363 }
364
368 public function updateImagePath(int $fieldId, string $oldPath, string $newPath): void
369 {
370 throw new NotImplementedException('updateImagePath is not supported with LegacyStorage gateway, inject DoctrineStorage gateway instead to use it');
371 }
372
376 public function countDistinctImagesData(): int
377 {
378 throw new NotImplementedException('countDistinctImagesData is not supported with LegacyStorage gateway, inject DoctrineStorage gateway instead to use it');
379 }
380 }
eZ\Publish\API\Repository\Exceptions\NotImplementedException
This Exception is thrown if a feature has not been implemented intentionally.
Definition: API/Repository/Exceptions/NotImplementedException.php:15
eZ\Publish\Core\FieldType\Image\ImageStorage\Gateway\LegacyStorage
Definition: Image/ImageStorage/Gateway/LegacyStorage.php:19
eZ\Publish\Core\FieldType\Image\ImageStorage\Gateway\LegacyStorage\canRemoveImageReference
canRemoveImageReference($path, $versionNo, $fieldId)
Checks if image $path can be removed when deleting $versionNo and $fieldId.
Definition: Image/ImageStorage/Gateway/LegacyStorage.php:266
eZ\Publish\Core\FieldType\Image\ImageStorage\Gateway\LegacyStorage\updateImagePath
updateImagePath(int $fieldId, string $oldPath, string $newPath)
Definition: Image/ImageStorage/Gateway/LegacyStorage.php:368
eZ\Publish\Core\FieldType\Image\ImageStorage\Gateway\LegacyStorage\$redecorator
$redecorator
Definition: Image/ImageStorage/Gateway/LegacyStorage.php:37
eZ\Publish\Core\FieldType\Image\ImageStorage\Gateway\LegacyStorage\countDistinctImagesData
countDistinctImagesData()
Definition: Image/ImageStorage/Gateway/LegacyStorage.php:376
eZ\Publish\Core\FieldType\Image\ImageStorage\Gateway\LegacyStorage\getXmlForImages
getXmlForImages($versionNo, array $fieldIds)
Returns a the XML content stored for the given $fieldIds.
Definition: Image/ImageStorage/Gateway/LegacyStorage.php:130
eZ\Publish\Core\FieldType\Image\ImageStorage\Gateway\LegacyStorage\getConnection
getConnection()
Returns the active connection.
Definition: Image/ImageStorage/Gateway/LegacyStorage.php:54
eZ\Publish\Core\FieldType\Image\ImageStorage\Gateway\LegacyStorage\$dbHandler
$dbHandler
Definition: Image/ImageStorage/Gateway/LegacyStorage.php:21
eZ\Publish\Core\FieldType\Image\ImageStorage\Gateway\LegacyStorage\storeImageReference
storeImageReference($uri, $fieldId)
Stores a reference to the image in $path for $fieldId.
Definition: Image/ImageStorage/Gateway/LegacyStorage.php:101
eZ\Publish\Core\FieldType\Image\ImageStorage\Gateway\LegacyStorage\getImagesData
getImagesData(int $offset, int $limit)
Definition: Image/ImageStorage/Gateway/LegacyStorage.php:360
eZ\Publish\Core\FieldType\Image\ImageStorage\Gateway\LegacyStorage\getNodePathString
getNodePathString(VersionInfo $versionInfo)
Returns the node path string of $versionInfo.
Definition: Image/ImageStorage/Gateway/LegacyStorage.php:66
eZ\Publish\Core\FieldType\Image\ImageStorage\Gateway\LegacyStorage\updateImageData
updateImageData(int $fieldId, int $versionNo, string $xml)
Definition: Image/ImageStorage/Gateway/LegacyStorage.php:352
eZ\Publish\Core\FieldType\Image\ImageStorage\Gateway\LegacyStorage\countImageReferences
countImageReferences($uri)
Returns the number of recorded references to the given $path.
Definition: Image/ImageStorage/Gateway/LegacyStorage.php:208
eZ\Publish\Core\FieldType\Image\ImageStorage\Gateway\LegacyStorage\$fieldNameMap
$fieldNameMap
Definition: Image/ImageStorage/Gateway/LegacyStorage.php:28
eZ\Publish\Core\FieldType\Image\ImageStorage\Gateway\LegacyStorage\getAllVersionsImageXmlForFieldId
getAllVersionsImageXmlForFieldId(int $fieldId)
Definition: Image/ImageStorage/Gateway/LegacyStorage.php:344
eZ\Publish\Core\FieldType\Image\ImageStorage\Gateway\LegacyStorage\isImageReferenced
isImageReferenced(string $uri)
Returns true if there is reference to the given $uri.
Definition: Image/ImageStorage/Gateway/LegacyStorage.php:234
eZ\Publish\Core\FieldType\Image\ImageStorage\Gateway\LegacyStorage\__construct
__construct(UrlRedecoratorInterface $redecorator, DatabaseHandler $dbHandler)
Definition: Image/ImageStorage/Gateway/LegacyStorage.php:39
eZ\Publish\Core\FieldType\Image\ImageStorage\Gateway\LegacyStorage\removeImageReferences
removeImageReferences($uri, $versionNo, $fieldId)
Removes all references from $fieldId to a path that starts with $path.
Definition: Image/ImageStorage/Gateway/LegacyStorage.php:171
eZ\Publish\Core\FieldType\Image\ImageStorage\Gateway
Image Field Type external storage gateway.
Definition: FieldType/Image/ImageStorage/Gateway.php:16
eZ\Publish\Core\FieldType\Image\ImageStorage\Gateway\extractFilesFromXml
extractFilesFromXml($xml)
Returns the public uris for the images stored in $xml.
eZ\Publish\SPI\Persistence\Content\VersionInfo
This class holds version information data.
Definition: SPI/Persistence/Content/VersionInfo.php:15
eZ\Publish\Core\IO\UrlRedecoratorInterface
Converts an URL from one decorator to another.
Definition: UrlRedecoratorInterface.php:28
eZ\Publish\Core\Persistence\Database\DatabaseHandler
Interface for relational database abstractions supported by eZ.
Definition: DatabaseHandler.php:18
eZ\Publish\Core\FieldType\Image\ImageStorage\Gateway
Definition: Image/ImageStorage/Gateway/DoctrineStorage.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.