19 declare(strict_types=1);
60 $this->db = $db ?? $DIC->database();
65 $this->mail_ids_for_path_stmt = $this->db->prepare(
66 'SELECT COUNT(*) cnt FROM mail_attachment WHERE path = ?',
76 $attachment_paths = [];
78 $res = $this->db->query(
80 SELECT path, COUNT(mail_id) cnt_mail_ids 82 WHERE ' . $this->db->in(
84 $this->collector->mailIdsToDelete(),
91 while ($row = $this->db->fetchAssoc(
$res)) {
92 if ($i > 0 && $i % self::PING_THRESHOLD) {
96 $num_usages_total = (
int) $this->db->fetchAssoc(
98 $this->mail_ids_for_path_stmt,
102 $num_usages_within_deleted_mails = (
int) $row[
'cnt_mail_ids'];
104 if ($num_usages_within_deleted_mails >= $num_usages_total) {
105 $attachment_paths[] = $row[
'path'];
111 return $attachment_paths;
116 if ($this->delete_directory_callback !==
null) {
117 \call_user_func($this->delete_directory_callback, $directory);
123 private function deleteAttachments():
void 128 foreach ($attachment_paths as
$path) {
129 if ($i > 0 && $i % self::PING_THRESHOLD) {
137 RecursiveIteratorIterator::CHILD_FIRST
140 foreach ($iter as $file) {
143 $path_name = $file->getPathname();
144 if ($file->isDir()) {
148 "Attachment directory '%s' deleted",
152 } elseif (is_file($path_name) && unlink($path_name)) {
155 "Attachment file '%s' deleted",
162 'Attachment file \'%s\' for mail_id could not be deleted due to missing file system permissions',
172 "Attachment directory '%s' deleted",
177 $this->
logger->warning($e->getMessage());
178 $this->
logger->warning($e->getTraceAsString());
184 $this->db->manipulate(
185 'DELETE FROM mail_attachment WHERE ' .
192 $this->db->manipulate(
193 'DELETE FROM mail WHERE ' .
200 if ((
int) $this->
settings->get(
'mail_notify_orphaned',
'0') >= 1) {
201 $this->db->manipulate(
202 'DELETE FROM mail_cron_orphaned WHERE ' .
206 $this->db->manipulate(
'DELETE FROM mail_cron_orphaned');
210 public function delete():
void 212 if ($this->collector->mailIdsToDelete() !== []) {
213 $this->deleteAttachments();
219 'Deleted mail_ids: %s',
220 implode(
', ', $this->collector->mailIdsToDelete())
227 'Deleted mail_cron_orphaned mail_ids: %s',
228 implode(
', ', $this->collector->mailIdsToDelete())
static getLogger(string $a_component_id)
Get component logger.
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
readonly ilSetting $settings
static delDir(string $a_dir, bool $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
determineDeletableAttachmentPaths()
deleteDirectory(string $directory)
readonly ilDBInterface $db
$delete_directory_callback
__construct(private readonly ilMailCronOrphanedMails $job, private readonly ExpiredOrOrphanedMailsCollector $collector, ?ilDBInterface $db=null, ?ilSetting $setting=null, ?ilLogger $logger=null, ?callable $delete_directory_callback=null)
readonly ilDBStatement $mail_ids_for_path_stmt
readonly ilLogger $logger