2023-02-06 01:15:18 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2022, Varun Patil <radialapps@gmail.com>
|
|
|
|
* @author Varun Patil <radialapps@gmail.com>
|
|
|
|
* @license AGPL-3.0-or-later
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Memories\Command;
|
|
|
|
|
2023-04-11 02:17:16 +00:00
|
|
|
use OCA\Memories\Service\Places;
|
2023-02-06 01:15:18 +00:00
|
|
|
use Symfony\Component\Console\Command\Command;
|
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
2023-05-30 06:12:49 +00:00
|
|
|
use Symfony\Component\Console\Input\InputOption;
|
2023-02-06 01:15:18 +00:00
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
|
2023-02-06 06:42:34 +00:00
|
|
|
class PlacesSetup extends Command
|
2023-02-06 01:15:18 +00:00
|
|
|
{
|
|
|
|
protected OutputInterface $output;
|
|
|
|
|
2023-10-15 01:51:17 +00:00
|
|
|
public function __construct(protected Places $places)
|
|
|
|
{
|
2023-02-06 01:15:18 +00:00
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function configure(): void
|
|
|
|
{
|
|
|
|
$this
|
2023-02-06 06:42:34 +00:00
|
|
|
->setName('memories:places-setup')
|
2023-02-06 01:15:18 +00:00
|
|
|
->setDescription('Setup reverse geocoding')
|
2023-05-30 06:12:49 +00:00
|
|
|
->addOption('recalculate', 'r', InputOption::VALUE_NONE, 'Only recalculate places for existing files')
|
2023-02-06 01:15:18 +00:00
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
|
|
|
{
|
|
|
|
$this->output = $output;
|
2023-05-30 06:12:49 +00:00
|
|
|
$recalculate = $input->getOption('recalculate');
|
2023-02-06 01:15:18 +00:00
|
|
|
|
|
|
|
$this->output->writeln('Attempting to set up reverse geocoding');
|
|
|
|
|
|
|
|
// Detect the GIS type
|
2023-04-11 02:17:16 +00:00
|
|
|
if ($this->places->detectGisType() <= 0) {
|
2023-02-06 01:15:18 +00:00
|
|
|
$this->output->writeln('<error>No supported GIS type detected</error>');
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2023-10-14 02:05:55 +00:00
|
|
|
$this->output->writeln('<info>Database support was detected</info>');
|
2023-02-06 01:15:18 +00:00
|
|
|
|
2023-04-11 02:17:16 +00:00
|
|
|
// Check if database is already set up
|
2023-05-30 06:12:49 +00:00
|
|
|
if ($this->places->geomCount() > 0 && !$recalculate && !$this->warnDownloaded()) {
|
|
|
|
return 1;
|
2023-02-06 06:42:34 +00:00
|
|
|
}
|
|
|
|
|
2023-05-30 06:12:49 +00:00
|
|
|
// Check if we only need to recalculate
|
|
|
|
if (!$recalculate) {
|
|
|
|
// Download the planet database
|
|
|
|
$datafile = $this->places->downloadPlanet();
|
2023-02-06 06:42:34 +00:00
|
|
|
|
2023-05-30 06:12:49 +00:00
|
|
|
// Import the planet database
|
|
|
|
$this->places->importPlanet($datafile);
|
|
|
|
}
|
2023-04-17 02:53:06 +00:00
|
|
|
|
|
|
|
// Recalculate all places
|
|
|
|
$this->places->recalculateAll();
|
|
|
|
|
2023-10-14 02:05:55 +00:00
|
|
|
$this->output->writeln('<info>Places set up successfully</info>');
|
2023-04-17 02:53:06 +00:00
|
|
|
|
|
|
|
return 0;
|
2023-02-06 01:15:18 +00:00
|
|
|
}
|
2023-05-30 06:12:49 +00:00
|
|
|
|
|
|
|
protected function warnDownloaded(): bool
|
|
|
|
{
|
|
|
|
$this->output->writeln('');
|
|
|
|
$this->output->writeln('<error>Database is already set up</error>');
|
|
|
|
$this->output->writeln('<error>This will drop and re-download the planet database</error>');
|
|
|
|
$this->output->writeln('<error>This is generally not necessary to do frequently </error>');
|
|
|
|
|
|
|
|
// Ask confirmation
|
|
|
|
$this->output->writeln('');
|
|
|
|
$this->output->writeln('Are you sure you want to download the planet database?');
|
|
|
|
$this->output->write('Proceed? [y/N] ');
|
|
|
|
$handle = fopen('php://stdin', 'r');
|
|
|
|
$line = fgets($handle);
|
|
|
|
if (false === $line) {
|
|
|
|
$this->output->writeln('<error>You need an interactive terminal to run this command</error>');
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ('y' !== trim($line)) {
|
|
|
|
$this->output->writeln('Aborting');
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2023-02-06 01:15:18 +00:00
|
|
|
}
|