Skip to content
Snippets Groups Projects
Commit eddbddc7 authored by Peter Rotich's avatar Peter Rotich
Browse files

Merge pull request #1 from greezybacon/feature/delete-orphans

Review - Add deleteOrphans() utility to the File class to remove unused files

* Thanks for the fix.
parents f519363f d91361d3
Branches
Tags
No related merge requests found
...@@ -143,30 +143,13 @@ class Canned { ...@@ -143,30 +143,13 @@ class Canned {
return $i; return $i;
} }
function deleteAttachment($fileId) {
$sql='DELETE FROM '.CANNED_ATTACHMENT_TABLE
.' WHERE canned_id='.db_input($this->getId())
.' AND file_id='.db_input($fileId)
.' LIMIT 1';
if(!db_query($sql) || !db_affected_rows())
return false;
if(($file=AttachmentFile::lookup($fileId)) && !$file->isInuse())
$file->delete();
return true;
}
function deleteAttachments(){ function deleteAttachments(){
$deleted=0; $deleted=0;
if(($attachments = $this->getAttachments())) { $sql='DELETE FROM '.CANNED_ATTACHMENT_TABLE
foreach($attachments as $attachment) .' WHERE canned_id='.db_input($this->getId());
if($attachment['id'] && $this->deleteAttachment($attachment['id'])) if(db_query($sql) && db_affected_rows()) {
$deleted++; $deleted = AttachmentFile::deleteOrphans();
} }
return $deleted; return $deleted;
......
...@@ -34,10 +34,16 @@ class Cron { ...@@ -34,10 +34,16 @@ class Cron {
Sys::purgeLogs(); Sys::purgeLogs();
} }
function CleanOrphanedFiles() {
require_once(INCLUDE_DIR.'class.file.php');
AttachmentFile::deleteOrphans();
}
function run(){ //called by outside cron NOT autocron function run(){ //called by outside cron NOT autocron
Cron::MailFetcher(); self::MailFetcher();
Cron::TicketMonitor(); self::TicketMonitor();
cron::PurgeLogs(); self::PurgeLogs();
self::CleanOrphanedFiles();
} }
} }
?> ?>
...@@ -217,29 +217,13 @@ class FAQ { ...@@ -217,29 +217,13 @@ class FAQ {
return $i; return $i;
} }
function deleteAttachment($fileId) {
$sql='DELETE FROM '.FAQ_ATTACHMENT_TABLE
.' WHERE faq_id='.db_input($this->getId())
.' AND file_id='.db_input($fileId)
.' LIMIT 1';
if(!db_query($sql) || !db_affected_rows())
return false;
if(($file=AttachmentFile::lookup($fileId)) && !$file->isInuse())
$file->delete();
return true;
}
function deleteAttachments(){ function deleteAttachments(){
$deleted=0; $deleted=0;
if(($attachments = $this->getAttachments())) { $sql='DELETE FROM '.FAQ_ATTACHMENT_TABLE
foreach($attachments as $attachment) .' WHERE faq_id='.db_input($this->getId());
if($attachment['id'] && $this->deleteAttachment($attachment['id'])) if(db_query($sql) && db_affected_rows()) {
$deleted++; $deleted = AttachmentFile::deleteOrphans();
} }
return $deleted; return $deleted;
......
...@@ -193,6 +193,24 @@ class AttachmentFile { ...@@ -193,6 +193,24 @@ class AttachmentFile {
return ($id && ($file = new AttachmentFile($id)) && $file->getId()==$id)?$file:null; return ($id && ($file = new AttachmentFile($id)) && $file->getId()==$id)?$file:null;
} }
/**
* Removes files and associated meta-data for files which no ticket,
* canned-response, or faq point to any more.
*/
/* static */ function deleteOrphans() {
$res=db_query(
'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'
.')');
return db_affected_rows();
}
} }
class AttachmentList { class AttachmentList {
......
...@@ -1424,15 +1424,10 @@ class Ticket{ ...@@ -1424,15 +1424,10 @@ class Ticket{
global $cfg; global $cfg;
$deleted=0; $deleted=0;
if(($attachments = $this->getAttachments())) { // Clear reference table
//Clear reference table - XXX: some attachments might be orphaned $res=db_query('DELETE FROM '.TICKET_ATTACHMENT_TABLE.' WHERE ticket_id='.db_input($this->getId()));
db_query('DELETE FROM '.TICKET_ATTACHMENT_TABLE.' WHERE ticket_id='.db_input($this->getId())); if ($res && db_affected_rows())
//Delete file from DB IF NOT inuse. $deleted = AttachmentFile::deleteOrphans();
foreach($attachments as $attachment) {
if(($file=AttachmentFile::lookup($attachment['file_id'])) && !$file->isInuse() && $file->delete())
$deleted++;
}
}
return $deleted; return $deleted;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment