diff options
author | diosmosis <diosmosis@users.noreply.github.com> | 2020-11-10 22:35:07 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-10 22:35:07 +0300 |
commit | c65e3b755443561e2c29f5190dc4918b25203dc9 (patch) | |
tree | 2a247fcd972ed1160077eff0cc0356ff1443b5eb /core | |
parent | 3c64683bf85aee739bc7aa18eeefe9dcf4c7896c (diff) |
Keep track of last activate/deactivate date for plugins (#16683)
Diffstat (limited to 'core')
-rw-r--r-- | core/Archive/ArchiveInvalidator.php | 37 | ||||
-rw-r--r-- | core/Plugin.php | 29 | ||||
-rw-r--r-- | core/Plugin/Manager.php | 26 |
3 files changed, 79 insertions, 13 deletions
diff --git a/core/Archive/ArchiveInvalidator.php b/core/Archive/ArchiveInvalidator.php index 52bc3a23b4..e151f1cdac 100644 --- a/core/Archive/ArchiveInvalidator.php +++ b/core/Archive/ArchiveInvalidator.php @@ -460,18 +460,12 @@ class ArchiveInvalidator { $date2 = Date::yesterday(); + $earliestDateToRearchive = $this->getEarliestDateToRearchive(); if (empty($startDate)) { - $lastNMonthsToInvalidate = Config::getInstance()->General['rearchive_reports_in_past_last_n_months']; - if (empty($lastNMonthsToInvalidate)) { - return; - } - - $lastNMonthsToInvalidate = (int) substr($lastNMonthsToInvalidate, 4); - if (empty($lastNMonthsToInvalidate)) { - return; - } - - $startDate = $date2->subMonth($lastNMonthsToInvalidate)->setDay(1); + $startDate = $earliestDateToRearchive; + } else if (!empty($earliestDateToRearchive)) { + // don't allow archiving further back than the rearchive_reports_in_past_last_n_months date allows + $startDate = $startDate->isEarlier($earliestDateToRearchive) ? $earliestDateToRearchive : $startDate; } if ($idSites === 'all') { @@ -538,11 +532,13 @@ class ArchiveInvalidator * * @param int|int[]|'all' $idSites * @param string $pluginName + * @param string|null $report + * @param Date|null $startDate */ - public function reArchiveReportSafely($idSites, $pluginName) + public function reArchiveReportSafely($idSites, string $pluginName, string $report = null, Date $startDate = null) { try { - $this->reArchiveReport($idSites, $pluginName); + $this->reArchiveReport($idSites, $pluginName, $report, $startDate); } catch (\Throwable $ex) { $logger = StaticContainer::get(LoggerInterface::class); $logger->info("Failed to schedule rearchiving of past reports for $pluginName plugin."); @@ -680,4 +676,19 @@ class ArchiveInvalidator $model = new \Piwik\Plugins\SitesManager\Model(); return $model->getSitesId(); } + + private function getEarliestDateToRearchive() + { + $lastNMonthsToInvalidate = Config::getInstance()->General['rearchive_reports_in_past_last_n_months']; + if (empty($lastNMonthsToInvalidate)) { + return null; + } + + $lastNMonthsToInvalidate = (int) substr($lastNMonthsToInvalidate, 4); + if (empty($lastNMonthsToInvalidate)) { + return null; + } + + return Date::yesterday()->subMonth($lastNMonthsToInvalidate)->setDay(1); + } } diff --git a/core/Plugin.php b/core/Plugin.php index 9f18f37c42..de37b52611 100644 --- a/core/Plugin.php +++ b/core/Plugin.php @@ -514,6 +514,34 @@ class Plugin } /** + * @return Date|null + * @throws \Exception + */ + public function getPluginLastActivationTime() + { + $optionName = Manager::LAST_PLUGIN_ACTIVATION_TIME_OPTION_PREFIX . $this->pluginName; + $time = Option::get($optionName); + if (empty($time)) { + return null; + } + return Date::factory($time); + } + + /** + * @return Date|null + * @throws \Exception + */ + public function getPluginLastDeactivationTime() + { + $optionName = Manager::LAST_PLUGIN_DEACTIVATION_TIME_OPTION_PREFIX . $this->pluginName; + $time = Option::get($optionName); + if (empty($time)) { + return null; + } + return Date::factory($time); + } + + /** * @param $directoryWithinPlugin * @param $expectedSubclass * @return array @@ -583,5 +611,6 @@ class Plugin return $dependency; } } + } diff --git a/core/Plugin/Manager.php b/core/Plugin/Manager.php index 67ba2bf6e1..f12c5ce88e 100644 --- a/core/Plugin/Manager.php +++ b/core/Plugin/Manager.php @@ -16,12 +16,14 @@ use Piwik\Common; use Piwik\Config; use Piwik\Config as PiwikConfig; use Piwik\Container\StaticContainer; +use Piwik\Date; use Piwik\Development; use Piwik\EventDispatcher; use Piwik\Exception\PluginDeactivatedException; use Piwik\Filesystem; use Piwik\Log; use Piwik\Notification; +use Piwik\Option; use Piwik\Piwik; use Piwik\Plugin; use Piwik\Plugin\Dimension\ActionDimension; @@ -39,6 +41,9 @@ use Piwik\Updater; */ class Manager { + const LAST_PLUGIN_ACTIVATION_TIME_OPTION_PREFIX = 'LastPluginActivation.'; + const LAST_PLUGIN_DEACTIVATION_TIME_OPTION_PREFIX = 'LastPluginDeactivation.'; + /** * @return self */ @@ -512,10 +517,17 @@ class Manager */ public function deactivatePlugin($pluginName) { + $plugins = $this->pluginList->getActivatedPlugins(); + if (!in_array($pluginName, $plugins)) { + // plugin is already deactivated + return; + } + $this->clearCache($pluginName); // execute deactivate() to let the plugin do cleanups $this->executePluginDeactivate($pluginName); + $this->savePluginDeactivationTime($pluginName); $this->unloadPluginFromMemory($pluginName); @@ -680,6 +692,8 @@ class Manager $this->installPluginIfNecessary($plugin); $plugin->activate(); + $this->savePluginActivationTime($pluginName); + EventDispatcher::getInstance()->postPendingEventsTo($plugin); $this->pluginsToLoad[] = $pluginName; @@ -1654,5 +1668,17 @@ class Manager $translator->addDirectory(self::getPluginDirectory($pluginName) . '/lang'); } } + + private function savePluginActivationTime($pluginName) + { + $optionName = self::LAST_PLUGIN_ACTIVATION_TIME_OPTION_PREFIX . $pluginName; + Option::set($optionName, time()); + } + + private function savePluginDeactivationTime($pluginName) + { + $optionName = self::LAST_PLUGIN_DEACTIVATION_TIME_OPTION_PREFIX . $pluginName; + Option::set($optionName, time()); + } } |