Skip to content

Commit b9a4a6c

Browse files
committed
Add CLI interface
1 parent 5ce0f61 commit b9a4a6c

File tree

9 files changed

+730
-431
lines changed

9 files changed

+730
-431
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
/**
4+
* webtrees: online genealogy
5+
* Copyright (C) 2023 webtrees development team
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
declare(strict_types=1);
19+
20+
namespace Fisharebest\Webtrees\Cli\Commands;
21+
22+
use Fisharebest\Localization\Translation;
23+
use Fisharebest\Webtrees\Webtrees;
24+
use Symfony\Component\Console\Command\Command;
25+
use Symfony\Component\Console\Input\InputInterface;
26+
use Symfony\Component\Console\Output\OutputInterface;
27+
use Symfony\Component\Console\Style\SymfonyStyle;
28+
29+
use function basename;
30+
use function count;
31+
use function dirname;
32+
use function file_put_contents;
33+
use function glob;
34+
use function realpath;
35+
use function var_export;
36+
37+
class CompilePoFiles extends Command
38+
{
39+
private const PO_FILE_PATTERN = Webtrees::ROOT_DIR . 'resources/lang/*/*.po';
40+
41+
protected function configure(): void
42+
{
43+
$this
44+
->setName(name: 'compile-po-files')
45+
->setDescription(description: 'Convert the PO files into PHP files');
46+
}
47+
48+
protected function execute(InputInterface $input, OutputInterface $output): int
49+
{
50+
$io = new SymfonyStyle(input: $input, output: $output);
51+
52+
$po_files = glob(pattern: self::PO_FILE_PATTERN);
53+
54+
if ($po_files === false || $po_files === []) {
55+
$io->error('Failed to find any PO files matching ' . self::PO_FILE_PATTERN);
56+
57+
return Command::FAILURE;
58+
}
59+
60+
$error = false;
61+
62+
foreach ($po_files as $po_file) {
63+
$po_file = realpath($po_file);
64+
$translation = new Translation(filename: $po_file);
65+
$translations = $translation->asArray();
66+
$php_file = dirname(path: $po_file) . '/' . basename(path: $po_file, suffix: '.po') . '.php';
67+
$php_code = "<?php\n\nreturn " . var_export(value: $translations, return: true) . ";\n";
68+
$bytes = file_put_contents(filename: $php_file, data: $php_code);
69+
70+
if ($bytes === false) {
71+
$io->error('Failed to write to ' . $php_file);
72+
$error = true;
73+
} else {
74+
$io->success('Created ' . $php_file . ' with ' . count(value: $translations) . ' translations');
75+
}
76+
}
77+
78+
return $error ? Command::FAILURE : Command::SUCCESS;
79+
}
80+
}

