From e04976596dac0f0a7add86be08a0f00a612b1832 Mon Sep 17 00:00:00 2001 From: Matthias Schmidt Date: Sat, 23 May 2015 07:44:49 +0200 Subject: [PATCH] Add name attribute for event listeners --- CHANGELOG.md | 1 + com.woltlab.wcf/eventListener.xml | 30 +++++- .../event/listener/EventListener.class.php | 7 ++ ...istenerPackageInstallationPlugin.class.php | 98 ++++++++++++++----- wcfsetup/setup/db/install.sql | 2 + 5 files changed, 113 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5bd41ca23d..9a1ba55aec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/com.woltlab.wcf/eventListener.xml b/com.woltlab.wcf/eventListener.xml index 29ef8c40ed..d3dd425772 100644 --- a/com.woltlab.wcf/eventListener.xml +++ b/com.woltlab.wcf/eventListener.xml @@ -1,21 +1,21 @@ - + readParameters admin 1 - + readParameters admin 1 - + readParameters admin @@ -25,6 +25,30 @@ + + + + readParameters + admin + 1 + + + + + readParameters + admin + 1 + + + + + readParameters + admin + 1 + + + + beforeParsing diff --git a/wcfsetup/install/files/lib/data/event/listener/EventListener.class.php b/wcfsetup/install/files/lib/data/event/listener/EventListener.class.php index bb64640b4d..49d91cae7a 100644 --- a/wcfsetup/install/files/lib/data/event/listener/EventListener.class.php +++ b/wcfsetup/install/files/lib/data/event/listener/EventListener.class.php @@ -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'; } diff --git a/wcfsetup/install/files/lib/system/package/plugin/EventListenerPackageInstallationPlugin.class.php b/wcfsetup/install/files/lib/system/package/plugin/EventListenerPackageInstallationPlugin.class.php index 0ef990f852..4af83f14c1 100644 --- a/wcfsetup/install/files/lib/system/package/plugin/EventListenerPackageInstallationPlugin.class.php +++ b/wcfsetup/install/files/lib/system/package/plugin/EventListenerPackageInstallationPlugin.class.php @@ -1,5 +1,7 @@ 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, diff --git a/wcfsetup/setup/db/install.sql b/wcfsetup/setup/db/install.sql index e38e02c01a..91525f3daf 100644 --- a/wcfsetup/setup/db/install.sql +++ b/wcfsetup/setup/db/install.sql @@ -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) ); -- 2.20.1