uri_map_redirect.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. * @file
  4. * Sample file for handling redirection from old to new URIs. Use an Apache
  5. * rewrite rule (or equivalent) to map legacy requests to this file. To use,
  6. * copy or symlink this file to the root of your drupal site. Customize this
  7. * file to your needs.
  8. *
  9. * CREATE TABLE `migrate_source_uri_map` (
  10. * `source_uri` varchar(255) NOT NULL DEFAULT '',
  11. * `migration_name` varchar(255) NOT NULL,
  12. * `source_id` int(11) NOT NULL, -- can be varchar for some migrations
  13. * PRIMARY KEY (`source_uri`)
  14. * )
  15. *
  16. */
  17. // For security, this script is disabled by default.
  18. die('Comment out this line when you are ready to use this script');
  19. // Based on custom patterns, build the destination_uri for given source_uri
  20. function migrate_build_url($destid1, $migration_name) {
  21. global $base_url;
  22. // TODO: Add an entry for each migration that we need to redirect.
  23. $patterns = variable_get('migrate_patterns', array(
  24. 'BeerTerm' => 'taxonomy/term/:source_id',
  25. 'BlogEntries' => 'node/:source_id',
  26. 'Slideshows' => 'node/:source_id',
  27. 'TagTerm' => 'taxonomy/term/:source_id',
  28. ));
  29. $pattern = $patterns[$migration_name];
  30. // Swap in the destination ID.
  31. $destination_uri = str_replace(':source_id', $destid1, $pattern);
  32. // For speed, we go right to aliases table rather than more bootstrapping.
  33. if ($uri_clean = db_query("SELECT alias FROM {url_alias} WHERE source = :destination_uri", array(':destination_uri' => $destination_uri))->fetchField()) {
  34. $destination_uri = $uri_clean;
  35. }
  36. // Build absolute url for 301 redirect.
  37. return $base_url . '/' . $destination_uri;
  38. }
  39. define('DRUPAL_ROOT', getcwd());
  40. require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
  41. // Only bootstrap to DB so we are as fast as possible. Much of the Drupal API
  42. // is not available to us.
  43. drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);
  44. // You must populate this querystring param from a rewrite rule or $_SERVER
  45. // On Apache, we could likely use _SERVER['REDIRECT_URL']. nginx?
  46. if (!$source_uri = $_GET['migrate_source_uri']) {
  47. print '$_GET[migrate_source_uri] was not found on the request.';
  48. exit();
  49. }
  50. // This is a tall table mapping legacy URLs to source_id and migration_name.
  51. // If you can already know the migration name and source_id based on the URI,
  52. // then the first lookup is not needed.
  53. $uri_table = variable_get('migrate_source_uri_table', 'migrate_source_uri_map');
  54. if ($uri_map = db_query("SELECT migration_name, source_id FROM $uri_table WHERE source_uri = :source_uri", array(':source_uri' => $source_uri))->fetchObject()) {
  55. // Hurray, we do recognize this URI.
  56. // Consult migrate_map_x table to determine corresponding Drupal nid/tid/cid/etc.
  57. $map_table = 'migrate_map_' . drupal_strtolower($uri_map->migration_name);
  58. $sql = "SELECT destid1 FROM $map_table WHERE sourceid1 = :source_id";
  59. if ($destid1 = $migrate_map = db_query($sql, array(':source_id' => $uri_map->source_id))->fetchField()) {
  60. // Hurray. We already migrated this content. Go there.
  61. header('Location: ' . migrate_build_url($destid1, $uri_map->migration_name), TRUE, 301);
  62. }
  63. else {
  64. // We recognize URI but don't have the content in Drupal. Very unlikely.
  65. }
  66. }
  67. else {
  68. // Can't find the source URI. TODO: Make nice 404 page.
  69. header('Status=Not Found', TRUE, 404);
  70. print 'Sorry folks. Park is closed.';
  71. }