ILIAS  trunk Revision v11.0_alpha-1761-g6dbbfa7b760
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator 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, title, 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(
64  (string) ($row['title'] ?? ''),
65  (string) ($row['copyright'] ?? '')
66  );
67  $fields['migrated'] = [\ilDBConstants::T_INTEGER, 1];
68  $this->db->update(
69  'il_md_cpr_selections',
70  $fields,
71  ['entry_id' => [\ilDBConstants::T_INTEGER, (int) $row['entry_id']]]
72  );
73  }
74  }
75 
76  public function getRemainingAmountOfSteps(): int
77  {
78  $res = $this->db->query(
79  'SELECT COUNT(*) AS count FROM il_md_cpr_selections WHERE migrated = 0'
80  );
81 
82  $row = $this->db->fetchAssoc($res);
83  return (int) $row['count'];
84  }
85 
86  protected function extractFields(
87  string $title,
88  string $copyright
89  ): array {
90  $full_name = '';
91  $link = '';
92  $image_link = '';
93  $alt_text = '';
94 
95  //find the image
96  if (preg_match('/<\s*img((?:.|\n)*?)\/>/i', $copyright, $img_matches)) {
97  if (preg_match('/src\s*=\s*(?:"|\')(.*?)(?:"|\')/i', $img_matches[1], $src_matches)) {
98  $image_link = strip_tags($src_matches[1]);
99  }
100  if (preg_match('/alt\s*=\s*(?:"|\')(.*?)(?:"|\')/i', $img_matches[1], $alt_matches)) {
101  $alt_text = strip_tags($alt_matches[1]);
102  }
103  }
104 
105  //find the link
106  if (preg_match('/<\s*a((?:.|\n)[^<]*?)<\s*\/a>/i', $copyright, $link_matches)) {
107  if (preg_match('/href\s*=\s*(?:"|\')(.*?)(?:"|\')/i', $link_matches[1], $name_matches)) {
108  $link = strip_tags($name_matches[1]);
109  }
110  if (preg_match('/>((?:\n|.)*)/i', $link_matches[1], $href_matches)) {
111  $full_name = strip_tags($href_matches[1]);
112  }
113  } else {
114  $full_name = strip_tags($copyright);
115  }
116 
117  $image_link = $this->translatePreInstalledLinksToSVG($image_link);
118  $full_name = $this->cleanupPreInstalledPublicDomainFullName($full_name);
119  $title = $this->updatePreInstalledTitles($link, $title);
120 
121  return [
122  'title' => [\ilDBConstants::T_TEXT, $title],
123  'full_name' => [\ilDBConstants::T_TEXT, $full_name],
124  'link' => [\ilDBConstants::T_TEXT, $link],
125  'image_link' => [\ilDBConstants::T_TEXT, $image_link],
126  'alt_text' => [\ilDBConstants::T_TEXT, $alt_text]
127  ];
128  }
129 
130  protected function translatePreInstalledLinksToSVG(string $image_link): string
131  {
132  $mapping = [
133  // 4.0
134  'https://i.creativecommons.org/l/by-nc-nd/4.0/88x31.png' => 'https://mirrors.creativecommons.org/presskit/buttons/88x31/svg/by-nc-nd.svg',
135  'https://i.creativecommons.org/l/by-nc-sa/4.0/88x31.png' => 'https://mirrors.creativecommons.org/presskit/buttons/88x31/svg/by-nc-sa.svg',
136  'https://i.creativecommons.org/l/by-nc/4.0/88x31.png' => 'https://mirrors.creativecommons.org/presskit/buttons/88x31/svg/by-nc.svg',
137  'https://i.creativecommons.org/l/by-nd/4.0/88x31.png' => 'https://mirrors.creativecommons.org/presskit/buttons/88x31/svg/by-nd.svg',
138  'https://i.creativecommons.org/l/by-sa/4.0/88x31.png' => 'https://mirrors.creativecommons.org/presskit/buttons/88x31/svg/by-sa.svg',
139  'https://i.creativecommons.org/l/by/4.0/88x31.png' => 'https://mirrors.creativecommons.org/presskit/buttons/88x31/svg/by.svg',
140  // 3.0
141  'http://i.creativecommons.org/l/by-nc-nd/3.0/88x31.png' => 'https://mirrors.creativecommons.org/presskit/buttons/88x31/svg/by-nc-nd.svg',
142  'http://i.creativecommons.org/l/by-nc-sa/3.0/88x31.png' => 'https://mirrors.creativecommons.org/presskit/buttons/88x31/svg/by-nc-sa.svg',
143  'http://i.creativecommons.org/l/by-nc/3.0/88x31.png' => 'https://mirrors.creativecommons.org/presskit/buttons/88x31/svg/by-nc.svg',
144  'http://i.creativecommons.org/l/by-nd/3.0/88x31.png' => 'https://mirrors.creativecommons.org/presskit/buttons/88x31/svg/by-nd.svg',
145  'http://i.creativecommons.org/l/by-sa/3.0/88x31.png' => 'https://mirrors.creativecommons.org/presskit/buttons/88x31/svg/by-sa.svg',
146  'http://i.creativecommons.org/l/by/3.0/88x31.png' => 'https://mirrors.creativecommons.org/presskit/buttons/88x31/svg/by.svg'
147  ];
148 
149  if (key_exists($image_link, $mapping)) {
150  return $mapping[$image_link];
151  }
152  return $image_link;
153  }
154 
158  protected function updatePreInstalledTitles(string $link, string $title): string
159  {
160  $old_titles_by_link = [
161  'http://creativecommons.org/licenses/by-nc-nd/4.0/' => 'Attribution Non-commercial No Derivatives (by-nc-nd)',
162  'http://creativecommons.org/licenses/by-nc-sa/4.0/' => 'Attribution Non-commercial Share Alike (by-nc-sa)',
163  'http://creativecommons.org/licenses/by-nc/4.0/' => 'Attribution Non-commercial (by-nc)',
164  'http://creativecommons.org/licenses/by-nd/4.0/' => 'Attribution No Derivatives (by-nd)',
165  'http://creativecommons.org/licenses/by-sa/4.0/' => 'Attribution Share Alike (by-sa)',
166  'http://creativecommons.org/licenses/by/4.0/' => 'Attribution (by)'
167  ];
168  $new_titles_by_link = [
169  'http://creativecommons.org/licenses/by-nc-nd/4.0/' => 'Attribution Non-commercial No Derivatives (BY-NC-ND) 4.0',
170  'http://creativecommons.org/licenses/by-nc-sa/4.0/' => 'Attribution Non-commercial Share Alike (BY-NC-SA) 4.0',
171  'http://creativecommons.org/licenses/by-nc/4.0/' => 'Attribution Non-commercial (BY-NC) 4.0',
172  'http://creativecommons.org/licenses/by-nd/4.0/' => 'Attribution No Derivatives (BY-ND) 4.0',
173  'http://creativecommons.org/licenses/by-sa/4.0/' => 'Attribution Share Alike (BY-SA) 4.0',
174  'http://creativecommons.org/licenses/by/4.0/' => 'Attribution (BY) 4.0'
175  ];
176  if (
177  key_exists($link, $old_titles_by_link) &&
178  $old_titles_by_link[$link] === $title &&
179  key_exists($link, $new_titles_by_link)
180  ) {
181  return $new_titles_by_link[$link];
182  }
183  return $title;
184  }
185 
189  protected function cleanupPreInstalledPublicDomainFullName(string $full_name): string
190  {
191  if ($full_name === 'This work has all rights reserved by the owner.') {
192  return 'All rights reserved';
193  }
194  return $full_name;
195  }
196 }
updatePreInstalledTitles(string $link, string $title)
see https://mantis.ilias.de/view.php?id=41301
$res
Definition: ltiservices.php:66
cleanupPreInstalledPublicDomainFullName(string $full_name)
see https://mantis.ilias.de/view.php?id=41301
getPreconditions(Environment $environment)
extractFields(string $title, string $copyright)
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)