From 587cc39e0362719332d410b7a4d5ddcc68788eeb Mon Sep 17 00:00:00 2001 From: Thomas Steur Date: Tue, 15 Nov 2016 14:03:59 +1300 Subject: Update Marketplace to work with new API (#10799) * starting to port marketplace to piwik 3 * updating tests * fix translation key * fix various issues * use material select * fix plugin upload * deprecate license_homepage plugin metadata and link to a LICENSE[.md|.txt] file if found (#10756) * deprecate license_homepage plugin metadata, and link to a LICENSE[.md|.txt] file if found * Make license view HTML only without menu * fix tests and update * fix some links did not work * we need to show warnings even when plugin is installed, not only when activated. otherwise it is not clear why something is not downloadable * fix install was not working * improved responsiveness of marketplace * fix more tests * fix search was shown when only a few plugins are there * fix ui tests * fix some translations * fix tests and remove duplicated test --- plugins/CorePluginsAdmin/PluginInstaller.php | 85 +++++++++++++++++++--------- 1 file changed, 57 insertions(+), 28 deletions(-) (limited to 'plugins/CorePluginsAdmin/PluginInstaller.php') diff --git a/plugins/CorePluginsAdmin/PluginInstaller.php b/plugins/CorePluginsAdmin/PluginInstaller.php index ab4f7b8622..d2fad35c76 100644 --- a/plugins/CorePluginsAdmin/PluginInstaller.php +++ b/plugins/CorePluginsAdmin/PluginInstaller.php @@ -8,13 +8,16 @@ */ namespace Piwik\Plugins\CorePluginsAdmin; +use Piwik\Common; use Piwik\Container\StaticContainer; use Piwik\Filechecks; use Piwik\Filesystem; use Piwik\Piwik; use Piwik\Plugin\Manager as PluginManager; use Piwik\Plugin\Dependency as PluginDependency; +use Piwik\Plugins\Marketplace\Marketplace; use Piwik\Unzip; +use Piwik\Plugins\Marketplace\Api\Client; /** * @@ -26,22 +29,38 @@ class PluginInstaller private $pluginName; - public function __construct($pluginName) + /** + * Null if Marketplace Plugin is not installed + * @var Client|null + */ + private $marketplaceClient; + + /** + * PluginInstaller constructor. + * @param Client|null $client + */ + public function __construct($client = null) { - $this->pluginName = $pluginName; + if (!empty($client)) { + $this->marketplaceClient = $client; + } elseif (Marketplace::isMarketplaceEnabled()) { + // we load it manually as marketplace might not be loaded + $this->marketplaceClient = StaticContainer::get('Piwik\Plugins\Marketplace\Api\Client'); + } } - public function installOrUpdatePluginFromMarketplace() + public function installOrUpdatePluginFromMarketplace($pluginName) { - $tmpPluginPath = StaticContainer::get('path.tmp') . '/latest/plugins/'; + $this->checkMarketplaceIsEnabled(); - $tmpPluginZip = $tmpPluginPath . $this->pluginName . '.zip'; - $tmpPluginFolder = $tmpPluginPath . $this->pluginName; + $this->pluginName = $pluginName; try { $this->makeSureFoldersAreWritable(); $this->makeSurePluginNameIsValid(); - $this->downloadPluginFromMarketplace($tmpPluginZip); + + $tmpPluginZip = $this->downloadPluginFromMarketplace(); + $tmpPluginFolder = dirname($tmpPluginZip) . '/' . basename($tmpPluginZip, '.zip') . '/'; $this->extractPluginFiles($tmpPluginZip, $tmpPluginFolder); $this->makeSurePluginJsonExists($tmpPluginFolder); $metadata = $this->getPluginMetadataIfValid($tmpPluginFolder); @@ -50,15 +69,22 @@ class PluginInstaller Filesystem::deleteAllCacheOnUpdate($this->pluginName); - $plugin = PluginManager::getInstance()->getLoadedPlugin($this->pluginName); - if (!empty($plugin)) { - $plugin->reloadPluginInformation(); + $pluginManager = PluginManager::getInstance(); + if ($pluginManager->isPluginLoaded($this->pluginName)) { + $plugin = PluginManager::getInstance()->getLoadedPlugin($this->pluginName); + if (!empty($plugin)) { + $plugin->reloadPluginInformation(); + } } } catch (\Exception $e) { - $this->removeFileIfExists($tmpPluginZip); - $this->removeFolderIfExists($tmpPluginFolder); + if (!empty($tmpPluginZip)) { + Filesystem::deleteFileIfExists($tmpPluginZip); + } + if (!empty($tmpPluginFolder)) { + $this->removeFolderIfExists($tmpPluginFolder); + } throw $e; } @@ -69,7 +95,8 @@ class PluginInstaller public function installOrUpdatePluginFromFile($pathToZip) { - $tmpPluginFolder = StaticContainer::get('path.tmp') . self::PATH_TO_DOWNLOAD . $this->pluginName; + $tmpPluginName = 'uploaded' . Common::generateUniqId(); + $tmpPluginFolder = StaticContainer::get('path.tmp') . self::PATH_TO_DOWNLOAD . $tmpPluginName; try { $this->makeSureFoldersAreWritable(); @@ -108,18 +135,18 @@ class PluginInstaller )); } - private function downloadPluginFromMarketplace($pluginZipTargetFile) + /** + * @return false|string false on failed download, or a path to the downloaded zip file + * @throws PluginInstallerException + */ + private function downloadPluginFromMarketplace() { - $this->removeFileIfExists($pluginZipTargetFile); - - $marketplace = new MarketplaceApiClient(); - try { - $marketplace->download($this->pluginName, $pluginZipTargetFile); + return $this->marketplaceClient->download($this->pluginName); } catch (\Exception $e) { try { - $downloadUrl = $marketplace->getDownloadUrl($this->pluginName); + $downloadUrl = $this->marketplaceClient->getDownloadUrl($this->pluginName); $errorMessage = sprintf('Failed to download plugin from %s: %s', $downloadUrl, $e->getMessage()); } catch (\Exception $ex) { @@ -166,10 +193,8 @@ class PluginInstaller $requires = (array) $metadata->require; } - $piwikVersion = MarketplaceApiClient::getPiwikVersion(); - $dependency = new PluginDependency(); - $dependency->setPiwikVersion($piwikVersion); + $dependency->setEnvironment($this->marketplaceClient->getEnvironment()); $missingDependencies = $dependency->getMissingDependencies($requires); if (!empty($missingDependencies)) { @@ -289,9 +314,7 @@ class PluginInstaller */ private function removeFileIfExists($targetTmpFile) { - if (file_exists($targetTmpFile)) { - unlink($targetTmpFile); - } + Filesystem::deleteFileIfExists($targetTmpFile); } /** @@ -300,8 +323,7 @@ class PluginInstaller private function makeSurePluginNameIsValid() { try { - $marketplace = new MarketplaceApiClient(); - $pluginDetails = $marketplace->getPluginInfo($this->pluginName); + $pluginDetails = $this->marketplaceClient->getPluginInfo($this->pluginName); } catch (\Exception $e) { throw new PluginInstallerException($e->getMessage()); } @@ -311,4 +333,11 @@ class PluginInstaller } } + private function checkMarketplaceIsEnabled() + { + if (!isset($this->marketplaceClient)) { + throw new PluginInstallerException('Marketplace plugin needs to be enabled to perform this action.'); + } + } + } -- cgit v1.2.3