-
Notifications
You must be signed in to change notification settings - Fork 13
/
ContentService.php
2714 lines (2409 loc) · 111 KB
/
ContentService.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);
namespace Ibexa\Core\Repository;
use function count;
use Exception;
use Ibexa\Contracts\Core\FieldType\Comparable;
use Ibexa\Contracts\Core\FieldType\FieldType;
use Ibexa\Contracts\Core\FieldType\Value;
use Ibexa\Contracts\Core\Limitation\Target;
use Ibexa\Contracts\Core\Limitation\Target\DestinationLocation as DestinationLocationTarget;
use Ibexa\Contracts\Core\Persistence\Content\ContentInfo as SPIContentInfo;
use Ibexa\Contracts\Core\Persistence\Content\CreateStruct as SPIContentCreateStruct;
use Ibexa\Contracts\Core\Persistence\Content\Field as SPIField;
use Ibexa\Contracts\Core\Persistence\Content\MetadataUpdateStruct as SPIMetadataUpdateStruct;
use Ibexa\Contracts\Core\Persistence\Content\Relation\CreateStruct as SPIRelationCreateStruct;
use Ibexa\Contracts\Core\Persistence\Content\UpdateStruct as SPIContentUpdateStruct;
use Ibexa\Contracts\Core\Persistence\Filter\Content\Handler as ContentFilteringHandler;
use Ibexa\Contracts\Core\Persistence\Handler;
use Ibexa\Contracts\Core\Repository\ContentService as ContentServiceInterface;
use Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException as APINotFoundException;
use Ibexa\Contracts\Core\Repository\NameSchema\NameSchemaServiceInterface;
use Ibexa\Contracts\Core\Repository\PermissionService;
use Ibexa\Contracts\Core\Repository\Repository as RepositoryInterface;
use Ibexa\Contracts\Core\Repository\Validator\ContentValidator;
use Ibexa\Contracts\Core\Repository\Values\Content\Content as APIContent;
use Ibexa\Contracts\Core\Repository\Values\Content\ContentCreateStruct as APIContentCreateStruct;
use Ibexa\Contracts\Core\Repository\Values\Content\ContentDraftList;
use Ibexa\Contracts\Core\Repository\Values\Content\ContentInfo;
use Ibexa\Contracts\Core\Repository\Values\Content\ContentList;
use Ibexa\Contracts\Core\Repository\Values\Content\ContentMetadataUpdateStruct;
use Ibexa\Contracts\Core\Repository\Values\Content\ContentUpdateStruct as APIContentUpdateStruct;
use Ibexa\Contracts\Core\Repository\Values\Content\DraftList\Item\ContentDraftListItem;
use Ibexa\Contracts\Core\Repository\Values\Content\DraftList\Item\UnauthorizedContentDraftListItem;
use Ibexa\Contracts\Core\Repository\Values\Content\Language;
use Ibexa\Contracts\Core\Repository\Values\Content\LocationCreateStruct;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\LanguageCode;
use Ibexa\Contracts\Core\Repository\Values\Content\Relation as APIRelation;
use Ibexa\Contracts\Core\Repository\Values\Content\RelationList;
use Ibexa\Contracts\Core\Repository\Values\Content\RelationList\Item\RelationListItem;
use Ibexa\Contracts\Core\Repository\Values\Content\RelationList\Item\UnauthorizedRelationListItem;
use Ibexa\Contracts\Core\Repository\Values\Content\VersionInfo as APIVersionInfo;
use Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType;
use Ibexa\Contracts\Core\Repository\Values\Filter\Filter;
use Ibexa\Contracts\Core\Repository\Values\Filter\FilteringCriterion;
use Ibexa\Contracts\Core\Repository\Values\User\User;
use Ibexa\Contracts\Core\Repository\Values\User\UserReference;
use Ibexa\Contracts\Core\Repository\Values\ValueObject;
use Ibexa\Core\Base\Exceptions\BadStateException;
use Ibexa\Core\Base\Exceptions\ContentFieldValidationException;
use Ibexa\Core\Base\Exceptions\InvalidArgumentException;
use Ibexa\Core\Base\Exceptions\NotFoundException;
use Ibexa\Core\Base\Exceptions\UnauthorizedException;
use Ibexa\Core\FieldType\FieldTypeRegistry;
use Ibexa\Core\Repository\Mapper\ContentDomainMapper;
use Ibexa\Core\Repository\Mapper\ContentMapper;
use Ibexa\Core\Repository\Values\Content\Content;
use Ibexa\Core\Repository\Values\Content\ContentCreateStruct;
use Ibexa\Core\Repository\Values\Content\ContentUpdateStruct;
use Ibexa\Core\Repository\Values\Content\Location;
use Ibexa\Core\Repository\Values\Content\VersionInfo;
use function sprintf;
/**
* This class provides service methods for managing content.
*/
class ContentService implements ContentServiceInterface
{
/** @var \Ibexa\Core\Repository\Repository */
protected $repository;
/** @var \Ibexa\Contracts\Core\Persistence\Handler */
protected $persistenceHandler;
/** @var array */
protected $settings;
/** @var \Ibexa\Core\Repository\Mapper\ContentDomainMapper */
protected $contentDomainMapper;
/** @var \Ibexa\Core\Repository\Helper\RelationProcessor */
protected $relationProcessor;
protected NameSchemaServiceInterface $nameSchemaService;
/** @var \Ibexa\Core\FieldType\FieldTypeRegistry */
protected $fieldTypeRegistry;
/** @var \Ibexa\Contracts\Core\Repository\PermissionResolver */
private $permissionResolver;
/** @var \Ibexa\Core\Repository\Mapper\ContentMapper */
private $contentMapper;
/** @var \Ibexa\Contracts\Core\Repository\Validator\ContentValidator */
private $contentValidator;
/** @var \Ibexa\Contracts\Core\Persistence\Filter\Content\Handler */
private $contentFilteringHandler;
public function __construct(
RepositoryInterface $repository,
Handler $handler,
ContentDomainMapper $contentDomainMapper,
Helper\RelationProcessor $relationProcessor,
NameSchemaServiceInterface $nameSchemaService,
FieldTypeRegistry $fieldTypeRegistry,
PermissionService $permissionService,
ContentMapper $contentMapper,
ContentValidator $contentValidator,
ContentFilteringHandler $contentFilteringHandler,
array $settings = []
) {
$this->repository = $repository;
$this->persistenceHandler = $handler;
$this->contentDomainMapper = $contentDomainMapper;
$this->relationProcessor = $relationProcessor;
$this->nameSchemaService = $nameSchemaService;
$this->fieldTypeRegistry = $fieldTypeRegistry;
// Union makes sure default settings are ignored if provided in argument
$this->settings = $settings + [
// Version archive limit (0-50), only enforced on publish, not on un-publish.
'default_version_archive_limit' => 5,
'remove_archived_versions_on_publish' => true,
];
$this->contentFilteringHandler = $contentFilteringHandler;
$this->permissionResolver = $permissionService;
$this->contentMapper = $contentMapper;
$this->contentValidator = $contentValidator;
}
/**
* Loads a content info object.
*
* To load fields use loadContent
*
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException if the user is not allowed to read the content
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException - if the content with the given id does not exist
*
* @param int $contentId
*
* @return \Ibexa\Contracts\Core\Repository\Values\Content\ContentInfo
*/
public function loadContentInfo(int $contentId): ContentInfo
{
$contentInfo = $this->internalLoadContentInfoById($contentId);
if (!$this->permissionResolver->canUser('content', 'read', $contentInfo)) {
throw new UnauthorizedException('content', 'read', ['contentId' => $contentId]);
}
return $contentInfo;
}
/**
* {@inheritdoc}
*/
public function loadContentInfoList(array $contentIds): iterable
{
$contentInfoList = [];
$spiInfoList = $this->persistenceHandler->contentHandler()->loadContentInfoList($contentIds);
foreach ($spiInfoList as $id => $spiInfo) {
$contentInfo = $this->contentDomainMapper->buildContentInfoDomainObject($spiInfo);
if ($this->permissionResolver->canUser('content', 'read', $contentInfo)) {
$contentInfoList[$id] = $contentInfo;
}
}
return $contentInfoList;
}
/**
* Loads a content info object.
*
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException - if the content with the given id does not exist
*
* @param int $id
*
* @return \Ibexa\Contracts\Core\Repository\Values\Content\ContentInfo
*/
public function internalLoadContentInfoById(int $id): ContentInfo
{
try {
return $this->contentDomainMapper->buildContentInfoDomainObject(
$this->persistenceHandler->contentHandler()->loadContentInfo($id)
);
} catch (APINotFoundException $e) {
throw new NotFoundException('Content', $id, $e);
}
}
/**
* Loads a content info object by remote id.
*
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException - if the content with the given id does not exist
*
* @param string $remoteId
*
* @return \Ibexa\Contracts\Core\Repository\Values\Content\ContentInfo
*/
public function internalLoadContentInfoByRemoteId(string $remoteId): ContentInfo
{
try {
return $this->contentDomainMapper->buildContentInfoDomainObject(
$this->persistenceHandler->contentHandler()->loadContentInfoByRemoteId($remoteId)
);
} catch (APINotFoundException $e) {
throw new NotFoundException('Content', $remoteId, $e);
}
}
/**
* Loads a content info object for the given remoteId.
*
* To load fields use loadContent
*
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException if the user is not allowed to read the content
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException - if the content with the given remote id does not exist
*
* @param string $remoteId
*
* @return \Ibexa\Contracts\Core\Repository\Values\Content\ContentInfo
*/
public function loadContentInfoByRemoteId(string $remoteId): ContentInfo
{
$contentInfo = $this->internalLoadContentInfoByRemoteId($remoteId);
if (!$this->permissionResolver->canUser('content', 'read', $contentInfo)) {
throw new UnauthorizedException('content', 'read', ['remoteId' => $remoteId]);
}
return $contentInfo;
}
/**
* Loads a version info of the given content object.
*
* If no version number is given, the method returns the current version
*
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException - if the version with the given number does not exist
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException if the user is not allowed to load this version
*
* @param \Ibexa\Contracts\Core\Repository\Values\Content\ContentInfo $contentInfo
* @param int|null $versionNo the version number. If not given the current version is returned.
*
* @return \Ibexa\Contracts\Core\Repository\Values\Content\VersionInfo
*/
public function loadVersionInfo(ContentInfo $contentInfo, ?int $versionNo = null): APIVersionInfo
{
return $this->loadVersionInfoById($contentInfo->getId(), $versionNo);
}
/**
* Loads a version info of the given content object id.
*
* If no version number is given, the method returns the current version
*
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException - if the version with the given number does not exist
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException if the user is not allowed to load this version
*
* @param int $contentId
* @param int|null $versionNo the version number. If not given the current version is returned.
*
* @return \Ibexa\Contracts\Core\Repository\Values\Content\VersionInfo
*/
public function loadVersionInfoById(int $contentId, ?int $versionNo = null): APIVersionInfo
{
try {
$spiVersionInfo = $this->persistenceHandler->contentHandler()->loadVersionInfo(
$contentId,
$versionNo
);
} catch (APINotFoundException $e) {
throw new NotFoundException(
'VersionInfo',
[
'contentId' => $contentId,
'versionNo' => $versionNo,
],
$e
);
}
$versionInfo = $this->contentDomainMapper->buildVersionInfoDomainObject($spiVersionInfo);
if ($versionInfo->isPublished()) {
$function = 'read';
} else {
$function = 'versionread';
}
if (!$this->permissionResolver->canUser('content', $function, $versionInfo)) {
throw new UnauthorizedException('content', $function, ['contentId' => $contentId]);
}
return $versionInfo;
}
public function loadVersionInfoListByContentInfo(array $contentInfoList): array
{
foreach ($contentInfoList as $idx => $contentInfo) {
if (!$contentInfo instanceof ContentInfo) {
throw new InvalidArgumentException(
'$contentInfoList',
sprintf(
'Element at position %d is not an instance of %s',
$idx,
ContentInfo::class
)
);
}
}
$contentIds = array_map(
static function (ContentInfo $contentInfo): int {
return $contentInfo->getId();
},
$contentInfoList
);
$persistenceVersionInfos = $this->persistenceHandler
->contentHandler()
->loadVersionInfoList($contentIds);
$versionInfoList = [];
foreach ($persistenceVersionInfos as $persistenceVersionInfo) {
$versionInfo = $this->contentDomainMapper->buildVersionInfoDomainObject($persistenceVersionInfo);
if ($this->permissionResolver->canUser('content', 'read', $versionInfo)) {
$versionInfoList[$versionInfo->getContentInfo()->getId()] = $versionInfo;
}
}
return $versionInfoList;
}
/**
* {@inheritdoc}
*/
public function loadContentByContentInfo(ContentInfo $contentInfo, array $languages = null, ?int $versionNo = null, bool $useAlwaysAvailable = true): APIContent
{
// Change $useAlwaysAvailable to false to avoid contentInfo lookup if we know alwaysAvailable is disabled
if ($useAlwaysAvailable && !$contentInfo->alwaysAvailable) {
$useAlwaysAvailable = false;
}
return $this->loadContent(
$contentInfo->id,
$languages,
$versionNo,// On purpose pass as-is and not use $contentInfo, to make sure to return actual current version on null
$useAlwaysAvailable
);
}
/**
* {@inheritdoc}
*/
public function loadContentByVersionInfo(APIVersionInfo $versionInfo, array $languages = null, bool $useAlwaysAvailable = true): APIContent
{
// Change $useAlwaysAvailable to false to avoid contentInfo lookup if we know alwaysAvailable is disabled
if ($useAlwaysAvailable && !$versionInfo->getContentInfo()->alwaysAvailable) {
$useAlwaysAvailable = false;
}
return $this->loadContent(
$versionInfo->getContentInfo()->id,
$languages,
$versionInfo->versionNo,
$useAlwaysAvailable
);
}
/**
* {@inheritdoc}
*/
public function loadContent(int $contentId, array $languages = null, ?int $versionNo = null, bool $useAlwaysAvailable = true): APIContent
{
$content = $this->internalLoadContentById($contentId, $languages, $versionNo, $useAlwaysAvailable);
if (!$this->permissionResolver->canUser('content', 'read', $content)) {
throw new UnauthorizedException('content', 'read', ['contentId' => $contentId]);
}
if (
!$content->getVersionInfo()->isPublished()
&& !$this->permissionResolver->canUser('content', 'versionread', $content)
) {
throw new UnauthorizedException('content', 'versionread', ['contentId' => $contentId, 'versionNo' => $versionNo]);
}
return $content;
}
public function internalLoadContentById(
int $id,
?array $languages = null,
int $versionNo = null,
bool $useAlwaysAvailable = true
): APIContent {
try {
$spiContentInfo = $this->persistenceHandler->contentHandler()->loadContentInfo($id);
return $this->internalLoadContentBySPIContentInfo(
$spiContentInfo,
$languages,
$versionNo,
$useAlwaysAvailable
);
} catch (APINotFoundException $e) {
throw new NotFoundException(
'Content',
[
'id' => $id,
'languages' => $languages,
'versionNo' => $versionNo,
],
$e
);
}
}
public function internalLoadContentByRemoteId(
string $remoteId,
array $languages = null,
int $versionNo = null,
bool $useAlwaysAvailable = true
): APIContent {
try {
$spiContentInfo = $this->persistenceHandler->contentHandler()->loadContentInfoByRemoteId($remoteId);
return $this->internalLoadContentBySPIContentInfo(
$spiContentInfo,
$languages,
$versionNo,
$useAlwaysAvailable
);
} catch (APINotFoundException $e) {
throw new NotFoundException(
'Content',
[
'remoteId' => $remoteId,
'languages' => $languages,
'versionNo' => $versionNo,
],
$e
);
}
}
private function internalLoadContentBySPIContentInfo(SPIContentInfo $spiContentInfo, array $languages = null, int $versionNo = null, bool $useAlwaysAvailable = true): APIContent
{
$loadLanguages = $languages;
$alwaysAvailableLanguageCode = null;
// Set main language on $languages filter if not empty (all) and $useAlwaysAvailable being true
// @todo Move use always available logic to SPI load methods, like done in location handler in 7.x
if (!empty($loadLanguages) && $useAlwaysAvailable && $spiContentInfo->alwaysAvailable) {
$loadLanguages[] = $alwaysAvailableLanguageCode = $spiContentInfo->mainLanguageCode;
$loadLanguages = array_unique($loadLanguages);
}
$spiContent = $this->persistenceHandler->contentHandler()->load(
$spiContentInfo->id,
$versionNo,
$loadLanguages
);
if ($languages === null) {
$languages = [];
}
return $this->contentDomainMapper->buildContentDomainObject(
$spiContent,
$this->repository->getContentTypeService()->loadContentType(
$spiContent->versionInfo->contentInfo->contentTypeId,
$languages
),
$languages,
$alwaysAvailableLanguageCode
);
}
/**
* Loads content in a version for the content object reference by the given remote id.
*
* If no version is given, the method returns the current version
*
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException - if the content or version with the given remote id does not exist
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException If the user has no access to read content and in case of un-published content: read versions
*
* @param string $remoteId
* @param array $languages A language filter for fields. If not given all languages are returned
* @param int $versionNo the version number. If not given the current version is returned
* @param bool $useAlwaysAvailable Add Main language to \$languages if true (default) and if alwaysAvailable is true
*
* @return \Ibexa\Contracts\Core\Repository\Values\Content\Content
*/
public function loadContentByRemoteId(string $remoteId, array $languages = null, ?int $versionNo = null, bool $useAlwaysAvailable = true): APIContent
{
$content = $this->internalLoadContentByRemoteId($remoteId, $languages, $versionNo, $useAlwaysAvailable);
if (!$this->permissionResolver->canUser('content', 'read', $content)) {
throw new UnauthorizedException('content', 'read', ['remoteId' => $remoteId]);
}
if (
!$content->getVersionInfo()->isPublished()
&& !$this->permissionResolver->canUser('content', 'versionread', $content)
) {
throw new UnauthorizedException('content', 'versionread', ['remoteId' => $remoteId, 'versionNo' => $versionNo]);
}
return $content;
}
/**
* Bulk-load Content items by the list of ContentInfo Value Objects.
*
* Note: it does not throw exceptions on load, just ignores erroneous Content item.
* Moreover, since the method works on pre-loaded ContentInfo list, it is assumed that user is
* allowed to access every Content on the list.
*
* @param \Ibexa\Contracts\Core\Repository\Values\Content\ContentInfo[] $contentInfoList
* @param string[] $languages A language priority, filters returned fields and is used as prioritized language code on
* returned value object. If not given all languages are returned.
* @param bool $useAlwaysAvailable Add Main language to \$languages if true (default) and if alwaysAvailable is true,
* unless all languages have been asked for.
*
* @return \Ibexa\Contracts\Core\Repository\Values\Content\Content[] list of Content items with Content Ids as keys
*/
public function loadContentListByContentInfo(
array $contentInfoList,
array $languages = [],
bool $useAlwaysAvailable = true
): iterable {
$loadAllLanguages = $languages === Language::ALL;
$contentIds = [];
$contentTypeIds = [];
$translations = $languages;
foreach ($contentInfoList as $contentInfo) {
$contentIds[] = $contentInfo->id;
$contentTypeIds[] = $contentInfo->contentTypeId;
// Unless we are told to load all languages, we add main language to translations so they are loaded too
// Might in some case load more languages then intended, but prioritised handling will pick right one
if (!$loadAllLanguages && $useAlwaysAvailable && $contentInfo->alwaysAvailable) {
$translations[] = $contentInfo->mainLanguageCode;
}
}
$contentList = [];
$translations = array_unique($translations);
$spiContentList = $this->persistenceHandler->contentHandler()->loadContentList(
$contentIds,
$translations
);
$contentTypeList = $this->repository->getContentTypeService()->loadContentTypeList(
array_unique($contentTypeIds),
$languages
);
foreach ($spiContentList as $contentId => $spiContent) {
$contentInfo = $spiContent->versionInfo->contentInfo;
$contentList[$contentId] = $this->contentDomainMapper->buildContentDomainObject(
$spiContent,
$contentTypeList[$contentInfo->contentTypeId],
$languages,
$contentInfo->alwaysAvailable ? $contentInfo->mainLanguageCode : null
);
}
return $contentList;
}
/**
* Creates a new content draft assigned to the authenticated user.
*
* If a different userId is given in $contentCreateStruct it is assigned to the given user
* but this required special rights for the authenticated user
* (this is useful for content staging where the transfer process does not
* have to authenticate with the user which created the content object in the source server).
* The user has to publish the draft if it should be visible.
* In 4.x at least one location has to be provided in the location creation array.
*
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException if the user is not allowed to create the content in the given location
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException if the provided remoteId exists in the system, required properties on
* struct are missing or invalid, or if multiple locations are under the
* same parent.
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\ContentFieldValidationException if a field in the $contentCreateStruct is not valid,
* or if a required field is missing / set to an empty value.
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\ContentValidationException If field definition does not exist in the ContentType,
* or value is set for non-translatable field in language
* other than main.
*
* @param \Ibexa\Contracts\Core\Repository\Values\Content\ContentCreateStruct $contentCreateStruct
* @param \Ibexa\Contracts\Core\Repository\Values\Content\LocationCreateStruct[] $locationCreateStructs For each location parent under which a location should be created for the content
*
* @return \Ibexa\Contracts\Core\Repository\Values\Content\Content - the newly created content draft
*/
public function createContent(APIContentCreateStruct $contentCreateStruct, array $locationCreateStructs = [], ?array $fieldIdentifiersToValidate = null): APIContent
{
if ($contentCreateStruct->mainLanguageCode === null) {
throw new InvalidArgumentException('$contentCreateStruct', "the 'mainLanguageCode' property must be set");
}
if ($contentCreateStruct->contentType === null) {
throw new InvalidArgumentException('$contentCreateStruct', "the 'contentType' property must be set");
}
$contentCreateStruct = clone $contentCreateStruct;
if ($contentCreateStruct->ownerId === null) {
$contentCreateStruct->ownerId = $this->permissionResolver->getCurrentUserReference()->getUserId();
}
if ($contentCreateStruct->alwaysAvailable === null) {
$contentCreateStruct->alwaysAvailable = $contentCreateStruct->contentType->defaultAlwaysAvailable ?: false;
}
$contentCreateStruct->contentType = $this->repository->getContentTypeService()->loadContentType(
$contentCreateStruct->contentType->id
);
$contentCreateStruct->fields = $this->contentMapper->getFieldsForCreate(
$contentCreateStruct->fields,
$contentCreateStruct->contentType
);
if (empty($contentCreateStruct->sectionId)) {
if (isset($locationCreateStructs[0])) {
$location = $this->repository->getLocationService()->loadLocation(
$locationCreateStructs[0]->parentLocationId
);
$contentCreateStruct->sectionId = $location->getContentInfo()->getSectionId();
} else {
$contentCreateStruct->sectionId = 1;
}
}
if (!$this->permissionResolver->canUser('content', 'create', $contentCreateStruct, $locationCreateStructs)) {
throw new UnauthorizedException(
'content',
'create',
[
'parentLocationId' => isset($locationCreateStructs[0]) ?
$locationCreateStructs[0]->parentLocationId :
null,
'sectionId' => $contentCreateStruct->sectionId,
]
);
}
if (!empty($contentCreateStruct->remoteId)) {
try {
$this->loadContentByRemoteId($contentCreateStruct->remoteId);
throw new InvalidArgumentException(
'$contentCreateStruct',
"Another Content item with remoteId '{$contentCreateStruct->remoteId}' exists"
);
} catch (APINotFoundException $e) {
// Do nothing
}
} else {
$contentCreateStruct->remoteId = $this->contentDomainMapper->getUniqueHash($contentCreateStruct);
}
$errors = $this->validate(
$contentCreateStruct,
[],
$fieldIdentifiersToValidate
);
if (!empty($errors)) {
throw new ContentFieldValidationException($errors);
}
$spiLocationCreateStructs = $this->buildSPILocationCreateStructs(
$locationCreateStructs,
$contentCreateStruct->contentType
);
$languageCodes = $this->contentMapper->getLanguageCodesForCreate($contentCreateStruct);
$fields = $this->contentMapper->mapFieldsForCreate($contentCreateStruct);
$fieldValues = [];
$spiFields = [];
$inputRelations = [];
$locationIdToContentIdMapping = [];
foreach ($contentCreateStruct->contentType->getFieldDefinitions() as $fieldDefinition) {
/** @var $fieldType \Ibexa\Core\FieldType\FieldType */
$fieldType = $this->fieldTypeRegistry->getFieldType(
$fieldDefinition->fieldTypeIdentifier
);
foreach ($languageCodes as $languageCode) {
$isEmptyValue = false;
$valueLanguageCode = $fieldDefinition->isTranslatable ? $languageCode : $contentCreateStruct->mainLanguageCode;
$isLanguageMain = $languageCode === $contentCreateStruct->mainLanguageCode;
$fieldValue = $this->contentMapper->getFieldValueForCreate(
$fieldDefinition,
$fields[$fieldDefinition->identifier][$valueLanguageCode] ?? null
);
if ($fieldType->isEmptyValue($fieldValue)) {
$isEmptyValue = true;
}
$this->relationProcessor->appendFieldRelations(
$inputRelations,
$locationIdToContentIdMapping,
$fieldType,
$fieldValue,
$fieldDefinition->id
);
$fieldValues[$fieldDefinition->identifier][$languageCode] = $fieldValue;
// Only non-empty value for: translatable field or in main language
if (
(!$isEmptyValue && $fieldDefinition->isTranslatable) ||
(!$isEmptyValue && $isLanguageMain)
) {
$spiFields[] = new SPIField(
[
'id' => null,
'fieldDefinitionId' => $fieldDefinition->id,
'type' => $fieldDefinition->fieldTypeIdentifier,
'value' => $fieldType->toPersistenceValue($fieldValue),
'languageCode' => $languageCode,
'versionNo' => null,
]
);
}
}
}
$spiContentCreateStruct = new SPIContentCreateStruct(
[
'name' => $this->nameSchemaService->resolveNameSchema(
$contentCreateStruct->contentType->nameSchema,
$contentCreateStruct->contentType,
$fieldValues,
$languageCodes
),
'typeId' => $contentCreateStruct->contentType->id,
'sectionId' => $contentCreateStruct->sectionId,
'ownerId' => $contentCreateStruct->ownerId,
'locations' => $spiLocationCreateStructs,
'fields' => $spiFields,
'alwaysAvailable' => $contentCreateStruct->alwaysAvailable,
'remoteId' => $contentCreateStruct->remoteId,
'modified' => isset($contentCreateStruct->modificationDate) ? $contentCreateStruct->modificationDate->getTimestamp() : time(),
'initialLanguageId' => $this->persistenceHandler->contentLanguageHandler()->loadByLanguageCode(
$contentCreateStruct->mainLanguageCode
)->id,
]
);
$defaultObjectStates = $this->getDefaultObjectStates();
$this->repository->beginTransaction();
try {
$spiContent = $this->persistenceHandler->contentHandler()->create($spiContentCreateStruct);
$this->relationProcessor->processFieldRelations(
$inputRelations,
$spiContent->versionInfo->contentInfo->id,
$spiContent->versionInfo->versionNo,
$contentCreateStruct->contentType
);
$objectStateHandler = $this->persistenceHandler->objectStateHandler();
foreach ($defaultObjectStates as $objectStateGroupId => $objectState) {
$objectStateHandler->setContentState(
$spiContent->versionInfo->contentInfo->id,
$objectStateGroupId,
$objectState->id
);
}
$this->repository->commit();
} catch (Exception $e) {
$this->repository->rollback();
throw $e;
}
return $this->contentDomainMapper->buildContentDomainObject(
$spiContent,
$contentCreateStruct->contentType
);
}
/**
* Returns an array of default content states with content state group id as key.
*
* @return \Ibexa\Contracts\Core\Persistence\Content\ObjectState[]
*/
protected function getDefaultObjectStates(): array
{
$defaultObjectStatesMap = [];
$objectStateHandler = $this->persistenceHandler->objectStateHandler();
foreach ($objectStateHandler->loadAllGroups() as $objectStateGroup) {
foreach ($objectStateHandler->loadObjectStates($objectStateGroup->id) as $objectState) {
// Only register the first object state which is the default one.
$defaultObjectStatesMap[$objectStateGroup->id] = $objectState;
break;
}
}
return $defaultObjectStatesMap;
}
/**
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException
*
* @param \Ibexa\Contracts\Core\Repository\Values\Content\LocationCreateStruct[] $locationCreateStructs
* @param \Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType|null $contentType
*
* @return \Ibexa\Contracts\Core\Persistence\Content\Location\CreateStruct[]
*/
protected function buildSPILocationCreateStructs(
array $locationCreateStructs,
?ContentType $contentType = null
): array {
$spiLocationCreateStructs = [];
$parentLocationIdSet = [];
$mainLocation = true;
foreach ($locationCreateStructs as $locationCreateStruct) {
if (isset($parentLocationIdSet[$locationCreateStruct->parentLocationId])) {
throw new InvalidArgumentException(
'$locationCreateStructs',
"You provided multiple LocationCreateStructs with the same parent Location '{$locationCreateStruct->parentLocationId}'"
);
}
if ($locationCreateStruct->sortField === null) {
$locationCreateStruct->sortField = $contentType->defaultSortField ?? Location::SORT_FIELD_NAME;
}
if ($locationCreateStruct->sortOrder === null) {
$locationCreateStruct->sortOrder = $contentType->defaultSortOrder ?? Location::SORT_ORDER_ASC;
}
$parentLocationIdSet[$locationCreateStruct->parentLocationId] = true;
$parentLocation = $this->repository->getLocationService()->loadLocation(
$locationCreateStruct->parentLocationId
);
$spiLocationCreateStructs[] = $this->contentDomainMapper->buildSPILocationCreateStruct(
$locationCreateStruct,
$parentLocation,
$mainLocation,
// For Content draft contentId and contentVersionNo are set in ContentHandler upon draft creation
null,
null,
false
);
// First Location in the list will be created as main Location
$mainLocation = false;
}
return $spiLocationCreateStructs;
}
/**
* Updates the metadata.
*
* (see {@link ContentMetadataUpdateStruct}) of a content object - to update fields use updateContent
*
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException if the user is not allowed to update the content meta data
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException if the remoteId in $contentMetadataUpdateStruct is set but already exists
*
* @param \Ibexa\Contracts\Core\Repository\Values\Content\ContentInfo $contentInfo
* @param \Ibexa\Contracts\Core\Repository\Values\Content\ContentMetadataUpdateStruct $contentMetadataUpdateStruct
*
* @return \Ibexa\Contracts\Core\Repository\Values\Content\Content the content with the updated attributes
*/
public function updateContentMetadata(ContentInfo $contentInfo, ContentMetadataUpdateStruct $contentMetadataUpdateStruct): APIContent
{
$propertyCount = 0;
foreach ($contentMetadataUpdateStruct as $propertyName => $propertyValue) {
if (isset($contentMetadataUpdateStruct->$propertyName)) {
++$propertyCount;
}
}
if ($propertyCount === 0) {
throw new InvalidArgumentException(
'$contentMetadataUpdateStruct',
'At least one property must be set'
);
}
$loadedContentInfo = $this->loadContentInfo($contentInfo->id);
if (!$this->permissionResolver->canUser('content', 'edit', $loadedContentInfo)) {
throw new UnauthorizedException('content', 'edit', ['contentId' => $loadedContentInfo->id]);
}
if (isset($contentMetadataUpdateStruct->remoteId)) {
try {
$existingContentInfo = $this->loadContentInfoByRemoteId($contentMetadataUpdateStruct->remoteId);
if ($existingContentInfo->id !== $loadedContentInfo->id) {
throw new InvalidArgumentException(
'$contentMetadataUpdateStruct',
"Another Content item with remoteId '{$contentMetadataUpdateStruct->remoteId}' exists"
);
}
} catch (APINotFoundException $e) {
// Do nothing
}
}
$this->repository->beginTransaction();
try {
if ($propertyCount > 1 || !isset($contentMetadataUpdateStruct->mainLocationId)) {
$this->persistenceHandler->contentHandler()->updateMetadata(
$loadedContentInfo->id,
new SPIMetadataUpdateStruct(
[
'ownerId' => $contentMetadataUpdateStruct->ownerId,
'publicationDate' => isset($contentMetadataUpdateStruct->publishedDate) ?
$contentMetadataUpdateStruct->publishedDate->getTimestamp() :
null,
'modificationDate' => isset($contentMetadataUpdateStruct->modificationDate) ?
$contentMetadataUpdateStruct->modificationDate->getTimestamp() :
null,
'mainLanguageId' => isset($contentMetadataUpdateStruct->mainLanguageCode) ?
$this->repository->getContentLanguageService()->loadLanguage(
$contentMetadataUpdateStruct->mainLanguageCode
)->id :
null,
'alwaysAvailable' => $contentMetadataUpdateStruct->alwaysAvailable,
'remoteId' => $contentMetadataUpdateStruct->remoteId,
'name' => $contentMetadataUpdateStruct->name,
]
)
);
}
// Change main location
if (isset($contentMetadataUpdateStruct->mainLocationId)
&& $loadedContentInfo->mainLocationId !== $contentMetadataUpdateStruct->mainLocationId) {
$this->persistenceHandler->locationHandler()->changeMainLocation(
$loadedContentInfo->id,
$contentMetadataUpdateStruct->mainLocationId
);
}
// Republish URL aliases to update always-available flag
if (isset($contentMetadataUpdateStruct->alwaysAvailable)
&& $loadedContentInfo->alwaysAvailable !== $contentMetadataUpdateStruct->alwaysAvailable) {
$content = $this->loadContent($loadedContentInfo->id);
$this->publishUrlAliasesForContent($content, false);
}
$this->repository->commit();
} catch (Exception $e) {
$this->repository->rollback();
throw $e;
}
return isset($content) ? $content : $this->loadContent($loadedContentInfo->id);
}
/**
* Publishes URL aliases for all locations of a given content.
*
* @param \Ibexa\Contracts\Core\Repository\Values\Content\Content $content
* @param bool $updatePathIdentificationString this parameter is legacy storage specific for updating
* ezcontentobject_tree.path_identification_string, it is ignored by other storage engines
*/
protected function publishUrlAliasesForContent(APIContent $content, bool $updatePathIdentificationString = true): void
{
$urlAliasNames = $this->nameSchemaService->resolveUrlAliasSchema($content);
$locations = $this->repository->getLocationService()->loadLocations(
$content->getVersionInfo()->getContentInfo()
);
$urlAliasHandler = $this->persistenceHandler->urlAliasHandler();
foreach ($locations as $location) {
foreach ($urlAliasNames as $languageCode => $name) {
$urlAliasHandler->publishUrlAliasForLocation(
$location->id,
$location->parentLocationId,
$name,
$languageCode,
$content->contentInfo->alwaysAvailable,
$updatePathIdentificationString ? $languageCode === $content->contentInfo->mainLanguageCode : false
);
}
// archive URL aliases of Translations that got deleted
$urlAliasHandler->archiveUrlAliasesForDeletedTranslations(
$location->id,
$location->parentLocationId,
$content->versionInfo->languageCodes
);