From ded86bb8518c263ee8cca24541cd8a612aafb330 Mon Sep 17 00:00:00 2001
From: Stefan Giehl <stefan@piwik.org>
Date: Tue, 1 Nov 2016 23:03:54 +0100
Subject: [PATCH] Ensure component updated event is triggered even if there is
 no update file (#10807)

* ensure component updated event is triggered even if there is no update file
* improve events triggered during component changes
* improve wordings
---
 CHANGELOG.md            |  2 ++
 core/Plugin/Manager.php |  2 +-
 core/Updater.php        | 76 +++++++++++++++++++++++++----------------
 3 files changed, 49 insertions(+), 31 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 08e09abd05..648a77f6c5 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -76,6 +76,8 @@ Read more about migrating a plugin from Piwik 2.X to Piwik 3 on our [Migration g
  * `Updater.componentUpdated` triggered after core or a plugin has been updated
  * `PluginManager.pluginInstalled` triggered after a plugin was installed
  * `PluginManager.pluginUninstalled` triggered after a plugin was uninstalled
+ * `Updater.componentInstalled` triggered after a component was installed
+ * `Updater.componentUninstalled` triggered after a component was uninstalled
 * New HTTP Tracking API parameter `pv_id` which accepts a six character unique ID that identifies which actions were performed on a specific page view. Read more about it in the [HTTP Tracking API](https://developer.piwik.org/api-reference/tracking-api);
 * New event `Segment.addSegments` that lets you add segments.
 
diff --git a/core/Plugin/Manager.php b/core/Plugin/Manager.php
index 9273ea1019..9455f06c0b 100644
--- a/core/Plugin/Manager.php
+++ b/core/Plugin/Manager.php
@@ -1087,7 +1087,7 @@ class Manager
             $pluginsInstalled[] = $pluginName;
             $this->updatePluginsInstalledConfig($pluginsInstalled);
             $updater = new Updater();
-            $updater->markComponentSuccessfullyUpdated($plugin->getPluginName(), $plugin->getVersion());
+            $updater->markComponentSuccessfullyUpdated($plugin->getPluginName(), $plugin->getVersion(), $isNew = true);
             $saveConfig = true;
 
             /**
diff --git a/core/Updater.php b/core/Updater.php
index d53280f4da..e969add924 100644
--- a/core/Updater.php
+++ b/core/Updater.php
@@ -83,14 +83,52 @@ class Updater
      *
      * @param string $name The component name. Eg, a plugin name, `'core'` or dimension column name.
      * @param string $version The component version (should use semantic versioning).
+     * @param bool   $isNew indicates if the component is a new one (for plugins)
      */
-    public function markComponentSuccessfullyUpdated($name, $version)
+    public function markComponentSuccessfullyUpdated($name, $version, $isNew = false)
     {
         try {
             Option::set(self::getNameInOptionTable($name), $version, $autoLoad = 1);
         } catch (\Exception $e) {
             // case when the option table is not yet created (before 0.2.10)
         }
+
+        if ($isNew) {
+
+            /**
+             * Event triggered after a new component has been installed.
+             *
+             * @param string $name The component that has been installed.
+             */
+            Piwik::postEvent('Updater.componentInstalled', array($name));
+
+            return;
+        }
+
+        /**
+         * Event triggered after a component has been updated.
+         *
+         * Can be used to handle logic that should be done after a component was updated
+         *
+         * **Example**
+         *
+         *     Piwik::addAction('Updater.componentUpdated', function ($componentName, $updatedVersion) {
+         *          $mail = new Mail();
+         *          $mail->setDefaultFromPiwik();
+         *          $mail->addTo('test@example.org');
+         *          $mail->setSubject('Component was updated);
+         *          $message = sprintf(
+         *              'Component %1$s has been updated to version %2$s',
+         *              $componentName, $updatedVersion
+         *          );
+         *          $mail->setBodyText($message);
+         *          $mail->send();
+         *     });
+         *
+         * @param string $componentName 'core', plugin name or dimension name
+         * @param string $updatedVersion version updated to
+         */
+        Piwik::postEvent('Updater.componentUpdated', array($name, $version));
     }
 
     /**
@@ -106,6 +144,13 @@ class Updater
         } catch (\Exception $e) {
             // case when the option table is not yet created (before 0.2.10)
         }
+
+        /**
+         * Event triggered after a component has been uninstalled.
+         *
+         * @param string $name The component that has been uninstalled.
+         */
+        Piwik::postEvent('Updater.componentUninstalled', array($name));
     }
 
     /**
@@ -280,35 +325,6 @@ class Updater
 
         $this->executeListenerHook('onComponentUpdateFinished', array($componentName, $updatedVersion, $warningMessages));
 
-        /**
-         * Event triggered after a component has been updated.
-         *
-         * Can be used to handle stuff that should be done after a component was updated
-         *
-         * **Example**
-         *
-         *     Piwik::addAction('Updater.componentUpdated', function ($componentName, $updatedVersion, $warningMessages) {
-         *          $mail = new Mail();
-         *          $mail->setDefaultFromPiwik();
-         *          $mail->addTo('test@example.org');
-         *          $mail->setSubject('Component was updated);
-         *          $message = sprintf(
-         *              'Component %1$s has been updated to version %2$s',
-         *              $componentName, $updatedVersion
-         *          );
-         *          if (!empty($warningMessages)) {
-         *              $message .= "Some warnings occured:\n" . implode("\n", $warningMessages);
-         *          }
-         *          $mail->setBodyText($message);
-         *          $mail->send();
-         *     });
-         *
-         * @param string $componentName 'core', or plugin name
-         * @param string $updatedVersion version updated to
-         * @param array  $warningMessages warnings occurred during update
-         */
-        Piwik::postEvent('Updater.componentUpdated', array($componentName, $updatedVersion, $warningMessages));
-
         return $warningMessages;
     }
 
-- 
GitLab