Add name attribute for event listeners
authorMatthias Schmidt <gravatronics@live.com>
Sat, 23 May 2015 05:44:49 +0000 (07:44 +0200)
committerMatthias Schmidt <gravatronics@live.com>
Sat, 23 May 2015 05:44:49 +0000 (07:44 +0200)
CHANGELOG.md
com.woltlab.wcf/eventListener.xml
wcfsetup/install/files/lib/data/event/listener/EventListener.class.php
wcfsetup/install/files/lib/system/package/plugin/EventListenerPackageInstallationPlugin.class.php
wcfsetup/setup/db/install.sql

index 5bd41ca23d082dcae46d863a8cfd9a2303c3b9c0..9a1ba55aec8970ef019450711d1225a0181f7c18 100644 (file)
@@ -6,6 +6,7 @@
 
 * `options` support for cronjobs.
 * `permissions` and `options` support for event listeners.
+* `name` attribute for event listener PIP (`listenerName` for event listener objects).
 * `permissions` and `options` support for template listeners.
 * `wcf\data\TDatabaseObjectOptions` and `wcf\data\TDatabaseObjectPermissions` for database object-bound options and permissions validation.
 * `wcf\system\cache\builder\EventListenerCacheBuilder` returns `wcf\data\event\listener\EventListener` objects instead of data arrays.
index 29ef8c40ed0a53fcd53fefa0064704dbb27c676f..d3dd425772470df61ca1f36613a8eef2d8b79cef 100644 (file)
@@ -1,21 +1,21 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <data xmlns="http://www.woltlab.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.woltlab.com http://www.woltlab.com/XSD/maelstrom/eventListener.xsd">
        <import>
-               <eventlistener>
+               <eventlistener name="sessionPageAccessLog">
                        <eventclassname><![CDATA[wcf\page\AbstractPage]]></eventclassname>
                        <eventname>readParameters</eventname>
                        <environment>admin</environment>
                        <inherit>1</inherit>
                        <listenerclassname><![CDATA[wcf\system\event\listener\SessionAccessLogListener]]></listenerclassname>
                </eventlistener>
-               <eventlistener>
+               <eventlistener name="sessionFormAccessLog">
                        <eventclassname><![CDATA[wcf\form\AbstractForm]]></eventclassname>
                        <eventname>readParameters</eventname>
                        <environment>admin</environment>
                        <inherit>1</inherit>
                        <listenerclassname><![CDATA[wcf\system\event\listener\SessionAccessLogListener]]></listenerclassname>
                </eventlistener>
-               <eventlistener>
+               <eventlistener name="sessionActionAccessLog">
                        <eventclassname><![CDATA[wcf\action\AbstractAction]]></eventclassname>
                        <eventname>readParameters</eventname>
                        <environment>admin</environment>
        </import>
        
        <delete>
+               <!-- event listeners with names now -->
+               <eventlistener>
+                       <eventclassname><![CDATA[wcf\page\AbstractPage]]></eventclassname>
+                       <eventname>readParameters</eventname>
+                       <environment>admin</environment>
+                       <inherit>1</inherit>
+                       <listenerclassname><![CDATA[wcf\system\event\listener\SessionAccessLogListener]]></listenerclassname>
+               </eventlistener>
+               <eventlistener>
+                       <eventclassname><![CDATA[wcf\form\AbstractForm]]></eventclassname>
+                       <eventname>readParameters</eventname>
+                       <environment>admin</environment>
+                       <inherit>1</inherit>
+                       <listenerclassname><![CDATA[wcf\system\event\listener\SessionAccessLogListener]]></listenerclassname>
+               </eventlistener>
+               <eventlistener>
+                       <eventclassname><![CDATA[wcf\action\AbstractAction]]></eventclassname>
+                       <eventname>readParameters</eventname>
+                       <environment>admin</environment>
+                       <inherit>1</inherit>
+                       <listenerclassname><![CDATA[wcf\system\event\listener\SessionAccessLogListener]]></listenerclassname>
+               </eventlistener>
+               <!-- /event listeners with names now -->
+               
                <eventlistener>
                        <eventclassname><![CDATA[wcf\system\bbcode\PreParser]]></eventclassname>
                        <eventname>beforeParsing</eventname>
index bb64640b4d3aa1ebcd95abfaed2814a1f4cab95b..49d91cae7a0a89b16cef3a33efe3171ffddf4dc8 100644 (file)
@@ -27,4 +27,11 @@ class EventListener extends DatabaseObject {
         * @see \wcf\data\DatabaseObject::$databaseTableIndexName
         */
        protected static $databaseTableIndexName = 'listenerID';
+       
+       /**
+        * prefix of automatically created event listener names
+        * @var string
+        * @deprecated  will be removed once listener names are mandatory
+        */
+       const AUTOMATIC_NAME_PREFIX = 'com.woltlab.wcf.eventListener';
 }
index 0ef990f8523716cc29ea89c911427c10bda8e438..4af83f14c1d9d46fd2038ba1759eacdadd60744f 100644 (file)
@@ -1,5 +1,7 @@
 <?php
 namespace wcf\system\package\plugin;
+use wcf\data\event\listener\EventListener;
+use wcf\data\event\listener\EventListenerEditor;
 use wcf\system\cache\builder\EventListenerCacheBuilder;
 use wcf\system\WCF;
 
@@ -35,16 +37,30 @@ class EventListenerPackageInstallationPlugin extends AbstractXMLPackageInstallat
                                        AND eventName = ?
                                        AND inherit = ?
                                        AND listenerClassName = ?";
