ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
RebuildMissingThumbnailMigration.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
26 
28 {
29  private \ilDBInterface $db;
31  private string $webdir;
32 
33  public function getLabel(): string
34  {
35  return 'Rebuild Missing Thumbnail Images';
36  }
37 
39  {
40  return 100;
41  }
42 
43  public function getPreconditions(Environment $environment): array
44  {
45  return [
46  new \ilDatabaseInitializedObjective()
47  ];
48  }
49 
50  public function prepare(Environment $environment): void
51  {
52  $this->db = $environment->getResource(Environment::RESOURCE_DATABASE);
53  $this->io = $environment->getResource(Environment::RESOURCE_ADMIN_INTERACTION);
54 
56  $this->webdir = "{$ini->readVariable('server', 'absolute_path')}/{$ini->readVariable('clients', 'path')}/{$ini->readVariable('clients', 'default')}";
57  }
58 
59  public function step(Environment $environment): void
60  {
61  $result = $this->db->query(
62  '(SELECT sa.imagefile, q.obj_fi, q.question_type_fi, q.question_id' . PHP_EOL
63  . 'FROM qpl_questions q JOIN qpl_qst_sc s on q.question_id = s.question_fi JOIN qpl_a_sc sa on q.question_id = sa.question_fi' . PHP_EOL
64  . 'WHERE q.question_type_fi = 1 AND s.thumb_size IS NULL AND NOT q.obj_fi = 0 and not sa.imagefile = "" AND NOT ISNULL(sa.imagefile) ORDER BY q.question_id)' . PHP_EOL
65  . 'UNION' . PHP_EOL
66  . '(SELECT ma.imagefile, q.obj_fi, q.question_type_fi, q.question_id ' . PHP_EOL
67  . 'FROM qpl_questions q JOIN qpl_qst_mc m on q.question_id = m.question_fi JOIN qpl_a_mc ma on q.question_id = ma.question_fi' . PHP_EOL
68  . 'WHERE q.question_type_fi = 2 AND m.thumb_size IS NULL AND NOT q.obj_fi = 0 AND NOT ma.imagefile = "" AND NOT ISNULL(ma.imagefile) ORDER BY q.question_id)' . PHP_EOL
69  . 'UNION' . PHP_EOL
70  . '(SELECT ka.imagefile, q.obj_fi, q.question_type_fi, q.question_id' . PHP_EOL
71  . 'FROM qpl_questions q JOIN qpl_qst_kprim k on q.question_id = k.question_fi JOIN qpl_a_kprim ka on q.question_id = ka.question_fi' . PHP_EOL
72  . 'WHERE q.question_type_fi = 17 AND k.thumb_size IS NULL AND NOT q.obj_fi = 0 AND NOT ka.imagefile = "" AND NOT ISNULL(ka.imagefile) ORDER BY q.question_id)'
73  );
74 
75  $previous_question_id = null;
76  while (($row = $this->db->fetchObject($result)) !== null) {
77  if ($previous_question_id !== null && $previous_question_id !== $row->question_id) {
78  $this->updateThumbSize($row->question_id, $row->question_type_fi, $image_width);
79  }
80  $image_width = $this->copyImageToThumb($row->obj_fi, $row->question_id, $row->imagefile);
81  $previous_question_id = $row->question_id;
82  $previous_question_type_id = $row->question_type_fi;
83  }
84 
85  if (isset($previous_question_type_id)) {
86  $this->updateThumbSize($previous_question_id, $previous_question_type_id, $image_width);
87  }
88  }
89 
90  public function getRemainingAmountOfSteps(): int
91  {
92  $result = $this->db->query(
93  'SELECT COUNT(*) cnt FROM' . PHP_EOL
94  . '(SELECT q.question_id' . PHP_EOL
95  . 'FROM qpl_questions q JOIN qpl_qst_sc s on q.question_id = s.question_fi JOIN qpl_a_sc sa on q.question_id = sa.question_fi' . PHP_EOL
96  . 'WHERE q.question_type_fi = 1 AND s.thumb_size IS NULL AND NOT q.obj_fi = 0 and not sa.imagefile = "" AND NOT ISNULL(sa.imagefile)' . PHP_EOL
97  . 'UNION' . PHP_EOL
98  . 'SELECT q.question_id' . PHP_EOL
99  . 'FROM qpl_questions q JOIN qpl_qst_mc m on q.question_id = m.question_fi JOIN qpl_a_mc ma on q.question_id = ma.question_fi' . PHP_EOL
100  . 'WHERE q.question_type_fi = 2 AND m.thumb_size IS NULL AND NOT q.obj_fi = 0 AND NOT ma.imagefile = "" AND NOT ISNULL(ma.imagefile)' . PHP_EOL
101  . 'UNION' . PHP_EOL
102  . 'SELECT q.question_id' . PHP_EOL
103  . 'FROM qpl_questions q JOIN qpl_qst_kprim k on q.question_id = k.question_fi JOIN qpl_a_kprim ka on q.question_id = ka.question_fi' . PHP_EOL
104  . 'WHERE q.question_type_fi = 17 AND k.thumb_size IS NULL AND NOT q.obj_fi = 0 AND NOT ka.imagefile = "" AND NOT ISNULL(ka.imagefile)) cnt'
105  );
106  $row = $this->db->fetchAssoc($result);
107 
108  return (int) ($row['cnt'] ?? 0);
109  }
110 
111  private function copyImageToThumb(
112  int $object_id,
113  int $question_id,
114  string $image_file_name
115  ): ?int {
116  $filepath = "{$this->webdir}/assessment/{$object_id}/{$question_id}/images/{$image_file_name}";
117  $thumbpath = "{$this->webdir}/assessment/{$object_id}/{$question_id}/images/thumb.{$image_file_name}";
118  if (!file_exists($filepath)
119  || filesize($filepath) <= 0
120  || !($image_info = getimagesize($filepath))
121  || !is_writable(dirname($thumbpath))) {
122  $this->io->inform("\nWARNING: Could not create thumbnail for image {$filepath} of question {$question_id}");
123  return 0;
124  }
125 
126  copy($filepath, $thumbpath);
127  return $image_info[0];
128  }
129 
130  private function updateThumbSize(
131  int $question_id,
132  int $question_type_id,
133  int $image_width
134  ): void {
135  $this->db->replace(
136  match ($question_type_id) {
137  1 => 'qpl_qst_sc',
138  2 => 'qpl_qst_mc',
139  17 => 'qpl_qst_kprim'
140  },
141  [
142  'thumb_size' => ['integer', $image_width],
143  ],
144  [
145  'question_fi' => ['integer', $question_id]
146  ]
147  );
148  }
149 }
getRemainingAmountOfSteps()
Count up how many "things" need to be migrated.
step(Environment $environment)
Run one step of the migration.
A migration is a potentially long lasting operation that can be broken into discrete steps...
Definition: Migration.php:28
copyImageToThumb(int $object_id, int $question_id, string $image_file_name)
getDefaultAmountOfStepsPerRun()
Tell the default amount of steps to be executed for one run of the migration.
This defines ways in which objectives may interact with admins during the setup.
getResource(string $id)
Consumers of this method should check if the result is what they expect, e.g.
An environment holds resources to be used in the setup process.
Definition: Environment.php:27
getPreconditions(Environment $environment)
Objectives the migration depend on.
updateThumbSize(int $question_id, int $question_type_id, int $image_width)
prepare(Environment $environment)
Prepare the migration by means of some environment.