Delete orphaned files
authorAlexander Ebert <ebert@woltlab.com>
Sat, 11 May 2024 11:56:22 +0000 (13:56 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Sat, 8 Jun 2024 10:19:39 +0000 (12:19 +0200)
com.woltlab.wcf/cronjob.xml
wcfsetup/install/files/lib/system/cronjob/FileCleanUpCronjob.class.php [new file with mode: 0644]

index f60b2607fbb4330ece8006fb74b4e944242f6744..e1c76816e11fab350191fb841e90add1321c121d 100644 (file)
                        <description language="de">Löscht verwaiste Dateianhänge</description>
                        <expression>0 2 * * *</expression>
                </cronjob>
+               <cronjob name="com.woltlab.wcf.fileCleanUp">
+                       <classname>wcf\system\cronjob\FileCleanUpCronjob</classname>
+                       <description>Deletes orphaned files</description>
+                       <description language="de">Löscht verwaiste Dateien</description>
+                       <expression>0 3 * * *</expression>
+               </cronjob>
                <cronjob name="com.woltlab.wcf.backgroundQueueCleanUp">
                        <classname>wcf\system\cronjob\BackgroundQueueCleanUpCronjob</classname>
                        <description>Requeues stuck queue items</description>
diff --git a/wcfsetup/install/files/lib/system/cronjob/FileCleanUpCronjob.class.php b/wcfsetup/install/files/lib/system/cronjob/FileCleanUpCronjob.class.php
new file mode 100644 (file)
index 0000000..3df780e
--- /dev/null
@@ -0,0 +1,37 @@
+<?php
+
+namespace wcf\system\cronjob;
+
+use wcf\data\cronjob\Cronjob;
+use wcf\data\file\FileEditor;
+use wcf\system\WCF;
+
+/**
+ * Deletes orphaned files.
+ *
+ * @author Alexander Ebert
+ * @copyright 2001-2024 WoltLab GmbH
+ * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @since 6.1
+ */
+class FileCleanUpCronjob extends AbstractCronjob
+{
+    #[\Override]
+    public function execute(Cronjob $cronjob)
+    {
+        parent::execute($cronjob);
+
+        $sql = "SELECT  fileID
+                FROM    wcf1_file
+                WHERE   objectTypeID IS NULL";
+        $statement = WCF::getDB()->prepare($sql, 1_000);
+        $statement->execute();
+        $fileIDs = $statement->fetchAll(\PDO::FETCH_COLUMN);
+
+        if ($fileIDs === []) {
+            return;
+        }
+
+        FileEditor::deleteAll($fileIDs);
+    }
+}