diff options
-rw-r--r-- | core/Archive/ArchiveInvalidator.php | 37 | ||||
-rw-r--r-- | core/Plugin.php | 29 | ||||
-rw-r--r-- | core/Plugin/Manager.php | 26 | ||||
m--------- | plugins/LoginLdap | 0 | ||||
-rw-r--r-- | tests/PHPUnit/Integration/Plugin/ManagerTest.php | 28 |
5 files changed, 106 insertions, 14 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()); + } } diff --git a/plugins/LoginLdap b/plugins/LoginLdap -Subproject eae0cb494dc424fb2d710db7c5170822cad32bf +Subproject e9cffd2a996ad001c6998d9ff00bfd99379d8a1 diff --git a/tests/PHPUnit/Integration/Plugin/ManagerTest.php b/tests/PHPUnit/Integration/Plugin/ManagerTest.php index 65070e54dd..52c7361bd2 100644 --- a/tests/PHPUnit/Integration/Plugin/ManagerTest.php +++ b/tests/PHPUnit/Integration/Plugin/ManagerTest.php @@ -78,13 +78,39 @@ class ManagerTest extends IntegrationTestCase $this->assertEquals(array(), $this->manager->getLoadedPlugins()); } - public function test_deactivatePlugin() + public function test_activateDeactivatePlugin() { + $plugin = new Plugin('ExampleTheme'); + + $this->assertNull($plugin->getPluginLastActivationTime()); + $this->assertNull($plugin->getPluginLastDeactivationTime()); + $this->assertFalse($this->manager->isPluginActivated('ExampleTheme')); $this->manager->activatePlugin('ExampleTheme'); + + $lastActivationTime = $plugin->getPluginLastActivationTime(); + $this->assertNotNull($lastActivationTime); + + $this->assertNull($plugin->getPluginLastDeactivationTime()); + $this->assertTrue($this->manager->isPluginActivated('ExampleTheme')); $this->manager->deactivatePlugin('ExampleTheme'); $this->assertFalse($this->manager->isPluginActivated('ExampleTheme')); + + $lastDeactivationTime = $plugin->getPluginLastDeactivationTime(); + $this->assertNotNull($lastDeactivationTime); + + sleep(1); + + $this->manager->activatePlugin('ExampleTheme'); + + $nextLastActivationTime = $plugin->getPluginLastActivationTime(); + $this->assertGreaterThan($lastActivationTime->getTimestamp(), $nextLastActivationTime->getTimestamp()); + + $this->manager->deactivatePlugin('ExampleTheme'); + + $nextLastDeactivationTime = $plugin->getPluginLastDeactivationTime(); + $this->assertGreaterThan($lastDeactivationTime->getTimestamp(), $nextLastDeactivationTime->getTimestamp()); } /** @see Issue https://github.com/piwik/piwik/issues/8422 */ |