Add update script to delete orphaned attachments

This commit is contained in:
Tim Düsterhus 2023-02-22 17:36:58 +01:00
parent 7837b61eb8
commit 0c9419c89b
Signed by: TimWolla
GPG Key ID: 8FF75566094168AF
1 changed files with 41 additions and 0 deletions

View File

@ -0,0 +1,41 @@
<?php
/**
* Deletes orphaned attachments.
*
* @author Tim Duesterhus
* @copyright 2001-2022 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
* @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();