Skip to content

Commit

Permalink
Add CLI interface
Browse files Browse the repository at this point in the history
  • Loading branch information
fisharebest committed Mar 31, 2024
1 parent 5ce0f61 commit 301dadf
Show file tree
Hide file tree
Showing 9 changed files with 730 additions and 433 deletions.
80 changes: 80 additions & 0 deletions app/Cli/Commands/CompilePoFiles.php
@@ -0,0 +1,80 @@
<?php

/**
* webtrees: online genealogy
* Copyright (C) 2023 webtrees development team
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

declare(strict_types=1);

namespace Fisharebest\Webtrees\Cli\Commands;

use Fisharebest\Localization\Translation;
use Fisharebest\Webtrees\Webtrees;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

use function basename;
use function count;
use function dirname;
use function file_put_contents;
use function glob;
use function realpath;
use function var_export;

class CompilePoFiles extends Command
{
private const PO_FILE_PATTERN = Webtrees::ROOT_DIR . 'resources/lang/*/*.po';

protected function configure(): void
{
$this
->setName(name: 'compile-po-files')
->setDescription(description: 'Convert the PO files into PHP files');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle(input: $input, output: $output);

$po_files = glob(pattern: self::PO_FILE_PATTERN);

if ($po_files === false || $po_files === []) {
$io->error('Failed to find any PO files matching ' . self::PO_FILE_PATTERN);

return Command::FAILURE;
}

$error = false;

foreach ($po_files as $po_file) {
$po_file = realpath($po_file);
$translation = new Translation(filename: $po_file);
$translations = $translation->asArray();
$php_file = dirname(path: $po_file) . '/' . basename(path: $po_file, suffix: '.po') . '.php';
$php_code = "<?php\n\nreturn " . var_export(value: $translations, return: true) . ";\n";
$bytes = file_put_contents(filename: $php_file, data: $php_code);

if ($bytes === false) {
$io->error('Failed to write to ' . $php_file);
$error = true;
} else {
$io->success('Created ' . $php_file . ' with ' . count(value: $translations) . ' translations');
}
}

return $error ? Command::FAILURE : Command::SUCCESS;
}
}
120 changes: 120 additions & 0 deletions app/Cli/Commands/UserCreate.php
@@ -0,0 +1,120 @@
<?php

