19 declare(strict_types=1);
59 $this->db = $db ?? $DIC->database();
67 $this->mail_ids_for_path_stmt = $this->db->prepare(
68 'SELECT COUNT(*) cnt FROM mail_attachment WHERE path = ?',
78 $attachment_paths = [];
80 $res = $this->db->query(
82 SELECT path, COUNT(mail_id) cnt_mail_ids 84 WHERE ' . $this->db->in(
86 $this->collector->mailIdsToDelete(),
93 while ($row = $this->db->fetchAssoc(
$res)) {
94 if (
$i > 0 &&
$i % self::PING_THRESHOLD) {
98 $num_usages_total = (
int) $this->db->fetchAssoc(
100 $this->mail_ids_for_path_stmt,
104 $num_usages_within_deleted_mails = (
int) $row[
'cnt_mail_ids'];
106 if ($num_usages_within_deleted_mails >= $num_usages_total) {
107 $attachment_paths[] = $row[
'path'];
113 return $attachment_paths;
118 if ($this->delete_directory_callback !== null) {
119 call_user_func($this->delete_directory_callback, $directory);
125 private function deleteAttachments():
void 130 foreach ($attachment_paths as
$path) {
131 if (
$i > 0 &&
$i % self::PING_THRESHOLD) {
139 RecursiveIteratorIterator::CHILD_FIRST
142 foreach ($iter as $file) {
145 $path_name = $file->getPathname();
146 if ($file->isDir()) {
150 "Attachment directory '%s' deleted",
154 } elseif (is_file($path_name) && unlink($path_name)) {
157 "Attachment file '%s' deleted",
164 "Attachment file '%s' for mail_id could not be deleted " .
165 "due to missing file system permissions",
175 "Attachment directory '%s' deleted",
180 $this->
logger->warning($e->getMessage());
181 $this->
logger->warning($e->getTraceAsString());
187 $this->db->manipulate(
188 'DELETE FROM mail_attachment WHERE ' .
195 $this->db->manipulate(
196 'DELETE FROM mail WHERE ' .
203 if ((
int) $this->
settings->get(
'mail_notify_orphaned',
'0') >= 1) {
204 $this->db->manipulate(
205 'DELETE FROM mail_cron_orphaned WHERE ' .
209 $this->db->manipulate(
'DELETE FROM mail_cron_orphaned');
213 public function delete():
void 215 if (count($this->collector->mailIdsToDelete()) > 0) {
216 $this->deleteAttachments();
222 'Deleted mail_ids: %s',
223 implode(
', ', $this->collector->mailIdsToDelete())
230 'Deleted mail_cron_orphaned mail_ids: %s',
231 implode(
', ', $this->collector->mailIdsToDelete())
ilMailCronOrphanedMails $job
static getLogger(string $a_component_id)
Get component logger.
ilDBStatement $mail_ids_for_path_stmt
ExpiredOrOrphanedMailsCollector $collector
static delDir(string $a_dir, bool $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
determineDeletableAttachmentPaths()
__construct(ilMailCronOrphanedMails $job, ExpiredOrOrphanedMailsCollector $collector, ?ilDBInterface $db=null, ?ilSetting $setting=null, ?ilLogger $logger=null, ?callable $delete_directory_callback=null)
deleteDirectory(string $directory)
$delete_directory_callback