1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
5
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
6
|
|
|
*/ |
7
|
|
|
namespace eZ\Publish\Core\Persistence\Legacy\URL\Gateway; |
8
|
|
|
|
9
|
|
|
use eZ\Publish\Core\Persistence\Database\SelectQuery; |
10
|
|
|
use eZ\Publish\API\Repository\Values\URL\Query\Criterion; |
11
|
|
|
use eZ\Publish\API\Repository\Values\URL\Query\SortClause; |
12
|
|
|
use eZ\Publish\Core\Persistence\Legacy\URL\Gateway; |
13
|
|
|
use eZ\Publish\Core\Persistence\Legacy\URL\Query\CriteriaConverter; |
14
|
|
|
use eZ\Publish\Core\Persistence\Database\DatabaseHandler; |
15
|
|
|
use eZ\Publish\SPI\Persistence\URL\URL; |
16
|
|
|
use PDO; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* URL gateway implementation using the Doctrine. |
20
|
|
|
*/ |
21
|
|
|
class DoctrineDatabase extends Gateway |
22
|
|
|
{ |
23
|
|
|
const URL_TABLE = 'ezurl'; |
24
|
|
|
const URL_LINK_TABLE = 'ezurl_object_link'; |
25
|
|
|
|
26
|
|
|
const COLUMN_ID = 'id'; |
27
|
|
|
const COLUMN_URL = 'url'; |
28
|
|
|
const COLUMN_ORIGINAL_URL_MD5 = 'original_url_md5'; |
29
|
|
|
const COLUMN_IS_VALID = 'is_valid'; |
30
|
|
|
const COLUMN_LAST_CHECKED = 'last_checked'; |
31
|
|
|
const COLUMN_MODIFIED = 'modified'; |
32
|
|
|
const COLUMN_CREATED = 'created'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Database handler. |
36
|
|
|
* |
37
|
|
|
* @param \eZ\Publish\Core\Persistence\Database\DatabaseHandler $dbHandler |
38
|
|
|
*/ |
39
|
|
|
protected $handler; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Criteria converter. |
43
|
|
|
* |
44
|
|
|
* @var \eZ\Publish\Core\Persistence\Legacy\URL\Query\CriteriaConverter |
45
|
|
|
*/ |
46
|
|
|
protected $criteriaConverter; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Creates a new Doctrine database Section Gateway. |
50
|
|
|
* |
51
|
|
|
* @param \eZ\Publish\Core\Persistence\Database\DatabaseHandler $dbHandler |
52
|
|
|
* @param \eZ\Publish\Core\Persistence\Legacy\URL\Query\CriteriaConverter $criteriaConverter |
53
|
|
|
*/ |
54
|
|
|
public function __construct(DatabaseHandler $dbHandler, CriteriaConverter $criteriaConverter) |
55
|
|
|
{ |
56
|
|
|
$this->handler = $dbHandler; |
57
|
|
|
$this->criteriaConverter = $criteriaConverter; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
|
|
public function find(Criterion $criterion, $offset, $limit, array $sortClauses = [], $doCount = true) |
64
|
|
|
{ |
65
|
|
|
$count = $doCount ? $this->doCount($criterion) : null; |
66
|
|
|
if (!$doCount && $limit === 0) { |
67
|
|
|
throw new \RuntimeException('Invalid query, can not disable count and request 0 items at the same time'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
View Code Duplication |
if ($limit === 0 || ($count !== null && $count <= $offset)) { |
|
|
|
|
71
|
|
|
return [ |
72
|
|
|
'count' => $count, |
73
|
|
|
'rows' => [], |
74
|
|
|
]; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$query = $this->createSelectQuery(); |
78
|
|
|
$query->where($this->criteriaConverter->convertCriteria($query, $criterion)); |
79
|
|
|
$query->limit($limit > 0 ? $limit : PHP_INT_MAX, $offset); |
80
|
|
|
|
81
|
|
|
foreach ($sortClauses as $sortClause) { |
82
|
|
|
$column = $this->handler->quoteColumn($sortClause->target, self::URL_TABLE); |
83
|
|
|
|
84
|
|
|
$direction = SelectQuery::ASC; |
85
|
|
|
if ($sortClause->direction === SortClause::SORT_DESC) { |
86
|
|
|
$direction = SelectQuery::DESC; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$query->orderBy($column, $direction); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$statement = $query->prepare(); |
93
|
|
|
$statement->execute(); |
94
|
|
|
|
95
|
|
|
return [ |
96
|
|
|
'count' => $count, |
97
|
|
|
'rows' => $statement->fetchAll(PDO::FETCH_ASSOC), |
98
|
|
|
]; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
|
|
public function findUsages($id) |
105
|
|
|
{ |
106
|
|
|
$query = $this->handler->createSelectQuery(); |
107
|
|
|
$query->selectDistinct( |
108
|
|
|
$this->handler->quoteColumn('id', 'ezcontentobject') |
109
|
|
|
)->from( |
110
|
|
|
$this->handler->quoteTable('ezcontentobject') |
111
|
|
|
)->innerJoin( |
112
|
|
|
$this->handler->quoteTable('ezcontentobject_attribute'), |
113
|
|
|
$query->expr->lAnd( |
114
|
|
|
$query->expr->eq( |
115
|
|
|
$this->handler->quoteColumn('id', 'ezcontentobject'), |
116
|
|
|
$this->handler->quoteColumn( |
117
|
|
|
'contentobject_id', |
118
|
|
|
'ezcontentobject_attribute' |
119
|
|
|
) |
120
|
|
|
), |
121
|
|
|
$query->expr->eq( |
122
|
|
|
$this->handler->quoteColumn('current_version', 'ezcontentobject'), |
123
|
|
|
$this->handler->quoteColumn('version', 'ezcontentobject_attribute') |
124
|
|
|
) |
125
|
|
|
) |
126
|
|
|
)->innerJoin( |
127
|
|
|
$this->handler->quoteTable(self::URL_LINK_TABLE), |
128
|
|
|
$query->expr->lAnd( |
129
|
|
|
$query->expr->eq( |
130
|
|
|
$this->handler->quoteColumn('id', 'ezcontentobject_attribute'), |
131
|
|
|
$this->handler->quoteColumn('contentobject_attribute_id', self::URL_LINK_TABLE) |
132
|
|
|
), |
133
|
|
|
$query->expr->eq( |
134
|
|
|
$this->handler->quoteColumn('version', 'ezcontentobject_attribute'), |
135
|
|
|
$this->handler->quoteColumn('contentobject_attribute_version', self::URL_LINK_TABLE) |
136
|
|
|
) |
137
|
|
|
) |
138
|
|
|
)->where( |
139
|
|
|
$query->expr->eq( |
140
|
|
|
$this->handler->quoteColumn('url_id', self::URL_LINK_TABLE), |
141
|
|
|
$query->bindValue($id, null, PDO::PARAM_INT) |
142
|
|
|
) |
143
|
|
|
); |
144
|
|
|
|
145
|
|
|
$statement = $query->prepare(); |
146
|
|
|
$statement->execute(); |
147
|
|
|
|
148
|
|
|
return array_column($statement->fetchAll(PDO::FETCH_ASSOC), 'id'); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* {@inheritdoc} |
153
|
|
|
*/ |
154
|
|
|
public function updateUrl(URL $url) |
155
|
|
|
{ |
156
|
|
|
$query = $this->handler->createUpdateQuery(); |
157
|
|
|
$query->update( |
158
|
|
|
$this->handler->quoteTable(self::URL_TABLE) |
159
|
|
|
)->set( |
160
|
|
|
$this->handler->quoteColumn(self::COLUMN_URL), |
161
|
|
|
$query->bindValue($url->url) |
162
|
|
|
)->set( |
163
|
|
|
$this->handler->quoteColumn(self::COLUMN_ORIGINAL_URL_MD5), |
164
|
|
|
$query->bindValue($url->originalUrlMd5) |
165
|
|
|
)->set( |
166
|
|
|
$this->handler->quoteColumn(self::COLUMN_MODIFIED), |
167
|
|
|
$query->bindValue($url->modified, null, PDO::PARAM_INT) |
168
|
|
|
)->set( |
169
|
|
|
$this->handler->quoteColumn(self::COLUMN_IS_VALID), |
170
|
|
|
$query->bindValue((int) $url->isValid, null, PDO::PARAM_INT) |
171
|
|
|
)->set( |
172
|
|
|
$this->handler->quoteColumn(self::COLUMN_LAST_CHECKED), |
173
|
|
|
$query->bindValue($url->lastChecked, null, PDO::PARAM_INT) |
174
|
|
|
)->where( |
175
|
|
|
$query->expr->eq( |
176
|
|
|
$this->handler->quoteColumn(self::COLUMN_ID), |
177
|
|
|
$query->bindValue($url->id, null, PDO::PARAM_INT) |
178
|
|
|
) |
179
|
|
|
); |
180
|
|
|
|
181
|
|
|
$query->prepare()->execute(); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* {@inheritdoc} |
186
|
|
|
*/ |
187
|
|
|
public function loadUrlData($id) |
188
|
|
|
{ |
189
|
|
|
$query = $this->createSelectQuery(); |
190
|
|
|
$query->where( |
191
|
|
|
$query->expr->eq( |
192
|
|
|
$this->handler->quoteColumn(self::COLUMN_ID), |
193
|
|
|
$query->bindValue($id, null, PDO::PARAM_INT) |
194
|
|
|
) |
195
|
|
|
); |
196
|
|
|
|
197
|
|
|
$statement = $query->prepare(); |
198
|
|
|
$statement->execute(); |
199
|
|
|
|
200
|
|
|
return $statement->fetchAll(PDO::FETCH_ASSOC); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* {@inheritdoc} |
205
|
|
|
*/ |
206
|
|
|
public function loadUrlDataByUrl($url) |
207
|
|
|
{ |
208
|
|
|
$query = $this->createSelectQuery(); |
209
|
|
|
$query->where( |
210
|
|
|
$query->expr->eq( |
211
|
|
|
$this->handler->quoteColumn(self::COLUMN_URL), |
212
|
|
|
$query->bindValue($url, null, PDO::PARAM_STR) |
213
|
|
|
) |
214
|
|
|
); |
215
|
|
|
|
216
|
|
|
$statement = $query->prepare(); |
217
|
|
|
$statement->execute(); |
218
|
|
|
|
219
|
|
|
return $statement->fetchAll(PDO::FETCH_ASSOC); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* {@inheritdoc} |
224
|
|
|
*/ |
225
|
|
|
protected function doCount(Criterion $criterion) |
226
|
|
|
{ |
227
|
|
|
$columnName = $this->handler->quoteColumn(self::COLUMN_ID, self::URL_TABLE); |
228
|
|
|
|
229
|
|
|
$query = $this->handler->createSelectQuery(); |
230
|
|
|
$query |
231
|
|
|
->select("COUNT(DISTINCT $columnName)") |
232
|
|
|
->from($this->handler->quoteTable(self::URL_TABLE)) |
233
|
|
|
->where($this->criteriaConverter->convertCriteria($query, $criterion)); |
234
|
|
|
|
235
|
|
|
$statement = $query->prepare(); |
236
|
|
|
$statement->execute(); |
237
|
|
|
|
238
|
|
|
return (int)$statement->fetchColumn(); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Creates a Url find query. |
243
|
|
|
* |
244
|
|
|
* @return \eZ\Publish\Core\Persistence\Database\SelectQuery |
245
|
|
|
*/ |
246
|
|
|
protected function createSelectQuery() |
247
|
|
|
{ |
248
|
|
|
$query = $this->handler->createSelectQuery(); |
249
|
|
|
$query->select( |
250
|
|
|
$this->handler->quoteColumn(self::COLUMN_ID), |
251
|
|
|
$this->handler->quoteColumn(self::COLUMN_URL), |
252
|
|
|
$this->handler->quoteColumn(self::COLUMN_ORIGINAL_URL_MD5), |
253
|
|
|
$this->handler->quoteColumn(self::COLUMN_IS_VALID), |
254
|
|
|
$this->handler->quoteColumn(self::COLUMN_LAST_CHECKED), |
255
|
|
|
$this->handler->quoteColumn(self::COLUMN_CREATED), |
256
|
|
|
$this->handler->quoteColumn(self::COLUMN_MODIFIED) |
257
|
|
|
)->from( |
258
|
|
|
$this->handler->quoteTable(self::URL_TABLE) |
259
|
|
|
); |
260
|
|
|
|
261
|
|
|
return $query; |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.