ILIAS  trunk Revision v11.0_alpha-1761-g6dbbfa7b760
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
ExpiredOrOrphanedMailsCollector.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
25 use ilSetting;
26 use ilDBConstants;
28 use ilDBInterface;
29 
31 {
32  private const PING_THRESHOLD = 500;
33 
34  private readonly ilDBInterface $db;
35  private readonly ilSetting $settings;
36  private readonly ClockInterface $clock;
38  private array $mail_ids = [];
39 
40  public function __construct(
41  private readonly ilMailCronOrphanedMails $job,
42  ?ilDBInterface $db = null,
44  ?ClockInterface $clock = null
45  ) {
46  global $DIC;
47 
48  $this->db = $db ?? $DIC->database();
49  $this->settings = $setting ?? $DIC->settings();
50  $this->clock = $clock ?? (new Factory())->clock()->system();
51 
52  $this->collect();
53  }
54 
55  private function collect(): void
56  {
57  $mail_only_inbox_trash = (bool) $this->settings->get('mail_only_inbox_trash', '0');
58  $mail_expiration_warning_days = (int) $this->settings->get('mail_notify_orphaned', '0');
59 
60  if ($mail_expiration_warning_days > 0) {
61  if ($mail_only_inbox_trash) {
62  // Only select determine mails which are now located in the inbox or trash folder
63  $res = $this->db->queryF(
64  "
65  SELECT mail_id FROM mail_cron_orphaned
66  LEFT JOIN mail_obj_data mdata ON mdata.obj_id = folder_id
67  WHERE ts_do_delete <= %s AND ((mdata.m_type = %s OR mdata.m_type = %s) OR mdata.obj_id IS NULL)
68  ",
70  [$this->clock->now()->getTimestamp(), 'inbox', 'trash']
71  );
72  } else {
73  // Select all determined emails independently of the folder
74  $res = $this->db->queryF(
75  "SELECT mail_id FROM mail_cron_orphaned WHERE ts_do_delete <= %s",
77  [$this->clock->now()->getTimestamp()]
78  );
79  }
80  } else {
81  // Mails should be deleted without notification
82  $mail_expiration_days = (int) $this->settings->get('mail_threshold', '0');
83  $left_interval_datetime = $this->clock->now()->modify('- ' . $mail_expiration_days . ' days');
84 
85  $types = [ilDBConstants::T_TIMESTAMP];
86  $data = [$left_interval_datetime->format('Y-m-d 23:59:59')];
87 
88  $mails_query = "
89  SELECT m.mail_id
90  FROM mail m
91  LEFT JOIN mail_obj_data mdata ON mdata.obj_id = m.folder_id
92  WHERE m.send_time <= %s
93  ";
94 
95  if ($mail_only_inbox_trash) {
96  $mails_query .= " AND ((mdata.m_type = %s OR mdata.m_type = %s) OR mdata.obj_id IS NULL)";
97  $types[] = ilDBConstants::T_TEXT;
98  $types[] = ilDBConstants::T_TEXT;
99  $data[] = 'inbox';
100  $data[] = 'trash';
101  }
102 
103  $res = $this->db->queryF($mails_query, $types, $data);
104  }
105 
106  $i = 0;
107  while ($row = $this->db->fetchAssoc($res)) {
108  if ($i > 0 && $i % self::PING_THRESHOLD) {
109  $this->job->ping();
110  }
111 
112  $this->addMailIdToDelete((int) $row['mail_id']);
113 
114  ++$i;
115  }
116  }
117 
118  private function addMailIdToDelete(int $mail_id): void
119  {
120  $this->mail_ids[] = $mail_id;
121  }
122 
126  public function mailIdsToDelete(): array
127  {
128  return $this->mail_ids;
129  }
130 }
$res
Definition: ltiservices.php:66
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
ilSetting $setting
Definition: class.ilias.php:68
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: shib_login.php:22
Builds data types.
Definition: Factory.php:35
__construct(private readonly ilMailCronOrphanedMails $job, ?ilDBInterface $db=null, ?ilSetting $setting=null, ?ClockInterface $clock=null)