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