Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save clonemeagain/46c069b0c9d5f43c1dc021c3ce098bea to your computer and use it in GitHub Desktop.
Save clonemeagain/46c069b0c9d5f43c1dc021c3ce098bea to your computer and use it in GitHub Desktop.
Patch to prevent deleting osTicket tickets from being too brutal.. especially when large number of database attachments exist.
Example crontab, every Friday night etc:
#m h dom mon dow command
0 23 * * 5 /path/to/support/prune_files.php
diff --git a/include/class.file.php b/include/class.file.php
index 8542889..8143f2f 100644
--- a/include/class.file.php
+++ b/include/class.file.php
@@ -330,26 +330,31 @@ class AttachmentFile {
* Removes files and associated meta-data for files which no ticket,
* canned-response, or faq point to any more.
*/
- /* static */ function deleteOrphans() {
-
- $sql = 'DELETE FROM '.FILE_TABLE.' WHERE id NOT IN ('
- # DISTINCT implies sort and may not be necessary
- .'SELECT DISTINCT(file_id) FROM ('
- .'SELECT file_id FROM '.TICKET_ATTACHMENT_TABLE
- .' UNION ALL '
- .'SELECT file_id FROM '.CANNED_ATTACHMENT_TABLE
- .' UNION ALL '
- .'SELECT file_id FROM '.FAQ_ATTACHMENT_TABLE
- .') still_loved'
- .') AND `ft` = "T"';
+ /* static */
+ function deleteOrphans()
+ {
+ return true;
+ }
+
+ /**
+ * Call this weekly to ensure the database doesn't fill up with old attachment data..
+ * VERY SLOW.
+ */
+ function offlinePrune()
+ {
+ $sql = 'DELETE FROM ' . FILE_TABLE . ' WHERE id NOT IN (' .
+ // DISTINCT implies sort and may not be necessary
+ 'SELECT DISTINCT(file_id) FROM (' . 'SELECT file_id FROM '
+ . TICKET_ATTACHMENT_TABLE
+ . ' UNION ALL '
+ . 'SELECT file_id FROM ' . CANNED_ATTACHMENT_TABLE
+ . ' UNION ALL '
+ . 'SELECT file_id FROM ' . FAQ_ATTACHMENT_TABLE . ') still_loved' . ') AND `ft` = "T"';
db_query($sql);
- //Delete orphaned chuncked data!
+ // Delete orphaned chunked data!
AttachmentChunkedData::deleteOrphans();
-
- return true;
-
}
/* static */
diff --git a/prune_files.php b/prune_files.php
new file mode 100644
index 0000000..c3de639
--- /dev/null
+++ b/prune_files.php
@@ -0,0 +1,10 @@
+#!/usr/bin/php
+<?php
+// Ensure we are in CLI mode
+if(php_sapi_name() !== 'cli'){
+ die("Invalid.");
+}
+
+require_once 'main.inc.php';
+require_once INCLUDE_DIR . 'class.file.php';
+
+AttachmentFile::offlinePrune();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment