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

BinaryBase/BinaryBaseStorage/Gateway/DoctrineStorage.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\FieldType\BinaryBase\BinaryBaseStorage\Gateway;
8
9 use Doctrine\DBAL\Connection;
10 use Doctrine\DBAL\ParameterType;
11 use Doctrine\DBAL\Query\QueryBuilder;
12 use eZ\Publish\SPI\Persistence\Content\VersionInfo;
13 use eZ\Publish\SPI\Persistence\Content\Field;
14 use eZ\Publish\Core\FieldType\BinaryBase\BinaryBaseStorage\Gateway;
15 use PDO;
16
20 abstract class DoctrineStorage extends Gateway
21 {
23 protected $connection;
24
25 public function __construct(Connection $connection)
26 {
27 $this->connection = $connection;
28 }
29
35 abstract protected function getStorageTable();
36
42 protected function getPropertyMapping()
43 {
44 return [
45 'filename' => [
46 'name' => 'id',
47 'cast' => 'strval',
48 ],
49 'mime_type' => [
50 'name' => 'mimeType',
51 'cast' => 'strval',
52 ],
53 'original_filename' => [
54 'name' => 'fileName',
55 'cast' => 'strval',
56 ],
57 ];
58 }
59
71 protected function setFetchColumns(QueryBuilder $queryBuilder, $fieldId, $versionNo)
72 {
73 $queryBuilder->select(
74 $this->connection->quoteIdentifier('filename'),
75 $this->connection->quoteIdentifier('mime_type'),
76 $this->connection->quoteIdentifier('original_filename')
77 );
78 }
79
91 protected function setInsertColumns(QueryBuilder $queryBuilder, VersionInfo $versionInfo, Field $field)
92 {
93 $queryBuilder
94 ->setValue('contentobject_attribute_id', ':fieldId')
95 ->setValue('filename', ':filename')
96 ->setValue('mime_type', ':mimeType')
97 ->setValue('original_filename', ':originalFilename')
98 ->setValue('version', ':versionNo')
99 ->setParameter(':fieldId', $field->id, PDO::PARAM_INT)
100 ->setParameter(':filename', $this->removeMimeFromPath($field->value->externalData['id']))
101 ->setParameter(':mimeType', $field->value->externalData['mimeType'])
102 ->setParameter(':originalFilename', $field->value->externalData['fileName'])
103 ->setParameter(':versionNo', $versionInfo->versionNo, PDO::PARAM_INT)
104 ;
105 }
106
112 protected function setUpdateColumns(QueryBuilder $queryBuilder, VersionInfo $versionInfo, Field $field)
113 {
114 $queryBuilder
115 ->set('contentobject_attribute_id', ':fieldId')
116 ->set('filename', ':filename')
117 ->set('mime_type', ':mimeType')
118 ->set('original_filename', ':originalFilename')
119 ->set('version', ':versionNo')
120 ->setParameter(':fieldId', $field->id, ParameterType::INTEGER)
121 ->setParameter(':filename', $this->removeMimeFromPath($field->value->externalData['id']))
122 ->setParameter(':mimeType', $field->value->externalData['mimeType'])
123 ->setParameter(':originalFilename', $field->value->externalData['fileName'])
124 ->setParameter(':versionNo', $versionInfo->versionNo, ParameterType::INTEGER)
125 ;
126 }
127
136 public function storeFileReference(VersionInfo $versionInfo, Field $field)
137 {
138 $referencedData = $this->getFileReferenceData($field->id, $versionInfo->versionNo);
139
140 if ($referencedData === null) {
141 $this->storeNewFieldData($versionInfo, $field);
142 } elseif (is_array($referencedData) && !empty(array_diff_assoc($referencedData, $field->value->externalData))) {
143 $this->updateFieldData($versionInfo, $field);
144 }
145
146 return false;
147 }
148
153 protected function updateFieldData(VersionInfo $versionInfo, Field $field)
154 {
155 $updateQuery = $this->connection->createQueryBuilder();
156 $updateQuery->update(
157 $this->connection->quoteIdentifier($this->getStorageTable())
158 );
159
160 $this->setUpdateColumns($updateQuery, $versionInfo, $field);
161 $updateQuery
162 ->where(
163 $updateQuery->expr()->andX(
164 $updateQuery->expr()->eq(
165 $this->connection->quoteIdentifier('contentobject_attribute_id'),
166 ':fieldId'
167 ),
168 $updateQuery->expr()->eq(
169 $this->connection->quoteIdentifier('version'),
170 ':versionNo'
171 )
172 )
173 )
174 ->setParameter(':fieldId', $field->id, ParameterType::INTEGER)
175 ->setParameter(':versionNo', $versionInfo->versionNo, ParameterType::INTEGER)
176 ;
177
178 $updateQuery->execute();
179 }
180
185 protected function storeNewFieldData(VersionInfo $versionInfo, Field $field)
186 {
187 $insertQuery = $this->connection->createQueryBuilder();
188 $insertQuery->insert(
189 $this->connection->quoteIdentifier($this->getStorageTable())
190 );
191
192 $this->setInsertColumns($insertQuery, $versionInfo, $field);
193
194 $insertQuery->execute();
195 }
196
204 public function removeMimeFromPath($path)
205 {
206 $res = substr($path, strpos($path, '/') + 1);
207
208 return $res;
209 }
210
219 public function getFileReferenceData($fieldId, $versionNo)
220 {
221 $selectQuery = $this->connection->createQueryBuilder();
222
223 $this->setFetchColumns($selectQuery, $fieldId, $versionNo);
224
225 $selectQuery
226 ->from($this->connection->quoteIdentifier($this->getStorageTable()))
227 ->where(
228 $selectQuery->expr()->andX(
229 $selectQuery->expr()->eq(
230 $this->connection->quoteIdentifier('contentobject_attribute_id'),
231 ':fieldId'
232 ),
233 $selectQuery->expr()->eq(
234 $this->connection->quoteIdentifier('version'),
235 ':versionNo'
236 )
237 )
238 )
239 ->setParameter(':fieldId', $fieldId, PDO::PARAM_INT)
240 ->setParameter(':versionNo', $versionNo, PDO::PARAM_INT)
241 ;
242
243 $statement = $selectQuery->execute();
244
245 $result = $statement->fetchAll(PDO::FETCH_ASSOC);
246
247 if (count($result) < 1) {
248 return null;
249 }
250
251 $convertedResult = [];
252 foreach (reset($result) as $column => $value) {
253 $convertedResult[$this->toPropertyName($column)] = $this->castToPropertyValue($value, $column);
254 }
255 $convertedResult['id'] = $this->prependMimeToPath(
256 $convertedResult['id'],
257 $convertedResult['mimeType']
258 );
259
260 return $convertedResult;
261 }
262
270 protected function toPropertyName($columnName)
271 {
272 $propertyMap = $this->getPropertyMapping();
273
274 return $propertyMap[$columnName]['name'];
275 }
276
285 protected function castToPropertyValue($value, $columnName)
286 {
287 $propertyMap = $this->getPropertyMapping();
288 $castFunction = $propertyMap[$columnName]['cast'];
289
290 return $castFunction($value);
291 }
292
301 public function prependMimeToPath($path, $mimeType)
302 {
303 $res = substr($mimeType, 0, strpos($mimeType, '/')) . '/' . $path;
304
305 return $res;
306 }
307
314 public function removeFileReferences(array $fieldIds, $versionNo)
315 {
316 if (empty($fieldIds)) {
317 return;
318 }
319
320 $deleteQuery = $this->connection->createQueryBuilder();
321 $deleteQuery
322 ->delete($this->connection->quoteIdentifier($this->getStorageTable()))
323 ->where(
324 $deleteQuery->expr()->andX(
325 $deleteQuery->expr()->in(
326 $this->connection->quoteIdentifier('contentobject_attribute_id'),
327 ':fieldIds'
328 ),
329 $deleteQuery->expr()->eq(
330 $this->connection->quoteIdentifier('version'),
331 ':versionNo'
332 )
333 )
334 )
335 ->setParameter(':fieldIds', $fieldIds, Connection::PARAM_INT_ARRAY)
336 ->setParameter(':versionNo', $versionNo, PDO::PARAM_INT)
337 ;
338
339 $deleteQuery->execute();
340 }
341
348 public function removeFileReference($fieldId, $versionNo)
349 {
350 $deleteQuery = $this->connection->createQueryBuilder();
351 $deleteQuery
352 ->delete($this->connection->quoteIdentifier($this->getStorageTable()))
353 ->where(
354 $deleteQuery->expr()->andX(
355 $deleteQuery->expr()->eq(
356 $this->connection->quoteIdentifier('contentobject_attribute_id'),
357 ':fieldId'
358 ),
359 $deleteQuery->expr()->eq(
360 $this->connection->quoteIdentifier('version'),
361 ':versionNo'
362 )
363 )
364 )
365 ->setParameter(':fieldId', $fieldId, PDO::PARAM_INT)
366 ->setParameter(':versionNo', $versionNo, PDO::PARAM_INT)
367 ;
368
369 $deleteQuery->execute();
370 }
371
379 public function getReferencedFiles(array $fieldIds, $versionNo)
380 {
381 if (empty($fieldIds)) {
382 return [];
383 }
384
385 $selectQuery = $this->connection->createQueryBuilder();
386 $selectQuery
387 ->select(
388 $this->connection->quoteIdentifier('filename'),
389 $this->connection->quoteIdentifier('mime_type')
390 )
391 ->from($this->connection->quoteIdentifier($this->getStorageTable()))
392 ->where(
393 $selectQuery->expr()->andX(
394 $selectQuery->expr()->in(
395 $this->connection->quoteIdentifier('contentobject_attribute_id'),
396 ':fieldIds'
397 ),
398 $selectQuery->expr()->eq(
399 $this->connection->quoteIdentifier('version'),
400 ':versionNo'
401 )
402 )
403 )
404 ->setParameter(':fieldIds', $fieldIds, Connection::PARAM_INT_ARRAY)
405 ->setParameter(':versionNo', $versionNo, PDO::PARAM_INT)
406 ;
407
408 $statement = $selectQuery->execute();
409
410 return array_map(
411 function ($row) {
412 return $this->prependMimeToPath($row['filename'], $row['mime_type']);
413 },
414 $statement->fetchAll(PDO::FETCH_ASSOC)
415 );
416 }
417
425 public function countFileReferences(array $files)
426 {
427 if (empty($files)) {
428 return [];
429 }
430
431 $selectQuery = $this->connection->createQueryBuilder();
432 $selectQuery
433 ->select(
434 $this->connection->quoteIdentifier('filename'),
435 $this->connection->quoteIdentifier('mime_type'),
436 sprintf(
437 'COUNT(%s) AS count',
438 $this->connection->quoteIdentifier('contentobject_attribute_id')
439 )
440 )
441 ->from($this->connection->quoteIdentifier($this->getStorageTable()))
442 ->where(
443 $selectQuery->expr()->in(
444 $this->connection->quoteIdentifier('filename'),
445 ':filenames'
446 )
447 )
448 ->groupBy(
449 $this->connection->quoteIdentifier('filename'),
450 $this->connection->quoteIdentifier('mime_type')
451 )
452 ->setParameter(
453 ':filenames',
454 array_map(
455 [$this, 'removeMimeFromPath'],
456 $files
457 ),
458 Connection::PARAM_STR_ARRAY
459 )
460 ;
461
462 $statement = $selectQuery->execute();
463
464 $countMap = [];
465 foreach ($statement->fetchAll(PDO::FETCH_ASSOC) as $row) {
466 $path = $this->prependMimeToPath($row['filename'], $row['mime_type']);
467 $countMap[$path] = (int)$row['count'];
468 }
469
470 // Complete counts
471 foreach ($files as $path) {
472 // This is already the correct path
473 if (!isset($countMap[$path])) {
474 $countMap[$path] = 0;
475 }
476 }
477
478 return $countMap;
479 }
480 }
eZ\Publish\Core\FieldType\BinaryBase\BinaryBaseStorage\Gateway\DoctrineStorage
Base class for binary files external storage DoctrineStorage gateways.
Definition: BinaryBase/BinaryBaseStorage/Gateway/DoctrineStorage.php:21
eZ\Publish\Core\FieldType\BinaryBase\BinaryBaseStorage\Gateway\DoctrineStorage\prependMimeToPath
prependMimeToPath($path, $mimeType)
Prepend $path with the first part of the given $mimeType.
Definition: BinaryBase/BinaryBaseStorage/Gateway/DoctrineStorage.php:301
eZ\Publish\Core\FieldType\BinaryBase\BinaryBaseStorage\Gateway\DoctrineStorage\setUpdateColumns
setUpdateColumns(QueryBuilder $queryBuilder, VersionInfo $versionInfo, Field $field)
Definition: BinaryBase/BinaryBaseStorage/Gateway/DoctrineStorage.php:112
eZ\Publish\Core\FieldType\BinaryBase\BinaryBaseStorage\Gateway\DoctrineStorage\getReferencedFiles
getReferencedFiles(array $fieldIds, $versionNo)
Return a set o file references, referenced by the given $fieldIds.
Definition: BinaryBase/BinaryBaseStorage/Gateway/DoctrineStorage.php:379
eZ\Publish\Core\FieldType\BinaryBase\BinaryBaseStorage\Gateway\DoctrineStorage\storeFileReference
storeFileReference(VersionInfo $versionInfo, Field $field)
Store the file reference in $field for $versionNo.
Definition: BinaryBase/BinaryBaseStorage/Gateway/DoctrineStorage.php:136
eZ\Publish\Core\FieldType\BinaryBase\BinaryBaseStorage\Gateway\DoctrineStorage\toPropertyName
toPropertyName($columnName)
Return the property name for the given $columnName.
Definition: BinaryBase/BinaryBaseStorage/Gateway/DoctrineStorage.php:270
eZ\Publish\Core\FieldType\BinaryBase\BinaryBaseStorage\Gateway\DoctrineStorage\removeFileReference
removeFileReference($fieldId, $versionNo)
Remove a specific file reference for $fieldId and $versionId.
Definition: BinaryBase/BinaryBaseStorage/Gateway/DoctrineStorage.php:348
eZ\Publish\Core\FieldType\BinaryBase\BinaryBaseStorage\Gateway\DoctrineStorage\getStorageTable
getStorageTable()
Return the table name to store data in.
eZ\Publish\Core\FieldType\BinaryBase\BinaryBaseStorage\Gateway\DoctrineStorage\removeFileReferences
removeFileReferences(array $fieldIds, $versionNo)
Remove all file references for the given $fieldIds.
Definition: BinaryBase/BinaryBaseStorage/Gateway/DoctrineStorage.php:314
eZ\Publish\Core\FieldType\BinaryBase\BinaryBaseStorage\Gateway\DoctrineStorage\setFetchColumns
setFetchColumns(QueryBuilder $queryBuilder, $fieldId, $versionNo)
Set columns to be fetched from the database.
Definition: BinaryBase/BinaryBaseStorage/Gateway/DoctrineStorage.php:71
eZ\Publish\Core\FieldType\BinaryBase\BinaryBaseStorage\Gateway\DoctrineStorage\removeMimeFromPath
removeMimeFromPath($path)
Remove the prepended mime-type directory from $path for legacy storage.
Definition: BinaryBase/BinaryBaseStorage/Gateway/DoctrineStorage.php:204
eZ\Publish\Core\FieldType\BinaryBase\BinaryBaseStorage\Gateway\DoctrineStorage\castToPropertyValue
castToPropertyValue($value, $columnName)
Return $value casted as specified by getPropertyMapping().
Definition: BinaryBase/BinaryBaseStorage/Gateway/DoctrineStorage.php:285
eZ\Publish\Core\FieldType\BinaryBase\BinaryBaseStorage\Gateway\DoctrineStorage\getPropertyMapping
getPropertyMapping()
Return a column to property mapping for the storage table.
Definition: BinaryBase/BinaryBaseStorage/Gateway/DoctrineStorage.php:42
eZ\Publish\Core\FieldType\BinaryBase\BinaryBaseStorage\Gateway\DoctrineStorage\storeNewFieldData
storeNewFieldData(VersionInfo $versionInfo, Field $field)
Definition: BinaryBase/BinaryBaseStorage/Gateway/DoctrineStorage.php:185
eZ\Publish\Core\FieldType\BinaryBase\BinaryBaseStorage\Gateway\DoctrineStorage\__construct
__construct(Connection $connection)
Definition: BinaryBase/BinaryBaseStorage/Gateway/DoctrineStorage.php:25
eZ\Publish\Core\FieldType\BinaryBase\BinaryBaseStorage\Gateway\DoctrineStorage\getFileReferenceData
getFileReferenceData($fieldId, $versionNo)
Return the file reference data for the given $fieldId in $versionNo.
Definition: BinaryBase/BinaryBaseStorage/Gateway/DoctrineStorage.php:219
eZ\Publish\Core\FieldType\BinaryBase\BinaryBaseStorage\Gateway\DoctrineStorage\countFileReferences
countFileReferences(array $files)
Return a map with the number of references each file from $files has.
Definition: BinaryBase/BinaryBaseStorage/Gateway/DoctrineStorage.php:425
eZ\Publish\Core\FieldType\BinaryBase\BinaryBaseStorage\Gateway\DoctrineStorage\updateFieldData
updateFieldData(VersionInfo $versionInfo, Field $field)
Definition: BinaryBase/BinaryBaseStorage/Gateway/DoctrineStorage.php:153
eZ\Publish\Core\FieldType\BinaryBase\BinaryBaseStorage\Gateway\DoctrineStorage\$connection
$connection
Definition: BinaryBase/BinaryBaseStorage/Gateway/DoctrineStorage.php:23
eZ\Publish\Core\FieldType\BinaryBase\BinaryBaseStorage\Gateway\DoctrineStorage\setInsertColumns
setInsertColumns(QueryBuilder $queryBuilder, VersionInfo $versionInfo, Field $field)
Set the required insert columns to insert query builder.
Definition: BinaryBase/BinaryBaseStorage/Gateway/DoctrineStorage.php:91
eZ\Publish\Core\FieldType\BinaryBase\BinaryBaseStorage\Gateway
Definition: FieldType/BinaryBase/BinaryBaseStorage/Gateway.php:14
eZ\Publish\SPI\Persistence\Content\Field
Definition: SPI/Persistence/Content/Field.php:12
eZ\Publish\SPI\Persistence\Content\VersionInfo
This class holds version information data.
Definition: SPI/Persistence/Content/VersionInfo.php:15
eZ\Publish\Core\FieldType\BinaryBase\BinaryBaseStorage\Gateway
Definition: BinaryBase/BinaryBaseStorage/Gateway/DoctrineStorage.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.