Completed
Push — ezp_30973 ( feb262...9f83f6 )
by
unknown
14:39
created

ContentHandler::listVersions()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 3
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the ContentHandler implementation.
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\Persistence\Cache;
10
11
use eZ\Publish\API\Repository\Values\Content\Relation as APIRelation;
12
use eZ\Publish\SPI\Persistence\Content\Handler as ContentHandlerInterface;
13
use eZ\Publish\SPI\Persistence\Content;
14
use eZ\Publish\SPI\Persistence\Content\VersionInfo;
15
use eZ\Publish\SPI\Persistence\Content\ContentInfo;
16
use eZ\Publish\SPI\Persistence\Content\CreateStruct;
17
use eZ\Publish\SPI\Persistence\Content\UpdateStruct;
18
use eZ\Publish\SPI\Persistence\Content\MetadataUpdateStruct;
19
use eZ\Publish\SPI\Persistence\Content\Relation\CreateStruct as RelationCreateStruct;
20
21
/**
22
 * @see \eZ\Publish\SPI\Persistence\Content\Handler
23
 */
24
class ContentHandler extends AbstractInMemoryPersistenceHandler implements ContentHandlerInterface
25
{
26
    const ALL_TRANSLATIONS_KEY = '0';
27
28
    /** @var callable */
29
    private $getContentInfoTags;
30
31
    /** @var callable */
32
    private $getContentInfoKeys;
33
34
    /** @var callable */
35
    private $getContentTags;
36
37
    protected function init(): void
38
    {
39
        $this->getContentInfoTags = function (ContentInfo $info, array $tags = []) {
40
            $tags[] = 'content-' . $info->id;
41
42
            if ($info->mainLocationId) {
43
                $locations = $this->persistenceHandler->locationHandler()->loadLocationsByContent($info->id);
44
                foreach ($locations as $location) {
45
                    $tags[] = 'location-' . $location->id;
46
                    foreach (explode('/', trim($location->pathString, '/')) as $pathId) {
47
                        $tags[] = 'location-path-' . $pathId;
48
                    }
49
                }
50
            }
51
52
            return $tags;
53
        };
54
        $this->getContentInfoKeys = function (ContentInfo $info) {
55
            return [
56
                'ez-content-info-' . $info->id,
57
                'ez-content-info-byRemoteId-' . $this->escapeForCacheKey($info->remoteId),
58
            ];
59
        };
60
61
        $this->getContentTags = function (Content $content) {
62
            $versionInfo = $content->versionInfo;
63
            $tags = [
64
                'content-fields-' . $versionInfo->contentInfo->id,
65
                'content-fields-type-' . $versionInfo->contentInfo->contentTypeId,
66
            ];
67
68
            return $this->getCacheTagsForVersion($versionInfo, $tags);
69
        };
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function create(CreateStruct $struct)
76
    {
77
        // Cached on demand when published or loaded
78
        $this->logger->logCall(__METHOD__, ['struct' => $struct]);
79
80
        return $this->persistenceHandler->contentHandler()->create($struct);
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function createDraftFromVersion($contentId, $srcVersion, $userId)
87
    {
88
        $this->logger->logCall(__METHOD__, ['content' => $contentId, 'version' => $srcVersion, 'user' => $userId]);
89
        $draft = $this->persistenceHandler->contentHandler()->createDraftFromVersion($contentId, $srcVersion, $userId);
90
        $this->cache->invalidateTags(["content-{$contentId}-version-list"]);
91
92
        return $draft;
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function copy($contentId, $versionNo = null, $newOwnerId = null)
99
    {
100
        $this->logger->logCall(__METHOD__, [
101
            'content' => $contentId,
102
            'version' => $versionNo,
103
            'newOwner' => $newOwnerId,
104
        ]);
105
106
        return $this->persistenceHandler->contentHandler()->copy($contentId, $versionNo, $newOwnerId);
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function load($contentId, $versionNo = null, array $translations = null)
113
    {
114
        $keySuffix = $versionNo ? "-${versionNo}-" : '-';
115
        $keySuffix .= empty($translations) ? self::ALL_TRANSLATIONS_KEY : implode('|', $translations);
116
117
        return $this->getCacheValue(
118
            (int) $contentId,
119
            'ez-content-',
120
            function ($id) use ($versionNo, $translations) {
121
                return $this->persistenceHandler->contentHandler()->load($id, $versionNo, $translations);
0 ignored issues
show
Bug introduced by
It seems like $translations defined by parameter $translations on line 112 can also be of type array; however, eZ\Publish\SPI\Persistence\Content\Handler::load() does only seem to accept null|array<integer,string>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
122
            },
123
            $this->getContentTags,
124
            static function (Content $content) use ($keySuffix) {
125
                // Version number & translations is part of keySuffix here and depends on what user asked for
126
                return ['ez-content-' . $content->versionInfo->contentInfo->id . $keySuffix];
127
            },
128
            $keySuffix,
129
            ['content' => $contentId, 'version' => $versionNo, 'translations' => $translations]
130
        );
131
    }
132
133
    public function loadContentList(array $contentIds, array $translations = null): array
134
    {
135
        $keySuffix = '-' . (empty($translations) ? self::ALL_TRANSLATIONS_KEY : implode('|', $translations));
136
137
        return $this->getMultipleCacheValues(
138
            $contentIds,
139
            'ez-content-',
140
            function (array $cacheMissIds) use ($translations) {
141
                return $this->persistenceHandler->contentHandler()->loadContentList($cacheMissIds, $translations);
0 ignored issues
show
Bug introduced by
It seems like $translations defined by parameter $translations on line 133 can also be of type array; however, eZ\Publish\SPI\Persisten...dler::loadContentList() does only seem to accept null|array<integer,string>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
142
            },
143
            $this->getContentTags,
144
            static function (Content $content) use ($keySuffix) {
145
                // Version number & translations is part of keySuffix here and depends on what user asked for
146
                return ['ez-content-' . $content->versionInfo->contentInfo->id . $keySuffix];
147
            },
148
            $keySuffix,
149
            ['content' => $contentIds, 'translations' => $translations]
150
        );
151
    }
152
153
    /**
154
     * {@inheritdoc}
155
     */
156
    public function loadContentInfo($contentId)
157
    {
158
        return $this->getCacheValue(
159
            $contentId,
160
            'ez-content-info-',
161
            function ($contentId) {
162
                return $this->persistenceHandler->contentHandler()->loadContentInfo($contentId);
163
            },
164
            $this->getContentInfoTags,
165
            $this->getContentInfoKeys,
166
            '',
167
            ['content' => $contentId]
168
        );
169
    }
170
171
    public function loadContentInfoList(array $contentIds)
172
    {
173
        return $this->getMultipleCacheValues(
174
            $contentIds,
175
            'ez-content-info-',
176
            function (array $cacheMissIds) {
177
                return $this->persistenceHandler->contentHandler()->loadContentInfoList($cacheMissIds);
178
            },
179
            $this->getContentInfoTags,
180
            $this->getContentInfoKeys,
181
            '',
182
            ['content' => $contentIds]
183
        );
184
    }
185
186
    /**
187
     * {@inheritdoc}
188
     */
189
    public function loadContentInfoByRemoteId($remoteId)
190
    {
191
        return $this->getCacheValue(
192
            $this->escapeForCacheKey($remoteId),
193
            'ez-content-info-byRemoteId-',
194
            function () use ($remoteId) {
195
                return $this->persistenceHandler->contentHandler()->loadContentInfoByRemoteId($remoteId);
196
            },
197
            $this->getContentInfoTags,
198
            $this->getContentInfoKeys,
199
            '',
200
            ['content' => $remoteId]
201
        );
202
    }
203
204
    /**
205
     * {@inheritdoc}
206
     */
207
    public function loadVersionInfo($contentId, $versionNo = null)
208
    {
209
        $keySuffix = $versionNo ? "-${versionNo}" : '';
210
        $cacheItem = $this->cache->getItem("ez-content-version-info-${contentId}${keySuffix}");
211
        if ($cacheItem->isHit()) {
212
            $this->logger->logCacheHit(['content' => $contentId, 'version' => $versionNo]);
213
214
            return $cacheItem->get();
215
        }
216
217
        $this->logger->logCacheMiss(['content' => $contentId, 'version' => $versionNo]);
218
        $versionInfo = $this->persistenceHandler->contentHandler()->loadVersionInfo($contentId, $versionNo);
219
        $cacheItem->set($versionInfo);
220
        $cacheItem->tag($this->getCacheTagsForVersion($versionInfo));
221
        $this->cache->save($cacheItem);
222
223
        return $versionInfo;
224
    }
225
226
    public function countDraftsForUser(int $userId): int
227
    {
228
        $this->logger->logCall(__METHOD__, ['user' => $userId]);
229
230
        return $this->persistenceHandler->contentHandler()->countDraftsForUser($userId);
231
    }
232
233
    /**
234
     * {@inheritdoc}
235
     */
236
    public function loadDraftsForUser($userId)
237
    {
238
        $this->logger->logCall(__METHOD__, ['user' => $userId]);
239
240
        return $this->persistenceHandler->contentHandler()->loadDraftsForUser($userId);
241
    }
242
243
    public function loadDraftListForUser(int $userId, int $offset = 0, int $limit = -1): array
244
    {
245
        $this->logger->logCall(__METHOD__, ['user' => $userId, 'offset' => $offset, 'limit' => $limit]);
246
247
        return $this->persistenceHandler->contentHandler()->loadDraftListForUser($userId, $offset, $limit);
248
    }
249
250
    /**
251
     * {@inheritdoc}
252
     */
253
    public function setStatus($contentId, $status, $versionNo)
254
    {
255
        $this->logger->logCall(__METHOD__, ['content' => $contentId, 'status' => $status, 'version' => $versionNo]);
256
        $return = $this->persistenceHandler->contentHandler()->setStatus($contentId, $status, $versionNo);
257
258
        if ($status === VersionInfo::STATUS_PUBLISHED) {
259
            $this->cache->invalidateTags(['content-' . $contentId]);
260
        } else {
261
            $this->cache->invalidateTags(["content-{$contentId}-version-{$versionNo}"]);
262
        }
263
264
        return $return;
265
    }
266
267
    /**
268
     * {@inheritdoc}
269
     */
270
    public function updateMetadata($contentId, MetadataUpdateStruct $struct)
271
    {
272
        $this->logger->logCall(__METHOD__, ['content' => $contentId, 'struct' => $struct]);
273
        $contentInfo = $this->persistenceHandler->contentHandler()->updateMetadata($contentId, $struct);
274
        $this->cache->invalidateTags(['content-' . $contentId]);
275
276
        return $contentInfo;
277
    }
278
279
    /**
280
     * {@inheritdoc}
281
     */
282
    public function updateContent($contentId, $versionNo, UpdateStruct $struct)
283
    {
284
        $this->logger->logCall(__METHOD__, ['content' => $contentId, 'version' => $versionNo, 'struct' => $struct]);
285
        $content = $this->persistenceHandler->contentHandler()->updateContent($contentId, $versionNo, $struct);
286
        $this->cache->invalidateTags(["content-{$contentId}-version-{$versionNo}"]);
287
288
        return $content;
289
    }
290
291
    /**
292
     * {@inheritdoc}
293
     */
294
    public function deleteContent($contentId)
295
    {
296
        $this->logger->logCall(__METHOD__, ['content' => $contentId]);
297
298
        // Load reverse field relations first
299
        $reverseRelations = $this->persistenceHandler->contentHandler()->loadReverseRelations(
300
            $contentId,
301
            APIRelation::FIELD | APIRelation::ASSET
302
        );
303
304
        $return = $this->persistenceHandler->contentHandler()->deleteContent($contentId);
305
306
        if (!empty($reverseRelations)) {
307
            $tags = \array_map(
308
                static function ($relation) {
309
                    // only the full content object *with* fields is affected by this
310
                    return 'content-fields-' . $relation->sourceContentId;
311
                },
312
                $reverseRelations
313
            );
314
        } else {
315
            $tags = [];
316
        }
317
        $tags[] = 'content-' . $contentId;
318
        $this->cache->invalidateTags($tags);
319
320
        return $return;
321
    }
322
323
    /**
324
     * {@inheritdoc}
325
     */
326
    public function deleteVersion($contentId, $versionNo)
327
    {
328
        $this->logger->logCall(__METHOD__, ['content' => $contentId, 'version' => $versionNo]);
329
        $return = $this->persistenceHandler->contentHandler()->deleteVersion($contentId, $versionNo);
330
        $this->cache->invalidateTags(["content-{$contentId}-version-{$versionNo}"]);
331
332
        return $return;
333
    }
334
335
    /**
336
     * {@inheritdoc}
337
     */
338
    public function listVersions($contentId, $status = null, $limit = -1)
339
    {
340
        $cacheItem = $this->cache->getItem("ez-content-${contentId}-version-list" . ($status ? "-byStatus-${status}" : '') . "-limit-{$limit}");
341
        if ($cacheItem->isHit()) {
342
            $this->logger->logCacheHit(['content' => $contentId, 'status' => $status]);
343
344
            return $cacheItem->get();
345
        }
346
347
        $this->logger->logCacheMiss(['content' => $contentId, 'status' => $status]);
348
        $versions = $this->persistenceHandler->contentHandler()->listVersions($contentId, $status, $limit);
349
        $cacheItem->set($versions);
350
        $tags = ["content-{$contentId}", "content-{$contentId}-version-list"];
351
        foreach ($versions as $version) {
352
            $tags = $this->getCacheTagsForVersion($version, $tags);
353
        }
354
        $cacheItem->tag($tags);
355
        $this->cache->save($cacheItem);
356
357
        return $versions;
358
    }
359
360
    /**
361
     * {@inheritdoc}
362
     */
363
    public function addRelation(RelationCreateStruct $relation)
364
    {
365
        $this->logger->logCall(__METHOD__, ['struct' => $relation]);
366
367
        return $this->persistenceHandler->contentHandler()->addRelation($relation);
368
    }
369
370
    /**
371
     * {@inheritdoc}
372
     */
373
    public function removeRelation($relationId, $type)
374
    {
375
        $this->logger->logCall(__METHOD__, ['relation' => $relationId, 'type' => $type]);
376
        $this->persistenceHandler->contentHandler()->removeRelation($relationId, $type);
377
    }
378
379
    /**
380
     * {@inheritdoc}
381
     */
382
    public function loadRelations($sourceContentId, $sourceContentVersionNo = null, $type = null)
383
    {
384
        $this->logger->logCall(
385
            __METHOD__,
386
            [
387
                'content' => $sourceContentId,
388
                'version' => $sourceContentVersionNo,
389
                'type' => $type,
390
            ]
391
        );
392
393
        return $this->persistenceHandler->contentHandler()->loadRelations($sourceContentId, $sourceContentVersionNo, $type);
394
    }
395
396
    /**
397
     * {@inheritdoc}
398
     */
399
    public function countReverseRelations(int $destinationContentId, ?int $type = null): int
400
    {
401
        $this->logger->logCall(__METHOD__, ['content' => $destinationContentId, 'type' => $type]);
402
403
        return $this->persistenceHandler->contentHandler()->countReverseRelations($destinationContentId, $type);
404
    }
405
406
    /**
407
     * {@inheritdoc}
408
     */
409
    public function loadReverseRelations($destinationContentId, $type = null)
410
    {
411
        $this->logger->logCall(__METHOD__, ['content' => $destinationContentId, 'type' => $type]);
412
413
        return $this->persistenceHandler->contentHandler()->loadReverseRelations($destinationContentId, $type);
414
    }
415
416
    /**
417
     * {@inheritdoc}
418
     */
419
    public function loadReverseRelationList(
420
        int $destinationContentId,
421
        int $offset = 0,
422
        int $limit = -1,
423
        ?int $type = null
424
    ): array {
425
        $this->logger->logCall(__METHOD__, [
426
            'content' => $destinationContentId,
427
            'offset' => $offset,
428
            'limit' => $limit,
429
            'type' => $type,
430
        ]);
431
432
        return $this->persistenceHandler->contentHandler()->loadReverseRelationList(
433
            $destinationContentId,
434
            $offset,
435
            $limit,
436
            $type
437
        );
438
    }
439
440
    /**
441
     * {@inheritdoc}
442
     */
443
    public function publish($contentId, $versionNo, MetadataUpdateStruct $struct)
444
    {
445
        $this->logger->logCall(__METHOD__, ['content' => $contentId, 'version' => $versionNo, 'struct' => $struct]);
446
        $content = $this->persistenceHandler->contentHandler()->publish($contentId, $versionNo, $struct);
447
        $this->cache->invalidateTags(['content-' . $contentId]);
448
449
        return $content;
450
    }
451
452
    /**
453
     * {@inheritdoc}
454
     */
455
    public function removeTranslationFromContent($contentId, $languageCode)
456
    {
457
        $this->deleteTranslationFromContent($contentId, $languageCode);
458
    }
459
460
    /**
461
     * {@inheritdoc}
462
     */
463
    public function deleteTranslationFromContent($contentId, $languageCode)
464
    {
465
        $this->logger->logCall(
466
            __METHOD__,
467
            [
468
                'contentId' => $contentId,
469
                'languageCode' => $languageCode,
470
            ]
471
        );
472
473
        $this->persistenceHandler->contentHandler()->deleteTranslationFromContent($contentId, $languageCode);
474
        $this->cache->invalidateTags(['content-' . $contentId]);
475
    }
476
477
    /**
478
     * {@inheritdoc}
479
     */
480
    public function deleteTranslationFromDraft($contentId, $versionNo, $languageCode)
481
    {
482
        $this->logger->logCall(
483
            __METHOD__,
484
            ['content' => $contentId, 'version' => $versionNo, 'languageCode' => $languageCode]
485
        );
486
        $content = $this->persistenceHandler->contentHandler()->deleteTranslationFromDraft(
487
            $contentId,
488
            $versionNo,
489
            $languageCode
490
        );
491
        $this->cache->invalidateTags(["content-{$contentId}-version-{$versionNo}"]);
492
493
        return $content;
494
    }
495
496
    /**
497
     * Return relevant content and location tags so cache can be purged reliably.
498
     *
499
     * For use when generating cache, not on invalidation.
500
     *
501
     * @param \eZ\Publish\SPI\Persistence\Content\VersionInfo $versionInfo
502
     * @param array $tags Optional, can be used to specify other tags.
503
     *
504
     * @return array
505
     */
506
    private function getCacheTagsForVersion(VersionInfo $versionInfo, array $tags = []): array
507
    {
508
        $contentInfo = $versionInfo->contentInfo;
509
        $tags[] = 'content-' . $contentInfo->id . '-version-' . $versionInfo->versionNo;
510
        $getContentInfoTagsFn = $this->getContentInfoTags;
511
512
        return $getContentInfoTagsFn($contentInfo, $tags);
513
    }
514
515
    private function getCacheTagsForContent(Content $content): array
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
516
    {
517
        $versionInfo = $content->versionInfo;
518
        $tags = [
519
            'content-fields-' . $versionInfo->contentInfo->id,
520
            'content-fields-type-' . $versionInfo->contentInfo->contentTypeId,
521
        ];
522
523
        return $this->getCacheTagsForVersion($versionInfo, $tags);
524
    }
525
}
526