Skip to content

Commit

Permalink
Automatically update all marketplace plugins when updating Piwik (#10527
Browse files Browse the repository at this point in the history
)

* update plugins and piwik at the same time

* make sure plugins are updated with piwik

* use only one try/catch

* reload plugin information once it has been installed

* make sure to clear caches after an update

* fix ui tests

* make sure to use correct php version without any extras
  • Loading branch information
tsteur authored and mattab committed Sep 28, 2016
1 parent cc96c2e commit 2929e0c
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 16 deletions.
17 changes: 11 additions & 6 deletions core/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,17 +137,22 @@ public function __construct($pluginName = false)
if ($cache->contains($cacheId)) {
$this->pluginInformation = $cache->fetch($cacheId);
} else {
$metadataLoader = new MetadataLoader($pluginName);
$this->pluginInformation = $metadataLoader->load();

if ($this->hasDefinedPluginInformationInPluginClass() && $metadataLoader->hasPluginJson()) {
throw new \Exception('Plugin ' . $pluginName . ' has defined the method getInformation() and as well as having a plugin.json file. Please delete the getInformation() method from the plugin class. Alternatively, you may delete the plugin directory from plugins/' . $pluginName);
}
$this->reloadPluginInformation();

$cache->save($cacheId, $this->pluginInformation);
}
}

public function reloadPluginInformation()
{
$metadataLoader = new MetadataLoader($this->pluginName);
$this->pluginInformation = $metadataLoader->load();

if ($this->hasDefinedPluginInformationInPluginClass() && $metadataLoader->hasPluginJson()) {
throw new \Exception('Plugin ' . $this->pluginName . ' has defined the method getInformation() and as well as having a plugin.json file. Please delete the getInformation() method from the plugin class. Alternatively, you may delete the plugin directory from plugins/' . $this->pluginName);
}
}

private function createCacheIfNeeded()
{
if (is_null($this->cache)) {
Expand Down
10 changes: 9 additions & 1 deletion plugins/CorePluginsAdmin/MarketplaceApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace Piwik\Plugins\CorePluginsAdmin;

use Piwik\Cache;
use Piwik\Container\StaticContainer;
use Piwik\Http;
use Piwik\Version;

Expand Down Expand Up @@ -123,9 +124,16 @@ public function searchForThemes($keywords, $query, $sort)
return array();
}

public static function getPiwikVersion()
{
return StaticContainer::get('marketplacePiwikVersion');
}

private function fetch($action, $params)
{
ksort($params);
$params['piwik'] = self::getPiwikVersion();
$params['php'] = PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION . '.' . PHP_RELEASE_VERSION;
$query = http_build_query($params);

$cacheId = $this->getCacheKey($action, $query);
Expand Down Expand Up @@ -180,7 +188,7 @@ public function getDownloadUrl($pluginOrThemeName)
$latestVersion = array_pop($plugin['versions']);
$downloadUrl = $latestVersion['download'];

return $this->domain . $downloadUrl . '?coreVersion=' . Version::VERSION;
return $this->domain . $downloadUrl . '?coreVersion=' . self::getPiwikVersion();
}

}
9 changes: 9 additions & 0 deletions plugins/CorePluginsAdmin/PluginInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Piwik\Filechecks;
use Piwik\Filesystem;
use Piwik\Piwik;
use Piwik\Plugin\Manager as PluginManager;
use Piwik\Plugin\Dependency as PluginDependency;
use Piwik\Unzip;

Expand Down Expand Up @@ -49,6 +50,11 @@ public function installOrUpdatePluginFromMarketplace()

Filesystem::deleteAllCacheOnUpdate($this->pluginName);

$plugin = PluginManager::getInstance()->getLoadedPlugin($this->pluginName);
if (!empty($plugin)) {
$plugin->reloadPluginInformation();
}

} catch (\Exception $e) {

$this->removeFileIfExists($tmpPluginZip);
Expand Down Expand Up @@ -160,7 +166,10 @@ private function makeSureThereAreNoMissingRequirements($metadata)
$requires = (array) $metadata->require;
}

$piwikVersion = MarketplaceApiClient::getPiwikVersion();

$dependency = new PluginDependency();
$dependency->setPiwikVersion($piwikVersion);
$missingDependencies = $dependency->getMissingDependencies($requires);

if (!empty($missingDependencies)) {
Expand Down
7 changes: 7 additions & 0 deletions plugins/CorePluginsAdmin/config/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

return array(
'marketplacePiwikVersion' => function () {
return \Piwik\Version::VERSION;
}
);
2 changes: 2 additions & 0 deletions plugins/CoreUpdater/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ public function oneClickUpdate()
$messages = $e->getUpdateLogMessages();
}

Filesystem::deleteAllCacheOnUpdate();

$view->feedbackMessages = $messages;
$this->addCustomLogoInfo($view);
return $view->render();
Expand Down
54 changes: 48 additions & 6 deletions plugins/CoreUpdater/Updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,20 @@

use Exception;
use Piwik\ArchiveProcessor\Rules;
use Piwik\Config;
use Piwik\Container\StaticContainer;
use Piwik\Filechecks;
use Piwik\Filesystem;
use Piwik\Http;
use Piwik\Option;
use Piwik\Plugin\Manager as PluginManager;
use Piwik\Plugin\ReleaseChannels;
use Piwik\Plugins\CorePluginsAdmin\CorePluginsAdmin;
use Piwik\Plugins\CorePluginsAdmin\MarketplaceApiClient;
use Piwik\Plugins\CorePluginsAdmin\MarketplaceApiException;
use Piwik\Plugins\CorePluginsAdmin\PluginInstaller;
use Piwik\SettingsServer;
use Piwik\Translation\Translator;
use Piwik\Unzip;
use Piwik\UpdateCheck;
use Piwik\Version;

