Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/Glpi/Console/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,22 @@

namespace Glpi\Console;

use Auth;
use DBmysql;
use Glpi\Console\Command\GlpiCommandInterface;
use Glpi\Console\Exception\EarlyExitException;
use Glpi\System\RequirementsManager;
use Override;
use Session;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use User;

use function Safe\preg_replace;

Expand Down Expand Up @@ -377,4 +381,33 @@ final protected function outputMessage(string $message, int $verbosity = OutputI
);
}
}

/**
* Load user in session.
*
* @throws InvalidArgumentException
*/
protected function loadUserSession(string $username): void
{
$user = new User();
if ($user->getFromDBbyName($username)) {
// Store computed output parameters
$lang = $_SESSION['glpilanguage'];
$session_use_mode = $_SESSION['glpi_use_mode'];

$auth = new Auth();
$auth->auth_succeded = true;
$auth->user = $user;
Session::init($auth);

// Force usage of computed output parameters
$_SESSION['glpilanguage'] = $lang;
$_SESSION['glpi_use_mode'] = $session_use_mode;
Session::loadLanguage();
} else {
throw new InvalidArgumentException(
__('User name defined by --username option is invalid.')
);
}
}
}
6 changes: 3 additions & 3 deletions src/Glpi/Console/Marketplace/DownloadCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
// If the plugin is already downloaded, refuse to download it again
if (!$input->getOption('force') && is_dir(GLPI_MARKETPLACE_DIR . '/' . $plugin)) {
if (Controller::hasVcsDirectory($plugin)) {
$error_msg = sprintf(__('Plugin "%s" as a local source versioning directory.'), $plugin);
$error_msg = sprintf(__('Plugin "%s" has a local source versioning directory.'), $plugin);
$error_msg .= "\n" . __('To avoid overwriting a potential branch under development, downloading is disabled.');
} else {
$error_msg = sprintf(__('Plugin "%s" is already downloaded. Use --force to force it to re-download.'), $plugin);
Expand All @@ -96,8 +96,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
$controller = new Controller($plugin);
if ($controller->canBeDownloaded($version)) {
$result = $controller->downloadPlugin(false, $version);
$success_msg = sprintf(__("Plugin %s downloaded successfully"), $plugin);
$error_msg = sprintf(__("Plugin %s could not be downloaded"), $plugin);
$success_msg = sprintf(__('Plugin "%s" downloaded successfully'), $plugin);
$error_msg = sprintf(__('Plugin "%s" could not be downloaded'), $plugin);
if ($result) {
$output->writeln("<info>$success_msg</info>");
} else {
Expand Down
216 changes: 216 additions & 0 deletions src/Glpi/Console/Marketplace/UpdateLocalPluginsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
<?php

/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2025 Teclib' and contributors.
* @licence https://www.gnu.org/licenses/gpl-3.0.html
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* 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/>.
*
* ---------------------------------------------------------------------
*/

namespace Glpi\Console\Marketplace;

use Glpi\Console\AbstractCommand;
use Glpi\Marketplace\Api\Plugins;
use Glpi\Marketplace\Controller;
use GLPINetwork;
use Plugin;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Throwable;

final class UpdateLocalPluginsCommand extends AbstractCommand
{
protected function configure()
{
parent::configure();

$this->setName('marketplace:update_local_plugins');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"marketplace:update"

$this->setDescription(__('Download up-to-date sources for all local plugins and process updates of active plugins'));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Download and update all plugins to their latest compatible versions, then reactivate active ones."


$this->addOption(
'username',
'u',
InputOption::VALUE_REQUIRED,
__('Name of user used during installation script (among other things to set plugin admin rights)')
);
}

protected function execute(InputInterface $input, OutputInterface $output)
{
if (!Controller::isCLIAllowed()) {
$output->writeln("<error>" . __('Access to the marketplace CLI commands is disallowed by the GLPI configuration') . "</error>");
return self::FAILURE;
}

if (!GLPINetwork::isRegistered()) {
$output->writeln("<error>" . __("The GLPI Network registration key is missing or invalid") . "</error>");
return self::FAILURE;
}

$username = $input->getOption('username');
if ($username !== null) {
$this->loadUserSession($username);
}

$has_errors = false;

$plugins_manager = new Plugin();
$plugins_api = new Plugins();

$local_plugins_data = $plugins_manager->find();
$local_versions = \array_column($local_plugins_data, 'version', 'directory');
$active_plugins = \array_column(
\array_filter($local_plugins_data, fn($plugin_data) => $plugin_data['state'] === Plugin::ACTIVATED),
'id',
'directory'
);

// Update all plugins sources, to be sure that all plugins have the latest version.
$updated_plugins = [];
foreach ($local_versions as $plugin_key => $local_version) {
$exists_on_filesystem = $plugins_manager->isLoadable($plugin_key);

$latest_version = $plugins_api->getPlugin($plugin_key)['version'] ?? null;

if ($latest_version === null) {
$msg = '<comment>'
. sprintf(__('Plugin "%s" is not available for your GLPI version.'), $plugin_key)
. '</comment>'
;
$output->writeln($msg);
continue;
}

if ($exists_on_filesystem && \version_compare($local_version, $latest_version, '<') === false) {
$msg = '<comment>'
. sprintf(__('Plugin "%s" is already up-to-date.'), $plugin_key)
. '</comment>'
;
$output->writeln($msg);
continue;
}

$controller = new Controller($plugin_key);
if (!$controller->canBeOverwritten()) {
if ($controller::hasVcsDirectory($plugin_key)) {
$msg = '<comment>'
. sprintf(__('Plugin "%s" has a local source versioning directory.'), $plugin_key)
. ' '
. __('To avoid overwriting a potential branch under development, downloading is disabled.')
. '</comment>'
;
} else {
$msg = '<comment>'
. sprintf(__('Plugin "%s" has an available update but its directory is not writable.'), $plugin_key)
. '</comment>'
;
}

$output->writeln($msg);
continue;
}

$result = $controller->downloadPlugin(false, $latest_version);
if ($result) {
$updated_plugins[] = $plugin_key;
$output->writeln('<info>' . sprintf(__('Plugin "%s" downloaded successfully'), $plugin_key) . '</info>');
} else {
$has_errors = true;
$output->writeln(
'<error>' . sprintf(__('Plugin "%s" could not be downloaded'), $plugin_key) . '</error>',
OutputInterface::VERBOSITY_QUIET
);
$this->outputSessionBufferedMessages([WARNING, ERROR]);
}
}

// Automatically process plugin update and reactivation of active plugins.
if (count($active_plugins) > 0) {
\asort($active_plugins);

Plugin::forcePluginsExecution(true); // Temporarly force the plugins execution
foreach ($active_plugins as $plugin_key => $plugin_id) {
if (!\in_array($plugin_key, $updated_plugins, true)) {
continue;
}

$plugin = new Plugin();

try {
$plugin->install($plugin_id);
$installed = \in_array($plugin->fields['state'], [Plugin::NOTACTIVATED, Plugin::TOBECONFIGURED]);
} catch (Throwable $e) {
global $PHPLOGGER;
$PHPLOGGER->error(
sprintf('Error while installing plugin `%s`, error was: `%s`.', $plugin_key, $e->getMessage()),
['exception' => $e]
);

$installed = false;
}
if (!$installed) {
$has_errors = true;
$output->writeln(
'<error>' . sprintf(__('Plugin "%s" installation failed.'), $plugin_key) . '</error>',
OutputInterface::VERBOSITY_QUIET
);
$this->outputSessionBufferedMessages([WARNING, ERROR]);
continue;
}

try {
$activated = $plugin->activate($plugin_id);
} catch (Throwable $e) {
global $PHPLOGGER;
$PHPLOGGER->error(
sprintf('Error while activating plugin `%s`, error was: `%s`.', $plugin_key, $e->getMessage()),
['exception' => $e]
);

$activated = false;
}

if (!$activated) {
$has_errors = true;
$output->writeln(
'<error>' . sprintf(__('Plugin "%s" activation failed.'), $plugin_key) . '</error>',
OutputInterface::VERBOSITY_QUIET
);
$this->outputSessionBufferedMessages([WARNING, ERROR]);
continue;
}

$output->writeln('<info>' . sprintf(__('Plugin "%1$s" has been updated and reactivated.'), $plugin_key) . '</info>', );
}
Plugin::forcePluginsExecution(false);
}

return $has_errors ? self::FAILURE : self::SUCCESS;
}
}
Loading
Loading