ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilUpdateNewAccountMailTemplatesForMustache.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
23 
25 {
26  public const NUMBER_OF_STEPS = 10000;
27 
28  protected ilDBInterface $db;
29 
30  public function getLabel(): string
31  {
32  return 'ilUpdateNewAccountMailTemplatesForMustache';
33  }
34 
36  {
37  return self::NUMBER_OF_STEPS;
38  }
39 
40  public function getPreconditions(Environment $environment): array
41  {
42  return [
43  new \ilDatabaseUpdatedObjective()
44  ];
45  }
46 
47  public function prepare(Environment $environment): void
48  {
49  $this->db = $environment->getResource(Environment::RESOURCE_DATABASE);
50  }
51 
52  public function step(Environment $environment): void
53  {
54  $lang = $this->getNextLangToBeUpdated();
55  if ($lang === null) {
56  return;
57  }
58 
59  $this->replace($lang, '/\[IF_PASSWORD\]/', '{{#IF_PASSWORD}}');
60  $this->replace($lang, '/\[IF_NO_PASSWORD\]/', '{{#IF_NO_PASSWORD}}');
61  $this->replace($lang, '/\[IF_TARGET\]/', '{{#IF_TARGET}}');
62  $this->replace($lang, '/\[IF_TIMELIMIT\]/', '{{#IF_TIMELIMIT}}');
64  }
65 
66  public function getRemainingAmountOfSteps(): int
67  {
68  $q = 'SELECT COUNT(*) AS open FROM mail_template ' . PHP_EOL . $this->getWhere();
69  $res = $this->db->query($q);
70  $row = $this->db->fetchAssoc($res);
71 
72  return (int) $row['open'];
73  }
74 
75  protected function getNextLangToBeUpdated(): ?string
76  {
77  $this->db->setLimit(1);
78  $q = 'SELECT lang FROM mail_template ' . PHP_EOL . $this->getWhere();
79  $res = $this->db->query($q);
80 
81  if ($this->db->numRows($res) === 0) {
82  return null;
83  }
84 
85  $row = $this->db->fetchAssoc($res);
86 
87  return $row['lang'];
88  }
89 
90  protected function getWhere(): string
91  {
92  return ' WHERE (' . PHP_EOL
93  . $this->db->like('body', ilDBConstants::T_TEXT, '%[%') . ' OR ' . PHP_EOL
94  . $this->db->like('body', ilDBConstants::T_TEXT, '%]%') . ' OR ' . PHP_EOL
95  . $this->db->like('subject', ilDBConstants::T_TEXT, '%[%') . ' OR ' . PHP_EOL
96  . $this->db->like('subject', ilDBConstants::T_TEXT, '%]%') . PHP_EOL
97  . ') AND type = ' . $this->db->quote('nacc', ilDBConstants::T_TEXT);
98  }
99 
100  protected function replace(string $lang, string $regex_search, string $replacement): void
101  {
102  $res = $this->db->queryF(
103  'SELECT subject, body FROM mail_template WHERE lang = %s AND type = %s',
105  [$lang, 'nacc']
106  );
107  if ($this->db->numRows($res) === 1) {
108  $row = $this->db->fetchAssoc($res);
109 
110  $subject = $this->replaceInText(
111  $row['subject'],
112  $regex_search,
113  $replacement
114  );
115  $body = $this->replaceInText(
116  $row['body'],
117  $regex_search,
118  $replacement
119  );
120 
121  $this->db->manipulateF(
122  'UPDATE mail_template SET subject = %s, body = %s WHERE lang = %s AND type = %s',
124  [$subject, $body, $lang, 'nacc']
125  );
126  }
127  }
128 
129  protected function replaceRemainingBrackets(string $lang): void
130  {
131  $res = $this->db->queryF(
132  'SELECT subject, body FROM mail_template WHERE lang = %s AND type = %s',
134  [$lang, 'nacc']
135  );
136  if ($this->db->numRows($res) === 1) {
137  $row = $this->db->fetchAssoc($res);
138 
139  $subject = $this->replaceInText(
140  $row['subject'],
141  '/\[([A-Z_\/]+?)\]/',
142  '{{$1}}'
143  );
144  $body = $this->replaceInText(
145  $row['body'],
146  '/\[([A-Z_\/]+?)\]/',
147  '{{$1}}'
148  );
149 
150  $this->db->manipulateF(
151  'UPDATE mail_template SET subject = %s, body = %s WHERE lang = %s AND type = %s',
153  [$subject, $body, $lang, 'nacc']
154  );
155  }
156  }
157 
158  protected function replaceInText(
159  ?string $text,
160  string $regex_search,
161  string $replacement
162  ): ?string {
163  if ($text === null) {
164  return null;
165  }
166 
167  return preg_replace(
168  $regex_search,
169  $replacement,
170  $text
171  );
172  }
173 }
prepare(Environment $environment)
Prepare the migration by means of some environment.
$res
Definition: ltiservices.php:69
step(Environment $environment)
Run one step of the migration.
A migration is a potentially long lasting operation that can be broken into discrete steps...
Definition: Migration.php:28
getDefaultAmountOfStepsPerRun()
Tell the default amount of steps to be executed for one run of the migration.
replaceInText(?string $text, string $regex_search, string $replacement)
getResource(string $id)
Consumers of this method should check if the result is what they expect, e.g.
$lang
Definition: xapiexit.php:26
An environment holds resources to be used in the setup process.
Definition: Environment.php:27
getPreconditions(Environment $environment)
Objectives the migration depend on.
$q
Definition: shib_logout.php:21
getRemainingAmountOfSteps()
Count up how many "things" need to be migrated.
replace(string $lang, string $regex_search, string $replacement)