ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilGlossaryDefinitionMigration.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
22 use ILIAS\Setup;
25 
29 final class ilGlossaryDefinitionMigration implements Setup\Migration
30 {
31  protected ilDBInterface $db;
32 
33  public function getLabel(): string
34  {
35  return "Migration of glossary definitions after abolition of multiple definitions";
36  }
37 
39  {
40  return Migration::INFINITE;
41  }
42 
43  public function getPreconditions(Environment $environment): array
44  {
45  return [
49  ];
50  }
51 
52  public function prepare(Environment $environment): void
53  {
54  $this->db = $environment->getResource(Environment::RESOURCE_DATABASE);
55  }
56 
57  public function step(Environment $environment): void
58  {
59  $set = $this->db->query(
60  "SELECT glossary_definition.id AS glo_def_id, glossary_definition.term_id AS glo_def_term_id, " .
61  " glossary_definition.short_text, glossary_definition.short_text_dirty, " .
62  " glossary_term.id AS glo_term_id, glossary_term.glo_id, glossary_term.term, glossary_term.language, " .
63  " glossary_term.create_date, glossary_term.last_update " .
64  " FROM glossary_definition JOIN glossary_term " .
65  " WHERE glossary_definition.term_id = glossary_term.id " .
66  " ORDER BY glossary_term.glo_id, glossary_term.id, glossary_definition.id"
67  );
68  $tmp = [];
69  while ($rec = $this->db->fetchAssoc($set)) {
70  // check if there are multiple definitions for a term
71  if (!empty($tmp)
72  && $tmp["glo_id"] == $rec["glo_id"]
73  && $tmp["glo_term_id"] == $rec["glo_term_id"]
74  ) {
75  $new_term_id = $this->db->nextId('glossary_term');
76  // create new term with same values, but new id
77  $this->log("Create new glossary term with id: " . $new_term_id);
78  $this->db->manipulate("INSERT INTO glossary_term (id, glo_id, term, language, import_id, create_date, last_update)" .
79  " VALUES (" .
80  $this->db->quote($new_term_id, "integer") . ", " .
81  $this->db->quote($rec["glo_id"], "integer") . ", " .
82  $this->db->quote($rec["term"], "text") . ", " .
83  $this->db->quote($rec["language"], "text") . "," .
84  $this->db->quote("", "text") . "," .
85  $this->db->quote($rec["create_date"], "text") . ", " .
86  $this->db->quote($rec["last_update"], "text") . ")");
87  // change definition with new term id
88  $this->db->manipulate(
89  "UPDATE glossary_definition SET " .
90  " term_id = " . $this->db->quote($new_term_id, "integer") .
91  " WHERE id = " . $this->db->quote($rec["glo_def_id"], "integer")
92  );
93  }
94  $tmp["glo_id"] = $rec["glo_id"];
95  $tmp["glo_term_id"] = $rec["glo_term_id"];
96  }
97 
98  $set = $this->db->query("SELECT * FROM glossary_definition WHERE migration = " . $this->db->quote("0", "integer"));
99  while ($rec = $this->db->fetchAssoc($set)) {
100  // merge glossary_term and glossary_definition table
101  $this->log("Add short text ('" . $rec["short_text"] . "') and short text dirty ('" .
102  $rec["short_text_dirty"] . "') to glossary term with id: " . $rec["term_id"]);
103  $this->db->manipulate(
104  "UPDATE glossary_term SET " .
105  " short_text = " . $this->db->quote($rec["short_text"], "text") . ", " .
106  " short_text_dirty = " . $this->db->quote($rec["short_text_dirty"], "integer") .
107  " WHERE id = " . $this->db->quote($rec["term_id"], "integer")
108  );
109  // update id and type in page objects
110  $this->log("Update id and type ('gdf' to 'term') for page object with id: " . $rec["id"]);
111  $this->db->manipulate(
112  "UPDATE page_object SET " .
113  " page_id = " . $this->db->quote($rec["term_id"], "integer") . ", " .
114  " parent_type = " . $this->db->quote("term", "text") .
115  " WHERE parent_type = " . $this->db->quote("gdf", "text") .
116  " AND page_id = " . $this->db->quote($rec["id"], "integer")
117  );
118  // set migration marker to 1 when it's done
119  $this->db->manipulate(
120  "UPDATE glossary_definition SET " .
121  " migration = " . $this->db->quote("1", "integer") .
122  " WHERE id = " . $this->db->quote($rec["id"], "integer")
123  );
124  }
125 
126  // update type in page object definition
127  $this->log("Update type ('gdf' to 'term') for page object definition.");
128  $this->db->manipulate(
129  "UPDATE copg_pobj_def SET " .
130  " parent_type = " . $this->db->quote("term", "text") .
131  " WHERE parent_type = " . $this->db->quote("gdf", "text")
132  );
133  }
134 
135  protected function log(string $str): void
136  {
137  echo "\n" . $str;
138  }
139 
140  protected function manipulate(string $query): void
141  {
142  $this->db->manipulate($query);
143  $this->log($query);
144  }
145 
146  public function getRemainingAmountOfSteps(): int
147  {
148  $set = $this->db->query(
149  "SELECT glossary_definition.id AS glo_def_id, glossary_definition.term_id AS glo_def_term_id, " .
150  " glossary_definition.short_text, glossary_definition.short_text_dirty, " .
151  " glossary_term.id AS glo_term_id, glossary_term.glo_id, glossary_term.term, glossary_term.language, " .
152  " glossary_term.create_date, glossary_term.last_update " .
153  " FROM glossary_definition JOIN glossary_term " .
154  " WHERE glossary_definition.term_id = glossary_term.id " .
155  " ORDER BY glossary_term.glo_id, glossary_term.id, glossary_definition.id"
156  );
157  $tmp = [];
158  while ($rec = $this->db->fetchAssoc($set)) {
159  // check if there are multiple definitions for a term
160  if (!empty($tmp)
161  && $tmp["glo_id"] == $rec["glo_id"]
162  && $tmp["glo_term_id"] == $rec["glo_term_id"]
163  ) {
164  return 1;
165  }
166  $tmp["glo_id"] = $rec["glo_id"];
167  $tmp["glo_term_id"] = $rec["glo_term_id"];
168  }
169 
170  $set = $this->db->query("SELECT * FROM glossary_definition WHERE migration = " . $this->db->quote("0", "integer"));
171  if ($rec = $this->db->fetchAssoc($set)) {
172  return 1;
173  }
174 
175  $set = $this->db->query("SELECT parent_type FROM copg_pobj_def WHERE parent_type = " . $this->db->quote("gdf", "text"));
176  if ($rec = $this->db->fetchAssoc($set)) {
177  return 1;
178  }
179 
180  return 0;
181  }
182 }
getResource(string $id)
Consumers of this method should check if the result is what they expect, e.g.
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:27