ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
class.ilPollImagesMigration.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
23use ILIAS\Poll\Image\Repository\Stakeholder\Handler as ilPollImageRepositoryStakeholder;
25
27{
28 protected ilDBInterface $db;
29 protected ?IOWrapper $io = null;
30
31 public function getLabel(): string
32 {
33 return "PollImagesMigration";
34 }
35
37 {
38 return 5;
39 }
40
41 public function getPreconditions(
42 Environment $environment
43 ): array {
44 return [
48 ];
49 }
50
51 public function prepare(
52 Environment $environment
53 ): void {
54 $this->db = $environment->getResource(Environment::RESOURCE_DATABASE);
55 $io = $environment->getResource(Environment::RESOURCE_ADMIN_INTERACTION);
56 if ($io instanceof IOWrapper) {
57 $this->io = $io;
58 }
59 }
60
61 public function step(
62 Environment $environment
63 ): void {
64 $res = $this->db->query("SELECT id, image FROM il_poll WHERE migrated = 0 LIMIT 1");
65 $row = $res->fetchAssoc();
66 $image = $row["image"] ?? "";
67 $id = (int) $row["id"];
68
69 if ($image === "") {
70 $this->db->manipulate("UPDATE il_poll SET migrated = 1 WHERE id = " . $this->db->quote($id, ilDBConstants::T_INTEGER));
71 return;
72 }
73
74 $file_path = $this->getImageFullPath($image, $id);
75 $thumbnail_path = $this->getThumbnailImagePath($image, $id);
76 $org_path = $this->getOrgImagePath($image, $id);
77 $stakeholder = (new ilPollImageRepositoryStakeholder())->withUserId(6);
78
79 $irss_helper = new ilResourceStorageMigrationHelper($stakeholder, $environment);
80 $rid = $irss_helper->movePathToStorage($file_path, 6, null, null, false);
81 $rid_thumbnail = $irss_helper->movePathToStorage($thumbnail_path, 6, null, null, false);
82 $org_thumbnail = $irss_helper->movePathToStorage($org_path, 6, null, null, false);
83
84 $res_existing = $this->db->query("SELECT * FROM il_poll_image WHERE object_id = " . $this->db->quote($id, ilDBConstants::T_INTEGER));
85 $row_existing = $res_existing->fetchAssoc();
86 if (!is_null($rid)) {
87 if (is_null($row_existing)) {
88 $this->db->manipulate(
89 "INSERT INTO il_poll_image (object_id, rid) VALUES "
90 . " (" . $this->db->quote($id, ilDBConstants::T_INTEGER)
91 . ", " . $this->db->quote($rid->serialize(), ilDBConstants::T_TEXT) . ")"
92 );
93 } else {
94 $irss_helper->getResourceBuilder()->remove($irss_helper->getResourceBuilder()->get($rid), $stakeholder);
95 }
96 } else {
97 $this->logError('Image ' . $file_path . ' of poll with object ID ' . $id . ' could not be moved to storage.');
98 }
99
100 if (!is_null($rid_thumbnail)) {
101 $irss_helper->getResourceBuilder()->remove($irss_helper->getResourceBuilder()->get($rid_thumbnail), $stakeholder);
102 }
103 if (!is_null($org_thumbnail)) {
104 $irss_helper->getResourceBuilder()->remove($irss_helper->getResourceBuilder()->get($org_thumbnail), $stakeholder);
105 }
106
107 $this->db->manipulate("UPDATE il_poll SET migrated = 1 WHERE id = " . $this->db->quote($id, ilDBConstants::T_INTEGER));
108 }
109
111 {
112 $res = $this->db->query(
113 'SELECT COUNT(*) AS count FROM il_poll WHERE migrated = 0'
114 );
115 $row = $this->db->fetchAssoc($res);
116 return (int) $row['count'];
117 }
118
119 public function getImageFullPath(string $img, int $id): ?string
120 {
121 return $this->getLegacyPath($id) . '/' . $img;
122 }
123
124 protected function getThumbnailImagePath(string $img, int $id): string
125 {
126 return $this->getLegacyPath($id) . "/thb_" . $img;
127 }
128
129 protected function getOrgImagePath(string $img, int $id): string
130 {
131 return $this->getLegacyPath($id) . "/org_" . $img;
132 }
133
134 protected function getLegacyPath(int $a_id): string
135 {
136 $path = 'sec/ilPoll/' . ilFileSystemAbstractionStorage::createPathFromId($a_id, 'poll');
137 return rtrim(CLIENT_WEB_DIR, '/') . '/' . rtrim($path, '/');
138 }
139
140 protected function logError(string $text): void
141 {
142 if ($this->io === null) {
143 return;
144 }
145
146 $this->io->error($text);
147 }
148}
$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
static createPathFromId(int $a_container_id, string $a_name)
getImageFullPath(string $img, int $id)
getRemainingAmountOfSteps()
Count up how many "things" need to be migrated.
step(Environment $environment)
Run one step of the migration.
getPreconditions(Environment $environment)
Objectives the migration depend on.
getDefaultAmountOfStepsPerRun()
Tell the default amount of steps to be executed for one run of the migration.
getThumbnailImagePath(string $img, int $id)
getOrgImagePath(string $img, int $id)
prepare(Environment $environment)
Prepare the migration by means of some environment.
const CLIENT_WEB_DIR
Definition: constants.php:47
return['delivery_method'=> 'php',]
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: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
Interface ilDBInterface.
$path
Definition: ltiservices.php:30
$res
Definition: ltiservices.php:69
$text
Definition: xapiexit.php:21