ILIAS  trunk Revision v11.0_alpha-1811-gd2d5443e411
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
RecordFilesMigration.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
25 use ilDBConstants;
26 use ilDBInterface;
27 use ilFileUtils;
28 use ILIAS\DI;
34 
36 {
37  protected const GLOBAL_KEY = "null";
38  protected ilDBInterface $db;
39 
40  public function getLabel(): string
41  {
42  return 'RecordFilesMigration';
43  }
44 
46  {
47  return 5;
48  }
49 
50  public function getPreconditions(
51  Environment $environment
52  ): array {
53  return [
57  ];
58  }
59 
60  public function prepare(
61  Environment $environment
62  ): void {
63  $this->db = $environment->getResource(Environment::RESOURCE_DATABASE);
64  }
65 
66  public function step(
67  Environment $environment
68  ): void {
69  $files = $this->getFiles();
70  $object_id = array_key_first($files);
71  $file_path = $files[$object_id][0];
72  $stakeholder = (new Stakeholder())->withOwnerId(6);
73  $irss_helper = new ilResourceStorageMigrationHelper($stakeholder, $environment);
74  $rid = $irss_helper->movePathToStorage($file_path, 6, null, null, false);
75  $this->db->manipulate(
76  "INSERT INTO adv_md_record_files VALUES ("
77  . $this->db->quote(($object_id === self::GLOBAL_KEY) ? 0 : $object_id, ilDBConstants::T_INTEGER) . ", "
78  . $this->db->quote($rid->serialize(), ilDBConstants::T_TEXT) . ", "
79  . $this->db->quote((int) ($object_id === self::GLOBAL_KEY), ilDBConstants::T_INTEGER) . ")"
80  );
81  }
82 
83  public function getRemainingAmountOfSteps(): int
84  {
85  $steps = 0;
86  foreach ($this->getFiles() as $object_id => $file_paths) {
87  $steps += count($file_paths);
88  }
89  return $steps;
90  }
91 
92  protected function getExportDir(): string
93  {
94  return ilFileUtils::getDataDir() . '/ilAdvancedMetaData';
95  }
96 
97  protected function getFiles(): array
98  {
99  $files = [];
100  $dirs = [];
101  if (!is_dir($this->getExportDir())) {
102  return $files;
103  }
104  foreach (scandir($this->getExportDir()) as $file) {
105  $matches = [];
106  if (
107  (!preg_match('/^export_([0-9]+)$/', $file, $matches) && $file !== "export") ||
108  in_array($file, ['.', '..', '.DS_Store'])
109  ) {
110  continue;
111  }
112  $object_id = count($matches) == 2
113  ? (int) $matches[1]
114  : self::GLOBAL_KEY;
115  $dirs[$object_id] = $this->getExportDir() . DIRECTORY_SEPARATOR . $file;
116  }
117  foreach ($dirs as $object_id => $dir) {
118  $files_in_dir = [];
119  foreach (ilFileUtils::getDir($dir) as $file_name => $file_data) {
120  if (in_array($file_name, ['.', '..', '.DS_Store'])) {
121  continue;
122  }
123  $files_in_dir[] = $dir . DIRECTORY_SEPARATOR . $file_name;
124  }
125  if (!empty($files_in_dir)) {
126  $files[$object_id] = $files_in_dir;
127  }
128  }
129  return $files;
130  }
131 }
getPreconditions(Environment $environment)
Objectives the migration depend on.
getRemainingAmountOfSteps()
Count up how many "things" need to be migrated.
getDefaultAmountOfStepsPerRun()
Tell the default amount of steps to be executed for one run of the migration.
A migration is a potentially long lasting operation that can be broken into discrete steps...
Definition: Migration.php:28
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Container.php:19
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
static getDir(string $a_dir, bool $a_rec=false, ?string $a_sub_dir="")
get directory
getResource(string $id)
Consumers of this method should check if the result is what they expect, e.g.
static getDataDir()
get data directory (outside webspace)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: class.Agent.php:19
An environment holds resources to be used in the setup process.
Definition: Environment.php:27
step(Environment $environment)
Run one step of the migration.
prepare(Environment $environment)
Prepare the migration by means of some environment.