ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
CertificateIdMigration.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
25 use ilDBConstants;
27 use ilDBStatement;
30 use JsonException;
31 use ReflectionClass;
33 use PDOException;
34 
36 {
37  public const NUMBER_OF_STEPS = 5;
38  public const NUMBER_OF_CERTS_PER_STEP = 100000;
39 
43 
44  public function getLabel(): string
45  {
46  return (new ReflectionClass($this))->getShortName();
47  }
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->prepared_statement = $this->db->prepareManip(
64  'UPDATE il_cert_user_cert SET certificate_id = ?, template_values = ? WHERE id = ?',
65  [
69  ]
70  );
71 
72  $this->uuid_factory = new Factory();
73  }
74 
79  public function step(Environment $environment): void
80  {
81  $this->db->setLimit(self::NUMBER_OF_CERTS_PER_STEP);
82  $result = $this->db->query(
83  'SELECT id, template_values FROM il_cert_user_cert WHERE certificate_id = ' .
84  $this->db->quote('-', ilDBConstants::T_TEXT)
85  );
86 
87  while ($row = $this->db->fetchAssoc($result)) {
88  try {
89  $template_values = json_decode(
90  $row['template_values'] ?? json_encode([], JSON_THROW_ON_ERROR),
91  true,
92  512,
93  JSON_THROW_ON_ERROR
94  );
95  } catch (JsonException) {
96  $template_values = [];
97  }
98  $certificate_id = $this->uuid_factory->uuid4AsString();
99 
100  $template_values['CERTIFICATE_ID'] = $certificate_id;
101 
102  $this->db->execute($this->prepared_statement, [
103  $certificate_id,
104  json_encode($template_values, JSON_THROW_ON_ERROR),
105  (int) $row['id']
106  ]);
107  }
108 
109  $this->applyUniqueConstraint();
110  }
111 
112  private function applyUniqueConstraint(): void
113  {
114  try {
115  $this->db->addUniqueConstraint('il_cert_user_cert', ['certificate_id'], 'c1');
117  // Nothing to do
118  }
119  }
120 
121  public function getRemainingAmountOfSteps(): int
122  {
123  $result = $this->db->query(
124  'SELECT COUNT(id) AS missing_cert_id FROM il_cert_user_cert WHERE certificate_id = ' .
125  $this->db->quote('-', ilDBConstants::T_TEXT)
126  );
127  $row = $this->db->fetchAssoc($result);
128 
129  $remaining_certs = (int) $row['missing_cert_id'];
130  if ($remaining_certs === 0) {
131  $this->applyUniqueConstraint();
132  }
133 
134  return (int) ceil($remaining_certs / self::NUMBER_OF_CERTS_PER_STEP);
135  }
136 }
prepare(Environment $environment)
Prepare the migration by means of some environment.
A migration is a potentially long lasting operation that can be broken into discrete steps...
Definition: Migration.php:28
getRemainingAmountOfSteps()
Count up how many "things" need to be migrated.
getPreconditions(Environment $environment)
Objectives the migration depend on.
getDefaultAmountOfStepsPerRun()
Tell the default amount of steps to be executed for one run of the migration.
Builds data types.
Definition: Factory.php:35
getResource(string $id)
Consumers of this method should check if the result is what they expect, e.g.
An environment holds resources to be used in the setup process.
Definition: Environment.php:27