ILIAS  trunk Revision v11.0_alpha-1769-g99a433fe2dc
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilBadgesFilesMigration.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
25 
27 {
28  private const TABLE_NAME = 'badge_badge';
29 
31  private ?IOWrapper $io = null;
32 
33  public function getLabel(): string
34  {
35  return 'Migration of files of badges 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 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 ' .
84  "file $image_path for id $id to the storage service."
85  );
86  } else {
87  $this->inform("IRSS identification for badge with id $id: {$identification->serialize()}", true);
88  }
89  } catch (Throwable $e) {
90  $this->error("Failed to move badge file {$image_path} for id {$id} to the storage service with exception: {$e->getMessage()}");
91  $this->error($e->getTraceAsString());
92  throw $e;
93  }
94 
95  if ($identification === null) {
96  $identification = '-';
97  } else {
98  $identification = $identification->serialize();
99  }
100 
101  $this->helper->getDatabase()->update(
102  self::TABLE_NAME,
103  [
104  'image_rid' => [ilDBConstants::T_TEXT, $identification],
105  'image' => [ilDBConstants::T_TEXT, null]
106  ],
107  ['id' => [ilDBConstants::T_INTEGER, $id]]
108  );
109  }
110  }
111 
112  private function getImagePath(int $id, string $image): string
113  {
114  $exp = explode('.', $image);
115  $suffix = strtolower(array_pop($exp));
116 
117  return $this->getFilePath($id) . '/img' . $id . '.' . $suffix;
118  }
119 
120  private function getFilePath(int $a_id): string
121  {
122  return ILIAS_ABSOLUTE_PATH . '/' . ILIAS_WEB_DIR . '/' . CLIENT_ID . '/sec/ilBadge/' . $this->createLegacyPathSegmentForBadgeId($a_id);
123  }
124 
125  private function createLegacyPathSegmentForBadgeId(int $id): string
126  {
127  $path = [];
128  $found = false;
129  $num = $id;
130  $path_string = '';
131  for ($i = 3; $i > 0; $i--) {
132  $factor = 100 ** $i;
133  if (($tmp = (int) ($num / $factor)) || $found) {
134  $path[] = $tmp;
135  $num %= $factor;
136  $found = true;
137  }
138  }
139 
140  if (count($path)) {
141  $path_string = (implode('/', $path) . '/');
142  }
143 
144  return $path_string . 'badge_' . $id;
145  }
146 
147  public function getRemainingAmountOfSteps(): int
148  {
149  $res = $this->helper->getDatabase()->query(
150  'SELECT COUNT(id) as amount FROM ' . self::TABLE_NAME . " WHERE image_rid IS NULL OR image_rid = ''"
151  );
152  $row = $this->helper->getDatabase()->fetchObject($res);
153 
154  return (int) ($row->amount ?? 0);
155  }
156 
160  public function getRevisionNameCallback(): Closure
161  {
162  return static function (string $file_name): string {
163  return md5($file_name);
164  };
165  }
166 
167  private function inform(string $text, bool $force = false): void
168  {
169  if ($this->io === null || (!$force && !$this->io->isVerbose())) {
170  return;
171  }
172 
173  $this->io->inform($text);
174  }
175 
176  private function error(string $text): void
177  {
178  if ($this->io === null) {
179  return;
180  }
181 
182  $this->io->error($text);
183  }
184 }
$res
Definition: ltiservices.php:66
Wrapper around symfonies input and output facilities to provide just the functionality required for t...
Definition: IOWrapper.php:32
getRemainingAmountOfSteps()
Count up how many "things" need to be migrated.
A migration is a potentially long lasting operation that can be broken into discrete steps...
Definition: Migration.php:28
if(!file_exists('../ilias.ini.php'))
getDefaultAmountOfStepsPerRun()
Tell the default amount of steps to be executed for one run of the migration.
inform(string $text, bool $force=false)
getImagePath(int $id, string $image)
$path
Definition: ltiservices.php:29
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
step(Environment $environment)
Run one step of the migration.
prepare(Environment $environment)
Prepare the migration by means of some environment.
const CLIENT_ID
Definition: constants.php:41
getResource(string $id)
Consumers of this method should check if the result is what they expect, e.g.
ilResourceStorageMigrationHelper $helper
getPreconditions(Environment $environment)
Objectives the migration depend on.
An environment holds resources to be used in the setup process.
Definition: Environment.php:27
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
const ILIAS_WEB_DIR
Definition: constants.php:45