diff --git a/files/acp/be.bastelstu.chat_update_deleteOrphanedAttachments.php b/files/acp/be.bastelstu.chat_update_deleteOrphanedAttachments.php new file mode 100644 index 0000000..24373fb --- /dev/null +++ b/files/acp/be.bastelstu.chat_update_deleteOrphanedAttachments.php @@ -0,0 +1,41 @@ + + * @package WoltLabSuite\Core + * @see https://github.com/WoltLab/com.woltlab.wcf.conversation/blob/f0d4964a0b8042c440d5a3f759078429dc43c5b8/files/acp/update_com.woltlab.wcf.conversation_5.5_cleanup_orphaned_attachments.php + */ + +use wcf\data\attachment\AttachmentAction; +use wcf\data\object\type\ObjectTypeCache; +use wcf\system\package\SplitNodeException; +use wcf\system\WCF; + +$objectType = ObjectTypeCache::getInstance() + ->getObjectTypeByName('com.woltlab.wcf.attachment.objectType', 'be.bastelstu.chat.message'); + +$sql = "SELECT attachmentID + FROM wcf1_attachment + WHERE objectTypeID = ? + AND objectID NOT IN ( + SELECT messageID + FROM chat1_message + )"; +$statement = WCF::getDB()->prepare($sql, 100); +$statement->execute([$objectType->objectTypeID]); +$attachmentIDs = $statement->fetchAll(\PDO::FETCH_COLUMN); + +if (empty($attachmentIDs)) { + return; +} + +(new AttachmentAction($attachmentIDs, 'delete'))->executeAction(); + +// If we reached this location we processed at least one attachment. +// If this was the final attachment the next iteration will abort this +// script early, thus not splitting the node. +throw new SplitNodeException();