Clean old versions of a module

This commit is contained in:
Jean-Christian Paul Denis 2023-11-14 22:05:41 +01:00
parent ac8516f30c
commit f53bab350b
Signed by: JcDenis
GPG Key ID: 1B5B8C5B90B6C951

View File

@ -24,6 +24,14 @@ class BackendBehaviors
{
private static bool $cleared = false;
/**
* Add "Hide" button on modules list.
*
* @param ModulesList $list The modules list
* @param ModuleDefine $define The current module
*
* @return string The button or empty string
*/
public static function adminModulesListGetActionsV2(ModulesList $list, ModuleDefine $define): string
{
return in_array($list->getList(), [
@ -33,7 +41,11 @@ class BackendBehaviors
}
/**
* Hide selected module on modules list.
*
* @param ModulesList $list Th emodules list
* @param array<int, ModuleDefine> $defines The modules
* @param string $type The modules type (plugins or themes)
*/
public static function adminModulesListDoActions(ModulesList $list, array $defines, string $type): void
{
@ -44,33 +56,61 @@ class BackendBehaviors
}
$type = str_starts_with($type, 'theme') ? 'themes' : 'plugins';
$to_add = key($_POST[My::id()]);
$update = key($_POST[My::id()]);
$tmp = explode(' ', $update);
$module = $tmp[0];
$logs = self::getLogs($type);
if (!$logs->isEmpty()) {
$ids = [];
while ($logs->fetch()) {
if ($to_add == $logs->f('log_msg')) {
// Allready hidden!
if ($update == $logs->f('log_msg')) {
// Same module version allready hidden
return;
}
if (str_starts_with($logs->f('log_msg'), $module)) {
// Same module but older version
$ids[] = $logs->f('log_id');
}
}
if (!empty($ids)) {
// Delete older versions
App::log()->deLogs($ids);
}
}
if (!empty($to_add)) {
if (!empty($update)) {
// Add module version
$cur = App::log()->openLogCursor();
$cur->setField('log_table', My::id() . '_' . $type);
$cur->setField('log_msg', $to_add);
$cur->setField('log_msg', $update);
App::log()->addLog($cur);
Notices::AddSuccessNotice(__('Module successfully hidden.'));
Notices::AddSuccessNotice(sprintf(__('Module %s successfully hidden.'), $update));
}
}
/**
* Hide modules from store update list.
*
* @param string $type The modules type (plugins or themes)
* @param ArrayObject<int, ModuleDefine> $defines The modules
*/
public static function afterCheckStoreUpdate(string $type, ArrayObject $defines): void
{
self::clearLogs($type);
if (!self::$cleared && !empty($_GET['nocache'])) {
// Clear (once) modules versions on manual check
$logs = self::getLogs($type);
if (!$logs->isEmpty()) {
$ids = [];
while ($logs->fetch()) {
$ids[] = (int) $logs->f('log_id');
}
App::log()->delLogs($ids);
self::$cleared = true;
return;
}
}
$logs = self::getLogs($type);
if ($logs->isEmpty()) {
@ -89,21 +129,6 @@ class BackendBehaviors
}
}
private static function clearLogs(string $type): void
{
if (!self::$cleared && !empty($_GET['nocache'])) {
$logs = self::getLogs($type);
if (!$logs->isEmpty()) {
$ids = [];
while ($logs->fetch()) {
$ids[] = (int) $logs->f('log_id');
}
App::log()->delLogs($ids);
}
self::$cleared = true;
}
}
private static function getLogs(string $type): MetaRecord
{
return App::log()->getLogs([