From 6f3a7ef840e8dc04420b4046ce9729ec620b864b Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Sun, 18 Jun 2017 21:19:06 +0200 Subject: [PATCH] Added missing changes from 3.0 See 971b1a9b767b87b79dd149401f01dcc1d60c97e8 --- .../MessageEmbeddedObjectManager.class.php | 54 +++++++++++++++++-- 1 file changed, 49 insertions(+), 5 deletions(-) diff --git a/wcfsetup/install/files/lib/system/message/embedded/object/MessageEmbeddedObjectManager.class.php b/wcfsetup/install/files/lib/system/message/embedded/object/MessageEmbeddedObjectManager.class.php index 51652119cf..f1112cedda 100644 --- a/wcfsetup/install/files/lib/system/message/embedded/object/MessageEmbeddedObjectManager.class.php +++ b/wcfsetup/install/files/lib/system/message/embedded/object/MessageEmbeddedObjectManager.class.php @@ -71,9 +71,10 @@ class MessageEmbeddedObjectManager extends SingletonFactory { * Registers the embedded objects found in given message. * * @param HtmlInputProcessor $htmlInputProcessor html input processor instance holding embedded object data + * @param boolean $isBulk true for bulk operations * @return boolean true if at least one embedded object was found */ - public function registerObjects(HtmlInputProcessor $htmlInputProcessor) { + public function registerObjects(HtmlInputProcessor $htmlInputProcessor, $isBulk = false) { $context = $htmlInputProcessor->getContext(); $messageObjectType = $context['objectType']; @@ -81,7 +82,13 @@ class MessageEmbeddedObjectManager extends SingletonFactory { $messageID = $context['objectID']; // delete existing assignments - $this->removeObjects($messageObjectType, [$messageID]); + if ($isBulk) { + if (!isset($this->bulkData['remove'][$messageObjectType])) $this->bulkData['remove'][$messageObjectType] = []; + $this->bulkData['remove'][$messageObjectType][] = $messageID; + } + else { + $this->removeObjects($messageObjectType, [$messageID]); + } // prepare statement $sql = "INSERT INTO wcf".WCF_N."_message_embedded_object @@ -90,7 +97,7 @@ class MessageEmbeddedObjectManager extends SingletonFactory { $statement = WCF::getDB()->prepareStatement($sql); // call embedded object handlers - WCF::getDB()->beginTransaction(); + if (!$isBulk) WCF::getDB()->beginTransaction(); $embeddedData = $htmlInputProcessor->getEmbeddedContent(); $returnValue = false; @@ -101,17 +108,54 @@ class MessageEmbeddedObjectManager extends SingletonFactory { if (!empty($objectIDs)) { foreach ($objectIDs as $objectID) { - $statement->execute([$messageObjectTypeID, $messageID, $handler->objectTypeID, $objectID]); + $parameters = [$messageObjectTypeID, $messageID, $handler->objectTypeID, $objectID]; + if ($isBulk) { + $this->bulkData['insert'][] = $parameters; + } + else { + $statement->execute($parameters); + } } $returnValue = true; } } - WCF::getDB()->commitTransaction(); + if (!$isBulk) WCF::getDB()->commitTransaction(); return $returnValue; } + /** + * Commits the bulk operation by performing all deletes and inserts + * in one big transaction to save performance. + */ + public function commitBulkOperation() { + // delete existing data + WCF::getDB()->beginTransaction(); + foreach ($this->bulkData['remove'] as $objectType => $objectIDs) { + $this->removeObjects($objectType, $objectIDs); + } + WCF::getDB()->commitTransaction(); + + // prepare statement + $sql = "INSERT INTO wcf".WCF_N."_message_embedded_object + (messageObjectTypeID, messageID, embeddedObjectTypeID, embeddedObjectID) + VALUES (?, ?, ?, ?)"; + $statement = WCF::getDB()->prepareStatement($sql); + + WCF::getDB()->beginTransaction(); + foreach ($this->bulkData['insert'] as $parameters) { + $statement->execute($parameters); + } + WCF::getDB()->commitTransaction(); + + // reset cache + $this->bulkData = [ + 'insert' => [], + 'remove' => [] + ]; + } + /** * Registers the embedded objects found in a message using the simplified syntax. * -- 2.20.1