ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilMDLOMConformanceMigration.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
22 use ILIAS\Setup;
26 
27 class ilMDLOMConformanceMigration implements Setup\Migration
28 {
29  protected const SELECT_LIMIT = 1000;
30  protected const MAX_LOOPS = 10000;
31 
32  protected ilDBInterface $db;
33  protected IOWrapper $io;
34 
35  public function getLabel(): string
36  {
37  return "Migration of some LOM elements that should be non-unique to their own tables.";
38  }
39 
41  {
42  return Migration::INFINITE;
43  }
44 
45  public function getPreconditions(Environment $environment): array
46  {
47  return [
51  ];
52  }
53 
54  public function prepare(Environment $environment): void
55  {
56  $this->db = $environment->getResource(Environment::RESOURCE_DATABASE);
57 
58  $io = $environment->getResource(Environment::RESOURCE_ADMIN_INTERACTION);
59  if ($io instanceof IOWrapper) {
60  $this->io = $io;
61  }
62  }
63 
64  public function step(Environment $environment): void
65  {
66  $this->startLog();
67 
68  $this->migrateCoverage(); // general > coverage
69  $this->setIndices('il_meta_coverage');
70 
71  $this->migrateSchema(); // metaMetadata > metadataSchema
72  $this->setIndices('il_meta_meta_schema');
73 
74  $this->migrateOrComposite(); // technical > requirement > orComposite
75  $this->setIndices('il_meta_or_composite');
76 
77  $this->migrateFromEducational();// educational > learningResourceType, intendedEndUserRole, context
78  $this->setIndices('il_meta_lr_type');
79  $this->setIndices('il_meta_end_usr_role');
80  $this->setIndices('il_meta_context');
81  }
82 
83  public function getRemainingAmountOfSteps(): int
84  {
85  if (
86  $this->countNotMigratedCoverages() > 0 ||
87  !$this->areIndicesSet('il_meta_coverage')
88  ) {
89  return 1;
90  }
91 
92  if (
93  $this->countNotMigratedSchemas() > 0 ||
94  !$this->areIndicesSet('il_meta_meta_schema')
95  ) {
96  return 1;
97  }
98 
99  if (
100  $this->countNotMigratedOrComposites() > 0 ||
101  !$this->areIndicesSet('il_meta_or_composite')
102  ) {
103  return 1;
104  }
105 
106  if (
107  $this->countNotMigratedFromEducational() > 0 ||
108  !$this->areIndicesSet('il_meta_lr_type') ||
109  !$this->areIndicesSet('il_meta_end_usr_role') ||
110  !$this->areIndicesSet('il_meta_context')
111  ) {
112  return 1;
113  }
114  return 0;
115  }
116 
117  protected function areIndicesSet(string $table): bool
118  {
119  return $this->db->indexExistsByFields($table, ['rbac_id', 'obj_id']);
120  }
121 
122  protected function setIndices(string $table): void
123  {
124  if (!$this->areIndicesSet($table)) {
125  $this->logDetailed('Set index for ' . $table);
126  $this->db->addIndex($table, ['rbac_id', 'obj_id'], 'i1');
127  }
128  }
129 
130  protected function migrateCoverage(): void
131  {
132  $count = 0;
133  while ($this->countNotMigratedCoverages() > 0 && $count < self::MAX_LOOPS) {
134  $this->migrateSetOfCoverages(self::SELECT_LIMIT);
135  $count++;
136  }
137  $this->logSuccess('Migrated all coverages.');
138  }
139 
140  protected function migrateSetOfCoverages(int $limit): void
141  {
142  $this->db->setLimit($limit);
143  $coverages = $this->db->query(
144  "SELECT meta_general_id, rbac_id, obj_id, obj_type, coverage,
145  coverage_language FROM il_meta_general WHERE CHAR_LENGTH(coverage) > 0
146  OR CHAR_LENGTH(coverage_language) > 0 ORDER BY meta_general_id"
147  );
148 
149  while ($coverage = $this->db->fetchAssoc($coverages)) {
150  $this->logDetailed('Insert coverage from meta_general_id ' . $coverage['meta_general_id']);
151  $next_id = $this->db->nextId('il_meta_coverage');
152  $this->db->manipulate(
153  "INSERT INTO il_meta_coverage (meta_coverage_id, rbac_id, obj_id,
154  obj_type, parent_type, parent_id, coverage, coverage_language) VALUES (" .
155  $this->db->quote($next_id, ilDBConstants::T_INTEGER) . ", " .
156  $this->db->quote($coverage['rbac_id'], ilDBConstants::T_INTEGER) . ", " .
157  $this->db->quote($coverage['obj_id'], ilDBConstants::T_INTEGER) . ", " .
158  $this->db->quote($coverage['obj_type'], ilDBConstants::T_TEXT) . ", " .
159  $this->db->quote('meta_general', ilDBConstants::T_TEXT) . ", " .
160  $this->db->quote($coverage['meta_general_id'], ilDBConstants::T_INTEGER) . ", " .
161  $this->db->quote($coverage['coverage'], ilDBConstants::T_TEXT) . ", " .
162  $this->db->quote($coverage['coverage_language'], ilDBConstants::T_TEXT) . ")"
163  );
164  $this->logDetailed('Success, deleting old entry');
165  $this->db->manipulate(
166  "UPDATE il_meta_general SET coverage = '', coverage_language = ''
167  WHERE meta_general_id = " . $this->db->quote($coverage['meta_general_id'], ilDBConstants::T_INTEGER)
168  );
169  }
170  }
171 
172  protected function countNotMigratedCoverages(): int
173  {
174  $res = $this->db->query(
175  "SELECT COUNT(*) AS count FROM il_meta_general WHERE CHAR_LENGTH(coverage) > 0
176  OR CHAR_LENGTH(coverage_language) > 0"
177  );
178  while ($rec = $this->db->fetchAssoc($res)) {
179  return (int) $rec['count'];
180  }
181  return 0;
182  }
183 
184  protected function migrateSchema(): void
185  {
186  $count = 0;
187  while ($this->countNotMigratedSchemas() > 0 && $count < self::MAX_LOOPS) {
188  $this->migrateSetOfSchemas(self::SELECT_LIMIT);
189  $count++;
190  }
191  $this->logSuccess('Migrated all schemas.');
192  }
193 
194  protected function migrateSetOfSchemas(int $limit): void
195  {
196  $this->db->setLimit($limit);
197  $schemas = $this->db->query(
198  "SELECT meta_meta_data_id, rbac_id, obj_id, obj_type, meta_data_scheme
199  FROM il_meta_meta_data WHERE CHAR_LENGTH(meta_data_scheme) > 0 ORDER BY meta_meta_data_id"
200  );
201 
202  while ($schema = $this->db->fetchAssoc($schemas)) {
203  $this->logDetailed('Insert schema from meta_meta_data_id ' . $schema['meta_meta_data_id']);
204  $next_id = $this->db->nextId('il_meta_meta_schema');
205  $this->db->manipulate(
206  "INSERT INTO il_meta_meta_schema (meta_meta_schema_id, rbac_id, obj_id,
207  obj_type, parent_type, parent_id, meta_data_schema) VALUES (" .
208  $this->db->quote($next_id, ilDBConstants::T_INTEGER) . ", " .
209  $this->db->quote($schema['rbac_id'], ilDBConstants::T_INTEGER) . ", " .
210  $this->db->quote($schema['obj_id'], ilDBConstants::T_INTEGER) . ", " .
211  $this->db->quote($schema['obj_type'], ilDBConstants::T_TEXT) . ", " .
212  $this->db->quote('meta_meta_data', ilDBConstants::T_TEXT) . ", " .
213  $this->db->quote($schema['meta_meta_data_id'], ilDBConstants::T_INTEGER) . ", " .
214  $this->db->quote('LOMv1.0', ilDBConstants::T_TEXT) . ")"
215  );
216  $this->logDetailed('Success, deleting old entry');
217  $this->db->manipulate(
218  "UPDATE il_meta_meta_data SET meta_data_scheme = '' WHERE meta_meta_data_id = " .
219  $this->db->quote($schema['meta_meta_data_id'], ilDBConstants::T_INTEGER)
220  );
221  }
222  }
223 
224  protected function countNotMigratedSchemas(): int
225  {
226  $res = $this->db->query(
227  "SELECT COUNT(*) AS count FROM il_meta_meta_data WHERE CHAR_LENGTH(meta_data_scheme) > 0"
228  );
229  while ($rec = $this->db->fetchAssoc($res)) {
230  return (int) $rec['count'];
231  }
232  return 0;
233  }
234 
235  protected function migrateOrComposite(): void
236  {
237  $count = 0;
238  while ($this->countNotMigratedOrComposites() > 0 && $count < self::MAX_LOOPS) {
239  $this->migrateSetOfOrComposites(self::SELECT_LIMIT);
240  $count++;
241  }
242  $this->logSuccess('Migrated all orComposites.');
243  }
244 
245  protected function migrateSetOfOrComposites(int $limit): void
246  {
247  $this->db->setLimit($limit);
248  $ors = $this->db->query(
249  "SELECT meta_requirement_id, rbac_id, obj_id, obj_type, operating_system_name,
250  os_min_version, os_max_version, browser_name, browser_minimum_version,
251  browser_maximum_version FROM il_meta_requirement WHERE
252  CHAR_LENGTH(operating_system_name) > 0 OR CHAR_LENGTH(os_min_version) > 0
253  OR CHAR_LENGTH(os_max_version) > 0 OR CHAR_LENGTH(browser_name) > 0
254  OR CHAR_LENGTH(browser_minimum_version) > 0 OR CHAR_LENGTH(browser_maximum_version) > 0
255  ORDER BY meta_requirement_id"
256  );
257 
258  while ($or = $this->db->fetchAssoc($ors)) {
259  $has_os =
260  ($or['operating_system_name'] ?? '') !== '' ||
261  ($or['os_min_version'] ?? '') !== '' ||
262  ($or['os_max_version'] ?? '') !== '';
263  $has_browser =
264  ($or['browser_name'] ?? '') !== '' ||
265  ($or['browser_minimum_version'] ?? '') !== '' ||
266  ($or['browser_maximum_version'] ?? '') !== '';
267 
268  if ($has_os) {
269  $this->logDetailed('Insert orComposite (type os) from meta_requirement_id ' . $or['meta_requirement_id']);
270  $next_id = $this->db->nextId('il_meta_or_composite');
271  $name = $this->normalizeOrCompositeName($or['operating_system_name']);
272  $this->db->manipulate(
273  "INSERT INTO il_meta_or_composite (meta_or_composite_id, rbac_id, obj_id,
274  obj_type, parent_type, parent_id, type, name, min_version, max_version) VALUES (" .
275  $this->db->quote($next_id, ilDBConstants::T_INTEGER) . ", " .
276  $this->db->quote($or['rbac_id'], ilDBConstants::T_INTEGER) . ", " .
277  $this->db->quote($or['obj_id'], ilDBConstants::T_INTEGER) . ", " .
278  $this->db->quote($or['obj_type'], ilDBConstants::T_TEXT) . ", " .
279  $this->db->quote('meta_requirement', ilDBConstants::T_TEXT) . ", " .
280  $this->db->quote($or['meta_requirement_id'], ilDBConstants::T_INTEGER) . ", " .
281  $this->db->quote('operating system', ilDBConstants::T_TEXT) . ", " .
282  $this->db->quote($or['operating_system_name'], ilDBConstants::T_TEXT) . ", " .
283  $this->db->quote($or['os_min_version'], ilDBConstants::T_TEXT) . ", " .
284  $this->db->quote($or['os_max_version'], ilDBConstants::T_TEXT) . ")"
285  );
286  $this->logDetailed('Success, deleting old entry');
287  $this->db->manipulate(
288  "UPDATE il_meta_requirement SET operating_system_name = '', os_min_version = '',
289  os_max_version = '' WHERE meta_requirement_id = " .
290  $this->db->quote($or['meta_requirement_id'], ilDBConstants::T_INTEGER)
291  );
292  }
293 
294  if ($has_browser) {
295  $this->logDetailed('Insert orComposite (type browser) from meta_requirement_id ' . $or['meta_requirement_id']);
296  $next_id = $this->db->nextId('il_meta_or_composite');
297  $name = $this->normalizeOrCompositeName($or['browser_name']);
298  $this->db->manipulate(
299  "INSERT INTO il_meta_or_composite (meta_or_composite_id, rbac_id, obj_id,
300  obj_type, parent_type, parent_id, type, name, min_version, max_version) VALUES (" .
301  $this->db->quote($next_id, ilDBConstants::T_INTEGER) . ", " .
302  $this->db->quote($or['rbac_id'], ilDBConstants::T_INTEGER) . ", " .
303  $this->db->quote($or['obj_id'], ilDBConstants::T_INTEGER) . ", " .
304  $this->db->quote($or['obj_type'], ilDBConstants::T_TEXT) . ", " .
305  $this->db->quote('meta_requirement', ilDBConstants::T_TEXT) . ", " .
306  $this->db->quote($or['meta_requirement_id'], ilDBConstants::T_INTEGER) . ", " .
307  $this->db->quote('browser', ilDBConstants::T_TEXT) . ", " .
308  $this->db->quote($name, ilDBConstants::T_TEXT) . ", " .
309  $this->db->quote($or['browser_minimum_version'], ilDBConstants::T_TEXT) . ", " .
310  $this->db->quote($or['browser_maximum_version'], ilDBConstants::T_TEXT) . ")"
311  );
312  $this->logDetailed('Success, deleting old entry');
313  $this->db->manipulate(
314  "UPDATE il_meta_requirement SET browser_name = '', browser_minimum_version = '',
315  browser_maximum_version = '' WHERE meta_requirement_id = " .
316  $this->db->quote($or['meta_requirement_id'], ilDBConstants::T_INTEGER)
317  );
318  }
319  }
320  }
321 
322  protected function countNotMigratedOrComposites(): int
323  {
324  $res = $this->db->query(
325  "SELECT COUNT(*) AS count FROM il_meta_requirement WHERE
326  CHAR_LENGTH(operating_system_name) > 0 OR CHAR_LENGTH(os_min_version) > 0
327  OR CHAR_LENGTH(os_max_version) > 0 OR CHAR_LENGTH(browser_name) > 0
328  OR CHAR_LENGTH(browser_minimum_version) > 0 OR CHAR_LENGTH(browser_maximum_version) > 0"
329  );
330  while ($rec = $this->db->fetchAssoc($res)) {
331  return (int) $rec['count'];
332  }
333  return 0;
334  }
335 
336  protected function normalizeOrCompositeName(string $name): string
337  {
338  $dict = [
339  'Any' => 'any',
340  'NetscapeCommunicator' => 'netscape communicator',
341  'MS-InternetExplorer' => 'ms-internet explorer',
342  'Opera' => 'opera',
343  'Amaya' => 'amaya',
344  'PC-DOS' => 'pc-dos',
345  'MS-Windows' => 'ms-windows',
346  'MAC-OS' => 'macos',
347  'Unix' => 'unix',
348  'Multi-OS' => 'multi-os',
349  'None' => 'none'
350  ];
351 
352  if (key_exists($name, $dict)) {
353  return $dict[$name];
354  }
355  return $name;
356  }
357 
358  protected function migrateFromEducational(): void
359  {
360  $count = 0;
361  while ($this->countNotMigratedFromEducational() > 0 && $count < self::MAX_LOOPS) {
362  $this->migrateSetFromEducational(self::SELECT_LIMIT);
363  $count++;
364  }
365  $this->logSuccess('Migrated all learningResourceTypes, intendedEndUserRoles, and contexts.');
366  }
367 
368  protected function migrateSetFromEducational(int $limit): void
369  {
370  $this->db->setLimit($limit);
371  $educationals = $this->db->query(
372  "SELECT meta_educational_id, rbac_id, obj_id, obj_type,
373  learning_resource_type, intended_end_user_role, context FROM il_meta_educational
374  WHERE CHAR_LENGTH(learning_resource_type) > 0 OR CHAR_LENGTH(intended_end_user_role) > 0
375  OR CHAR_LENGTH(context) > 0 ORDER BY meta_educational_id"
376  );
377 
378  while ($educational = $this->db->fetchAssoc($educationals)) {
379  $has_type = ($educational['learning_resource_type'] ?? '') !== '';
380  $has_role = ($educational['intended_end_user_role'] ?? '') !== '';
381  $has_context = ($educational['context'] ?? '') !== '';
382 
383  if ($has_type) {
384  $this->logDetailed('Insert learningResourceType from meta_educational_id ' . $educational['meta_educational_id']);
385  $next_id = $this->db->nextId('il_meta_lr_type');
386  $type = $this->normalizeLearningResourceType($educational['learning_resource_type']);
387  $this->db->manipulate(
388  "INSERT INTO il_meta_lr_type (meta_lr_type_id, rbac_id, obj_id, obj_type,
389  parent_type, parent_id, learning_resource_type) VALUES (" .
390  $this->db->quote($next_id, ilDBConstants::T_INTEGER) . ", " .
391  $this->db->quote($educational['rbac_id'], ilDBConstants::T_INTEGER) . ", " .
392  $this->db->quote($educational['obj_id'], ilDBConstants::T_INTEGER) . ", " .
393  $this->db->quote($educational['obj_type'], ilDBConstants::T_TEXT) . ", " .
394  $this->db->quote('meta_educational', ilDBConstants::T_TEXT) . ", " .
395  $this->db->quote($educational['meta_educational_id'], ilDBConstants::T_INTEGER) . ", " .
396  $this->db->quote($type, ilDBConstants::T_TEXT) . ")"
397  );
398  $this->logDetailed('Success, deleting old entry');
399  $this->db->manipulate(
400  "UPDATE il_meta_educational SET learning_resource_type = '' WHERE meta_educational_id = " .
401  $this->db->quote($educational['meta_educational_id'], ilDBConstants::T_INTEGER)
402  );
403  }
404 
405  if ($has_role) {
406  $this->logDetailed('Insert intendedEndUserRole from meta_educational_id ' . $educational['meta_educational_id']);
407  $next_id = $this->db->nextId('il_meta_end_usr_role');
408  $role = $this->normalizeIntendedEndUserRole($educational['intended_end_user_role']);
409  $this->db->manipulate(
410  "INSERT INTO il_meta_end_usr_role (meta_end_usr_role_id, rbac_id, obj_id, obj_type,
411  parent_type, parent_id, intended_end_user_role) VALUES (" .
412  $this->db->quote($next_id, ilDBConstants::T_INTEGER) . ", " .
413  $this->db->quote($educational['rbac_id'], ilDBConstants::T_INTEGER) . ", " .
414  $this->db->quote($educational['obj_id'], ilDBConstants::T_INTEGER) . ", " .
415  $this->db->quote($educational['obj_type'], ilDBConstants::T_TEXT) . ", " .
416  $this->db->quote('meta_educational', ilDBConstants::T_TEXT) . ", " .
417  $this->db->quote($educational['meta_educational_id'], ilDBConstants::T_INTEGER) . ", " .
418  $this->db->quote($role, ilDBConstants::T_TEXT) . ")"
419  );
420  $this->logDetailed('Success, deleting old entry');
421  $this->db->manipulate(
422  "UPDATE il_meta_educational SET intended_end_user_role = '' WHERE meta_educational_id = " .
423  $this->db->quote($educational['meta_educational_id'], ilDBConstants::T_INTEGER)
424  );
425  }
426 
427  if ($has_context) {
428  $this->logDetailed('Insert context from meta_educational_id ' . $educational['meta_educational_id']);
429  $next_id = $this->db->nextId('il_meta_context');
430  $context = $this->normalizeContext($educational['context']);
431  $this->db->manipulate(
432  "INSERT INTO il_meta_context (meta_context_id, rbac_id, obj_id, obj_type,
433  parent_type, parent_id, context) VALUES (" .
434  $this->db->quote($next_id, ilDBConstants::T_INTEGER) . ", " .
435  $this->db->quote($educational['rbac_id'], ilDBConstants::T_INTEGER) . ", " .
436  $this->db->quote($educational['obj_id'], ilDBConstants::T_INTEGER) . ", " .
437  $this->db->quote($educational['obj_type'], ilDBConstants::T_TEXT) . ", " .
438  $this->db->quote('meta_educational', ilDBConstants::T_TEXT) . ", " .
439  $this->db->quote($educational['meta_educational_id'], ilDBConstants::T_INTEGER) . ", " .
440  $this->db->quote($context, ilDBConstants::T_TEXT) . ")"
441  );
442  $this->logDetailed('Success, deleting old entry');
443  $this->db->manipulate(
444  "UPDATE il_meta_educational SET context = '' WHERE meta_educational_id = " .
445  $this->db->quote($educational['meta_educational_id'], ilDBConstants::T_INTEGER)
446  );
447  }
448  }
449  }
450 
451  protected function countNotMigratedFromEducational(): int
452  {
453  $res = $this->db->query(
454  "SELECT COUNT(*) AS count FROM il_meta_educational WHERE
455  CHAR_LENGTH(learning_resource_type) > 0 OR CHAR_LENGTH(intended_end_user_role) > 0
456  OR CHAR_LENGTH(context) > 0"
457  );
458  while ($rec = $this->db->fetchAssoc($res)) {
459  return (int) $rec['count'];
460  }
461  return 0;
462  }
463 
464  protected function normalizeLearningResourceType(string $name): string
465  {
466  $dict = [
467  'Exercise' => 'exercise',
468  'Simulation' => 'simulation',
469  'Questionnaire' => 'questionnaire',
470  'Diagram' => 'diagram',
471  'Figure' => 'figure',
472  'Graph' => 'graph',
473  'Index' => 'index',
474  'Slide' => 'slide',
475  'Table' => 'table',
476  'NarrativeText' => 'narrative text',
477  'Exam' => 'exam',
478  'Experiment' => 'experiment',
479  'ProblemStatement' => 'problem statement',
480  'SelfAssessment' => 'self assessment',
481  'Lecture' => 'lecture'
482  ];
483 
484  if (key_exists($name, $dict)) {
485  return $dict[$name];
486  }
487  return $name;
488  }
489 
490  protected function normalizeIntendedEndUserRole(string $name): string
491  {
492  $dict = [
493  'Teacher' => 'teacher',
494  'Author' => 'author',
495  'Learner' => 'learner',
496  'Manager' => 'manager'
497  ];
498 
499  if (key_exists($name, $dict)) {
500  return $dict[$name];
501  }
502  return $name;
503  }
504 
505  protected function normalizeContext(string $name): string
506  {
507  $dict = [
508  'School' => 'school',
509  'HigherEducation' => 'higher education',
510  'Training' => 'training',
511  'Other' => 'other'
512  ];
513 
514  if (key_exists($name, $dict)) {
515  return $dict[$name];
516  }
517  return $name;
518  }
519 
520  protected function logDetailed(string $str): void
521  {
522  if (!isset($this->io) || !$this->io->isVerbose()) {
523  return;
524  }
525  $this->io->inform($str);
526  }
527 
528  protected function logSuccess(string $str): void
529  {
530  if (!isset($this->io)) {
531  return;
532  }
533  $this->io->success($str);
534  }
535 
536  protected function startLog(): void
537  {
538  if (!isset($this->io)) {
539  return;
540  }
541  $this->io->text('');
542  }
543 }
$res
Definition: ltiservices.php:69
Wrapper around symfonies input and output facilities to provide just the functionality required for t...
Definition: IOWrapper.php:32
$context
Definition: webdav.php:31
$educational
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