/**
* webtrees: online genealogy
* Copyright (C) 2023 webtrees development team
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

declare(strict_types=1);

namespace Fisharebest\Webtrees\Cli\Commands;

use Composer\Console\Input\InputOption;
use Fisharebest\Webtrees\Contracts\UserInterface;
use Fisharebest\Webtrees\DB;
use Fisharebest\Webtrees\Services\UserService;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

use function bin2hex;
use function random_bytes;

class UserCreate extends Command
{
public function __construct(private readonly UserService $user_service)
{
parent::__construct();
}

protected function configure(): void
{
$this
->setName(name: 'user-create')
->setDescription(description: 'Create a new user account')
->addOption(name: 'admin', shortcut: 'a', mode: InputOption::VALUE_NONE, description: 'Make the new user an administrator')
->addOption(name: 'username', shortcut: 'u', mode: InputOption::VALUE_REQUIRED, description: 'The username of the new user')
->addOption(name: 'realname', shortcut: 'r', mode: InputOption::VALUE_REQUIRED, description: 'The real name of the new user')
->addOption(name: 'email', shortcut: 'e', mode: InputOption::VALUE_REQUIRED, description: 'The email of the new user')
->addOption(name: 'password', shortcut: 'p', mode: InputOption::VALUE_OPTIONAL, description: 'The password of the new user');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle(input: $input, output: $output);

$username = $input->getOption(name: 'username');
$realname = $input->getOption(name: 'realname');
$email = $input->getOption(name: 'email');
$password = $input->getOption(name: 'password');
$admin = (bool) $input->getOption(name: 'admin');

$missing = false;

if ($username === null) {
$io->error(message: 'Missing required option: --username');
$missing = true;
}

if ($realname === null) {
$io->error(message: 'Missing required option: --realname');
$missing = true;
}

if ($email === null) {
$io->error(message: 'Missing required option: --email');
$missing = true;
}

if ($missing) {
return Command::FAILURE;
}

$user = $this->user_service->findByUserName(user_name: $username);

if ($user !== null) {
$io->error(message: 'A user with the username "' . $username . '" already exists.');

return Command::FAILURE;
}

$user = $this->user_service->findByEmail(email: $email);

if ($user !== null) {
$io->error(message: 'A user with the email "' . $email . '" already exists');

return Command::FAILURE;
}

if ($password === null) {
$password = bin2hex(string: random_bytes(length: 8));
$io->info(message: 'Generated password: ' . $password);
}

$user = $this->user_service->create(user_name: $username, real_name:$realname, email: $email, password: $password);
$user->setPreference(setting_name: UserInterface::PREF_IS_ACCOUNT_APPROVED, setting_value: '1');
$user->setPreference(setting_name: UserInterface::PREF_IS_EMAIL_VERIFIED, setting_value: '1');
$io->success('User ' . $user->id() . ' created.');

if ($admin) {
$user->setPreference(setting_name: UserInterface::PREF_IS_ADMINISTRATOR, setting_value: '1');
$io->success(message: 'User granted administrator role.');
}

DB::exec(sql: 'COMMIT');

return Command::SUCCESS;
}
}
97 changes: 97 additions & 0 deletions app/Cli/Commands/UserList.php
@@ -0,0 +1,97 @@
<?php

/**
* webtrees: online genealogy
* Copyright (C) 2023 webtrees development team
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

declare(strict_types=1);

namespace Fisharebest\Webtrees\Cli\Commands;

use Composer\Console\Input\InputOption;
use Fisharebest\Webtrees\Auth;
use Fisharebest\Webtrees\Contracts\UserInterface;
use Fisharebest\Webtrees\DB;
use Fisharebest\Webtrees\Registry;
use Fisharebest\Webtrees\Services\UserService;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

use function bin2hex;
use function random_bytes;

class UserList extends Command
{
public function __construct(private readonly UserService $user_service)
{
parent::__construct();
}

protected function configure(): void
{
$this
->setName(name: 'user-list')
->setDescription(description: 'List users');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle(input: $input, output: $output);

$users = $this->user_service->all()->sort(fn($a, $b) => $a->id() <=> $b->id());

$table = new Table(output: $output);

$table->setHeaders(headers: ['ID', 'Username', 'Real Name', 'Email', 'Admin', 'Approved', 'Verified', 'Language', 'Time zone', 'Registered', 'Last login']);

foreach ($users as $user) {
$registered = (int) $user->getPreference(setting_name: UserInterface::PREF_TIMESTAMP_REGISTERED);
$last_login = (int) $user->getPreference(setting_name: UserInterface::PREF_TIMESTAMP_ACTIVE);

if ($registered === 0) {
$registered = 'Never';
} else {
$registered = Registry::timestampFactory()->make(timestamp: $registered)->format(format: 'Y-m-d H:i:s');
}

if ($last_login === 0) {
$last_login = 'Never';
} else {
$last_login = Registry::timestampFactory()->make(timestamp: $last_login)->format(format: 'Y-m-d H:i:s');
}

$table->addRow(row: [
$user->id(),
$user->userName(),
$user->realName(),
$user->email(),
Auth::isAdmin(user: $user) ? 'Yes' : 'No',
$user->getPreference(setting_name: UserInterface::PREF_IS_ACCOUNT_APPROVED) ? 'Yes' : 'No',
$user->getPreference(setting_name: UserInterface::PREF_IS_EMAIL_VERIFIED) ? 'Yes' : 'No',
$user->getPreference(setting_name: UserInterface::PREF_LANGUAGE),
$user->getPreference(setting_name: UserInterface::PREF_TIME_ZONE),
$registered,
$last_login,
]);
}

$table->render();

return Command::SUCCESS;
}
}
73 changes: 73 additions & 0 deletions app/Cli/Console.php
@@ -0,0 +1,73 @@
<?php

/**
* webtrees: online genealogy
* Copyright (C) 2023 webtrees development team
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

declare(strict_types=1);

namespace Fisharebest\Webtrees\Cli;

use Fisharebest\Webtrees\DB;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Registry;
use Fisharebest\Webtrees\Webtrees;
use Symfony\Component\Console\Application;

use function parse_ini_file;

final class Console extends Application
{
public function loadCommands(): self
{
$commands = glob(pattern: __DIR__ . '/Commands/*.php') ?: [];

foreach ($commands as $command) {
$class = __NAMESPACE__ . '\\Commands\\' . basename(path: $command, suffix: '.php');

$this->add(Registry::container()->get($class));
}

return $this;
}

public function bootstrap(): self
{
I18N::init(code: 'en-US', setup: true);

$config = parse_ini_file(filename: Webtrees::CONFIG_FILE);

if ($config === false) {
return $this;
}

DB::connect(
driver: $config['dbtype'] ?? DB::MYSQL,
host: $config['dbhost'],
port: $config['dbport'],
database: $config['dbname'],
username: $config['dbuser'],
password: $config['dbpass'],
prefix: $config['tblpfx'],
key: $config['dbkey'] ?? '',
certificate: $config['dbcert'] ?? '',
ca: $config['dbca'] ?? '',
verify_certificate: (bool) ($config['dbverify'] ?? ''),
);

DB::exec('START TRANSACTION');

return $this;
}
}

0 comments on commit 301dadf

Please sign in to comment.