|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +use GetOpt\{ |
| 5 | + GetOpt, |
| 6 | + Option |
| 7 | +}; |
| 8 | +use ParagonIE\EasyDB\Factory; |
| 9 | +use ParagonIE\Chronicle\Chronicle; |
| 10 | +use ParagonIE\Chronicle\Exception\InstanceNotFoundException; |
| 11 | + |
| 12 | +$root = \dirname(__DIR__); |
| 13 | +/** @psalm-suppress UnresolvableInclude */ |
| 14 | +require_once $root . '/cli-autoload.php'; |
| 15 | + |
| 16 | +if (!\is_readable($root . '/local/settings.json')) { |
| 17 | + echo 'Settings are not loaded.', PHP_EOL; |
| 18 | + exit(1); |
| 19 | +} |
| 20 | + |
| 21 | +/** @var array $settings */ |
| 22 | +$settings = \json_decode( |
| 23 | + (string) \file_get_contents($root . '/local/settings.json'), |
| 24 | + true |
| 25 | +); |
| 26 | + |
| 27 | +$db = Factory::create( |
| 28 | + $settings['database']['dsn'], |
| 29 | + $settings['database']['username'] ?? '', |
| 30 | + $settings['database']['password'] ?? '', |
| 31 | + $settings['database']['options'] ?? [] |
| 32 | +); |
| 33 | + |
| 34 | +// Pass database instance to Chronicle |
| 35 | +Chronicle::setDatabase($db); |
| 36 | + |
| 37 | +/** |
| 38 | + * This defines the Command Line options. |
| 39 | + * |
| 40 | + * These two are equivalent: |
| 41 | + * php add-mirror.php -u chronicle.example.com -p foo |
| 42 | + * php add-mirror.php --url chronicle.example.com --publickey=foo |
| 43 | + */ |
| 44 | +$getopt = new Getopt([ |
| 45 | + new Option('p', 'publickey', Getopt::REQUIRED_ARGUMENT), |
| 46 | + new Option('u', 'url', Getopt::REQUIRED_ARGUMENT), |
| 47 | + new Option('c', 'comment', Getopt::OPTIONAL_ARGUMENT), |
| 48 | + new Option('s', 'sort', Getopt::OPTIONAL_ARGUMENT), |
| 49 | + new Option('i', 'instance', Getopt::OPTIONAL_ARGUMENT), |
| 50 | +]); |
| 51 | +$getopt->process(); |
| 52 | + |
| 53 | +/** @var string $url */ |
| 54 | +$url = $getopt->getOption('url'); |
| 55 | +/** @var string $publicKey */ |
| 56 | +$publicKey = $getopt->getOption('publickey'); |
| 57 | +/** @var string|null $comment */ |
| 58 | +$comment = $getopt->getOption('comment'); |
| 59 | +$sort = (int) ($getopt->getOption('sort') ?? 0); |
| 60 | +$instance = (string) $getopt->getOption('instance'); |
| 61 | + |
| 62 | +try { |
| 63 | + if (!empty($instance)) { |
| 64 | + if (!\array_key_exists($instance, $settings['instances'])) { |
| 65 | + throw new InstanceNotFoundException( |
| 66 | + 'Instance ' . $instance . ' not found' |
| 67 | + ); |
| 68 | + } |
| 69 | + Chronicle::setTablePrefix($settings['instances'][$instance]); |
| 70 | + } |
| 71 | +} catch (InstanceNotFoundException $ex) { |
| 72 | + echo $ex->getMessage(), PHP_EOL; |
| 73 | + exit(1); |
| 74 | +} |
| 75 | + |
| 76 | +if (empty($publicKey)) { |
| 77 | + echo 'Usage:', PHP_EOL, "\t", |
| 78 | + 'php add-mirror.php -u url -p publickeygoeshere [-c comment]', PHP_EOL, PHP_EOL; |
| 79 | + exit(1); |
| 80 | +} |
| 81 | + |
| 82 | +$isSQLite = strpos($settings['database']['dsn'] ?? '', 'sqlite:') !== false; |
| 83 | + |
| 84 | +$db->beginTransaction(); |
| 85 | +$db->insert( |
| 86 | + Chronicle::getTableNameUnquoted('mirrors', $isSQLite), |
| 87 | + [ |
| 88 | + 'url' => $url, |
| 89 | + 'publickey' => $publicKey, |
| 90 | + 'comment' => $comment, |
| 91 | + 'sortpriority' => $sort |
| 92 | + ] |
| 93 | +); |
| 94 | +if (!$db->commit()) { |
| 95 | + $db->rollBack(); |
| 96 | + /** @var array<int, string> $errorInfo */ |
| 97 | + $errorInfo = $db->errorInfo(); |
| 98 | + echo $errorInfo[0], PHP_EOL; |
| 99 | + exit(1); |
| 100 | +} |
| 101 | +echo 'Mirror added successfully!', PHP_EOL; |
0 commit comments