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