ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilMailCronOrphanedMailsDeletionProcessor.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2015 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4require_once './Services/Logging/classes/public/class.ilLoggerFactory.php';
5
11{
12 private const PING_THRESHOLD = 250;
13
14 private $job;
18 protected $collector;
19
23 protected $db;
24
28 protected $settings;
30
35 {
36 global $DIC;
37
38 $this->settings = $DIC->settings();
39 $this->db = $DIC->database();
40
41 $this->job = $job;
42 $this->collector = $collector;
43
44 $this->mail_ids_for_path_stmt = $this->db->prepare(
45 'SELECT COUNT(*) cnt FROM mail_attachment WHERE path = ?',
47 );
48 }
49
53 private function deleteAttachments()
54 {
55 $attachment_paths = array();
56
57 $res = $this->db->query('
58 SELECT path, COUNT(mail_id) cnt_mail_ids
59 FROM mail_attachment
60 WHERE ' . $this->db->in('mail_id', $this->collector->getMailIdsToDelete(), false, 'integer') . '
61 GROUP BY path');
62
63 $i = 0;
64 while ($row = $this->db->fetchAssoc($res)) {
65 if ($i > 0 && $i % self::PING_THRESHOLD) {
66 $this->job->ping();
67 }
68
69 $usage_res = $this->db->execute(
70 $this->mail_ids_for_path_stmt,
71 [$row['path']]
72 );
73
74 $count_usages_data = $this->db->fetchAssoc($usage_res);
75 if (is_array($count_usages_data) && $count_usages_data !== [] && (int) $row['cnt_mail_ids'] >= (int) $count_usages_data['cnt']) {
76 // collect path to delete attachment file
77 $attachment_paths[] = $row['path'];
78 }
79
80 ++$i;
81 }
82
83 $i = 0;
84 foreach ($attachment_paths as $path) {
85 if ($i > 0 && $i % self::PING_THRESHOLD) {
86 $this->job->ping();
87 }
88
89 try {
90 $path = CLIENT_DATA_DIR . '/mail/' . $path;
91 $iter = new RecursiveIteratorIterator(
92 new RecursiveDirectoryIterator($path),
93 RecursiveIteratorIterator::CHILD_FIRST
94 );
95
96 foreach ($iter as $file) {
101 $path_name = $file->getPathname();
102 if ($file->isDir()) {
103 ilUtil::delDir($path_name);
104 ilLoggerFactory::getLogger('mail')->info(sprintf(
105 "Attachment directory '%s' deleted",
106 $path_name
107 ));
108 } else {
109 if (file_exists($path_name) && unlink($path_name)) {
110 ilLoggerFactory::getLogger('mail')->info(sprintf(
111 "Attachment file '%s' deleted",
112 $path_name
113 ));
114 } else {
115 ilLoggerFactory::getLogger('mail')->info(sprintf(
116 "Attachment file '%s' for mail_id could not be deleted " .
117 "due to missing file system permissions",
118 $path_name
119 ));
120 }
121 }
122 }
123
124 ilUtil::delDir($path);
125 ilLoggerFactory::getLogger('mail')->info(sprintf(
126 "Attachment directory '%s' deleted",
127 $path
128 ));
129 } catch (Exception $e) {
130 ilLoggerFactory::getLogger('mail')->warning($e->getMessage());
131 ilLoggerFactory::getLogger('mail')->warning($e->getTraceAsString());
132 } finally {
133 ++$i;
134 }
135 }
136
137 $this->db->manipulate('DELETE FROM mail_attachment WHERE ' . $this->db->in('mail_id', $this->collector->getMailIdsToDelete(), false, 'integer'));
138 }
139
143 private function deleteMails()
144 {
145 $this->db->manipulate('DELETE FROM mail WHERE ' . $this->db->in('mail_id', $this->collector->getMailIdsToDelete(), false, 'integer'));
146 }
147
151 private function deleteMarkedAsNotified()
152 {
153 if ((int) $this->settings->get('mail_notify_orphaned') >= 1) {
154 $this->db->manipulate('DELETE FROM mail_cron_orphaned WHERE ' . $this->db->in('mail_id', $this->collector->getMailIdsToDelete(), false, 'integer'));
155 } else {
156 $this->db->manipulate('DELETE FROM mail_cron_orphaned');
157 }
158 }
159
163 public function processDeletion()
164 {
165 if (count($this->collector->getMailIdsToDelete()) > 0) {
166 // delete possible attachments ...
167 $this->deleteAttachments();
168
169 $this->deleteMails();
170 require_once './Services/Logging/classes/public/class.ilLoggerFactory.php';
171 ilLoggerFactory::getLogger('mail')->info(sprintf(
172 'Deleted mail_ids: %s',
173 implode(', ', $this->collector->getMailIdsToDelete())
174 ));
175
176 $this->deleteMarkedAsNotified();
177 ilLoggerFactory::getLogger('mail')->info(sprintf(
178 'Deleted mail_cron_orphaned mail_ids: %s',
179 implode(', ', $this->collector->getMailIdsToDelete())
180 ));
181 }
182 }
183}
An exception for terminatinating execution or to throw for unit testing.
static getLogger($a_component_id)
Get component logger.
__construct(ilMailCronOrphanedMails $job, ilMailCronOrphanedMailsDeletionCollector $collector)
static delDir($a_dir, $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
const CLIENT_DATA_DIR
Definition: constants.php:44
global $DIC
Definition: goto.php:24
$i
Definition: metadata.php:24
foreach($_POST as $key=> $value) $res
settings()
Definition: settings.php:2