From: Matthias Schmidt Date: Mon, 21 Dec 2015 14:12:35 +0000 (+0100) Subject: Add AbstractModificationLogHandler X-Git-Tag: 3.0.0_Beta_1~2084 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=28854f57445a35894483db28e250f94b444962a0;p=GitHub%2FWoltLab%2FWCF.git Add AbstractModificationLogHandler --- diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f5e221468..10abde33cd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 index 0000000000..3d058e26e0 --- /dev/null +++ b/wcfsetup/install/files/lib/system/log/modification/AbstractModificationLogHandler.class.php @@ -0,0 +1,167 @@ + + * @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())); + } +} diff --git a/wcfsetup/install/files/lib/system/log/modification/ModificationLogHandler.class.php b/wcfsetup/install/files/lib/system/log/modification/ModificationLogHandler.class.php index 65cd79bd0a..81405903a7 100644 --- a/wcfsetup/install/files/lib/system/log/modification/ModificationLogHandler.class.php +++ b/wcfsetup/install/files/lib/system/log/modification/ModificationLogHandler.class.php @@ -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 { /**