app/Cli/Commands/UserCreate.php

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?php
2+
3+
/**
4+
* webtrees: online genealogy
5+
* Copyright (C) 2023 webtrees development team
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
declare(strict_types=1);
19+
20+
namespace Fisharebest\Webtrees\Cli\Commands;
21+
22+
use Composer\Console\Input\InputOption;
23+
use Fisharebest\Webtrees\Contracts\UserInterface;
24+
use Fisharebest\Webtrees\DB;
25+
use Fisharebest\Webtrees\Services\UserService;
26+
use Symfony\Component\Console\Command\Command;
27+
use Symfony\Component\Console\Input\InputArgument;
28+
use Symfony\Component\Console\Input\InputInterface;
29+
use Symfony\Component\Console\Output\OutputInterface;
30+
use Symfony\Component\Console\Style\SymfonyStyle;
31+
32+
use function bin2hex;
33+
use function random_bytes;
34+
35+
class UserCreate extends Command
36+
{
37+
public function __construct(private readonly UserService $user_service)
38+
{
39+
parent::__construct();
40+
}
41+
42+
protected function configure(): void
43+
{
44+
$this
45+
->setName(name: 'user-create')
46+
->setDescription(description: 'Create a new user account')
47+
->addOption(name: 'admin', shortcut: 'a', mode: InputOption::VALUE_NONE, description: 'Make the new user an administrator')
48+
->addOption(name: 'username', shortcut: 'u', mode: InputOption::VALUE_REQUIRED, description: 'The username of the new user')
49+
->addOption(name: 'realname', shortcut: 'r', mode: InputOption::VALUE_REQUIRED, description: 'The real name of the new user')
50+
->addOption(name: 'email', shortcut: 'e', mode: InputOption::VALUE_REQUIRED, description: 'The email of the new user')
51+
->addOption(name: 'password', shortcut: 'p', mode: InputOption::VALUE_OPTIONAL, description: 'The password of the new user');
52+
}
53+
54+
protected function execute(InputInterface $input, OutputInterface $output): int
55+
{
56+
$io = new SymfonyStyle(input: $input, output: $output);
57+
58+
$username = $input->getOption(name: 'username');
59+
$realname = $input->getOption(name: 'realname');
60+
$email = $input->getOption(name: 'email');
61+
$password = $input->getOption(name: 'password');
62+
$admin = (bool) $input->getOption(name: 'admin');
63+
64+
$missing = false;
65+
66+
if ($username === null) {
67+
$io->error(message: 'Missing required option: --username');
68+
$missing = true;
69+
}
70+
71+
if ($realname === null) {
72+
$io->error(message: 'Missing required option: --realname');
73+
$missing = true;
74+
}
75+
76+
if ($email === null) {
77+
$io->error(message: 'Missing required option: --email');
78+
$missing = true;
79+
}
80+
81+
if ($missing) {
82+
return Command::FAILURE;
83+
}
84+
85+
$user = $this->user_service->findByUserName(user_name: $username);
86+
87+
if ($user !== null) {
88+
$io->error(message: 'A user with the username "' . $username . '" already exists.');
89+
90+
return Command::FAILURE;
91+
}
92+
93+
$user = $this->user_service->findByEmail(email: $email);
94+
95+
if ($user !== null) {
96+
$io->error(message: 'A user with the email "' . $email . '" already exists');
97+
98+
return Command::FAILURE;
99+
}
100+
101+
if ($password === null) {
102+
$password = bin2hex(string: random_bytes(length: 8));
103+
$io->info(message: 'Generated password: ' . $password);
104+
}
105+
106+
$user = $this->user_service->create(user_name: $username, real_name:$realname, email: $email, password: $password);
107+
$user->setPreference(setting_name: UserInterface::PREF_IS_ACCOUNT_APPROVED, setting_value: '1');
108+
$user->setPreference(setting_name: UserInterface::PREF_IS_EMAIL_VERIFIED, setting_value: '1');
109+
$io->success('User ' . $user->id() . ' created.');
110+
111+
if ($admin) {
112+
$user->setPreference(setting_name: UserInterface::PREF_IS_ADMINISTRATOR, setting_value: '1');
113+
$io->success(message: 'User granted administrator role.');
114+
}
115+
116+
DB::exec(sql: 'COMMIT');
117+
118+
return Command::SUCCESS;
119+
}
120+
}

app/Cli/Commands/UserList.php

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
/**
4+
* webtrees: online genealogy
5+
* Copyright (C) 2023 webtrees development team
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
declare(strict_types=1);
19+
20+
namespace Fisharebest\Webtrees\Cli\Commands;
21+
22+
use Composer\Console\Input\InputOption;
23+
use Fisharebest\Webtrees\Auth;
24+
use Fisharebest\Webtrees\Contracts\UserInterface;
25+
use Fisharebest\Webtrees\DB;
26+
use Fisharebest\Webtrees\Registry;
27+
use Fisharebest\Webtrees\Services\UserService;
28+
use Symfony\Component\Console\Command\Command;
29+
use Symfony\Component\Console\Helper\Table;
30+
use Symfony\Component\Console\Input\InputArgument;
31+
use Symfony\Component\Console\Input\InputInterface;
32+
use Symfony\Component\Console\Output\OutputInterface;
33+
use Symfony\Component\Console\Style\SymfonyStyle;
34+
35+
use function bin2hex;
36+
use function random_bytes;
37+
38+
class UserList extends Command
39+
{
40+
public function __construct(private readonly UserService $user_service)
41+
{
42+
parent::__construct();
43+
}
44+
45+
protected function configure(): void
46+
{
47+
$this
48+
->setName(name: 'user-list')
49+
->setDescription(description: 'List users');
50+
}
51+
52+
protected function execute(InputInterface $input, OutputInterface $output): int
53+
{
54+
$io = new SymfonyStyle(input: $input, output: $output);
55+
56+
$users = $this->user_service->all()->sort(fn($a, $b) => $a->id() <=> $b->id());
57+
58+
$table = new Table(output: $output);
59+
60+
$table->setHeaders(headers: ['ID', 'Username', 'Real Name', 'Email', 'Admin', 'Approved', 'Verified', 'Language', 'Time zone', 'Registered', 'Last login']);
61+
62+
foreach ($users as $user) {
63+
$registered = (int) $user->getPreference(setting_name: UserInterface::PREF_TIMESTAMP_REGISTERED);
64+
$last_login = (int) $user->getPreference(setting_name: UserInterface::PREF_TIMESTAMP_ACTIVE);
65+
66+
if ($registered === 0) {
67+
$registered = 'Never';
68+
} else {
69+
$registered = Registry::timestampFactory()->make(timestamp: $registered)->format(format: 'Y-m-d H:i:s');
70+
}
71+
72+
if ($last_login === 0) {
73+
$last_login = 'Never';
74+
} else {
75+
$last_login = Registry::timestampFactory()->make(timestamp: $last_login)->format(format: 'Y-m-d H:i:s');
76+
}
77+
78+
$table->addRow(row: [
79+
$user->id(),
80+
$user->userName(),
81+
$user->realName(),
82+
$user->email(),
83+
Auth::isAdmin(user: $user) ? 'Yes' : 'No',
84+
$user->getPreference(setting_name: UserInterface::PREF_IS_ACCOUNT_APPROVED) ? 'Yes' : 'No',
85+
$user->getPreference(setting_name: UserInterface::PREF_IS_EMAIL_VERIFIED) ? 'Yes' : 'No',
86+
$user->getPreference(setting_name: UserInterface::PREF_LANGUAGE),
87+
$user->getPreference(setting_name: UserInterface::PREF_TIME_ZONE),
88+
$registered,
89+
$last_login,
90+
]);
91+
}
92+
93+
$table->render();
94+
95+
return Command::SUCCESS;
96+
}
97+
}

app/Cli/Console.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
/**
4+
* webtrees: online genealogy
5+
* Copyright (C) 2023 webtrees development team
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
declare(strict_types=1);
19+
20+
namespace Fisharebest\Webtrees\Cli;
21+
22+
use Fisharebest\Webtrees\DB;
23+
use Fisharebest\Webtrees\I18N;
24+
use Fisharebest\Webtrees\Registry;
25+
use Fisharebest\Webtrees\Webtrees;
26+
use Symfony\Component\Console\Application;
27+
28+
use function parse_ini_file;
29+
30+
final class Console extends Application
31+
{
32+
public function loadCommands(): self
33+
{
34+
$commands = glob(pattern: __DIR__ . '/Commands/*.php') ?: [];
35+
36+
foreach ($commands as $command) {
37+
$class = __NAMESPACE__ . '\\Commands\\' . basename(path: $command, suffix: '.php');
38+
39+
$this->add(Registry::container()->get($class));
40+
}
41+
42+
return $this;
43+
}
44+
45+
public function bootstrap(): self
46+
{
47+
I18N::init(code: 'en-US', setup: true);
48+
49+
$config = parse_ini_file(filename: Webtrees::CONFIG_FILE);
50+
51+
if ($config === false) {
52+
return $this;
53+
}
54+
55+
DB::connect(
56+
driver: $config['dbtype'] ?? DB::MYSQL,
57+
host: $config['dbhost'],
58+
port: $config['dbport'],
59+
database: $config['dbname'],
60+
username: $config['dbuser'],
61+
password: $config['dbpass'],
62+
prefix: $config['tblpfx'],
63+
key: $config['dbkey'] ?? '',
64+
certificate: $config['dbcert'] ?? '',
65+
ca: $config['dbca'] ?? '',
66+
verify_certificate: (bool) ($config['dbverify'] ?? ''),
67+
);
68+
69+
DB::exec('START TRANSACTION');
70+
71+
return $this;
72+
}
73+
}

0 commit comments

Comments
 (0)