diff options
| author | Greg Roach <greg@subaqua.co.uk> | 2025-01-13 13:01:10 +0000 |
|---|---|---|
| committer | Greg Roach <greg@subaqua.co.uk> | 2025-01-13 13:05:40 +0000 |
| commit | 068396513012e7863213c49719f8323ec3c76b45 (patch) | |
| tree | 608cde2ac9dfd66438f71e299005b3f512856c30 /app/Cli | |
| parent | dfe98cc4ce485abb095c5cbbfa6e5dbffe54c7d6 (diff) | |
| download | webtrees-068396513012e7863213c49719f8323ec3c76b45.tar.gz webtrees-068396513012e7863213c49719f8323ec3c76b45.tar.bz2 webtrees-068396513012e7863213c49719f8323ec3c76b45.zip | |
Add CLI command to manage site-settings
Diffstat (limited to 'app/Cli')
| -rw-r--r-- | app/Cli/Commands/SiteSetting.php | 160 | ||||
| -rw-r--r-- | app/Cli/Console.php | 4 |
2 files changed, 161 insertions, 3 deletions
diff --git a/app/Cli/Commands/SiteSetting.php b/app/Cli/Commands/SiteSetting.php new file mode 100644 index 0000000000..a1d57ee571 --- /dev/null +++ b/app/Cli/Commands/SiteSetting.php @@ -0,0 +1,160 @@ +<?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\Webtrees\DB; +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\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Style\SymfonyStyle; + +class SiteSetting extends Command +{ + protected function configure(): void + { + $this + ->setName(name: 'site-setting') + ->setDescription(description: 'Configure site settings') + ->addOption(name: 'list', shortcut: 'l', mode: InputOption::VALUE_NONE, description: 'List site settings (optionally filtered by setting name)') + ->addOption(name: 'delete', shortcut: 'd', mode: InputOption::VALUE_NONE, description: 'Delete a site setting') + ->addArgument(name: 'setting-name', mode: InputArgument::OPTIONAL, description: 'The setting to update') + ->addArgument(name: 'setting-value', mode: InputArgument::OPTIONAL, description: 'The new value of the setting.'); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $quiet = (bool) $input->getOption(name: 'quiet'); + $list = (bool) $input->getOption(name: 'list'); + $delete = (bool) $input->getOption(name: 'delete'); + + /** @var string|null $setting_name */ + $setting_name = $input->getArgument(name: 'setting-name'); + + /** @var string|null $setting_value */ + $setting_value = $input->getArgument(name: 'setting-value'); + + $io = new SymfonyStyle(input: $input, output: $output); + + if ($list) { + if ($delete) { + $io->error(message: 'Cannot specify --list and --delete together.'); + + return Command::FAILURE; + } + + if ($setting_value !== null) { + $io->error(message: 'Cannot specify --list and a new value together.'); + + return Command::FAILURE; + } + + $table = new Table(output: $output); + $table->setHeaders(headers: ['Setting name', 'Setting value']); + + /** @var array<object{setting_name:string,setting_value:string}> $settings */ + $settings = DB::table(table: 'site_setting') + ->orderBy(column: 'setting_name') + ->select(columns: ['setting_name', 'setting_value']) + ->get() + ->all(); + + foreach ($settings as $setting) { + if ($setting_name === null || str_contains(haystack: $setting->setting_name, needle: $setting_name)) { + $table->addRow(row: [$setting->setting_name, $setting->setting_value]); + } + } + + $table->render(); + + return Command::SUCCESS; + } + + /** @var string|null $old_setting_value */ + $old_setting_value = DB::table('site_setting') + ->where(column: 'setting_name', operator: '=', value: $setting_name) + ->value(column: 'setting_value'); + + if ($delete) { + if ($setting_value !== null) { + $io->error(message: 'Cannot specify --delete and a new value together.'); + + return Command::FAILURE; + } + + if ($old_setting_value === null) { + $io->warning(message: 'Site setting ‘' . $setting_name . '’ not found. Nothing to delete.'); + } else { + DB::table('site_setting') + ->where('setting_name', '=', $setting_name) + ->delete(); + + $io->success(message: 'Site setting ‘' . $setting_name . '’ deleted. Previous value was ‘' . $old_setting_value . '’.'); + } + + return Command::SUCCESS; + } + + + if ($setting_name === null) { + $io->error(message: 'A setting name is required, unless the --list option is used.'); + + return Command::FAILURE; + } + + if ($setting_value === null) { + if ($old_setting_value === null) { + $io->info(message: 'Site setting ‘' . $setting_name . '’ is not currently set.'); + } elseif ($quiet) { + $verbosity = $io->getVerbosity(); + $io->setVerbosity(level: OutputInterface::VERBOSITY_NORMAL); + $io->writeln(messages: $old_setting_value); + $io->setVerbosity(level: $verbosity); + } else { + $io->info(message: 'Site setting ‘' . $setting_name . '’ is currently set to ‘' . $old_setting_value . '’.'); + } + + return Command::SUCCESS; + } + + if ($old_setting_value === $setting_value) { + $io->warning(message: 'Site setting ' . $setting_name . ' is already set to ' . $setting_value); + + return Command::SUCCESS; + } + + if ($old_setting_value === null) { + DB::table('site_setting') + ->insert(['setting_name' => $setting_name, 'setting_value' => $setting_value]); + + $io->success(message: 'Site setting ‘' . $setting_name . '’ was created as ‘' . $setting_value . '’.'); + } else { + DB::table('site_setting') + ->where(column: 'setting_name', operator: '=', value: $setting_name) + ->update(values: ['setting_value' => $setting_value]); + + $io->success(message: 'Site setting ‘' . $setting_name . '’ was changed from ‘' . $old_setting_value . '’ to ‘' . $setting_value . '’.'); + } + + return Command::SUCCESS; + } +} diff --git a/app/Cli/Console.php b/app/Cli/Console.php index f0fe104293..f8425cf5ed 100644 --- a/app/Cli/Console.php +++ b/app/Cli/Console.php @@ -30,7 +30,7 @@ use function parse_ini_file; final class Console extends Application { private const array COMMANDS = [ - Commands\CompilePoFiles::class, + Commands\SiteSetting::class, Commands\TreeCreate::class, Commands\TreeList::class, Commands\UserCreate::class, @@ -75,8 +75,6 @@ final class Console extends Application verify_certificate: (bool) ($config['dbverify'] ?? ''), ); - DB::exec('START TRANSACTION'); - return $this; } } |
