From efa1372cdcffea6997ee1b9c8f29cce660b24187 Mon Sep 17 00:00:00 2001 From: "Fedor A. Fetisov" Date: Tue, 6 Feb 2018 01:28:48 +0300 Subject: [PATCH] Fix handling of obsolete information on updates --- .../Mibew/Maintenance/UpdateChecker.php | 68 ++++++++++++++----- 1 file changed, 51 insertions(+), 17 deletions(-) diff --git a/src/mibew/libs/classes/Mibew/Maintenance/UpdateChecker.php b/src/mibew/libs/classes/Mibew/Maintenance/UpdateChecker.php index 772086ad..27a82610 100644 --- a/src/mibew/libs/classes/Mibew/Maintenance/UpdateChecker.php +++ b/src/mibew/libs/classes/Mibew/Maintenance/UpdateChecker.php @@ -245,20 +245,25 @@ class UpdateChecker protected function processUpdates($updates) { // Process updates of the core. + $success = false; if (version_compare($updates['core']['version'], MIBEW_VERSION) > 0) { $update = $updates['core']; - // Save info about update for the core only if its version changed + // Save info about update for the core only if its version changed. $success = $this->saveUpdate( 'core', $update['version'], $update['download'], empty($update['description']) ? '' : $update['description'] ); - if (!$success) { - // Something went wrong. The error is already logged so just - // notify the outer code. - return false; - } + } + else { + // Remove obsolete info if core already was updated. + $success = $this->deleteUpdate('core'); + } + if (!$success) { + // Something went wrong. The error is already logged so just + // notify the outer code. + return false; } // Process plugins updates. @@ -271,19 +276,21 @@ class UpdateChecker } $info = $plugins_info[$plugin_name]; - - if (version_compare($update['version'], $info['version']) <= 0) { - // Version of the plugin is not updated. Just do nothing. - continue; + $success = false; + if (version_compare($update['version'], $info['version']) > 0) { + // Save the update + $success = $this->saveUpdate( + $plugin_name, + $update['version'], + $update['download'], + empty($update['description']) ? '' : $update['description'] + ); + } + else { + // Version of the plugin is not updated. Remove obsolete info if need to. + $success = $this->deleteUpdate($plugin_name); } - // Save the update - $success = $this->saveUpdate( - $plugin_name, - $update['version'], - $update['download'], - empty($update['description']) ? '' : $update['description'] - ); if (!$success) { // Something went wrong. The error is already logged so just // notify the outer code. @@ -329,6 +336,33 @@ class UpdateChecker return true; } + /** + * Deletes record about available update from the database. + * + * @param string $target Update's target. Can be either "core" or fully + * qualified plugin's name. + * @return boolean False on failure and true otherwise. To get more info + * about the error call {@link UpdateChecker::getErrors()} method. + */ + protected function deleteUpdate($target) + { + try { + $update = AvailableUpdate::loadByTarget($target); + if (!$update) { + // There is no such update in the database. Do nothing. + return true; + } + + $update->delete(); + } catch (\Exception $e) { + $this->errors[] = 'Cannot delete obsolete update: ' + $e->getMessage(); + + return false; + } + + return true; + } + /** * Builds human-readable message about error in json_* PHP's function. *