PDA

View Full Version : Deleting a message/attachment


g00fy
06-24-2008, 11:04 AM
Browsing through the code I saw that in DAO_Message, the links with a message are deleted, but NOT the attachments! Is this correct behaviour?

Hildy
06-24-2008, 05:09 PM
DAO_Message::delete() is static function delete($ids) {
if(!is_array($ids)) $ids = array($ids);
if(empty($ids)) return;

$db = DevblocksPlatform::getDatabaseService();

$message_ids = implode(',', $ids);

$sql = sprintf("DELETE FROM message WHERE id IN (%s)", $message_ids);
$db->Execute($sql) or die(__CLASS__ . '('.__LINE__.')'. ':' . $db->ErrorMsg()); /* @var $rs ADORecordSet */

// Content
$sql = sprintf("DELETE FROM message_content WHERE message_id IN (%s)", $message_ids);
$db->Execute($sql) or die(__CLASS__ . '('.__LINE__.')'. ':' . $db->ErrorMsg()); /* @var $rs ADORecordSet */

// Headers
$sql = sprintf("DELETE FROM message_header WHERE message_id IN (%s)", $message_ids);
$db->Execute($sql) or die(__CLASS__ . '('.__LINE__.')'. ':' . $db->ErrorMsg()); /* @var $rs ADORecordSet */

// Notes
$sql = sprintf("DELETE FROM message_note WHERE message_id IN (%s)", $message_ids);
$db->Execute($sql) or die(__CLASS__ . '('.__LINE__.')'. ':' . $db->ErrorMsg()); /* @var $rs ADORecordSet */

// Attachments
DAO_Attachment::deleteByMessageIds($ids);
}
That last line ('DAO_Attachment::deleteByMessageIds($ids);') cascades the deletion to the attachments. ;-)

jstanden
06-24-2008, 08:19 PM
Older versions of Cerb4 had a bug that didn't delete attachments. We have a cleanup script floating around to re-sync things.

cleanup.php

<?php
require(dirname(__FILE__) . '/framework.config.php');
require(DEVBLOCKS_PATH . 'Devblocks.class.php');
require(APP_PATH . '/api/Application.class.php');

@$do = intval($_REQUEST['do']);
@$v = intval($_REQUEST['v']);

define("DO_DELETE_FILES", $do);
define("DO_DELETE_DB", $do);
define("VERBOSE", $v);

// Try to grab as many resources as we can
@ini_set('memory_limit','128M');
@set_time_limit(0);

DevblocksPlatform::init();

$db = DevblocksPlatform::getDatabaseService();
$attachment_path = APP_PATH . '/storage/attachments/';

// Look up all our valid file ids
$sql = sprintf("SELECT id,filepath FROM attachment");
$rs = $db->Execute($sql);

// Build a hash of valid ids
$valid_ids_set = array();
while(!$rs->EOF) {
$valid_ids_set[intval($rs->fields['id'])] = $rs->fields['filepath'];
$rs->MoveNext();
}

$total_files_db = count($valid_ids_set);

// Get all our attachment hash directories
$dir_handles = glob($attachment_path.'*',GLOB_ONLYDIR|GLOB_NOSORT );

$orphans = 0;
$checked = 0;

// Loop through all our hash directories and check that IDs are valid
if(!empty($dir_handles))
foreach($dir_handles as $dir) {
$dirinfo = pathinfo($dir);

if(!is_numeric($dirinfo['basename']))
continue;

if(false == ($dh = opendir($dir)))
die("Couldn't open " . $dir);

while($file = readdir($dh)) {
// Skip dirs and files we can't change
if(is_dir($file))
continue;

$info = pathinfo($file);
$disk_file_id = $info['filename'];

// Only numeric filenames are valid
if(!is_numeric($disk_file_id))
continue;

if(!isset($valid_ids_set[$disk_file_id])) {
$orphans++;

if(DO_DELETE_FILES)
unlink($dir . DIRECTORY_SEPARATOR . $file);

} else {
unset($valid_ids_set[$disk_file_id]);
}
$checked++;
}
closedir($dh);
}

$db_orphans = count($valid_ids_set);

// Print out the DB orphans
if(DO_DELETE_DB && intval($db_orphans)) {
foreach($valid_ids_set as $db_id => $null) {
$db->Execute(sprintf("DELETE FROM attachment WHERE id = %d", $db_id));
}
} elseif (VERBOSE && intval($db_orphans)) {
echo "<h2>In DB and not in filesystem:</h2>";
foreach($valid_ids_set as $db_id => $filepath) {
echo $filepath,"<BR>";
}
echo "<BR>";
}

echo sprintf("Checked %s disk files against %s db rows.<br>", $checked, $total_files_db);
echo sprintf("There were %s orphans on the disk and not in db.<br>", $orphans);
echo sprintf("There were %s orphans in the db and not on disk.<br>", $db_orphans);
?>
Drop that file in your /cerb4/ directory, then access it through the browser like:
http://your-website/cerb4/cleanup.php

If you run it without arguments it is read-only and won't delete anything.

To write the changes, use:
http://your-website/cerb4/cleanup.php?do=1

Then delete the cleanup.php file, since it doesn't do any security checking.

g00fy
06-24-2008, 08:20 PM
Ah thanks :) that's the reason!!

giz
07-19-2008, 04:24 PM
This dropped my attachments from 77mb to just over 2mb on the Helpdesk Setup>Storage tab

Attachments:
Total Disk Space: 2.08 MB