Skip to content

Commit 625b815

Browse files
Merge pull request #54018 from nextcloud/backport/54003/stable31
2 parents 1e794a2 + 4348217 commit 625b815

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

lib/private/AppConfig.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,9 @@ public function getApps(): array {
9797
* @inheritDoc
9898
*
9999
* @param string $app id of the app
100-
*
101100
* @return list<string> list of stored config keys
101+
* @see searchKeys to not load lazy config keys
102+
*
102103
* @since 29.0.0
103104
*/
104105
public function getKeys(string $app): array {
@@ -110,6 +111,32 @@ public function getKeys(string $app): array {
110111
return array_values(array_unique($keys));
111112
}
112113

114+
/**
115+
* @inheritDoc
116+
*
117+
* @param string $app id of the app
118+
* @param string $prefix returns only keys starting with this value
119+
* @param bool $lazy TRUE to search in lazy config keys
120+
* @return list<string> list of stored config keys
121+
* @since 32.0.0
122+
*/
123+
public function searchKeys(string $app, string $prefix = '', bool $lazy = false): array {
124+
$this->assertParams($app);
125+
$this->loadConfig($app, $lazy);
126+
if ($lazy) {
127+
$keys = array_keys($this->lazyCache[$app] ?? []);
128+
} else {
129+
$keys = array_keys($this->fastCache[$app] ?? []);
130+
}
131+
132+
if ($prefix !== '') {
133+
$keys = array_filter($keys, static fn (string $key): bool => str_starts_with($key, $prefix));
134+
}
135+
136+
sort($keys);
137+
return array_values(array_unique($keys));
138+
}
139+
113140
/**
114141
* @inheritDoc
115142
*

lib/public/IAppConfig.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,26 @@ public function getApps(): array;
6565
* **WARNING:** ignore lazy filtering, all config values are loaded from database
6666
*
6767
* @param string $app id of the app
68-
*
6968
* @return list<string> list of stored config keys
69+
* @see searchKeys to avoid loading lazy config keys
70+
*
7071
* @since 29.0.0
7172
*/
7273
public function getKeys(string $app): array;
7374

75+
/**
76+
* Returns list of keys stored in database, related to an app.
77+
* Please note that the values are not returned.
78+
*
79+
* @param string $app id of the app
80+
* @param string $prefix returns only keys starting with this value
81+
* @param bool $lazy TRUE to search in lazy config keys
82+
*
83+
* @return list<string> list of stored config keys
84+
* @since 32.0.0
85+
*/
86+
public function searchKeys(string $app, string $prefix = '', bool $lazy = false): array;
87+
7488
/**
7589
* Check if a key exists in the list of stored config values.
7690
*

tests/lib/AppConfigTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ class AppConfigTest extends TestCase {
4444
'deletethis' => ['deletethis', 'deletethis'],
4545
'key' => ['key', 'value']
4646
],
47+
'searchtest' => [
48+
'search_key1' => ['search_key1', 'key1', IAppConfig::VALUE_STRING],
49+
'search_key2' => ['search_key2', 'key2', IAppConfig::VALUE_STRING],
50+
'search_key3' => ['search_key3', 'key3', IAppConfig::VALUE_STRING],
51+
'searchnot_key4' => ['searchnot_key4', 'key4', IAppConfig::VALUE_STRING],
52+
'search_key5_lazy' => ['search_key5_lazy', 'key5', IAppConfig::VALUE_STRING, true],
53+
],
4754
'someapp' => [
4855
'key' => ['key', 'value'],
4956
'otherkey' => ['otherkey', 'othervalue']
@@ -1439,6 +1446,23 @@ public function testUpdateNonSensitiveValueToSensitiveWithUpdateSensitive(): voi
14391446
$this->assertConfigValueNotEquals('testapp', $key, $secret);
14401447
}
14411448

1449+
public function testSearchKeyNoLazyLoading(): void {
1450+
$appConfig = $this->generateAppConfig();
1451+
$appConfig->searchKeys('searchtest', 'search_');
1452+
$status = $appConfig->statusCache();
1453+
$this->assertFalse($status['lazyLoaded'], 'searchKeys() loaded lazy config');
1454+
}
1455+
1456+
public function testSearchKeyFast(): void {
1457+
$appConfig = $this->generateAppConfig();
1458+
$this->assertEquals(['search_key1', 'search_key2', 'search_key3'], $appConfig->searchKeys('searchtest', 'search_'));
1459+
}
1460+
1461+
public function testSearchKeyLazy(): void {
1462+
$appConfig = $this->generateAppConfig();
1463+
$this->assertEquals(['search_key5_lazy'], $appConfig->searchKeys('searchtest', 'search_', true));
1464+
}
1465+
14421466
protected function loadConfigValueFromDatabase(string $app, string $key): string|false {
14431467
$sql = $this->connection->getQueryBuilder();
14441468
$sql->select('configvalue')

0 commit comments

Comments
 (0)