ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilBadgeTemplatesFilesMigration.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
25
27{
28 private const TABLE_NAME = 'badge_image_template';
29
31 private ?IOWrapper $io = null;
32
33 public function getLabel(): string
34 {
35 return 'Migration of files of badge templates to the resource storage service.';
36 }
37
39 {
40 return 1000;
41 }
42
43 public function getPreconditions(Environment $environment): array
44 {
46 }
47
48 public function prepare(Environment $environment): void
49 {
50 $this->helper = new ilResourceStorageMigrationHelper(
52 $environment
53 );
54 $io = $environment->getResource(Environment::RESOURCE_ADMIN_INTERACTION);
55 if ($io instanceof IOWrapper) {
56 $this->io = $io;
57 }
58 }
59
60 public function step(Environment $environment): void
61 {
62 $this->helper->getDatabase()->setLimit(1);
63 $res = $this->helper->getDatabase()->query(
64 'SELECT id, image, image_rid FROM ' . self::TABLE_NAME . " WHERE image_rid IS NULL OR image_rid = ''"
65 );
66 $row = $this->helper->getDatabase()->fetchObject($res);
67 if (!($row instanceof stdClass)) {
68 return;
69 }
70
71 $id = (int) $row->id;
72 $image = $row->image;
73
74 if ($image !== '' && $image !== null) {
75 $image_path = $this->getImagePath($id, $image);
76
77 try {
78 $this->inform("Trying to move badge image template file $image_path for id $id to the storage service.");
79 $identification = $this->helper->movePathToStorage($image_path, ResourceCollection::NO_SPECIFIC_OWNER);
80 $this->inform('Migration proceeded without error.');
81 if ($identification === null) {
82 $this->error(
83 'IRSS returned NULL as identification when trying to move badge image template ' .
84 "file $image_path for id $id to the storage service."
85 );
86 } else {
87 $this->inform(
88 "IRSS identification for badge image template with id $id: {$identification->serialize()}",
89 true
90 );
91 }
92 } catch (Throwable $e) {
93 $this->error("Failed to move badge image template file $image_path for id $id to the storage service with exception: {$e->getMessage()}");
94 $this->error($e->getTraceAsString());
95 throw $e;
96 }
97
98 if ($identification === null) {
99 $identification = '-';
100 } else {
101 $identification = $identification->serialize();
102 }
103
104 $this->helper->getDatabase()->update(
105 self::TABLE_NAME,
106 [
107 'image_rid' => [ilDBConstants::T_TEXT, $identification],
108 'image' => [ilDBConstants::T_TEXT, null]
109 ],
110 ['id' => [ilDBConstants::T_INTEGER, $id]]
111 );
112 } else {
113 $this->inform(
114 'Cannot migrate badge image template with id ' . $id
115 . ' (table: ' . self::TABLE_NAME . ') because no image is set.',
116 true
117 );
118 }
119 }
120
121 private function getImagePath(int $id, string $image): string
122 {
123 $exp = explode('.', $image);
124 $suffix = strtolower(array_pop($exp));
125
126 return $this->getFilePath($id) . '/img' . $id . '.' . $suffix;
127 }
128
129 private function getFilePath(int $a_id): string
130 {
131 return ILIAS_ABSOLUTE_PATH . '/' . ILIAS_WEB_DIR . '/' . CLIENT_ID . '/sec/ilBadge/' . $this->createLegacyPathSegmentForBadgeTemplateId($a_id);
132 }
133
134 private function createLegacyPathSegmentForBadgeTemplateId(int $id): string
135 {
136 $path = [];
137 $found = false;
138 $num = $id;
139 $path_string = '';
140 for ($i = 3; $i > 0; $i--) {
141 $factor = 100 ** $i;
142 if (($tmp = (int) ($num / $factor)) || $found) {
143 $path[] = $tmp;
144 $num %= $factor;
145 $found = true;
146 }
147 }
148
149 if (count($path)) {
150 $path_string = (implode('/', $path) . '/');
151 }
152
153 return $path_string . 'badgetmpl_' . $id;
154 }
155
157 {
158 $res = $this->helper->getDatabase()->query(
159 'SELECT COUNT(id) as amount FROM ' . self::TABLE_NAME . " WHERE image_rid IS NULL OR image_rid = ''"
160 );
161 $row = $this->helper->getDatabase()->fetchObject($res);
162
163 return (int) ($row->amount ?? 0);
164 }
165
166 private function inform(string $text, bool $force = false): void
167 {
168 if ($this->io === null || (!$force && !$this->io->isVerbose())) {
169 return;
170 }
171
172 $this->io->inform($text);
173 }
174
175 private function error(string $text): void
176 {
177 if ($this->io === null) {
178 return;
179 }
180
181 $this->io->error($text);
182 }
183}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Wrapper around symfonies input and output facilities to provide just the functionality required for t...
Definition: IOWrapper.php:33
prepare(Environment $environment)
Prepare the migration by means of some environment.
getPreconditions(Environment $environment)
Objectives the migration depend on.
getRemainingAmountOfSteps()
Count up how many "things" need to be migrated.
step(Environment $environment)
Run one step of the migration.
getDefaultAmountOfStepsPerRun()
Tell the default amount of steps to be executed for one run of the migration.
const CLIENT_ID
Definition: constants.php:41
const ILIAS_WEB_DIR
Definition: constants.php:45
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
$path
Definition: ltiservices.php:30
$res
Definition: ltiservices.php:69
if(!file_exists('../ilias.ini.php'))