ILIAS  release_8 Revision v8.19-1-g4e8f2f9140c
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilMDCopyrightMigration.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 use ILIAS\Setup;
24 
25 class ilMDCopyrightMigration implements Setup\Migration
26 {
27  protected ilDBInterface $db;
28 
29  public function getLabel(): string
30  {
31  return "Migration of available copyrights.";
32  }
33 
35  {
36  return Migration::INFINITE;
37  }
38 
39  public function getPreconditions(Environment $environment): array
40  {
41  return [
45  ];
46  }
47 
48  public function prepare(Environment $environment): void
49  {
50  $this->db = $environment->getResource(Environment::RESOURCE_DATABASE);
51  }
52 
53  public function step(Environment $environment): void
54  {
55  $res = $this->db->query(
56  'SELECT entry_id, copyright FROM il_md_cpr_selections WHERE migrated = 0 LIMIT 1'
57  );
58 
59  if ($row = $this->db->fetchAssoc($res)) {
60  if (!isset($row['entry_id'])) {
61  return;
62  }
63  $fields = $this->extractFields((string) ($row['copyright'] ?? ''));
64  $fields['migrated'] = [\ilDBConstants::T_INTEGER, 1];
65  $this->db->update(
66  'il_md_cpr_selections',
67  $fields,
68  ['entry_id' => [\ilDBConstants::T_INTEGER, (int) $row['entry_id']]]
69  );
70  }
71  }
72 
73  public function getRemainingAmountOfSteps(): int
74  {
75  $res = $this->db->query(
76  'SELECT COUNT(*) AS count FROM il_md_cpr_selections WHERE migrated = 0'
77  );
78 
79  $row = $this->db->fetchAssoc($res);
80  return (int) $row['count'];
81  }
82 
83  protected function extractFields(string $copyright): array
84  {
85  $full_name = '';
86  $link = '';
87  $image_link = '';
88  $alt_text = '';
89 
90  //find the image
91  if (preg_match('/<\s*img((?:.|\n)*?)\/>/i', $copyright, $img_matches)) {
92  if (preg_match('/src\s*=\s*(?:"|\')(.*?)(?:"|\')/i', $img_matches[1], $src_matches)) {
93  $image_link = strip_tags($src_matches[1]);
94  }
95  if (preg_match('/alt\s*=\s*(?:"|\')(.*?)(?:"|\')/i', $img_matches[1], $alt_matches)) {
96  $alt_text = strip_tags($alt_matches[1]);
97  }
98  }
99 
100  //find the link
101  if (preg_match('/<\s*a((?:.|\n)[^<]*?)<\s*\/a>/i', $copyright, $link_matches)) {
102  if (preg_match('/href\s*=\s*(?:"|\')(.*?)(?:"|\')/i', $link_matches[1], $name_matches)) {
103  $link = strip_tags($name_matches[1]);
104  }
105  if (preg_match('/>((?:\n|.)*)/i', $link_matches[1], $href_matches)) {
106  $full_name = strip_tags($href_matches[1]);
107  }
108  } else {
109  $full_name = strip_tags($copyright);
110  }
111 
112  $image_link = $this->translatePreInstalledLinksToSVG($image_link);
113 
114  return [
115  'full_name' => [\ilDBConstants::T_TEXT, $full_name],
116  'link' => [\ilDBConstants::T_TEXT, $link],
117  'image_link' => [\ilDBConstants::T_TEXT, $image_link],
118  'alt_text' => [\ilDBConstants::T_TEXT, $alt_text]
119  ];
120  }
121 
122  protected function translatePreInstalledLinksToSVG(string $image_link): string
123  {
124  $mapping = [
125  // 4.0
126  'https://i.creativecommons.org/l/by-nc-nd/4.0/88x31.png' => 'https://mirrors.creativecommons.org/presskit/buttons/88x31/svg/by-nc-nd.svg',
127  'https://i.creativecommons.org/l/by-nc-sa/4.0/88x31.png' => 'https://mirrors.creativecommons.org/presskit/buttons/88x31/svg/by-nc-sa.svg',
128  'https://i.creativecommons.org/l/by-nc/4.0/88x31.png' => 'https://mirrors.creativecommons.org/presskit/buttons/88x31/svg/by-nc.svg',
129  'https://i.creativecommons.org/l/by-nd/4.0/88x31.png' => 'https://mirrors.creativecommons.org/presskit/buttons/88x31/svg/by-nd.svg',
130  'https://i.creativecommons.org/l/by-sa/4.0/88x31.png' => 'https://mirrors.creativecommons.org/presskit/buttons/88x31/svg/by-sa.svg',
131  'https://i.creativecommons.org/l/by/4.0/88x31.png' => 'https://mirrors.creativecommons.org/presskit/buttons/88x31/svg/by.svg',
132  // 3.0
133  'http://i.creativecommons.org/l/by-nc-nd/3.0/88x31.png' => 'https://mirrors.creativecommons.org/presskit/buttons/88x31/svg/by-nc-nd.svg',
134  'http://i.creativecommons.org/l/by-nc-sa/3.0/88x31.png' => 'https://mirrors.creativecommons.org/presskit/buttons/88x31/svg/by-nc-sa.svg',
135  'http://i.creativecommons.org/l/by-nc/3.0/88x31.png' => 'https://mirrors.creativecommons.org/presskit/buttons/88x31/svg/by-nc.svg',
136  'http://i.creativecommons.org/l/by-nd/3.0/88x31.png' => 'https://mirrors.creativecommons.org/presskit/buttons/88x31/svg/by-nd.svg',
137  'http://i.creativecommons.org/l/by-sa/3.0/88x31.png' => 'https://mirrors.creativecommons.org/presskit/buttons/88x31/svg/by-sa.svg',
138  'http://i.creativecommons.org/l/by/3.0/88x31.png' => 'https://mirrors.creativecommons.org/presskit/buttons/88x31/svg/by.svg'
139  ];
140 
141  if (key_exists($image_link, $mapping)) {
142  return $mapping[$image_link];
143  }
144  return $image_link;
145  }
146 }
$res
Definition: ltiservices.php:69
getPreconditions(Environment $environment)
step(Environment $environment)
getResource(string $id)
Consumers of this method should check if the result is what they expect, e.g.
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:27
prepare(Environment $environment)
translatePreInstalledLinksToSVG(string $image_link)