-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExport.php
More file actions
129 lines (113 loc) · 4.03 KB
/
Export.php
File metadata and controls
129 lines (113 loc) · 4.03 KB
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
<?php
/**
* SPDX-FileCopyrightText: 2026 LibreCode coop and contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
declare(strict_types=1);
namespace OCA\ProfileFields\Command\Data;
use JsonException;
use OCA\ProfileFields\Db\FieldValue;
use OCA\ProfileFields\Db\FieldValueMapper;
use OCA\ProfileFields\Service\FieldDefinitionService;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class Export extends Command {
private const SCHEMA_VERSION = 1;
public function __construct(
private FieldDefinitionService $fieldDefinitionService,
private FieldValueMapper $fieldValueMapper,
) {
parent::__construct();
}
#[\Override]
protected function configure(): void {
$this
->setName('profile_fields:data:export')
->setDescription('Export persisted Profile Fields definitions and values as JSON')
->addOption(
name: 'output',
shortcut: 'o',
mode: InputOption::VALUE_REQUIRED,
description: 'Write the JSON export to a file instead of stdout',
)
->addOption(
name: 'pretty',
shortcut: null,
mode: InputOption::VALUE_NONE,
description: 'Pretty-print the JSON output',
);
}
#[\Override]
protected function execute(InputInterface $input, OutputInterface $output): int {
try {
$definitions = $this->fieldDefinitionService->findAllOrdered();
$fieldKeysByDefinitionId = [];
foreach ($definitions as $definition) {
$fieldKeysByDefinitionId[$definition->getId()] = $definition->getFieldKey();
}
$payload = [
'schema_version' => self::SCHEMA_VERSION,
'exported_at' => gmdate(DATE_ATOM),
'definitions' => array_map(
static fn ($definition): array => $definition->jsonSerialize(),
$definitions,
),
'values' => array_map(
fn (FieldValue $value): array => $this->serializeValue($value, $fieldKeysByDefinitionId),
$this->fieldValueMapper->findAllOrdered(),
),
];
} catch (\Throwable $exception) {
$output->writeln('<error>Failed to build export payload.</error>');
$output->writeln(sprintf('<error>%s</error>', $exception->getMessage()));
return self::FAILURE;
}
try {
$json = json_encode(
$payload,
JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES | ((bool)$input->getOption('pretty') ? JSON_PRETTY_PRINT : 0),
);
} catch (JsonException $exception) {
$output->writeln('<error>Failed to encode export payload.</error>');
$output->writeln(sprintf('<error>%s</error>', $exception->getMessage()));
return self::FAILURE;
}
$targetPath = $input->getOption('output');
if (is_string($targetPath) && $targetPath !== '') {
if (@file_put_contents($targetPath, $json . PHP_EOL) === false) {
$output->writeln(sprintf('<error>Could not write export to %s.</error>', $targetPath));
return self::FAILURE;
}
$output->writeln(sprintf('<info>Profile Fields data exported to %s.</info>', $targetPath));
return self::SUCCESS;
}
$output->writeln($json);
return self::SUCCESS;
}
/**
* @return array<string, mixed>
*/
private function serializeValue(FieldValue $value, array $fieldKeysByDefinitionId): array {
try {
$decodedValue = json_decode($value->getValueJson(), true, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $exception) {
throw new \RuntimeException('Failed to decode stored field value JSON.', 0, $exception);
}
$fieldKey = $fieldKeysByDefinitionId[$value->getFieldDefinitionId()] ?? null;
if (!is_string($fieldKey) || $fieldKey === '') {
throw new \RuntimeException(sprintf('Could not resolve field_key for field definition %d.', $value->getFieldDefinitionId()));
}
return [
'id' => $value->getId(),
'field_definition_id' => $value->getFieldDefinitionId(),
'field_key' => $fieldKey,
'user_uid' => $value->getUserUid(),
'value' => $decodedValue,
'current_visibility' => $value->getCurrentVisibility(),
'updated_by_uid' => $value->getUpdatedByUid(),
'updated_at' => $value->getUpdatedAt()->format(DATE_ATOM),
];
}
}