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

BinaryBase/BinaryBaseStorage/Gateway/LegacyStorage.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\FieldType\BinaryBase\BinaryBaseStorage\Gateway;
8
9 use eZ\Publish\Core\Persistence\Database\DatabaseHandler;
10 use eZ\Publish\SPI\Persistence\Content\VersionInfo;
11 use eZ\Publish\SPI\Persistence\Content\Field;
12 use eZ\Publish\Core\FieldType\BinaryBase\BinaryBaseStorage\Gateway;
13 use eZ\Publish\Core\Persistence\Database\SelectQuery;
14 use eZ\Publish\Core\Persistence\Database\InsertQuery;
15
19 abstract class LegacyStorage extends Gateway
20 {
22 protected $dbHandler;
23
24 public function __construct(DatabaseHandler $dbHandler)
25 {
26 @trigger_error(
27 sprintf('%s is deprecated, use %s instead', self::class, DoctrineStorage::class),
28 E_USER_DEPRECATED
29 );
30 $this->dbHandler = $dbHandler;
31 }
32
38 abstract protected function getStorageTable();
39
45 protected function getPropertyMapping()
46 {
47 return [
48 'filename' => [
49 'name' => 'id',
50 'cast' => 'strval',
51 ],
52 'mime_type' => [
53 'name' => 'mimeType',
54 'cast' => 'strval',
55 ],
56 'original_filename' => [
57 'name' => 'fileName',
58 'cast' => 'strval',
59 ],
60 ];
61 }
62
74 protected function setFetchColumns(SelectQuery $selectQuery, $fieldId, $versionNo)
75 {
76 $connection = $this->getConnection();
77
78 $selectQuery->select(
79 $connection->quoteColumn('filename'),
80 $connection->quoteColumn('mime_type'),
81 $connection->quoteColumn('original_filename')
82 );
83 }
84
96 protected function setInsertColumns(InsertQuery $insertQuery, VersionInfo $versionInfo, Field $field)
97 {
98 $connection = $this->getConnection();
99
100 $insertQuery->set(
101 $connection->quoteColumn('contentobject_attribute_id'),
102 $insertQuery->bindValue($field->id, null, \PDO::PARAM_INT)
103 )->set(
104 $connection->quoteColumn('filename'),
105 $insertQuery->bindValue(
106 $this->removeMimeFromPath($field->value->externalData['id'])
107 )
108 )->set(
109 $connection->quoteColumn('mime_type'),
110 $insertQuery->bindValue($field->value->externalData['mimeType'])
111 )->set(
112 $connection->quoteColumn('original_filename'),
113 $insertQuery->bindValue($field->value->externalData['fileName'])
114 )->set(
115 $connection->quoteColumn('version'),
116 $insertQuery->bindValue($versionInfo->versionNo, null, \PDO::PARAM_INT)
117 );
118 }
119
125 protected function getConnection()
126 {
127 return $this->dbHandler;
128 }
129
138 public function storeFileReference(VersionInfo $versionInfo, Field $field)
139 {
140 $connection = $this->getConnection();
141
142 $insertQuery = $connection->createInsertQuery();
143 $insertQuery->insertInto(
144 $connection->quoteTable($this->getStorageTable())
145 );
146
147 $this->setInsertColumns($insertQuery, $versionInfo, $field);
148
149 $insertQuery->prepare()->execute();
150
151 return false;
152 }
153
161 public function removeMimeFromPath($path)
162 {
163 $res = substr($path, strpos($path, '/') + 1);
164
165 return $res;
166 }
167
176 public function getFileReferenceData($fieldId, $versionNo)
177 {
178 $connection = $this->getConnection();
179
180 $selectQuery = $connection->createSelectQuery();
181
182 $this->setFetchColumns($selectQuery, $fieldId, $versionNo);
183
184 $selectQuery->from(
185 $connection->quoteTable($this->getStorageTable())
186 )->where(
187 $selectQuery->expr->lAnd(
188 $selectQuery->expr->eq(
189 $connection->quoteColumn('contentobject_attribute_id'),
190 $selectQuery->bindValue($fieldId, null, \PDO::PARAM_INT)
191 ),
192 $selectQuery->expr->eq(
193 $connection->quoteColumn('version'),
194 $selectQuery->bindValue($versionNo, null, \PDO::PARAM_INT)
195 )
196 )
197 );
198
199 $statement = $selectQuery->prepare();
200 $statement->execute();
201
202 $result = $statement->fetchAll(\PDO::FETCH_ASSOC);
203
204 if (count($result) < 1) {
205 return null;
206 }
207
208 $convertedResult = [];
209 foreach (reset($result) as $column => $value) {
210 $convertedResult[$this->toPropertyName($column)] = $this->castToPropertyValue($value, $column);
211 }
212 $convertedResult['id'] = $this->prependMimeToPath(
213 $convertedResult['id'],
214 $convertedResult['mimeType']
215 );
216
217 return $convertedResult;
218 }
219
227 protected function toPropertyName($columnName)
228 {
229 $propertyMap = $this->getPropertyMapping();
230
231 return $propertyMap[$columnName]['name'];
232 }
233
242 protected function castToPropertyValue($value, $columnName)
243 {
244 $propertyMap = $this->getPropertyMapping();
245 $castFunction = $propertyMap[$columnName]['cast'];
246
247 return $castFunction($value);
248 }
249
258 public function prependMimeToPath($path, $mimeType)
259 {
260 $res = substr($mimeType, 0, strpos($mimeType, '/')) . '/' . $path;
261
262 return $res;
263 }
264
270 public function removeFileReferences(array $fieldIds, $versionNo)
271 {
272 if (empty($fieldIds)) {
273 return;
274 }
275
276 $connection = $this->getConnection();
277
278 $deleteQuery = $connection->createDeleteQuery();
279 $deleteQuery->deleteFrom(
280 $connection->quoteTable($this->getStorageTable())
281 )->where(
282 $deleteQuery->expr->lAnd(
283 $deleteQuery->expr->in(
284 $connection->quoteColumn('contentobject_attribute_id'),
285 $fieldIds
286 ),
287 $deleteQuery->expr->eq(
288 $connection->quoteColumn('version'),
289 $deleteQuery->bindValue($versionNo, null, \PDO::PARAM_INT)
290 )
291 )
292 );
293
294 $deleteQuery->prepare()->execute();
295 }
296
303 public function removeFileReference($fieldId, $versionNo)
304 {
305 $connection = $this->getConnection();
306
307 $deleteQuery = $connection->createDeleteQuery();
308 $deleteQuery->deleteFrom(
309 $connection->quoteTable($this->getStorageTable())
310 )->where(
311 $deleteQuery->expr->lAnd(
312 $deleteQuery->expr->eq(
313 $connection->quoteColumn('contentobject_attribute_id'),
314 $deleteQuery->bindValue($fieldId, null, \PDO::PARAM_INT)
315 ),
316 $deleteQuery->expr->eq(
317 $connection->quoteColumn('version'),
318 $deleteQuery->bindValue($versionNo, null, \PDO::PARAM_INT)
319 )
320 )
321 );
322
323 $deleteQuery->prepare()->execute();
324 }
325
333 public function getReferencedFiles(array $fieldIds, $versionNo)
334 {
335 if (empty($fieldIds)) {
336 return [];
337 }
338
339 $connection = $this->getConnection();
340
341 $selectQuery = $connection->createSelectQuery();
342 $selectQuery->select(
343 $connection->quoteColumn('filename'),
344 $connection->quoteColumn('mime_type')
345 )->from(
346 $connection->quoteTable($this->getStorageTable())
347 )->where(
348 $selectQuery->expr->lAnd(
349 $selectQuery->expr->in(
350 $connection->quoteColumn('contentobject_attribute_id'),
351 $fieldIds
352 ),
353 $selectQuery->expr->eq(
354 $connection->quoteColumn('version'),
355 $selectQuery->bindValue($versionNo, null, \PDO::PARAM_INT)
356 )
357 )
358 );
359
360 $statement = $selectQuery->prepare();
361 $statement->execute();
362
363 $gateway = $this;
364
365 return array_map(
366 function ($row) use ($gateway) {
367 return $gateway->prependMimeToPath($row['filename'], $row['mime_type']);
368 },
369 $statement->fetchAll(\PDO::FETCH_ASSOC)
370 );
371 }
372
380 public function countFileReferences(array $files)
381 {
382 if (empty($files)) {
383 return [];
384 }
385
386 $connection = $this->getConnection();
387
388 $selectQuery = $connection->createSelectQuery();
389 $selectQuery->select(
390 $connection->quoteColumn('filename'),
391 $connection->quoteColumn('mime_type'),
392 $selectQuery->alias(
393 $selectQuery->expr->count($connection->quoteColumn('contentobject_attribute_id')),
394 'count'
395 )
396 )->from(
397 $connection->quoteTable($this->getStorageTable())
398 )->where(
399 $selectQuery->expr->in(
400 $connection->quoteColumn('filename'),
401 array_map(
402 [$this, 'removeMimeFromPath'],
403 $files
404 )
405 )
406 )->groupBy(
407 $connection->quoteColumn('filename'),
408 $connection->quoteColumn('mime_type')
409 );
410
411 $statement = $selectQuery->prepare();
412 $statement->execute();
413
414 $countMap = [];
415 foreach ($statement->fetchAll(\PDO::FETCH_ASSOC) as $row) {
416 $path = $this->prependMimeToPath($row['filename'], $row['mime_type']);
417 $countMap[$path] = (int)$row['count'];
418 }
419
420 // Complete counts
421 foreach ($files as $path) {
422 // This is already the correct path
423 if (!isset($countMap[$path])) {
424 $countMap[$path] = 0;
425 }
426 }
427
428 return $countMap;
429 }
430 }
eZ\Publish\Core\FieldType\BinaryBase\BinaryBaseStorage\Gateway\LegacyStorage
Definition: BinaryBase/BinaryBaseStorage/Gateway/LegacyStorage.php:20
eZ\Publish\Core\FieldType\BinaryBase\BinaryBaseStorage\Gateway\LegacyStorage\toPropertyName
toPropertyName($columnName)
Returns the property name for the given $columnName.
Definition: BinaryBase/BinaryBaseStorage/Gateway/LegacyStorage.php:227
eZ\Publish\Core\FieldType\BinaryBase\BinaryBaseStorage\Gateway\LegacyStorage\getPropertyMapping
getPropertyMapping()
Returns a column to property mapping for the storage table.
Definition: BinaryBase/BinaryBaseStorage/Gateway/LegacyStorage.php:45
eZ\Publish\Core\FieldType\BinaryBase\BinaryBaseStorage\Gateway\LegacyStorage\getConnection
getConnection()
Returns the active connection.
Definition: BinaryBase/BinaryBaseStorage/Gateway/LegacyStorage.php:125
eZ\Publish\Core\FieldType\BinaryBase\BinaryBaseStorage\Gateway\LegacyStorage\getStorageTable
getStorageTable()
Returns the table name to store data in.
eZ\Publish\Core\FieldType\BinaryBase\BinaryBaseStorage\Gateway\LegacyStorage\storeFileReference
storeFileReference(VersionInfo $versionInfo, Field $field)
Stores the file reference in $field for $versionNo.
Definition: BinaryBase/BinaryBaseStorage/Gateway/LegacyStorage.php:138
eZ\Publish\Core\FieldType\BinaryBase\BinaryBaseStorage\Gateway\LegacyStorage\setInsertColumns
setInsertColumns(InsertQuery $insertQuery, VersionInfo $versionInfo, Field $field)
Sets the required insert columns to $selectQuery.
Definition: BinaryBase/BinaryBaseStorage/Gateway/LegacyStorage.php:96
eZ\Publish\Core\FieldType\BinaryBase\BinaryBaseStorage\Gateway\LegacyStorage\removeFileReferences
removeFileReferences(array $fieldIds, $versionNo)
Removes all file references for the given $fieldIds.
Definition: BinaryBase/BinaryBaseStorage/Gateway/LegacyStorage.php:270
eZ\Publish\Core\FieldType\BinaryBase\BinaryBaseStorage\Gateway\LegacyStorage\removeFileReference
removeFileReference($fieldId, $versionNo)
Removes a specific file reference for $fieldId and $versionId.
Definition: BinaryBase/BinaryBaseStorage/Gateway/LegacyStorage.php:303
eZ\Publish\Core\FieldType\BinaryBase\BinaryBaseStorage\Gateway\LegacyStorage\removeMimeFromPath
removeMimeFromPath($path)
Removes the prepended mime-type directory from $path for legacy storage.
Definition: BinaryBase/BinaryBaseStorage/Gateway/LegacyStorage.php:161
eZ\Publish\Core\FieldType\BinaryBase\BinaryBaseStorage\Gateway\LegacyStorage\getFileReferenceData
getFileReferenceData($fieldId, $versionNo)
Returns the file reference data for the given $fieldId in $versionNo.
Definition: BinaryBase/BinaryBaseStorage/Gateway/LegacyStorage.php:176
eZ\Publish\Core\FieldType\BinaryBase\BinaryBaseStorage\Gateway\LegacyStorage\getReferencedFiles
getReferencedFiles(array $fieldIds, $versionNo)
Returns a set o file references, referenced by the given $fieldIds.
Definition: BinaryBase/BinaryBaseStorage/Gateway/LegacyStorage.php:333
eZ\Publish\Core\FieldType\BinaryBase\BinaryBaseStorage\Gateway\LegacyStorage\prependMimeToPath
prependMimeToPath($path, $mimeType)
Prepends $path with the first part of the given $mimeType.
Definition: BinaryBase/BinaryBaseStorage/Gateway/LegacyStorage.php:258
eZ\Publish\Core\FieldType\BinaryBase\BinaryBaseStorage\Gateway\LegacyStorage\countFileReferences
countFileReferences(array $files)
Returns a map with the number of references each file from $files has.
Definition: BinaryBase/BinaryBaseStorage/Gateway/LegacyStorage.php:380
eZ\Publish\Core\FieldType\BinaryBase\BinaryBaseStorage\Gateway\LegacyStorage\__construct
__construct(DatabaseHandler $dbHandler)
Definition: BinaryBase/BinaryBaseStorage/Gateway/LegacyStorage.php:24
eZ\Publish\Core\FieldType\BinaryBase\BinaryBaseStorage\Gateway\LegacyStorage\$dbHandler
$dbHandler
Definition: BinaryBase/BinaryBaseStorage/Gateway/LegacyStorage.php:22
eZ\Publish\Core\FieldType\BinaryBase\BinaryBaseStorage\Gateway\LegacyStorage\setFetchColumns
setFetchColumns(SelectQuery $selectQuery, $fieldId, $versionNo)
Set columns to be fetched from the database.
Definition: BinaryBase/BinaryBaseStorage/Gateway/LegacyStorage.php:74
eZ\Publish\Core\FieldType\BinaryBase\BinaryBaseStorage\Gateway\LegacyStorage\castToPropertyValue
castToPropertyValue($value, $columnName)
Returns $value casted as specified by getPropertyMapping().
Definition: BinaryBase/BinaryBaseStorage/Gateway/LegacyStorage.php:242
eZ\Publish\Core\FieldType\BinaryBase\BinaryBaseStorage\Gateway
Definition: FieldType/BinaryBase/BinaryBaseStorage/Gateway.php:14
eZ\Publish\Core\FieldType\BinaryBase\BinaryBaseStorage\$gateway
$gateway
Definition: BinaryBaseStorage.php:40
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\Persistence\Database\DatabaseHandler
Interface for relational database abstractions supported by eZ.
Definition: DatabaseHandler.php:18
eZ\Publish\Core\Persistence\Database\InsertQuery
Definition: InsertQuery.php:16
eZ\Publish\Core\Persistence\Database\InsertQuery\set
set($column, $expression)
The insert query will set the column $column to the value $expression.
eZ\Publish\Core\Persistence\Database\Query\bindValue
bindValue($value, $placeHolder=null, $type=PDO::PARAM_STR)
Binds the value $value to the specified variable name $placeHolder.
eZ\Publish\Core\Persistence\Database\SelectQuery
Definition: SelectQuery.php:16
eZ\Publish\Core\Persistence\Database\SelectQuery\select
select()
Opens the query and selects which columns you want to return with the query.
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+.