Skip to content

Commit 579decb

Browse files
update admin UI for sampledata
1 parent f8a6466 commit 579decb

3 files changed

Lines changed: 89 additions & 15 deletions

File tree

public/admin/index.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,15 @@
3535
<button onclick="cliUpdatesRun()">Run available updates</button>
3636
</div>
3737
<div id="cli-options">
38-
<button onclick="cliSeedList()">List sample data profiles</button>
39-
<select id="seed-profile">
38+
<button onclick="sampleDataList()">List sample data profiles</button>
39+
<select id="sample-data-profile">
4040
<option value="en_community_radio">English Community Radio Station</option>
4141
</select>
42-
<button onclick="cliSeedRun()">Import sample data</button>
42+
<input type="text" id="sample-data-username" placeholder="Observer admin username" value="admin">
43+
<input type="password" id="sample-data-password" placeholder="Observer admin password">
44+
<button onclick="sampleDataRun()">Import sample data</button>
4345
</div>
4446
<div id="cli-output" class="ansi_color_bg_black"></div>
4547
</main>
4648
</body>
47-
</html>
49+
</html>

public/admin/run.php

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,74 @@
3232
exit();
3333
}
3434

35-
$validCommands = ['check', 'cron run', 'updates list all', 'updates run all', 'seed list'];
35+
$validCommands = ['check', 'cron run', 'updates list all', 'updates run all'];
3636

37-
// Allow seed run with a validated profile name.
38-
if (preg_match('/^seed run ([a-z0-9_]+)$/', $json->command, $matches)) {
39-
$validCommands[] = $json->command;
37+
// Sample-data tool: lives in tools/sampledata/ and talks to the Observer
38+
// API over HTTP. We pass Observer admin credentials through the environment
39+
// so they don't appear in the process list.
40+
if ($json->command === 'sampledata list') {
41+
$output = [];
42+
$resultCode = 0;
43+
$cmd = 'php ' . escapeshellarg(__DIR__ . '/../../tools/sampledata/seed.php') . ' list 2>&1';
44+
exec($cmd, $output, $resultCode);
45+
46+
echo json_encode([
47+
'message' => 'Listing sample data profiles.',
48+
'result' => $converter->convert(implode(PHP_EOL, $output) ?: 'No output.'),
49+
'theme' => $theme->asCss()
50+
]);
51+
52+
exit();
53+
}
54+
55+
if (preg_match('/^sampledata run ([a-z0-9_]+)$/', $json->command, $matches)) {
56+
$profile = $matches[1];
57+
$obUser = $json->sampleDataUsername ?? null;
58+
$obPass = $json->sampleDataPassword ?? null;
59+
60+
if (!$obUser || !$obPass) {
61+
http_response_code(400);
62+
echo json_encode(['message' => 'Observer admin credentials required to import sample data.']);
63+
exit();
64+
}
65+
66+
$baseUrl = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ? 'https' : 'http')
67+
. '://' . ($_SERVER['HTTP_HOST'] ?? '127.0.0.1');
68+
69+
$env = [
70+
'OB_BASE_URL' => $baseUrl,
71+
'OB_USERNAME' => $obUser,
72+
'OB_PASSWORD' => $obPass,
73+
// Preserve PATH so curl etc. resolve.
74+
'PATH' => getenv('PATH') ?: '/usr/local/bin:/usr/bin:/bin',
75+
];
76+
77+
$cmd = 'php ' . escapeshellarg(__DIR__ . '/../../tools/sampledata/seed.php')
78+
. ' run ' . escapeshellarg($profile) . ' 2>&1';
79+
80+
$descriptors = [1 => ['pipe', 'w'], 2 => ['pipe', 'w']];
81+
$process = proc_open($cmd, $descriptors, $pipes, null, $env);
82+
83+
if (!is_resource($process)) {
84+
http_response_code(500);
85+
echo json_encode(['message' => 'Failed to start the sample-data tool.']);
86+
exit();
87+
}
88+
89+
$stdout = stream_get_contents($pipes[1]);
90+
fclose($pipes[1]);
91+
fclose($pipes[2]);
92+
$resultCode = proc_close($process);
93+
94+
echo json_encode([
95+
'message' => $resultCode === 0 ? 'Sample data imported.' : 'Sample data import failed.',
96+
'result' => $converter->convert($stdout ?: 'No output.'),
97+
'theme' => $theme->asCss()
98+
]);
99+
100+
exit();
40101
}
102+
41103
if (in_array($json->command, $validCommands)) {
42104
$output = [];
43105
$resultCode = 0;

public/admin/script.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ async function run(data)
1212
});
1313

1414
const output = document.querySelector("#cli-output");
15-
15+
1616
response.json().then((data) => {
1717
if (! response.ok) {
1818
output.innerHTML += '<p class="error">' + data.message + '</p>';
@@ -60,25 +60,35 @@ async function cliUpdatesRun()
6060
run(data);
6161
}
6262

63-
async function cliSeedList()
63+
async function sampleDataList()
6464
{
6565
const data = {
66-
command: "seed list"
66+
command: "sampledata list"
6767
};
6868

6969
run(data);
7070
}
7171

72-
async function cliSeedRun()
72+
async function sampleDataRun()
7373
{
74-
const profile = document.querySelector("#seed-profile").value;
74+
const profile = document.querySelector("#sample-data-profile").value;
75+
const username = document.querySelector("#sample-data-username").value;
76+
const password = document.querySelector("#sample-data-password").value;
77+
78+
if (! username || ! password) {
79+
alert("Observer admin username and password are required to import sample data.");
80+
return;
81+
}
82+
7583
if (! confirm("Import sample data from profile: " + profile + "?\n\nThis will add categories, genres, users, playlists, and schedules. Existing data will not be overwritten.")) {
7684
return;
7785
}
7886

7987
const data = {
80-
command: "seed run " + profile
88+
command: "sampledata run " + profile,
89+
sampleDataUsername: username,
90+
sampleDataPassword: password
8191
};
8292

8393
run(data);
84-
}
94+
}

0 commit comments

Comments
 (0)