ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilPollImagesMigration.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
23 use ILIAS\Poll\Image\Repository\Stakeholder\Handler as ilPollImageRepositoryStakeholder;
24 
26 {
27  protected ilDBInterface $db;
28 
29  public function getLabel(): string
30  {
31  return "PollImagesMigration";
32  }
33 
35  {
36  return 5;
37  }
38 
39  public function getPreconditions(
40  Environment $environment
41  ): array {
42  return [
46  ];
47  }
48 
49  public function prepare(
50  Environment $environment
51  ): void {
52  $this->db = $environment->getResource(Environment::RESOURCE_DATABASE);
53  }
54 
55  public function step(
56  Environment $environment
57  ): void {
58  $res = $this->db->query("SELECT id, image FROM il_poll WHERE migrated = 0 LIMIT 1");
59  $row = $res->fetchAssoc();
60  $image = $row["image"] ?? "";
61  $id = (int) $row["id"];
62 
63  if ($image === "") {
64  $this->db->manipulate("UPDATE il_poll SET migrated = 1 WHERE id = " . $this->db->quote($id, ilDBConstants::T_INTEGER));
65  return;
66  }
67 
68  $file_path = $this->getImageFullPath($image, $id);
69  $thumbnail_path = $this->getThumbnailImagePath($image, $id);
70  $org_path = $this->getOrgImagePath($image, $id);
71  $stakeholder = (new ilPollImageRepositoryStakeholder())->withUserId(6);
72  $irss_helper = new ilResourceStorageMigrationHelper($stakeholder, $environment);
73  $rid = $irss_helper->movePathToStorage($file_path, 6, null, null, false);
74  $rid_thumbnail = $irss_helper->movePathToStorage($thumbnail_path, 6, null, null, false);
75  $org_thumbnail = $irss_helper->movePathToStorage($org_path, 6, null, null, false);
76 
77  $res_existing = $this->db->query("SELECT * FROM il_poll_image WHERE object_id = " . $this->db->quote($id, ilDBConstants::T_INTEGER));
78  $row_existing = $res_existing->fetchAssoc();
79  if (is_null($row_existing)) {
80  $this->db->manipulate(
81  "INSERT INTO il_poll_image (object_id, rid) VALUES "
82  . " (" . $this->db->quote($id, ilDBConstants::T_INTEGER)
83  . ", " . $this->db->quote($rid->serialize(), ilDBConstants::T_TEXT) . ")"
84  );
85  }
86 
87  if (!is_null($row_existing)) {
88  $irss_helper->getResourceBuilder()->remove($irss_helper->getResourceBuilder()->get($rid), $stakeholder);
89  }
90  $irss_helper->getResourceBuilder()->remove($irss_helper->getResourceBuilder()->get($rid_thumbnail), $stakeholder);
91  $irss_helper->getResourceBuilder()->remove($irss_helper->getResourceBuilder()->get($org_thumbnail), $stakeholder);
92 
93  $this->db->manipulate("UPDATE il_poll SET migrated = 1 WHERE id = " . $this->db->quote($id, ilDBConstants::T_INTEGER));
94  }
95 
96  public function getRemainingAmountOfSteps(): int
97  {
98  $res = $this->db->query(
99  'SELECT COUNT(*) AS count FROM il_poll WHERE migrated = 0'
100  );
101  $row = $this->db->fetchAssoc($res);
102  return (int) $row['count'];
103  }
104 
105  public function getImageFullPath(string $img, int $id): ?string
106  {
107  return $this->getLegacyPath($id) . '/' . $img;
108  }
109 
110  protected function getThumbnailImagePath(string $img, int $id): string
111  {
112  return $this->getLegacyPath($id) . "/thb_" . $img;
113  }
114 
115  protected function getOrgImagePath(string $img, int $id): string
116  {
117  return $this->getLegacyPath($id) . "/org_" . $img;
118  }
119 
120  protected function getLegacyPath(int $a_id): string
121  {
122  $path = 'sec/ilPoll/' . ilFileSystemAbstractionStorage::createPathFromId($a_id, 'poll');
123  return rtrim(CLIENT_WEB_DIR, '/') . '/' . rtrim($path, '/');
124  }
125 }
$res
Definition: ltiservices.php:66
A migration is a potentially long lasting operation that can be broken into discrete steps...
Definition: Migration.php:28
getThumbnailImagePath(string $img, int $id)
getRemainingAmountOfSteps()
Count up how many "things" need to be migrated.
step(Environment $environment)
Run one step of the migration.
$path
Definition: ltiservices.php:29
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
static createPathFromId(int $a_container_id, string $a_name)
getOrgImagePath(string $img, int $id)
const CLIENT_WEB_DIR
Definition: constants.php:47
getResource(string $id)
Consumers of this method should check if the result is what they expect, e.g.
getDefaultAmountOfStepsPerRun()
Tell the default amount of steps to be executed for one run of the migration.
getPreconditions(Environment $environment)
Objectives the migration depend on.
getImageFullPath(string $img, int $id)
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
prepare(Environment $environment)
Prepare the migration by means of some environment.