1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* File containing the eZ\Publish\Core\Repository\TrashService class. |
5
|
|
|
* |
6
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
7
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
namespace eZ\Publish\Core\Repository; |
10
|
|
|
|
11
|
|
|
use eZ\Publish\API\Repository\TrashService as TrashServiceInterface; |
12
|
|
|
use eZ\Publish\API\Repository\Repository as RepositoryInterface; |
13
|
|
|
use eZ\Publish\SPI\Persistence\Handler; |
14
|
|
|
use eZ\Publish\API\Repository\Values\Content\Location; |
15
|
|
|
use eZ\Publish\Core\Repository\Values\Content\TrashItem; |
16
|
|
|
use eZ\Publish\API\Repository\Values\Content\TrashItem as APITrashItem; |
17
|
|
|
use eZ\Publish\API\Repository\Values\Content\Query; |
18
|
|
|
use eZ\Publish\SPI\Persistence\Content\Location\Trashed; |
19
|
|
|
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentValue; |
20
|
|
|
use eZ\Publish\Core\Base\Exceptions\UnauthorizedException; |
21
|
|
|
use eZ\Publish\API\Repository\Values\Content\SearchResult; |
22
|
|
|
use eZ\Publish\API\Repository\Values\Content\Query\Criterion; |
23
|
|
|
use eZ\Publish\API\Repository\Values\Content\Query\SortClause; |
24
|
|
|
use DateTime; |
25
|
|
|
use Exception; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Trash service, used for managing trashed content. |
29
|
|
|
*/ |
30
|
|
|
class TrashService implements TrashServiceInterface |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var \eZ\Publish\Core\Repository\Repository |
34
|
|
|
*/ |
35
|
|
|
protected $repository; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var \eZ\Publish\SPI\Persistence\Handler |
39
|
|
|
*/ |
40
|
|
|
protected $persistenceHandler; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
protected $settings; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var \eZ\Publish\Core\Repository\Helper\NameSchemaService |
49
|
|
|
*/ |
50
|
|
|
protected $nameSchemaService; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Setups service with reference to repository object that created it & corresponding handler. |
54
|
|
|
* |
55
|
|
|
* @param \eZ\Publish\API\Repository\Repository $repository |
56
|
|
|
* @param \eZ\Publish\SPI\Persistence\Handler $handler |
57
|
|
|
* @param \eZ\Publish\Core\Repository\Helper\NameSchemaService $nameSchemaService |
58
|
|
|
* @param array $settings |
59
|
|
|
*/ |
60
|
|
|
public function __construct( |
61
|
|
|
RepositoryInterface $repository, |
62
|
|
|
Handler $handler, |
63
|
|
|
Helper\NameSchemaService $nameSchemaService, |
64
|
|
|
array $settings = array() |
65
|
|
|
) { |
66
|
|
|
$this->repository = $repository; |
|
|
|
|
67
|
|
|
$this->persistenceHandler = $handler; |
68
|
|
|
$this->nameSchemaService = $nameSchemaService; |
69
|
|
|
// Union makes sure default settings are ignored if provided in argument |
70
|
|
|
$this->settings = $settings + array( |
71
|
|
|
//'defaultSetting' => array(), |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Loads a trashed location object from its $id. |
77
|
|
|
* |
78
|
|
|
* Note that $id is identical to original location, which has been previously trashed |
79
|
|
|
* |
80
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to read the trashed location |
81
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - if the location with the given id does not exist |
82
|
|
|
* |
83
|
|
|
* @param mixed $trashItemId |
84
|
|
|
* |
85
|
|
|
* @return \eZ\Publish\API\Repository\Values\Content\TrashItem |
86
|
|
|
*/ |
87
|
|
|
public function loadTrashItem($trashItemId) |
88
|
|
|
{ |
89
|
|
|
if ($this->repository->hasAccess('content', 'restore') !== true) { |
|
|
|
|
90
|
|
|
throw new UnauthorizedException('content', 'restore'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$spiTrashItem = $this->persistenceHandler->trashHandler()->loadTrashItem($trashItemId); |
94
|
|
|
$trash = $this->buildDomainTrashItemObject($spiTrashItem); |
95
|
|
|
if (!$this->repository->canUser('content', 'read', $trash->getContentInfo())) { |
|
|
|
|
96
|
|
|
throw new UnauthorizedException('content', 'read'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $trash; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Sends $location and all its children to trash and returns the corresponding trash item, or true if the user is not allowed to read the trashed location. |
104
|
|
|
* |
105
|
|
|
* Content is left untouched. |
106
|
|
|
* |
107
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to trash the given location |
108
|
|
|
* |
109
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\Location $location |
110
|
|
|
* |
111
|
|
|
* @return null|true|\eZ\Publish\API\Repository\Values\Content\TrashItem null if location was deleted, otherwise TrashItem, or true if the user is not allowed to read the trashed location |
112
|
|
|
*/ |
113
|
|
|
public function trash(Location $location) |
114
|
|
|
{ |
115
|
|
|
if (!is_numeric($location->id)) { |
116
|
|
|
throw new InvalidArgumentValue('id', $location->id, 'Location'); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
if ($this->repository->canUser('content', 'manage_locations', $location->getContentInfo(), $location) !== true) { |
|
|
|
|
120
|
|
|
throw new UnauthorizedException('content', 'manage_locations'); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$this->repository->beginTransaction(); |
124
|
|
|
try { |
125
|
|
|
$spiTrashItem = $this->persistenceHandler->trashHandler()->trashSubtree($location->id); |
126
|
|
|
$this->persistenceHandler->urlAliasHandler()->locationDeleted($location->id); |
127
|
|
|
$this->repository->commit(); |
128
|
|
|
} catch (Exception $e) { |
129
|
|
|
$this->repository->rollback(); |
130
|
|
|
throw $e; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
try { |
134
|
|
|
return isset($spiTrashItem) |
135
|
|
|
? $this->buildDomainTrashItemObject($spiTrashItem) |
136
|
|
|
: null; |
137
|
|
|
} catch (UnauthorizedException $e) { |
138
|
|
|
return true; |
|
|
|
|
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Recovers the $trashedLocation at its original place if possible. |
144
|
|
|
* |
145
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to recover the trash item at the parent location location |
146
|
|
|
* |
147
|
|
|
* If $newParentLocation is provided, $trashedLocation will be restored under it. |
148
|
|
|
* |
149
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\TrashItem $trashItem |
150
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\Location $newParentLocation |
151
|
|
|
* |
152
|
|
|
* @return \eZ\Publish\API\Repository\Values\Content\Location the newly created or recovered location |
153
|
|
|
*/ |
154
|
|
|
public function recover(APITrashItem $trashItem, Location $newParentLocation = null) |
155
|
|
|
{ |
156
|
|
|
if (!is_numeric($trashItem->id)) { |
|
|
|
|
157
|
|
|
throw new InvalidArgumentValue('id', $trashItem->id, 'TrashItem'); |
|
|
|
|
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
if ($newParentLocation === null && !is_numeric($trashItem->parentLocationId)) { |
|
|
|
|
161
|
|
|
throw new InvalidArgumentValue('parentLocationId', $trashItem->parentLocationId, 'TrashItem'); |
|
|
|
|
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
if ($newParentLocation !== null && !is_numeric($newParentLocation->id)) { |
165
|
|
|
throw new InvalidArgumentValue('parentLocationId', $newParentLocation->id, 'Location'); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
if ($this->repository->hasAccess('content', 'restore') !== true) { |
|
|
|
|
169
|
|
|
throw new UnauthorizedException('content', 'restore'); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
$this->repository->beginTransaction(); |
173
|
|
|
try { |
174
|
|
|
$newParentLocationId = $newParentLocation ? $newParentLocation->id : $trashItem->parentLocationId; |
|
|
|
|
175
|
|
|
$newLocationId = $this->persistenceHandler->trashHandler()->recover( |
176
|
|
|
$trashItem->id, |
|
|
|
|
177
|
|
|
$newParentLocationId |
178
|
|
|
); |
179
|
|
|
|
180
|
|
|
$content = $this->repository->getContentService()->loadContent($trashItem->contentId); |
181
|
|
|
$urlAliasNames = $this->nameSchemaService->resolveUrlAliasSchema($content); |
182
|
|
|
|
183
|
|
|
// Publish URL aliases for recovered location |
184
|
|
|
foreach ($urlAliasNames as $languageCode => $name) { |
185
|
|
|
$this->persistenceHandler->urlAliasHandler()->publishUrlAliasForLocation( |
186
|
|
|
$newLocationId, |
187
|
|
|
$newParentLocationId, |
188
|
|
|
$name, |
189
|
|
|
$languageCode, |
190
|
|
|
$content->contentInfo->alwaysAvailable |
191
|
|
|
); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
$this->repository->commit(); |
195
|
|
|
} catch (Exception $e) { |
196
|
|
|
$this->repository->rollback(); |
197
|
|
|
throw $e; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
return $this->repository->getLocationService()->loadLocation($newLocationId); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Empties trash. |
205
|
|
|
* |
206
|
|
|
* All locations contained in the trash will be removed. Content objects will be removed |
207
|
|
|
* if all locations of the content are gone. |
208
|
|
|
* |
209
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to empty the trash |
210
|
|
|
*/ |
211
|
|
View Code Duplication |
public function emptyTrash() |
212
|
|
|
{ |
213
|
|
|
if ($this->repository->hasAccess('content', 'cleantrash') !== true) { |
|
|
|
|
214
|
|
|
throw new UnauthorizedException('content', 'cleantrash'); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
$this->repository->beginTransaction(); |
218
|
|
|
try { |
219
|
|
|
// Persistence layer takes care of deleting content objects |
220
|
|
|
$this->persistenceHandler->trashHandler()->emptyTrash(); |
221
|
|
|
$this->repository->commit(); |
222
|
|
|
} catch (Exception $e) { |
223
|
|
|
$this->repository->rollback(); |
224
|
|
|
throw $e; |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Deletes a trash item. |
230
|
|
|
* |
231
|
|
|
* The corresponding content object will be removed |
232
|
|
|
* |
233
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to delete this trash item |
234
|
|
|
* |
235
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\TrashItem $trashItem |
236
|
|
|
*/ |
237
|
|
View Code Duplication |
public function deleteTrashItem(APITrashItem $trashItem) |
238
|
|
|
{ |
239
|
|
|
if ($this->repository->hasAccess('content', 'cleantrash') !== true) { |
|
|
|
|
240
|
|
|
throw new UnauthorizedException('content', 'cleantrash'); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
if (!is_numeric($trashItem->id)) { |
|
|
|
|
244
|
|
|
throw new InvalidArgumentValue('id', $trashItem->id, 'TrashItem'); |
|
|
|
|
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
$this->repository->beginTransaction(); |
248
|
|
|
try { |
249
|
|
|
$this->persistenceHandler->trashHandler()->deleteTrashItem($trashItem->id); |
|
|
|
|
250
|
|
|
$this->repository->commit(); |
251
|
|
|
} catch (Exception $e) { |
252
|
|
|
$this->repository->rollback(); |
253
|
|
|
throw $e; |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Returns a collection of Trashed locations contained in the trash. |
259
|
|
|
* |
260
|
|
|
* $query allows to filter/sort the elements to be contained in the collection. |
261
|
|
|
* |
262
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\Query $query |
263
|
|
|
* |
264
|
|
|
* @return \eZ\Publish\API\Repository\Values\Content\SearchResult |
265
|
|
|
*/ |
266
|
|
|
public function findTrashItems(Query $query) |
267
|
|
|
{ |
268
|
|
|
if ($query->filter !== null && !$query->filter instanceof Criterion) { |
269
|
|
|
throw new InvalidArgumentValue('query->filter', $query->filter, 'Query'); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
if ($query->sortClauses !== null) { |
273
|
|
|
if (!is_array($query->sortClauses)) { |
274
|
|
|
throw new InvalidArgumentValue('query->sortClauses', $query->sortClauses, 'Query'); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
foreach ($query->sortClauses as $sortClause) { |
278
|
|
|
if (!$sortClause instanceof SortClause) { |
279
|
|
|
throw new InvalidArgumentValue('query->sortClauses', 'only instances of SortClause class are allowed'); |
280
|
|
|
} |
281
|
|
|
} |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
if ($query->offset !== null && !is_numeric($query->offset)) { |
285
|
|
|
throw new InvalidArgumentValue('query->offset', $query->offset, 'Query'); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
if ($query->limit !== null && !is_numeric($query->limit)) { |
289
|
|
|
throw new InvalidArgumentValue('query->limit', $query->limit, 'Query'); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
$spiTrashItems = $this->persistenceHandler->trashHandler()->findTrashItems( |
293
|
|
|
$query->filter !== null ? $query->filter : null, |
294
|
|
|
$query->offset !== null && $query->offset > 0 ? (int)$query->offset : 0, |
295
|
|
|
$query->limit !== null && $query->limit >= 1 ? (int)$query->limit : null, |
296
|
|
|
$query->sortClauses !== null ? $query->sortClauses : null |
|
|
|
|
297
|
|
|
); |
298
|
|
|
|
299
|
|
|
$trashItems = array(); |
300
|
|
|
foreach ($spiTrashItems as $spiTrashItem) { |
301
|
|
|
$trashItems[] = $this->buildDomainTrashItemObject($spiTrashItem); |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
$searchResult = new SearchResult(); |
|
|
|
|
305
|
|
|
$searchResult->count = count($trashItems); |
306
|
|
|
$searchResult->items = $trashItems; |
307
|
|
|
$searchResult->query = $query; |
308
|
|
|
|
309
|
|
|
return $searchResult; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Builds the domain TrashItem object from provided persistence trash item. |
314
|
|
|
* |
315
|
|
|
* @param \eZ\Publish\SPI\Persistence\Content\Location\Trashed $spiTrashItem |
316
|
|
|
* |
317
|
|
|
* @return \eZ\Publish\API\Repository\Values\Content\TrashItem |
318
|
|
|
*/ |
319
|
|
View Code Duplication |
protected function buildDomainTrashItemObject(Trashed $spiTrashItem) |
320
|
|
|
{ |
321
|
|
|
return new TrashItem( |
322
|
|
|
array( |
323
|
|
|
'contentInfo' => $this->repository->getContentService()->loadContentInfo($spiTrashItem->contentId), |
324
|
|
|
'id' => $spiTrashItem->id, |
325
|
|
|
'priority' => $spiTrashItem->priority, |
326
|
|
|
'hidden' => $spiTrashItem->hidden, |
327
|
|
|
'invisible' => $spiTrashItem->invisible, |
328
|
|
|
'remoteId' => $spiTrashItem->remoteId, |
329
|
|
|
'parentLocationId' => $spiTrashItem->parentId, |
330
|
|
|
'pathString' => $spiTrashItem->pathString, |
331
|
|
|
'depth' => $spiTrashItem->depth, |
332
|
|
|
'sortField' => $spiTrashItem->sortField, |
333
|
|
|
'sortOrder' => $spiTrashItem->sortOrder, |
334
|
|
|
) |
335
|
|
|
); |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* @param int $timestamp |
340
|
|
|
* |
341
|
|
|
* @return \DateTime |
342
|
|
|
*/ |
343
|
|
|
protected function getDateTime($timestamp) |
344
|
|
|
{ |
345
|
|
|
$dateTime = new DateTime(); |
346
|
|
|
$dateTime->setTimestamp($timestamp); |
347
|
|
|
|
348
|
|
|
return $dateTime; |
349
|
|
|
} |
350
|
|
|
} |
351
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.