ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilUpdateMailTemplatesForMustache.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 'ilUpdateMailTemplatesForMustache';
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  $tpl_values = $this->getNextTemplateToBeUpdated();
55  if ($tpl_values === null) {
56  return;
57  }
58 
59  [$tpl_id, $lang] = $tpl_values;
60 
61  $this->replace($tpl_id, $lang);
62  }
63 
64  public function getRemainingAmountOfSteps(): int
65  {
66  $q = 'SELECT COUNT(tpl_id) AS open FROM mail_man_tpl ' . PHP_EOL . $this->getWhere();
67  $res = $this->db->query($q);
68  $row = $this->db->fetchAssoc($res);
69 
70  return (int) $row['open'];
71  }
72 
76  protected function getNextTemplateToBeUpdated(): ?array
77  {
78  $this->db->setLimit(1);
79  $q = 'SELECT tpl_id, lang FROM mail_man_tpl ' . PHP_EOL . $this->getWhere();
80  $res = $this->db->query($q);
81 
82  if ($this->db->numRows($res) === 0) {
83  return null;
84  }
85 
86  $row = $this->db->fetchAssoc($res);
87 
88  return [
89  (int) $row['tpl_id'],
90  $row['lang']
91  ];
92  }
93 
94  protected function getWhere(): string
95  {
96  return " WHERE (m_subject REGEXP '\[[A-Z_]+?\]' OR m_message REGEXP '\[[A-Z_]+?\]')" . PHP_EOL;
97  }
98 
99  protected function replace(int $tpl_id, string $lang): void
100  {
101  $res = $this->db->queryF(
102  'SELECT m_subject, m_message FROM mail_man_tpl WHERE tpl_id = %s AND lang = %s',
104  [$tpl_id, $lang]
105  );
106  if ($this->db->numRows($res) === 1) {
107  $row = $this->db->fetchAssoc($res);
108 
109  $subject = isset($row['m_subject']) ? preg_replace(
110  '/\[([A-Z_]+?)\]/',
111  '{{$1}}',
112  $row['m_subject']
113  ) : null;
114  $message = isset($row['m_message']) ? preg_replace(
115  '/\[([A-Z_]+?)\]/',
116  '{{$1}}',
117  $row['m_message']
118  ) : null;
119 
120  $this->db->manipulateF(
121  'UPDATE mail_man_tpl SET m_subject = %s, m_message = %s WHERE tpl_id = %s AND lang = %s',
123  [$subject, $message, $tpl_id, $lang]
124  );
125  }
126  }
127 }
step(Environment $environment)
Run one step of the migration.
$res
Definition: ltiservices.php:69
A migration is a potentially long lasting operation that can be broken into discrete steps...
Definition: Migration.php:28
getPreconditions(Environment $environment)
Objectives the migration depend on.
getResource(string $id)
Consumers of this method should check if the result is what they expect, e.g.
getRemainingAmountOfSteps()
Count up how many "things" need to be migrated.
$lang
Definition: xapiexit.php:26
An environment holds resources to be used in the setup process.
Definition: Environment.php:27
getDefaultAmountOfStepsPerRun()
Tell the default amount of steps to be executed for one run of the migration.
$q
Definition: shib_logout.php:21
$message
Definition: xapiexit.php:32
prepare(Environment $environment)
Prepare the migration by means of some environment.