Skip to content

Commit 18351be

Browse files
Merge pull request nextcloud#54520 from nextcloud/fix/noid/preset-for-userconfig
feat(preset): compare userconfig lexicon entries
2 parents e878ff9 + b7cdfdd commit 18351be

File tree

4 files changed

+48
-13
lines changed

4 files changed

+48
-13
lines changed

core/Command/Config/Preset.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
4444
$list = $this->presetManager->retrieveLexiconPreset();
4545
if ($input->getOption('output') === 'plain') {
4646
$table = new Table($output);
47-
$table->setHeaders(['app', 'config key', 'value', ...array_map(static fn (ConfigLexiconPreset $p): string => $p->name, ConfigLexiconPreset::cases())]);
47+
$table->setHeaders(['app', 'config', 'config key', 'value', ...array_map(static fn (ConfigLexiconPreset $p): string => $p->name, ConfigLexiconPreset::cases())]);
4848
foreach ($list as $appId => $entries) {
4949
foreach ($entries as $item) {
50-
$table->addRow([$appId, $item['entry']['key'], '<comment>' . ($item['value'] ?? '') . '</comment>', ...($item['defaults'] ?? [])]);
50+
$table->addRow([$appId, $item['config'], $item['entry']['key'], '<comment>' . ($item['value'] ?? '') . '</comment>', ...($item['defaults'] ?? [])]);
5151
}
5252
}
5353
$table->render();

lib/private/AppConfig.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,6 +1232,7 @@ public function deleteApp(string $app): void {
12321232
*
12331233
* @param bool $reload set to TRUE to refill cache instantly after clearing it
12341234
*
1235+
* @internal
12351236
* @since 29.0.0
12361237
*/
12371238
public function clearCache(bool $reload = false): void {

lib/private/Config/PresetManager.php

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use OC\Installer;
1414
use OCP\App\AppPathNotFoundException;
1515
use OCP\App\IAppManager;
16+
use OCP\Config\IUserConfig;
1617
use OCP\Config\Lexicon\Preset;
1718
use OCP\Exceptions\AppConfigUnknownKeyException;
1819
use OCP\IAppConfig;
@@ -65,7 +66,7 @@ public function getLexiconPreset(): Preset {
6566
*
6667
* **Warning** This method MUST be considered resource-needy!
6768
*
68-
* @return array<string, list<array{defaults: array{CLUB: null|string, FAMILY: null|string, LARGE: null|string, MEDIUM: null|string, NONE: null|string, PRIVATE: null|string, SCHOOL: null|string, SHARED: null|string, SMALL: null|string, UNIVERSITY: null|string}, entry: array{definition: string, deprecated: bool, key: string, lazy: bool, note: string, type: 'ARRAY'|'BOOL'|'FLOAT'|'INT'|'MIXED'|'STRING'}, value?: mixed}>>
69+
* @return array<string, list<array{config: string, defaults: array{CLUB: null|string, FAMILY: null|string, LARGE: null|string, MEDIUM: null|string, NONE: null|string, PRIVATE: null|string, SCHOOL: null|string, SHARED: null|string, SMALL: null|string, UNIVERSITY: null|string}, entry: array{definition: string, deprecated: bool, key: string, lazy: bool, note: string, type: 'ARRAY'|'BOOL'|'FLOAT'|'INT'|'MIXED'|'STRING'}, value?: mixed}>>
6970
*/
7071
public function retrieveLexiconPreset(?string $appId = null): array {
7172
if ($appId === null) {
@@ -77,17 +78,41 @@ public function retrieveLexiconPreset(?string $appId = null): array {
7778
return $apps;
7879
}
7980

80-
/** @var AppConfig|null $appConfig */
81-
$appConfig = Server::get(IAppConfig::class);
82-
$lexicon = $appConfig->getConfigDetailsFromLexicon($appId);
81+
return [
82+
$appId => array_merge(
83+
$this->extractLexiconPresetFromConfigClass($appId, 'app', Server::get(IAppConfig::class)),
84+
$this->extractLexiconPresetFromConfigClass($appId, 'user', Server::get(IUserConfig::class))
85+
),
86+
];
87+
}
88+
89+
/**
90+
* @param string $appId
91+
*
92+
* @return list<array{config: string, defaults: array{CLUB: null|string, FAMILY: null|string, LARGE: null|string, MEDIUM: null|string, NONE: null|string, PRIVATE: null|string, SCHOOL: null|string, SHARED: null|string, SMALL: null|string, UNIVERSITY: null|string}, entry: array{definition: string, deprecated: bool, key: string, lazy: bool, note: string, type: 'ARRAY'|'BOOL'|'FLOAT'|'INT'|'MIXED'|'STRING'}, value?: mixed}>
93+
*/
94+
private function extractLexiconPresetFromConfigClass(
95+
string $appId,
96+
string $configType,
97+
AppConfig|UserConfig $config,
98+
): array {
8399
$presets = [];
100+
$lexicon = $config->getConfigDetailsFromLexicon($appId);
84101
foreach ($lexicon['entries'] as $entry) {
85102
$defaults = [];
86103
foreach (Preset::cases() as $case) {
87104
// for each case, we need to use a fresh IAppConfig with clear cache
88105
// cloning to avoid conflict while emulating preset
89-
$newConfig = clone $appConfig;
90-
$newConfig->clearCache(); // needed to ignore cache and rebuild default
106+
$newConfig = clone $config;
107+
if ($newConfig instanceof AppConfig) {
108+
// needed to ignore cache and rebuild default
109+
$newConfig->clearCache();
110+
}
111+
if ($newConfig instanceof UserConfig) {
112+
// in the case of IUserConfig, clear all users' cache
113+
$newConfig->clearCacheAll();
114+
}
115+
91116
$newLexicon = $newConfig->getLexiconEntry($appId, $entry->getKey());
92117
$defaults[$case->name] = $newLexicon?->getDefault($case);
93118
}
@@ -99,6 +124,7 @@ public function retrieveLexiconPreset(?string $appId = null): array {
99124
}
100125

101126
$details = [
127+
'config' => $configType,
102128
'entry' => [
103129
'key' => $entry->getKey(),
104130
'type' => $entry->getValueType()->name,
@@ -111,14 +137,17 @@ public function retrieveLexiconPreset(?string $appId = null): array {
111137
];
112138

113139
try {
114-
$details['value'] = $appConfig->getDetails($appId, $entry->getKey())['value'];
140+
// not interested if a users config value is already set
141+
if ($config instanceof AppConfig) {
142+
$details['value'] = $config->getDetails($appId, $entry->getKey())['value'];
143+
}
115144
} catch (AppConfigUnknownKeyException) {
116145
}
117146

118147
$presets[] = $details;
119148
}
120149

121-
return [$appId => $presets];
150+
return $presets;
122151
}
123152

124153
/**

lib/private/Config/UserConfig.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1999,8 +1999,7 @@ private function applyLexiconStrictness(?Strictness $strictness, string $line =
19991999
* @param string $appId
20002000
*
20012001
* @return array{entries: array<string, Entry>, aliases: array<string, string>, strictness: Strictness}
2002-
*@internal
2003-
*
2002+
* @internal
20042003
*/
20052004
public function getConfigDetailsFromLexicon(string $appId): array {
20062005
if (!array_key_exists($appId, $this->configLexiconDetails)) {
@@ -2024,7 +2023,13 @@ public function getConfigDetailsFromLexicon(string $appId): array {
20242023
return $this->configLexiconDetails[$appId];
20252024
}
20262025

2027-
private function getLexiconEntry(string $appId, string $key): ?Entry {
2026+
/**
2027+
* get Lexicon Entry using appId and config key entry
2028+
*
2029+
* @return Entry|null NULL if entry does not exist in user's Lexicon
2030+
* @internal
2031+
*/
2032+
public function getLexiconEntry(string $appId, string $key): ?Entry {
20282033
return $this->getConfigDetailsFromLexicon($appId)['entries'][$key] ?? null;
20292034
}
20302035

0 commit comments

Comments
 (0)