|
| 1 | +<?php |
| 2 | + |
| 3 | +use Drupal\Component\Utility\UrlHelper; |
| 4 | + |
| 5 | +$argv = $_SERVER['argv']; |
| 6 | +if (count($argv) < 4) { |
| 7 | + exit("This script supposed to be run ONLY via drush with full path to CSV file as first argument. |
| 8 | +Example: drush scr script.php /path/to/redirects.csv |
| 9 | +CSV file format: |
| 10 | +\"/source/redirect/path\", \"http://external.url/destination/redirect/path\" |
| 11 | +\"/source/path\", \"internal:/destination/redirect/path\" |
| 12 | + "); |
| 13 | +} |
| 14 | + |
| 15 | +$storage = \Drupal::entityTypeManager()->getStorage('redirect'); |
| 16 | +$messenger = \Drupal::service('messenger'); |
| 17 | + |
| 18 | +$added = 0; |
| 19 | +$skipped = 0; |
| 20 | +/** @var \Drupal\redirect\RedirectRepository $repository */ |
| 21 | +$repository = \Drupal::service('redirect.repository'); |
| 22 | +$data = getImportData($argv[3], $messenger); |
| 23 | +if (empty($data)) { |
| 24 | + exit('No input data.'); |
| 25 | +} |
| 26 | + |
| 27 | +foreach ($data as $row) { |
| 28 | + $source = $row[0]; |
| 29 | + $destination = $row[1]; |
| 30 | + if (!UrlHelper::isValid($source)) { |
| 31 | + $messenger->addWarning(sprintf('Source URL "%s" is not valid.', $source)); |
| 32 | + $skipped++; |
| 33 | + continue; |
| 34 | + } |
| 35 | + if (!UrlHelper::isValid($destination)) { |
| 36 | + $messenger->addWarning(sprintf('Destination URL "%s" is not valid.', $destination)); |
| 37 | + $skipped++; |
| 38 | + continue; |
| 39 | + } |
| 40 | + $existing = $repository->findBySourcePath(ltrim ($source, '/')); |
| 41 | + if (!empty($existing)) { |
| 42 | + $messenger->addWarning('Redirect for URL ' . $source . ' already exists.'); |
| 43 | + $skipped++; |
| 44 | + continue; |
| 45 | + } |
| 46 | + |
| 47 | + /** @var \Drupal\redirect\Entity\Redirect $redirect */ |
| 48 | + $redirect = $storage->create(); |
| 49 | + $redirect->setSource($source); |
| 50 | + $redirect->setRedirect($destination); |
| 51 | + $redirect->setStatusCode(301); |
| 52 | + $redirect->save(); |
| 53 | + $messenger->addMessage('Added redirect for URL ' . $source); |
| 54 | + $added++; |
| 55 | +} |
| 56 | +$messenger->addMessage(sprintf('Redirects adding finished: total: %d, added %d, skipped %d', count($data), $added, $skipped)); |
| 57 | + |
| 58 | +function getImportData($data_file_path, $messenger) { |
| 59 | + $path = $data_file_path; |
| 60 | + $messenger->addMessage(sprintf('Getting data from CSV file %s.', $path)); |
| 61 | + $file = fopen($path, "r"); |
| 62 | + if (!$file) { |
| 63 | + return NULL; |
| 64 | + } |
| 65 | + $data = []; |
| 66 | + while (!feof($file)) { |
| 67 | + $row = fgetcsv($file, 0, ","); |
| 68 | + if (empty($row)) { |
| 69 | + continue; |
| 70 | + } |
| 71 | + if (!is_array($row)) { |
| 72 | + $messenger->addWarning(sprintf('Wrong line %s during parsing %s.', $row, $path)); |
| 73 | + continue; |
| 74 | + } |
| 75 | + $row = array_map("utf8_encode", $row); |
| 76 | + $data[] = $row; |
| 77 | + |
| 78 | + } |
| 79 | + fclose($file); |
| 80 | + $messenger->addMessage(sprintf('Read %d rows from csv file %s.', count($data), $data_file_path)); |
| 81 | + return $data; |
| 82 | +} |
| 83 | + |
0 commit comments