diff --git a/composer.lock b/composer.lock index 1f24a390f..15b295c13 100644 --- a/composer.lock +++ b/composer.lock @@ -11371,12 +11371,12 @@ "source": { "type": "git", "url": "https://github.com/OS2web/os2web_meetings.git", - "reference": "e673a414342f91c9ab605d323a480fe112464a84" + "reference": "b99ec26bdd90145c4db051cda52e52e09d39f3e9" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/OS2web/os2web_meetings/zipball/e673a414342f91c9ab605d323a480fe112464a84", - "reference": "e673a414342f91c9ab605d323a480fe112464a84", + "url": "https://api.github.com/repos/OS2web/os2web_meetings/zipball/b99ec26bdd90145c4db051cda52e52e09d39f3e9", + "reference": "b99ec26bdd90145c4db051cda52e52e09d39f3e9", "shasum": "" }, "require": { @@ -11394,7 +11394,7 @@ "issues": "https://github.com/OS2web/os2web_meetings/issues", "source": "https://github.com/OS2web/os2web_meetings/tree/develop" }, - "time": "2021-06-08T08:01:50+00:00" + "time": "2021-06-14T08:25:58+00:00" }, { "name": "os2web/os2web_meetings_agman", diff --git a/web/modules/custom/ballerup_d7_migration/scripts/redirects_import.php b/web/modules/custom/ballerup_d7_migration/scripts/redirects_import.php new file mode 100644 index 000000000..9564141cd --- /dev/null +++ b/web/modules/custom/ballerup_d7_migration/scripts/redirects_import.php @@ -0,0 +1,83 @@ +getStorage('redirect'); +$messenger = \Drupal::service('messenger'); + +$added = 0; +$skipped = 0; +/** @var \Drupal\redirect\RedirectRepository $repository */ +$repository = \Drupal::service('redirect.repository'); +$data = getImportData($argv[3], $messenger); +if (empty($data)) { + exit('No input data.'); +} + +foreach ($data as $row) { + $source = $row[0]; + $destination = $row[1]; + if (!UrlHelper::isValid($source)) { + $messenger->addWarning(sprintf('Source URL "%s" is not valid.', $source)); + $skipped++; + continue; + } + if (!UrlHelper::isValid($destination)) { + $messenger->addWarning(sprintf('Destination URL "%s" is not valid.', $destination)); + $skipped++; + continue; + } + $existing = $repository->findBySourcePath(ltrim ($source, '/')); + if (!empty($existing)) { + $messenger->addWarning('Redirect for URL ' . $source . ' already exists.'); + $skipped++; + continue; + } + + /** @var \Drupal\redirect\Entity\Redirect $redirect */ + $redirect = $storage->create(); + $redirect->setSource($source); + $redirect->setRedirect($destination); + $redirect->setStatusCode(301); + $redirect->save(); + $messenger->addMessage('Added redirect for URL ' . $source); + $added++; +} +$messenger->addMessage(sprintf('Redirects adding finished: total: %d, added %d, skipped %d', count($data), $added, $skipped)); + +function getImportData($data_file_path, $messenger) { + $path = $data_file_path; + $messenger->addMessage(sprintf('Getting data from CSV file %s.', $path)); + $file = fopen($path, "r"); + if (!$file) { + return NULL; + } + $data = []; + while (!feof($file)) { + $row = fgetcsv($file, 0, ","); + if (empty($row)) { + continue; + } + if (!is_array($row)) { + $messenger->addWarning(sprintf('Wrong line %s during parsing %s.', $row, $path)); + continue; + } + $row = array_map("utf8_encode", $row); + $data[] = $row; + + } + fclose($file); + $messenger->addMessage(sprintf('Read %d rows from csv file %s.', count($data), $data_file_path)); + return $data; +} +