Add AbstractModificationLogHandler
authorMatthias Schmidt <gravatronics@live.com>
Mon, 21 Dec 2015 14:12:35 +0000 (15:12 +0100)
committerMatthias Schmidt <gravatronics@live.com>
Mon, 21 Dec 2015 14:12:35 +0000 (15:12 +0100)
CHANGELOG.md
wcfsetup/install/files/lib/system/log/modification/AbstractModificationLogHandler.class.php [new file with mode: 0644]
wcfsetup/install/files/lib/system/log/modification/ModificationLogHandler.class.php

index 6f5e221468bbd7178f1c18b273321e5048fa2577..10abde33cdf666429072fd21cc5accd8bb515f9d 100644 (file)
@@ -34,3 +34,4 @@
        * Linebreaks mode instead of using paragraphs, works better with the PHP-side parser which works with linebreaks
        * Ported the PHP-BBCode parser, massively improves accuracy and ensures validity
 * Show error message if poll options are given but not question instead of discarding poll options.
+* `parentObjectID` column added to `modification_log` and `wcf\system\log\modification\AbstractModificationLogHandler` introduced as a replacement for `wcf\system\log\modification\ModificationLogHandler`.
diff --git a/wcfsetup/install/files/lib/system/log/modification/AbstractModificationLogHandler.class.php b/wcfsetup/install/files/lib/system/log/modification/AbstractModificationLogHandler.class.php
new file mode 100644 (file)
index 0000000..3d058e2
--- /dev/null
@@ -0,0 +1,167 @@
+<?php
+namespace wcf\system\log\modification;
+use wcf\data\modification\log\ModificationLog;
+use wcf\data\modification\log\ModificationLogAction;
+use wcf\data\object\type\ObjectType;
+use wcf\data\object\type\ObjectTypeCache;
+use wcf\system\database\exception\DatabaseQueryException;
+use wcf\system\database\exception\DatabaseQueryExecutionException;
+use wcf\system\database\util\PreparedStatementConditionBuilder;
+use wcf\system\SingletonFactory;
+use wcf\system\WCF;
+
+/**
+ * Abstract implementation of a modification log handler for a certain modifiable content object type.
+ * 
+ * @author     Alexander Ebert, Matthias Schmidt
+ * @copyright  2001-2015 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package    com.woltlab.wcf
+ * @subpackage system.log.modification
+ * @category   Community Framework
+ * @since      2.2
+ */
+abstract class AbstractModificationLogHandler extends SingletonFactory {
+       /**
+        * modifiable content object type
+        * @var ObjectType
+        */
+       protected $objectType = null;
+       
+       /**
+        * name of the modifiable content object type
+        * @var string
+        */
+       protected $objectTypeName = '';
+       
+       /**
+        * @inheritdoc
+        * @throws      SystemException
+        */
+       protected function init() {
+               $this->objectType = ObjectTypeCache::getInstance()->getObjectTypeByName('com.woltlab.wcf.modifiableContent', $this->objectTypeName);
+               if ($this->objectType === null) {
+                       throw new SystemException("Object type '".$this->objectTypeName."' not found within definition 'com.woltlab.wcf.modifiableContent'");
+               }
+       }
+       
+       /**
+        * Creates a modification log entry.
+        * 
+        * @param       string          $action
+        * @param       integer         $objectID
+        * @param       integer|null    $parentObjectID
+        * @param       array           $additionalData
+        * @param       integer         $time
+        * @param       integer|null    $userID
+        * @param       string|null     $username
+        * @return      ModificationLog
+        */
+       public function createLog($action, $objectID, $parentObjectID = null, array $additionalData = [], $time = TIME_NOW, $userID = null, $username = null) {
+               // set default user data
+               if ($userID === null) {
+                       if (WCF::getUser()->userID) {
+                               $userID = WCF::getUser()->userID;
+                       }
+                       else if ($username === null) {
+                               $username = 'System';
+                       }
+               }
+               if ($username === null) {
+                       if (WCF::getUser()->username) {
+                               $username = WCF::getUser()->username;
+                       }
+                       else {
+                               $username = '';
+                       }
+               }
+               
+               $modificationLogAction = new ModificationLogAction([], 'create', [
+                       'data' => [
+                               'objectTypeID' => $this->objectType->objectTypeID,
+                               'objectID' => $objectID,
+                               'parentObjectID' => $parentObjectID,
+                               'action' => $action,
+                               'userID' => $userID,
+                               'username' => $username,
+                               'time' => $time,
+                               'additionalData' => serialize($additionalData)
+                       ]
+               ]);
+               
+               return $modificationLogAction->executeAction()['returnValues'];
+       }
+       
+       /**
+        * Deletes modification log entries.
+        * 
+        * @param       integer[]       $objectIDs
+        * @param       string[]        $ignoredActions         names of actions whose log entries will not be deleted
+        * @throws      DatabaseQueryException
+        * @throws      DatabaseQueryExecutionException
+        */
+       public function deleteLogs(array $objectIDs, array $ignoredActions = []) {
+               if (empty($objectIDs)) return;
+               
+               $conditionBuilder = new PreparedStatementConditionBuilder();
+               $conditionBuilder->add('objectTypeID = ?', [$this->objectType->objectTypeID]);
+               $conditionBuilder->add('objectID IN (?)', [$objectIDs]);
+               if (!empty($ignoredActions)) {
+                       $conditionBuilder->add('action NOT IN (?)', [$ignoredActions]);
+               }
+               
+               $sql = "DELETE FROM     wcf".WCF_N."_modification_log
+                       ".$conditionBuilder;
+               $statement = WCF::getDB()->prepareStatement($sql);
+               $statement->execute($conditionBuilder->getParameters());
+       }
+       
+       /**
+        * Deletes modification log entries by the id of the parent object.
+        * 
+        * @param       integer[]       $parentObjectIDs
+        * @throws      DatabaseQueryException
+        * @throws      DatabaseQueryExecutionException
+        */
+       public function deleteLogsByParentIDs(array $parentObjectIDs) {
+               if (empty($parentObjectIDs)) return;
+               
+               $conditionBuilder = new PreparedStatementConditionBuilder();
+               $conditionBuilder->add('objectTypeID = ?', [$this->objectType->objectTypeID]);
+               $conditionBuilder->add('parentObjectID IN (?)', [$parentObjectIDs]);
+               
+               $sql = "DELETE FROM     wcf".WCF_N."_modification_log
+                       ".$conditionBuilder;
+               $statement = WCF::getDB()->prepareStatement($sql);
+               $statement->execute($conditionBuilder->getParameters());
+       }
+       
+       /**
+        * Returns the modifiable content object type.
+        * 
+        * @return      ObjectType
+        */
+       public function getObjectType() {
+               return $this->objectType;
+       }
+       
+       /**
+        * Updates the parent object id of modification log entries.
+        * 
+        * @param       integer[]       $objectIDs
+        * @param       integer         $newParentObjectID
+        */
+       public function updateParentObjectID(array $objectIDs, $newParentObjectID) {
+               if (empty($objectIDs)) return;
+               
+               $conditionBuilder = new PreparedStatementConditionBuilder();
+               $conditionBuilder->add('objectTypeID = ?', [$this->objectType->objectTypeID]);
+               $conditionBuilder->add('objectID IN (?)', [$objectIDs]);
+               
+               $sql = "UPDATE  wcf".WCF_N."_modification_log
+                       SET     parentObjectID = ?
+                       ".$conditionBuilder;
+               $statement = WCF::getDB()->prepareStatement($sql);
+               $statement->execute(array_merge([$newParentObjectID], $conditionBuilder->getParameters()));
+       }
+}
index 65cd79bd0ab808722d93cb7431ed501eda9c7a4a..81405903a758c039909d3c05bd1ee56b1aed826d 100644 (file)
@@ -16,6 +16,7 @@ use wcf\system\WCF;
  * @package    com.woltlab.wcf
  * @subpackage system.log.modification
  * @category   Community Framework
+ * @deprecated since 2.2, use AbstractModificationLogHandler
  */
 class ModificationLogHandler extends SingletonFactory {
        /**