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