Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Steur <thomas.steur@gmail.com>2013-09-13 06:05:01 +0400
committerThomas Steur <thomas.steur@gmail.com>2013-09-13 06:09:32 +0400
commita752dec91b0e236c0ecf10f3f3d67e69cdc5a08e (patch)
tree762c26487be87b32026d0c06350d13ba5bf04f01 /plugins/CorePluginsAdmin/PluginInstaller.php
parent330bde599424559dee2d08fef4fc1743aa76c2fe (diff)
current state of installing plugin from marketplace
Diffstat (limited to 'plugins/CorePluginsAdmin/PluginInstaller.php')
-rw-r--r--plugins/CorePluginsAdmin/PluginInstaller.php111
1 files changed, 111 insertions, 0 deletions
diff --git a/plugins/CorePluginsAdmin/PluginInstaller.php b/plugins/CorePluginsAdmin/PluginInstaller.php
new file mode 100644
index 0000000000..c6c54b89c1
--- /dev/null
+++ b/plugins/CorePluginsAdmin/PluginInstaller.php
@@ -0,0 +1,111 @@
+<?php
+/**
+ * Piwik - Open source web analytics
+ *
+ * @link http://piwik.org
+ * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
+ *
+ * @category Piwik_Plugins
+ * @package CorePluginsAdmin
+ */
+namespace Piwik\Plugins\CorePluginsAdmin;
+use Piwik\Http;
+use Piwik\Piwik;
+use Piwik\Unzip;
+
+/**
+ *
+ * @package CorePluginsAdmin
+ */
+class PluginInstaller
+{
+ const PATH_TO_DOWNLOAD = '/tmp/plugins/';
+ const PATH_TO_EXTRACT = '/plugins/';
+
+ private $pluginName;
+
+ public function __construct($pluginName)
+ {
+ $this->pluginName = $pluginName;
+ }
+
+ public function installOrUpdatePluginFromMarketplace()
+ {
+ $tmpPluginZip = PIWIK_USER_PATH . self::PATH_TO_DOWNLOAD . $this->pluginName . '.zip';
+ $tmpPluginFolder = PIWIK_USER_PATH . self::PATH_TO_DOWNLOAD . $this->pluginName;
+
+ $this->makeSureFoldersAreWritable();
+ $this->downloadPluginFromMarketplace($tmpPluginZip);
+ $this->extractPluginFiles($tmpPluginZip, $tmpPluginFolder);
+ $this->copyPluginToDestination($tmpPluginFolder);
+
+ $this->removeFileIfExists($tmpPluginZip);
+ $this->removeFolderIfExists($tmpPluginFolder);
+ }
+
+ private function makeSureFoldersAreWritable()
+ {
+ Piwik::dieIfDirectoriesNotWritable(array(self::PATH_TO_DOWNLOAD, self::PATH_TO_EXTRACT));
+ }
+
+ private function downloadPluginFromMarketplace($pluginZipTargetFile)
+ {
+ $this->removeFileIfExists($pluginZipTargetFile);
+
+ $marketplace = new MarketplaceApiClient();
+ $success = $marketplace->download($this->pluginName, $pluginZipTargetFile);
+
+ if (!$success) {
+ throw new \Exception('Failed to download plugin');
+ }
+ }
+
+ /**
+ * @param $pluginZipFile
+ * @param $pathExtracted
+ * @throws \Exception
+ */
+ private function extractPluginFiles($pluginZipFile, $pathExtracted)
+ {
+ $archive = Unzip::factory('PclZip', $pluginZipFile);
+
+ $this->removeFolderIfExists($pathExtracted);
+
+ if (0 == ($pluginFiles = $archive->extract($pathExtracted))) {
+ throw new \Exception(Piwik_TranslateException('Plugin_ExceptionArchiveIncompatible', $archive->errorInfo()));
+ }
+
+ if (0 == count($pluginFiles)) {
+ throw new \Exception(Piwik_TranslateException('Plugin Zip File Is Empty'));
+ }
+ }
+
+ private function copyPluginToDestination($tmpPluginFolder)
+ {
+ $pluginTargetPath = PIWIK_USER_PATH . self::PATH_TO_EXTRACT . $this->pluginName;
+
+ $this->removeFolderIfExists($pluginTargetPath);
+ Piwik::copyRecursive($tmpPluginFolder, $pluginTargetPath);
+ }
+
+ /**
+ * @param $pathExtracted
+ */
+ private function removeFolderIfExists($pathExtracted)
+ {
+ if (file_exists($pathExtracted)) {
+ Piwik::unlinkRecursive($pathExtracted, true);
+ }
+ }
+
+ /**
+ * @param $targetTmpFile
+ */
+ private function removeFileIfExists($targetTmpFile)
+ {
+ if (file_exists($targetTmpFile)) {
+ unlink($targetTmpFile);
+ }
+ }
+
+}