+               $legacyStatement = WCF::getDB()->prepareStatement($sql);
+               
+               $sql = "DELETE FROM     wcf".WCF_N."_".$this->tableName."
+                       WHERE           packageID = ?
+                                       AND listenerName = ?";
                $statement = WCF::getDB()->prepareStatement($sql);
+               
                foreach ($items as $item) {
-                       $statement->execute(array(
-                               $this->installation->getPackageID(),
-                               (isset($item['elements']['environment']) ? $item['elements']['environment'] : 'user'),
-                               $item['elements']['eventclassname'],
-                               $item['elements']['eventname'],
-                               (isset($item['elements']['inherit'])) ? $item['elements']['inherit'] : 0,
-                               $item['elements']['listenerclassname']
-                       ));
+                       if (!isset($item['attributes']['name'])) {
+                               $legacyStatement->execute(array(
+                                       $this->installation->getPackageID(),
+                                       (isset($item['elements']['environment']) ? $item['elements']['environment'] : 'user'),
+                                       $item['elements']['eventclassname'],
+                                       $item['elements']['eventname'],
+                                       (isset($item['elements']['inherit'])) ? $item['elements']['inherit'] : 0,
+                                       $item['elements']['listenerclassname']
+                               ));
+                       }
+                       else {
+                               $statement->execute(array(
+                                       $this->installation->getPackageID(),
+                                       $item['attributes']['name']
+                               ));
+                       }
                }
        }
        
@@ -62,30 +78,68 @@ class EventListenerPackageInstallationPlugin extends AbstractXMLPackageInstallat
                        'eventName' => $data['elements']['eventname'],
                        'inherit' => (isset($data['elements']['inherit'])) ? intval($data['elements']['inherit']) : 0,
                        'listenerClassName' => $data['elements']['listenerclassname'],
+                       'listenerName' => (isset($data['attributes']['name']) ? $data['attributes']['name'] : ''),
                        'niceValue' => $nice,
                        'options' => (isset($data['elements']['options']) ? $data['elements']['options'] : ''),
                        'permissions' => (isset($data['elements']['permissions']) ? $data['elements']['permissions'] : '')
                );
        }
        
+       /**
+        * @see \wcf\system\package\plugin\AbstractXMLPackageInstallationPlugin::import()
+        */
+       protected function import(array $row, array $data) {
+               // if an event listener is updated without a name given, keep the
+               // old automatically assigned name
+               if (!empty($row) && !$data['listenerName']) {
+                       unset($data['listenerName']);
+               }
+               
+               $eventListener = parent::import($row, $data);
+               
+               // update event listener name
+               if (!$eventListener->listenerName) {
+                       $eventListenerEditor = new EventListenerEditor($eventListener);
+                       $eventListenerEditor->update(array(
+                               'listenerName' => EventListener::AUTOMATIC_NAME_PREFIX.$eventListener->listenerID
+                       ));
+                       
+                       $eventListener = new EventListener($eventListener->listenerID);
+               }
+               
+               return $eventListener;
+       }
+       
        /**
         * @see \wcf\system\package\plugin\AbstractXMLPackageInstallationPlugin::findExistingItem()
         */
        protected function findExistingItem(array $data) {
-               $sql = "SELECT  *
-                       FROM    wcf".WCF_N."_".$this->tableName."
-                       WHERE   packageID = ?
-                               AND environment = ?
-                               AND eventClassName = ?
-                               AND eventName = ?
-                               AND listenerClassName = ?";
-               $parameters = array(
-                       $this->installation->getPackageID(),
-                       $data['environment'],
-                       $data['eventClassName'],
-                       $data['eventName'],
-                       $data['listenerClassName']
-               );
+               if (!$data['listenerName']) {
+                       $sql = "SELECT  *
+                               FROM    wcf".WCF_N."_".$this->tableName."
+                               WHERE   packageID = ?
+                                       AND environment = ?
+                                       AND eventClassName = ?
+                                       AND eventName = ?
+                                       AND listenerClassName = ?";
+                       $parameters = array(
+                               $this->installation->getPackageID(),
+                               $data['environment'],
+                               $data['eventClassName'],
+                               $data['eventName'],
+                               $data['listenerClassName']
+                       );
+               }
+               else {
+                       $sql = "SELECT  *
+                               FROM    wcf".WCF_N."_".$this->tableName."
+                               WHERE   packageID = ?
+                                       AND listenerName = ?";
+                       $parameters = array(
+                               $this->installation->getPackageID(),
+                               $data['listenerName']
+                       );
+               }
                
                return array(
                        'sql' => $sql,
index e38e02c01a16b2fc57d5b641504901d0d52158cd..91525f3daf5d2a9b09b5e86ba3ffcc585baf653b 100644 (file)
@@ -373,6 +373,7 @@ CREATE TABLE wcf1_event_listener (
        listenerID INT(10) NOT NULL AUTO_INCREMENT PRIMARY KEY,
        packageID INT(10) NOT NULL,
        environment ENUM('user', 'admin') NOT NULL DEFAULT 'user',
+       listenerName VARCHAR(255) NOT NULL,
        eventClassName VARCHAR(80) NOT NULL DEFAULT '',
        eventName VARCHAR(50) NOT NULL DEFAULT '',
        listenerClassName VARCHAR(200) NOT NULL DEFAULT '',
@@ -381,6 +382,7 @@ CREATE TABLE wcf1_event_listener (
        permissions TEXT,
        options TEXT,
        
+       UNIQUE KEY listenerName (listenerName, packageID),
        UNIQUE KEY packageID (packageID, environment, eventClassName, eventName, listenerClassName)
 );