Support firing multiple activity event at once
authorMatthias Schmidt <gravatronics@live.com>
Sun, 23 Jun 2019 13:39:25 +0000 (15:39 +0200)
committerMatthias Schmidt <gravatronics@live.com>
Sun, 23 Jun 2019 13:39:25 +0000 (15:39 +0200)
wcfsetup/install/files/lib/system/user/activity/event/UserActivityEventHandler.class.php

index b2ec8d62ec607891c2db5d20c9acd8199a0d49c3..906a907ce3bc70a0f91e1080d1a7223073ab0119 100644 (file)
@@ -99,6 +99,49 @@ class UserActivityEventHandler extends SingletonFactory {
                return $returnValues['returnValues'];
        }
        
+       /**
+        * Fires multiple new activity events for the same activity event type.
+        * 
+        * This method is intended for bulk processing.
+        * 
+        * @param       string          $objectType
+        * @param       array           $eventData
+        * @throws      SystemException
+        */
+       public function fireEvents($objectType, array $eventData) {
+               $objectTypeID = $this->getObjectTypeID($objectType);
+               if ($objectTypeID === null) {
+                       throw new SystemException("Unknown recent activity event '".$objectType."'");
+               }
+               
+               $itemsPerLoop = 1000;
+               $batchCount = ceil(count($eventData) / $itemsPerLoop);
+               
+               WCF::getDB()->beginTransaction();
+               for ($i = 0; $i < $batchCount; $i++) {
+                       $batchEventData = array_slice($eventData, $i * $itemsPerLoop, $itemsPerLoop);
+                       
+                       $parameters = [];
+                       foreach ($batchEventData as $data) {
+                               $parameters = array_merge($parameters, [
+                                       $objectTypeID,
+                                       $data['objectID'],
+                                       $data['languageID'] ?? null,
+                                       $data['userID'] ?? WCF::getUser()->userID,
+                                       $data['time'] ?? TIME_NOW,
+                                       serialize($data['additionalData'] ?? [])
+                               ]);
+                       }
+                       
+                       $sql = "INSERT INTO     wcf" . WCF_N . "_user_activity_event
+                                               (objectTypeID, objectID, languageID, userID, time, additionalData)
+                               VALUES          (?, ?, ?, ?, ?, ?)" . str_repeat(', (?, ?, ?, ?, ?, ?)', count($batchEventData) - 1);
+                       $statement = WCF::getDB()->prepareStatement($sql);
+                       $statement->execute($parameters);
+               }
+               WCF::getDB()->commitTransaction();
+       }
+       
        /**
         * Removes an activity event.
         *