class Updater
Expand Down Expand Up @@ -112,19 +115,58 @@ public function updatePiwik($https = true)
$this->verifyDecompressedArchive($extractedArchiveDirectory);
$messages[] = $this->translator->translate('CoreUpdater_VerifyingUnpackedFiles');

$disabledPluginNames = $this->disableIncompatiblePlugins($newVersion);
if (!empty($disabledPluginNames)) {
$messages[] = $this->translator->translate('CoreUpdater_DisablingIncompatiblePlugins', implode(', ', $disabledPluginNames));
}
// we need to load the marketplace already here, otherwise it will use the new, updated file in Piwik 3
$marketplace = new MarketplaceApiClient();
require_once PIWIK_DOCUMENT_ROOT . '/plugins/CorePluginsAdmin/PluginInstaller.php';
require_once PIWIK_DOCUMENT_ROOT . '/plugins/CorePluginsAdmin/MarketplaceApiException.php';

$this->installNewFiles($extractedArchiveDirectory);
$messages[] = $this->translator->translate('CoreUpdater_InstallingTheLatestVersion');

} catch (ArchiveDownloadException $e) {
throw $e;
} catch (Exception $e) {
throw new UpdaterException($e, $messages);
}

try {
if (CorePluginsAdmin::isMarketplaceEnabled()) {
$messages[] = $this->translator->translate('CoreUpdater_CheckingForPluginUpdates');

$pluginManager = PluginManager::getInstance();
$pluginManager->loadAllPluginsAndGetTheirInfo();
$loadedPlugins = $pluginManager->getLoadedPlugins();

MarketplaceApiClient::clearAllCacheEntries();
StaticContainer::getContainer()->set('marketplacePiwikVersion', $newVersion);

$pluginsWithUpdate = $marketplace->checkUpdates($loadedPlugins);

foreach ($pluginsWithUpdate as $pluginWithUpdate) {
$pluginName = $pluginWithUpdate['name'];

$messages[] = $this->translator->translate('CoreUpdater_UpdatingPluginXToVersionY',
array($pluginName, $pluginWithUpdate['version']));

$pluginInstaller = new PluginInstaller($pluginName);
$pluginInstaller->installOrUpdatePluginFromMarketplace();
}
}
} catch (MarketplaceApiException $e) {
// there is a problem with the connection to the server, ignore for now
} catch (Exception $e) {
throw new UpdaterException($e, $messages);
}

try {
$disabledPluginNames = $this->disableIncompatiblePlugins($newVersion);
if (!empty($disabledPluginNames)) {
$messages[] = $this->translator->translate('CoreUpdater_DisablingIncompatiblePlugins', implode(', ', $disabledPluginNames));
}
} catch (Exception $e) {
throw new UpdaterException($e, $messages);
}

return $messages;
}

Expand Down
4 changes: 3 additions & 1 deletion plugins/CoreUpdater/lang/en.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"CoreUpdater": {
"CheckingForPluginUpdates": "Checking for new plugin updates",
"ClickHereToViewSqlQueries": "Click here to view and copy the list of SQL queries that will get executed",
"CriticalErrorDuringTheUpgradeProcess": "Critical Error during the update process:",
"DatabaseUpgradeRequired": "Database Upgrade Required",
Expand All @@ -23,7 +24,7 @@
"HelpMessageIntroductionWhenError": "The above is the core error message. It should help explain the cause, but if you require further help please:",
"HelpMessageIntroductionWhenWarning": "The update completed successfuly, however there were issues during the process. Please read the above descriptions for details. For further help:",
"HighTrafficPiwikServerEnableMaintenance": "If you manage a high traffic Piwik server, we recommend to %1$smomentarily disable visitor Tracking and put the Piwik User Interface in maintenance mode%2$s.",
"IncompatbilePluginsWillBeDisabledInfo": "Note: some plugins are not compatible with Piwik %s. They will be disabled when you upgrade:",
"IncompatbilePluginsWillBeDisabledInfo": "Note: some plugins are not compatible with Piwik %s. We will update them if there is an update on the Marketplace, otherwise we will be disable them when you upgrade:",
"InstallingTheLatestVersion": "Installing the latest version",
"LatestBetaRelease": "Latest beta release",
"LatestStableRelease": "Latest stable release",
Expand Down Expand Up @@ -52,6 +53,7 @@
"UpdateAutomatically": "Update Automatically",
"UpdateHasBeenCancelledExplanation": "Piwik One Click Update has been cancelled. If you can't fix the above error message, it is recommended that you manually update Piwik. %1$s Please check out the %2$sUpdate documentation%3$s to get started!",
"UpdateTitle": "Update",
"UpdatingPluginXToVersionY": "Updating plugin %1$s to version %2$s",
"UpdateSuccessTitle": "Piwik has been successfully updated!",
"UpdateErrorTitle": "Update error",
"ThankYouUpdatePiwik": "Thank you for using Piwik and keeping it up to date!",
Expand Down
4 changes: 2 additions & 2 deletions tests/UI/expected-screenshots/CoreUpdaterCode_newVersion.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 2929e0c

Please sign in to comment.