ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
MailOutboxMigration.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
30use ReflectionClass;
31
32class MailOutboxMigration implements Migration
33{
34 public const int NUMBER_OF_STEPS = 10;
35 public const int NUMBER_OF_PATHS_PER_STEP = 10;
36
37 private ilDBStatement $ps_in_fold_entry;
38 private ilDBStatement $ps_in_tree_entry;
39 private ilDBStatement $ps_up_tree_entry;
40 private ?IOWrapper $io = null;
41 private ilDBInterface $db;
42
43 public function getLabel(): string
44 {
45 return (new ReflectionClass($this))->getShortName();
46 }
47
48 public function getDefaultAmountOfStepsPerRun(): int
49 {
50 return self::NUMBER_OF_STEPS;
51 }
52
53 public function getPreconditions(Environment $environment): array
54 {
55 return [
57 ];
58 }
59
60 public function prepare(Environment $environment): void
61 {
62 $this->db = $environment->getResource(Environment::RESOURCE_DATABASE);
63 $this->ps_in_fold_entry = $this->db->prepareManip(
64 'INSERT INTO mail_obj_data (obj_id, user_id, title, m_type) VALUES(?, ?, ?, ?)',
66 );
67
68 $this->ps_in_tree_entry = $this->db->prepareManip(
69 'INSERT INTO mail_tree (tree, child, parent, lft, rgt, depth) VALUES(?, ?, ?, ?, ?, ?)',
70 [
77 ]
78 );
79
80 $this->ps_up_tree_entry = $this->db->prepareManip(
81 <<<SQL
82 UPDATE mail_tree
83 SET lft = CASE WHEN lft > ? THEN lft + 2 ELSE lft END,
84 rgt = CASE WHEN rgt > ? THEN rgt + 2 ELSE rgt END
85 WHERE tree = ?
86 SQL,
88 );
89 }
90
91 public function step(Environment $environment): void
92 {
93 $this->db->setLimit(self::NUMBER_OF_PATHS_PER_STEP);
94 $sql = <<<SQL
95 SELECT mail_obj_data.*, mail_tree.*
96 FROM usr_data AS ud
97 INNER JOIN mail_obj_data ON mail_obj_data.user_id = ud.usr_id AND mail_obj_data.title = %s AND mail_obj_data.m_type = %s
98 INNER JOIN mail_tree ON mail_tree.child = mail_obj_data.obj_id AND mail_tree.tree = ud.usr_id
99 LEFT JOIN mail_obj_data AS outbox ON outbox.user_id = ud.usr_id AND outbox.title = %s
100 WHERE outbox.obj_id IS NULL
101 SQL;
102
103 $res = $this->db->queryF(
104 $sql,
106 ['d_drafts', 'drafts', 'e_outbox']
107 );
108 while ($draft_folder_row = $this->db->fetchAssoc($res)) {
109 $outbox_folder_id = $this->db->nextId('mail_obj_data');
110 $this->db->execute(
111 $this->ps_in_fold_entry,
112 [$outbox_folder_id, (int) $draft_folder_row['user_id'], 'e_outbox', 'outbox']
113 );
114 $this->inform(
115 "Created outbox folder (obj_id: $outbox_folder_id) for user_id: " . $draft_folder_row['user_id']
116 );
117
118 $right = $draft_folder_row['rgt'];
119 $lft = $right + 1 ;
120 $rgt = $right + 2;
121
122 $this->db->execute($this->ps_up_tree_entry, [$right, $right, $draft_folder_row['user_id']]);
123 $this->db->execute(
124 $this->ps_in_tree_entry,
125 [
126 $draft_folder_row['user_id'],
127 $outbox_folder_id,
128 $draft_folder_row['parent'],
129 $lft,
130 $rgt,
131 2
132 ]
133 );
134 $this->inform(
135 "Inserted outbox folder (obj_id: $outbox_folder_id) into tree for user_id: " . $draft_folder_row['user_id']
136 );
137 }
138 }
139
140 public function getRemainingAmountOfSteps(): int
141 {
142 $sql = <<<SQL
143 SELECT COUNT(*) AS paths
144 FROM usr_data AS ud
145 INNER JOIN mail_obj_data ON mail_obj_data.user_id = ud.usr_id AND mail_obj_data.title = %s AND mail_obj_data.m_type = %s
146 INNER JOIN mail_tree ON mail_tree.child = mail_obj_data.obj_id AND mail_tree.tree = ud.usr_id
147 LEFT JOIN mail_obj_data AS outbox ON outbox.user_id = ud.usr_id AND outbox.title = %s
148 WHERE outbox.obj_id IS NULL
149 SQL;
150
151 $res = $this->db->queryF(
152 $sql,
153 [ilDBConstants::T_TEXT, ilDBConstants::T_TEXT, ilDBConstants::T_TEXT],
154 ['d_drafts', 'drafts', 'e_outbox']
155 );
156 $row = $this->db->fetchAssoc($res);
157 $paths = (int) ($row['paths'] ?? 0);
158 $num_steps = (int) ceil($paths / self::NUMBER_OF_PATHS_PER_STEP);
159
160 $this->inform(
161 "Remaining outbox folders to create: $paths / Estimated steps remaining: $num_steps",
162 true
163 );
164
165 return $num_steps;
166 }
167
168 private function inform(string $text, bool $force = false): void
169 {
170 if ($this->io === null || (!$force && !$this->io->isVerbose())) {
171 return;
172 }
173
174 $this->io->inform($text);
175 }
176
177 private function error(string $text): void
178 {
179 if ($this->io === null) {
180 return;
181 }
182
183 $this->io->error($text);
184 }
185}
Wrapper around symfonies input and output facilities to provide just the functionality required for t...
Definition: IOWrapper.php:33
error(string $a_errmsg)
return true
Class ilDBConstants.
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
An environment holds resources to be used in the setup process.
Definition: Environment.php:28
A migration is a potentially long lasting operation that can be broken into discrete steps.
Definition: Migration.php:29
Interface ilDBInterface.
Interface ilDBStatement.
$res
Definition: ltiservices.php:69
to(\GdImage $image, ?int $quality=null)
Currently this is the only way to make a FileStream from a GD image resource.
$text
Definition: xapiexit.php:21