-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathViewContentMetaDataCommand.php
130 lines (106 loc) · 5.37 KB
/
ViewContentMetaDataCommand.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
<?php declare(strict_types=1);
namespace App\Command;
use Ibexa\Contracts\Core\Repository\ContentService;
use Ibexa\Contracts\Core\Repository\Iterator\BatchIterator;
use Ibexa\Contracts\Core\Repository\Iterator\BatchIteratorAdapter\RelationListIteratorAdapter;
use Ibexa\Contracts\Core\Repository\LocationService;
use Ibexa\Contracts\Core\Repository\ObjectStateService;
use Ibexa\Contracts\Core\Repository\PermissionResolver;
use Ibexa\Contracts\Core\Repository\URLAliasService;
use Ibexa\Contracts\Core\Repository\UserService;
use Ibexa\Contracts\Core\Repository\Values\Content\VersionInfo;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class ViewContentMetaDataCommand extends Command
{
private ContentService $contentService;
private LocationService $locationService;
private URLAliasService $urlAliasService;
private UserService $userService;
private ObjectStateService $objectStateService;
private PermissionResolver $permissionResolver;
public function __construct(ContentService $contentService, LocationService $locationService, URLAliasService $urlAliasService, UserService $userService, ObjectStateService $objectStateService, PermissionResolver $permissionResolver)
{
$this->contentService = $contentService;
$this->locationService = $locationService;
$this->urlAliasService = $urlAliasService;
$this->userService = $userService;
$this->objectStateService = $objectStateService;
$this->permissionResolver = $permissionResolver;
parent::__construct('doc:view_metadata');
}
protected function configure(): void
{
$this
->setDescription('Output various metadata about a content item.')
->setDefinition([
new InputArgument('contentId', InputArgument::REQUIRED, 'An existing content ID'),
]);
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$user = $this->userService->loadUserByLogin('admin');
$this->permissionResolver->setCurrentUserReference($user);
$contentId = (int) $input->getArgument('contentId');
// Metadata
$contentInfo = $this->contentService->loadContentInfo($contentId);
$output->writeln("Name: $contentInfo->name");
$output->writeln('Last modified: ' . $contentInfo->modificationDate->format('Y-m-d'));
$output->writeln('Published: ' . $contentInfo->publishedDate->format('Y-m-d'));
$output->writeln("RemoteId: $contentInfo->remoteId");
$output->writeln("Main Language: $contentInfo->mainLanguageCode");
$output->writeln('Always available: ' . ($contentInfo->alwaysAvailable ? 'Yes' : 'No'));
// Locations
$locations = $this->locationService->loadLocations($contentInfo);
foreach ($locations as $location) {
$output->writeln('Location: ' . $location->pathString);
$urlAlias = $this->urlAliasService->reverseLookup($location);
$output->writeln('URL alias: ' . $urlAlias->path);
}
// Content type
$content = $this->contentService->loadContent($contentId);
$output->writeln('Content type: ' . $content->getContentType()->getName());
// Versions
$versionInfos = $this->contentService->loadVersions($contentInfo);
foreach ($versionInfos as $versionInfo) {
$output->write("Version $versionInfo->versionNo");
$output->write(' by ' . $versionInfo->getCreator()->getName());
$output->writeln(' in ' . $versionInfo->getInitialLanguage()->name);
}
$versionInfoArray = iterator_to_array($this->contentService->loadVersions($contentInfo, VersionInfo::STATUS_ARCHIVED));
if (count($versionInfoArray)) {
$output->writeln('Archived versions:');
foreach ($versionInfoArray as $versionInfo) {
$creator = $this->userService->loadUser($versionInfo->creatorId);
$output->write("Version $versionInfo->versionNo");
$output->write(' by ' . $creator->contentInfo->name);
$output->writeln(' in ' . $versionInfo->initialLanguageCode);
}
}
// Relations
$versionInfo = $this->contentService->loadVersionInfo($contentInfo);
$relationListIterator = new BatchIterator(
new RelationListIteratorAdapter(
$this->contentService,
$versionInfo
)
);
foreach ($relationListIterator as $relationListItem) {
$name = $relationListItem->hasRelation() ? $relationListItem->getRelation()->destinationContentInfo->name : '(Unauthorized)';
$output->writeln("Relation to content '$name'");
}
// Owner
$output->writeln('Owner: ' . $contentInfo->getOwner()->getName());
// Section
$output->writeln('Section: ' . $contentInfo->getSection()->name);
// Object states
$stateGroups = $this->objectStateService->loadObjectStateGroups();
foreach ($stateGroups as $stateGroup) {
$state = $this->objectStateService->getContentState($contentInfo, $stateGroup);
$output->writeln("Object state: $state->identifier");
}
return self::SUCCESS;
}
}