mirror of
https://git.code.sf.net/p/seeddms/code
synced 2025-11-28 02:20:41 +00:00
164 lines
7.6 KiB
PHP
164 lines
7.6 KiB
PHP
<?php
|
|
namespace Seeddms\Console\Commands;
|
|
|
|
use Symfony\Component\Console\Command\Command;
|
|
use Symfony\Component\Console\Helper\TableCell;
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
use Symfony\Component\Console\Input\InputArgument;
|
|
use Symfony\Component\Console\Input\InputOption;
|
|
use Symfony\Component\Console\Helper\Table;
|
|
use Symfony\Component\Console\Helper\QuestionHelper;
|
|
use Symfony\Component\Console\Question\Question;
|
|
|
|
use Seeddms\Seeddms\Settings;
|
|
use Seeddms\Seeddms\Translator;
|
|
use SeedDMS_Core_File;
|
|
use SeedDMS_View_Common;
|
|
use SeedDMS_Extension_Mgr;
|
|
use Log_file;
|
|
|
|
class UpdateextensionCommand extends Command
|
|
{
|
|
protected $settings;
|
|
|
|
protected $logger;
|
|
|
|
protected $translator;
|
|
|
|
protected $extmgr;
|
|
|
|
public function __construct(Settings $settings, Log_file $logger, Translator $translator, SeedDMS_Extension_Mgr $extmgr)
|
|
{
|
|
$this->settings = $settings;
|
|
$this->logger = $logger;
|
|
$this->translator = $translator;
|
|
$this->extmgr = $extmgr;
|
|
parent::__construct();
|
|
}
|
|
|
|
protected function configure()
|
|
{
|
|
$this->setName('ext:update')
|
|
->setDescription('Check for extension upates')
|
|
->setHelp('Retrieves the list of extensions from the repository and offers to either update or install those extension, which met the dependencies. If --url is not set, the repository url from the SeedDMS configuraton is used.')
|
|
->addOption('url', '', InputOption::VALUE_REQUIRED, 'Url of repository.', null)
|
|
;
|
|
}
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) : int
|
|
{
|
|
$settings = $this->settings;
|
|
$logger = $this->logger;
|
|
$translator = $this->translator;
|
|
$extmgr = $this->extmgr;
|
|
|
|
$output->writeln("<comment>Using configuration from '".$settings->_configFilePath."'.</comment>", OutputInterface::VERBOSITY_VERBOSE);
|
|
|
|
if (!is_writable($settings->_cacheDir)) {
|
|
$output->writeln(sprintf("<error>The cache dir '%s' is not writable for the system user running this script.</error>", $settings->_cacheDir));
|
|
return Command::FAILURE;
|
|
}
|
|
|
|
$reposurl = $input->getOption('url');
|
|
if($reposurl)
|
|
$extmgr->setRepositoryUrl($reposurl);
|
|
|
|
$updates = [];
|
|
$installs = [];
|
|
/* Get a list of available extensions from the repository */
|
|
if($ret = $extmgr->updateExtensionList('', true)) {
|
|
// list of installed extensions
|
|
$extconfs = $extmgr->getExtensionConfiguration();
|
|
// list of extensions in repository, this will just return the
|
|
// latest version of an extension
|
|
$list = $extmgr->getExtensionList();
|
|
foreach($list as $extname=>$data) {
|
|
$extversions = $extmgr->getExtensionListByName($extname);
|
|
// $updates[$extname] = [];
|
|
// $installs[$extname] = [];
|
|
foreach($extversions as $version=>$extversion) {
|
|
/* Only version which pass the check will be offered for
|
|
* update or install.
|
|
*/
|
|
$check = $extmgr->checkExtensionByName($extname, $extversion);
|
|
if ($check) {
|
|
if (isset($extconfs[$extname])) {
|
|
if (\Seeddms\Seeddms\ExtensionMgr::cmpVersion($version, $extconfs[$extname]['version']) > 0) {
|
|
$updates[$extname][$version] = $extversion;
|
|
}
|
|
} else {
|
|
$installs[$extname][$version] = $extversion;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
$helper = new QuestionHelper();
|
|
if($updates) {
|
|
$output->writeln(sprintf("<options=bold>%d extensions can be updated.</>", count($updates)));
|
|
foreach($updates as $extname=>$update) {
|
|
$availableversions = array_keys($update);
|
|
if ($update) {
|
|
$output->writeln(sprintf("<comment>Extension '%s' can be updated from %s to %s.</comment>", $extname, $extconfs[$extname]['version'], implode(', ', $availableversions)));
|
|
$question = new Question(sprintf("<question>Enter version to update:</question> "), '');
|
|
if ($answer = $helper->ask($input, $output, $question)) {
|
|
if (in_array($answer, $availableversions)) {
|
|
$output->writeln(sprintf("<info>Update extension '%s' to version %s.</info>", $extname, $answer));
|
|
if ($tmpfile = $extmgr->getExtensionFromRepository($update[$answer]['filename'])) {
|
|
|
|
if (!$extmgr->updateExtension($tmpfile)) {
|
|
foreach ($extmgr->getErrorMsgs() as $msg) {
|
|
$output->writeln(sprintf("<error>%s</error>", $msg));
|
|
}
|
|
unlink($tmpfile);
|
|
return Command::FAILURE;
|
|
} else {
|
|
unlink($tmpfile);
|
|
}
|
|
}
|
|
} else {
|
|
$output->writeln(sprintf("<error>Invalid version %s.</error>", $answer));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if($installs) {
|
|
$output->writeln(sprintf("<options=bold>%d extensions can be installed.</>", count($installs)));
|
|
foreach($installs as $extname=>$install) {
|
|
$availableversions = array_keys($install);
|
|
if ($install) {
|
|
$output->writeln(sprintf("<comment>Extension '%s' can be installed as version %s.</comment>", $extname, implode(', ', $availableversions)));
|
|
$question = new Question(sprintf("<question>Enter version to install:</question> "), '');
|
|
if ($answer = $helper->ask($input, $output, $question)) {
|
|
if (in_array($answer, $availableversions)) {
|
|
$output->writeln(sprintf("<info>Install extension '%s' to version %s.</info>", $extname, $answer));
|
|
if ($tmpfile = $extmgr->getExtensionFromRepository($install[$answer]['filename'])) {
|
|
|
|
if (!$extmgr->updateExtension($tmpfile)) {
|
|
foreach ($extmgr->getErrorMsgs() as $msg) {
|
|
$output->writeln(sprintf("<error>%s</error>", $msg));
|
|
}
|
|
unlink($tmpfile);
|
|
return Command::FAILURE;
|
|
} else {
|
|
unlink($tmpfile);
|
|
}
|
|
}
|
|
} else {
|
|
$output->writeln(sprintf("<error>Invalid version %s.</error>", $answer));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return Command::SUCCESS;
|
|
} else {
|
|
$output->writeln(sprintf("<error>Could not get extension list from repository.</error>"));
|
|
return Command::FAILURE;
|
|
}
|
|
}
|
|
}
|
|
|
|
// vim: ts=4 sw=4 expandtab
|