ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilHTLMMigration.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
24
25class ilHTLMMigration implements Migration
26{
28
29 public function getLabel(): string
30 {
31 return 'Migration of HTML Learning Modules to the Resource Storage Service.';
32 }
33
35 {
36 return 10000;
37 }
38
39 public function getPreconditions(Environment $environment): array
40 {
42 }
43
44 public function prepare(Environment $environment): void
45 {
46 $this->helper = new ilResourceStorageMigrationHelper(
48 $environment
49 );
50 }
51
52 public function step(Environment $environment): void
53 {
54 $r = $this->helper->getDatabase()->query(
55 "SELECT id FROM file_based_lm WHERE rid IS NULL OR rid = '' LIMIT 1;"
56 );
57
58 $d = $this->helper->getDatabase()->fetchObject($r);
59 $object_id = (int) ($d->id ?? null);
60
61 $resource_owner_id = (int) ($d->owner_id ?? 6); // TODO JOIN
62
63 $lm_path = $this->buildBasePath($object_id);
64
65 $rid = $this->helper->moveDirectoryToContainerResource(
66 $lm_path,
67 $resource_owner_id
68 );
69
70 if ($rid !== null) {
71 $this->helper->getDatabase()->update(
72 'file_based_lm',
73 ['rid' => ['text', $rid->serialize()]],
74 ['id' => ['integer', $object_id],]
75 );
76
77 $this->recursiveRmDir($lm_path);
78 } else {
79 $this->helper->getDatabase()->update(
80 'file_based_lm',
81 ['rid' => ['text', '-']],
82 ['id' => ['integer', $object_id],]
83 );
84 }
85 }
86
87 private function recursiveRmDir(string $path): void
88 {
89 // recursively remove directory
90 $files = array_diff(scandir($path), ['.', '..']);
91 foreach ($files as $file) {
92 (is_dir("$path/$file")) ? $this->recursiveRmDir("$path/$file") : unlink("$path/$file");
93 }
94 }
95
96 public function getRemainingAmountOfSteps(): int
97 {
98 $r = $this->helper->getDatabase()->query(
99 "SELECT COUNT(id) AS amount FROM file_based_lm WHERE rid IS NULL OR rid = ''"
100 );
101 $d = $this->helper->getDatabase()->fetchObject($r) ?? new stdClass();
102
103 return (int) ($d->amount ?? 0);
104 }
105
106 protected function buildBasePath(int $object_id): string
107 {
108 return CLIENT_WEB_DIR . '/lm_data/lm_' . $object_id;
109 }
110
111 public function getFileNameCallback(string $pattern): Closure
112 {
113 return static function (string $file_name) use ($pattern): string {
114 if (preg_match($pattern, $file_name, $matches)) {
115 return $matches[1] ?? $file_name;
116 }
117 return $file_name;
118 };
119 }
120
121 public function getRevisionNameCallback(): Closure
122 {
123 return static function (string $file_name): string {
124 return md5($file_name);
125 };
126 }
127}
getRemainingAmountOfSteps()
Count up how many "things" need to be migrated.
getPreconditions(Environment $environment)
Objectives the migration depend on.
step(Environment $environment)
Run one step of the migration.
getFileNameCallback(string $pattern)
buildBasePath(int $object_id)
ilResourceStorageMigrationHelper $helper
prepare(Environment $environment)
Prepare the migration by means of some environment.
recursiveRmDir(string $path)
getDefaultAmountOfStepsPerRun()
Tell the default amount of steps to be executed for one run of the migration.
const CLIENT_WEB_DIR
Definition: constants.php:47
An environment holds resources to be used in the setup process.
Definition: Environment.php:28
A migration is a potentially long lasting operation that can be broken into discrete steps.
Definition: Migration.php:29
$path
Definition: ltiservices.php:30