Chamilo is a learning management system focused on ease of use and accessibility
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
chamilo-lms/tests/scripts/delete_old_message_attachme...

120 lines
5.2 KiB

<?php
/* For licensing terms, see /license.txt */
/**
* This script removes attachments of old messages.
* Messages sent through announcements in Chamilo 1.11.* will replicate
* the attachment as many times as there are recipients of the message.
* This is highly inefficient. This script allows you to remove (from disk)
* such attachments from the recipients, without removing it from the originals.
* This script should be located inside the tests/scripts/ folder to work
* @author Yannick Warnier <yannick.warnier@beeznest.com> - Cleanup and debug
*/
die();
require __DIR__.'/../../main/inc/global.inc.php';
$beforeDate = '2021-08-31'; // message must be older than date to be considered
$simulate = false;
$userBasePath = api_get_path(SYS_UPLOAD_PATH).'users/';
if (PHP_SAPI !== 'cli') {
die('This script can only be executed from the command line');
}
echo PHP_EOL."Usage: php ".basename(__FILE__)." [options]".PHP_EOL;
echo "Where [options] can be ".PHP_EOL;
echo " -s Simulate execution - Do not delete anything, just show numbers".PHP_EOL.PHP_EOL;
echo "Processing...".PHP_EOL.PHP_EOL;
if (!empty($argv[1]) && $argv[1] == '-s') {
$simulate = true;
echo "Simulation mode is enabled".PHP_EOL;
}
echo "[".time()."] Querying messages\n";
$sql = "SELECT m.id mid, m.msg_status, m.user_sender_id,
user_receiver_id, ma.id maid, ma.path
FROM message m, message_attachment ma
WHERE m.id = ma.message_id
AND m.msg_status IN (0,1,3,4)
AND m.send_date < '$beforeDate'";
$res = Database::query($sql);
if ($res === false) {
die("Error querying messages\n");
}
$countMessages = Database::num_rows($res);
$sql = "SELECT count(*) nbr FROM message WHERE msg_status IN (0,1,3,4)";
$resc = Database::query($sql);
if ($resc === false) {
die("Error querying total messages\n");
}
$countAllMessages = Database::result($resc, 0, 'nbr');
echo "[".time()."]"
." Found $countMessages messages before $beforeDate with attachments on a total of $countAllMessages messages."."\n";
$sqlDeleteAttach = "DELETE FROM message_attachment WHERE id = ";
/**
* Locate and destroy the expired message attachments
*/
$totalSize = 0;
$senderMessageSize = 0;
while ($message = Database::fetch_assoc($res)) {
switch ($message['msg_status']) {
case '4':
//echo "Message ".$message['mid']." is in user ".$message['user_sender_id']." outbox".PHP_EOL;
$usi = $message['user_sender_id'];
$filePath = substr($usi, 0, 1).'/'.$usi.'/message_attachments/'.$message['path'];
if (file_exists($userBasePath.$filePath)) {
//echo " File found".PHP_EOL;
$senderMessageSize += filesize($userBasePath.$filePath);
}
break;
case '0':
case '1':
//echo "Message ".$message['mid']." is in user ".$message['user_receiver_id']." inbox".PHP_EOL;
$usi = $message['user_receiver_id'];
$filePath = substr($usi, 0, 1).'/'.$usi.'/message_attachments/'.$message['path'];
if (file_exists($userBasePath.$filePath)) {
//echo " File found".PHP_EOL;
$totalSize += filesize($userBasePath.$filePath);
if ($simulate == false) {
exec('rm '.$userBasePath.$filePath);
$deleteResult = Database::query($sqlDeleteAttach.$message['maid']);
} else {
echo "Would delete ".$userBasePath.$filePath.PHP_EOL;
echo "Query: ".$sqlDeleteAttach.$message['maid'].PHP_EOL;
}
}
break;
case '3':
//echo "Message ".$message['mid']." can be in two different folders".PHP_EOL;
$usi = $message['user_receiver_id'];
$filePath = substr($usi, 0, 1).'/'.$usi.'/message_attachments/'.$message['path'];
if (file_exists($userBasePath.$filePath)) {
//echo " File found in receiver's path".PHP_EOL;
$totalSize += filesize($userBasePath.$filePath);
if ($simulate == false) {
exec('rm '.$userBasePath.$filePath);
$deleteResult = Database::query($sqlDeleteAttach.$message['maid']);
} else {
echo "Would delete ".$userBasePath.$filePath.PHP_EOL;
echo "Query: ".$sqlDeleteAttach.$message['maid'].PHP_EOL;
}
} else {
$usi = $message['user_sender_id'];
$filePath = substr($usi, 0, 1).'/'.$usi.'/message_attachments/'.$message['path'];
if (file_exists($userBasePath.$filePath)) {
//echo " File found in sender's path".PHP_EOL;
$senderMessageSize += filesize($userBasePath.$filePath);
}
}
break;
}
}
echo "[".time()."] ".($simulate ? "Would delete" : "Deleted")
." attachments from $countMessages messages before $beforeDate on a total of $countAllMessages"
." messages, for a total estimated size of "
.round($totalSize / (1024 * 1024))." MB.".PHP_EOL;
echo "A total of ".round($senderMessageSize / (1024 * 1024))." MB could still be freed by removing attachments from senders messages.".PHP_EOL;
exit;