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

Url/UrlStorage/Gateway/DoctrineStorage.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\FieldType\Url\UrlStorage\Gateway;
8
9 use Doctrine\DBAL\Connection;
10 use Doctrine\DBAL\ParameterType;
11 use eZ\Publish\Core\FieldType\Url\UrlStorage\Gateway;
12 use PDO;
13
14 class DoctrineStorage extends Gateway
15 {
16 const URL_TABLE = 'ezurl';
17 const URL_LINK_TABLE = 'ezurl_object_link';
18
20 protected $connection;
21
22 public function __construct(Connection $connection)
23 {
24 $this->connection = $connection;
25 }
26
36 public function getIdUrlMap(array $ids)
37 {
38 $map = [];
39
40 if (!empty($ids)) {
41 $query = $this->connection->createQueryBuilder();
42 $query
43 ->select(
44 $this->connection->quoteIdentifier('id'),
45 $this->connection->quoteIdentifier('url')
46 )
47 ->from(self::URL_TABLE)
48 ->where('id IN (:ids)')
49 ->setParameter(':ids', $ids, Connection::PARAM_INT_ARRAY);
50
51 $statement = $query->execute();
52 foreach ($statement->fetchAll(PDO::FETCH_ASSOC) as $row) {
53 $map[$row['id']] = $row['url'];
54 }
55 }
56
57 return $map;
58 }
59
69 public function getUrlIdMap(array $urls)
70 {
71 $map = [];
72
73 if (!empty($urls)) {
74 $query = $this->connection->createQueryBuilder();
75 $query
76 ->select(
77 $this->connection->quoteIdentifier('id'),
78 $this->connection->quoteIdentifier('url')
79 )
80 ->from(self::URL_TABLE)
81 ->where(
82 $query->expr()->in('url', ':urls')
83 )
84 ->setParameter(':urls', $urls, Connection::PARAM_STR_ARRAY);
85
86 $statement = $query->execute();
87 foreach ($statement->fetchAll(PDO::FETCH_ASSOC) as $row) {
88 $map[$row['url']] = $row['id'];
89 }
90 }
91
92 return $map;
93 }
94
102 public function insertUrl($url)
103 {
104 $time = time();
105
106 $query = $this->connection->createQueryBuilder();
107
108 $query
109 ->insert($this->connection->quoteIdentifier(self::URL_TABLE))
110 ->values(
111 [
112 'created' => ':created',
113 'modified' => ':modified',
114 'original_url_md5' => ':original_url_md5',
115 'url' => ':url',
116 ]
117 )
118 ->setParameter(':created', $time, PDO::PARAM_INT)
119 ->setParameter(':modified', $time, PDO::PARAM_INT)
120 ->setParameter(':original_url_md5', md5($url))
121 ->setParameter(':url', $url)
122 ;
123
124 $query->execute();
125
126 return (int)$this->connection->lastInsertId(
127 $this->getSequenceName(self::URL_TABLE, 'id')
128 );
129 }
130
138 public function linkUrl($urlId, $fieldId, $versionNo)
139 {
140 $query = $this->connection->createQueryBuilder();
141
142 $query
143 ->insert($this->connection->quoteIdentifier(self::URL_LINK_TABLE))
144 ->values(
145 [
146 'contentobject_attribute_id' => ':contentobject_attribute_id',
147 'contentobject_attribute_version' => ':contentobject_attribute_version',
148 'url_id' => ':url_id',
149 ]
150 )
151 ->setParameter(':contentobject_attribute_id', $fieldId, PDO::PARAM_INT)
152 ->setParameter(':contentobject_attribute_version', $versionNo, PDO::PARAM_INT)
153 ->setParameter(':url_id', $urlId, PDO::PARAM_INT)
154 ;
155
156 $query->execute();
157 }
158
166 public function unlinkUrl($fieldId, $versionNo, array $excludeUrlIds = [])
167 {
168 $deleteQuery = $this->connection->createQueryBuilder();
169
170 $deleteQuery
171 ->delete($this->connection->quoteIdentifier(self::URL_LINK_TABLE))
172 ->where(
173 $deleteQuery->expr()->andX(
174 $deleteQuery->expr()->in(
175 'contentobject_attribute_id',
176 ':contentobject_attribute_id'
177 ),
178 $deleteQuery->expr()->in(
179 'contentobject_attribute_version',
180 ':contentobject_attribute_version'
181 )
182 )
183 )
184 ->setParameter(':contentobject_attribute_id', $fieldId, ParameterType::INTEGER)
185 ->setParameter(':contentobject_attribute_version', $versionNo, ParameterType::INTEGER);
186
187 if (empty($excludeUrlIds) === false) {
188 $deleteQuery
189 ->andWhere(
190 $deleteQuery->expr()->notIn(
191 'url_id',
192 ':url_ids'
193 )
194 )
195 ->setParameter('url_ids', $excludeUrlIds, Connection::PARAM_INT_ARRAY);
196 }
197
198 $deleteQuery->execute();
199
200 $this->deleteOrphanedUrls();
201 }
202
210 private function deleteOrphanedUrls()
211 {
212 $query = $this->connection->createQueryBuilder();
213 $query
214 ->select($this->connection->quoteIdentifier('url.id'))
215 ->from($this->connection->quoteIdentifier(self::URL_TABLE), 'url')
216 ->leftJoin(
217 'url',
218 $this->connection->quoteIdentifier(self::URL_LINK_TABLE),
219 'link',
220 'url.id = link.url_id'
221 )
222 ->where($query->expr()->isNull('link.url_id'))
223 ;
224
225 $statement = $query->execute();
226
227 $ids = $statement->fetchAll(PDO::FETCH_COLUMN);
228 if (empty($ids)) {
229 return;
230 }
231
232 $deleteQuery = $this->connection->createQueryBuilder();
233 $deleteQuery
234 ->delete($this->connection->quoteIdentifier(self::URL_TABLE))
235 ->where($deleteQuery->expr()->in('id', ':ids'))
236 ->setParameter(':ids', $ids, Connection::PARAM_STR_ARRAY)
237 ;
238
239 $deleteQuery->execute();
240 }
241 }
eZ\Publish\Core\FieldType\Url\UrlStorage\Gateway\DoctrineStorage
Definition: Url/UrlStorage/Gateway/DoctrineStorage.php:15
eZ\Publish\Core\FieldType\Url\UrlStorage\Gateway\DoctrineStorage\URL_TABLE
const URL_TABLE
Definition: Url/UrlStorage/Gateway/DoctrineStorage.php:16
eZ\Publish\Core\FieldType\Url\UrlStorage\Gateway\DoctrineStorage\deleteOrphanedUrls
deleteOrphanedUrls()
Delete all orphaned URLs.
Definition: Url/UrlStorage/Gateway/DoctrineStorage.php:210
eZ\Publish\Core\FieldType\Url\UrlStorage\Gateway\DoctrineStorage\getUrlIdMap
getUrlIdMap(array $urls)
Return a list of URL ids for a list of URLs.
Definition: Url/UrlStorage/Gateway/DoctrineStorage.php:69
eZ\Publish\Core\FieldType\Url\UrlStorage\Gateway\DoctrineStorage\__construct
__construct(Connection $connection)
Definition: Url/UrlStorage/Gateway/DoctrineStorage.php:22
eZ\Publish\Core\FieldType\Url\UrlStorage\Gateway\DoctrineStorage\unlinkUrl
unlinkUrl($fieldId, $versionNo, array $excludeUrlIds=[])
Remove link to URL for $fieldId in $versionNo and cleans up possibly orphaned URLs.
Definition: Url/UrlStorage/Gateway/DoctrineStorage.php:166
eZ\Publish\Core\FieldType\Url\UrlStorage\Gateway\DoctrineStorage\URL_LINK_TABLE
const URL_LINK_TABLE
Definition: Url/UrlStorage/Gateway/DoctrineStorage.php:17
eZ\Publish\Core\FieldType\Url\UrlStorage\Gateway\DoctrineStorage\linkUrl
linkUrl($urlId, $fieldId, $versionNo)
Create link to URL with $urlId for field with $fieldId in $versionNo.
Definition: Url/UrlStorage/Gateway/DoctrineStorage.php:138
eZ\Publish\Core\FieldType\Url\UrlStorage\Gateway\DoctrineStorage\getIdUrlMap
getIdUrlMap(array $ids)
Return a list of URLs for a list of URL ids.
Definition: Url/UrlStorage/Gateway/DoctrineStorage.php:36
eZ\Publish\Core\FieldType\Url\UrlStorage\Gateway\DoctrineStorage\insertUrl
insertUrl($url)
Insert a new $url and returns its id.
Definition: Url/UrlStorage/Gateway/DoctrineStorage.php:102
eZ\Publish\Core\FieldType\Url\UrlStorage\Gateway\DoctrineStorage\$connection
$connection
Definition: Url/UrlStorage/Gateway/DoctrineStorage.php:20
eZ\Publish\Core\FieldType\Url\UrlStorage\Gateway
Abstract gateway class for Url field type.
Definition: FieldType/Url/UrlStorage/Gateway.php:16
eZ\Publish\SPI\FieldType\StorageGateway\getSequenceName
getSequenceName($table, $column)
Get sequence name bound to database table and column.
Definition: SPI/FieldType/StorageGateway.php:26
eZ\Publish\Core\FieldType\Url\UrlStorage\Gateway
Definition: Url/UrlStorage/Gateway/DoctrineStorage.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.