From 38a01a27be3937d2e3d17ea8c0cb9edbb5355aca Mon Sep 17 00:00:00 2001 From: marcusx Date: Thu, 1 Sep 2011 19:23:11 +0200 Subject: [PATCH 1/3] From cf4938c0c36b0bb27ac24c087b0bf2a77e7ab4e9 Mon Sep 17 00:00:00 2001 From: Dylan Tack Date: Tue, 9 Aug 2011 12:20:43 -0700 Subject: [PATCH] Issue #1116408: Add support for Migrate module --- migrate.inc | 39 +++++++++++++++++++++++++++++++++++++++ redirect.info | 1 + redirect.module | 10 ++++++++++ 3 files changed, 50 insertions(+), 0 deletions(-) create mode 100644 migrate.inc diff --git a/migrate.inc b/migrate.inc new file mode 100644 index 0000000..22cd453 --- /dev/null +++ b/migrate.inc @@ -0,0 +1,39 @@ +registerTypes(array('entity')); + } + + public function fields() { + return array('migrate_redirects' => t('Original path(s) to redirect from.')); + } + + public function complete($entity, stdClass $row) { + if (($destination = entity_uri($entity->type, $entity)) && !empty($entity->migrate_redirects)) { + if (!is_array($entity->migrate_redirects)) { + $entity->migrate_redirects = array($entity->migrate_redirects); + } + + foreach ($entity->migrate_redirects as $path) { + $redirect_defaults = array( + 'uid' => $entity->uid, + 'status_code' => 301, + 'language' => $entity->language, + ); + $redirect = new stdClass(); + redirect_object_prepare($redirect, $redirect_defaults); + $redirect->redirect = $destination['path']; + $parsed = redirect_parse_url($path); + $redirect->source = $parsed['path']; + $redirect->source_options['query'] = $parsed['query']; + redirect_save($redirect); + } + } + } +} diff --git a/redirect.info b/redirect.info index 5134738..9b273a5 100644 --- a/redirect.info +++ b/redirect.info @@ -5,6 +5,7 @@ files[] = redirect.module files[] = redirect.admin.inc files[] = redirect.install files[] = redirect.test +files[] = migrate.inc files[] = views/redirect.views.inc ;files[] = views/redirect_handler_field_redirect_type.inc files[] = views/redirect_handler_filter_redirect_type.inc diff --git a/redirect.module b/redirect.module index 3705d87..a5f6b08 100644 --- a/redirect.module +++ b/redirect.module @@ -374,6 +374,16 @@ function redirect_views_api() { ); } +/* + * Implements hook_migrate_api(). + */ +function redirect_migrate_api() { + $api = array( + 'api' => 2, + ); + return $api; +} + /** * Implements hook_page_build(). * -- 1.7.4.1 From 9ba61f2d407c6d949ab9c7779a487ed2c6ec9328 Mon Sep 17 00:00:00 2001 From: marcusx Date: Thu, 1 Sep 2011 19:46:43 +0200 Subject: [PATCH 2/3] Issue #1116408: Add support for Migrate module Bugfix: Looking up and setting the correct entity type for the redirect destination --- migrate.inc | 10 ++++++++-- 1 files changed, 8 insertions(+), 2 deletions(-) diff --git a/migrate.inc b/migrate.inc index 22cd453..61518b3 100644 --- a/migrate.inc +++ b/migrate.inc @@ -15,7 +15,13 @@ class MigrateRedirectEntityHandler extends MigrateDestinationHandler { } public function complete($entity, stdClass $row) { - if (($destination = entity_uri($entity->type, $entity)) && !empty($entity->migrate_redirects)) { + + //Looking up the destination entity type for the current Migration + $migration = Migration::currentMigration(); + $destination = $migration->getDestination(); + $entity_type = $destination->getEntityType(); + + if (($redirect_destination = entity_uri($entity_type, $entity)) && !empty($entity->migrate_redirects)) { if (!is_array($entity->migrate_redirects)) { $entity->migrate_redirects = array($entity->migrate_redirects); } @@ -28,7 +34,7 @@ class MigrateRedirectEntityHandler extends MigrateDestinationHandler { ); $redirect = new stdClass(); redirect_object_prepare($redirect, $redirect_defaults); - $redirect->redirect = $destination['path']; + $redirect->redirect = $redirect_destination['path']; $parsed = redirect_parse_url($path); $redirect->source = $parsed['path']; $redirect->source_options['query'] = $parsed['query']; -- 1.7.4.1 From fef21a8f0d9db95862a4e8071634af9248e2183d Mon Sep 17 00:00:00 2001 From: marcusx Date: Thu, 1 Sep 2011 19:52:17 +0200 Subject: [PATCH 3/3] Issue #1116408: Add support for Migrate module * Looking up the current migration data in the constructor so we can use them in several places * Adding validation before redirect saving to avoid database errors due to duplicate entries --- migrate.inc | 55 ++++++++++++++++++++++++++++++++++++++++++++++++------- 1 files changed, 48 insertions(+), 7 deletions(-) diff --git a/migrate.inc b/migrate.inc index 61518b3..0b834c1 100644 --- a/migrate.inc +++ b/migrate.inc @@ -6,22 +6,59 @@ */ class MigrateRedirectEntityHandler extends MigrateDestinationHandler { + + public $migration; + public $destination; + public $entity_type; + public function __construct() { $this->registerTypes(array('entity')); + + //set current migration data that all other methods can use + $this->migration = Migration::currentMigration(); + $this->destination = $this->migration->getDestination(); + $this->entity_type = $this->destination->getEntityType(); } public function fields() { return array('migrate_redirects' => t('Original path(s) to redirect from.')); } + + +/** + * Validate a redirect. + * We need to check if a redirect already exists + * otherwise if we call redirect_save in complete we get an db + * error due to duplicate entries. + * + * This function is simmilar to the validate function in the + * redirect module but the feedback is handled via the Migrate + * showMessage functionality. + */ + private function redirect_validate($redirect) { + $redirect = (object) $redirect; + + // check that there there are no redirect loops + if (url($redirect->source) == url($redirect->redirect)) { + $this->migration->showMessage(t('You are attempting to redirect the page to itself. This will result in an infinite loop.'), 'error'); + return FALSE; + } + + redirect_hash($redirect); + if ($existing = redirect_load_by_hash($redirect->hash)) { + if ($redirect->rid != $existing->rid) { + $this->migration->showMessage(t('The source path is already being redirected.'), 'error'); + return FALSE; + } + } + + return TRUE; + } public function complete($entity, stdClass $row) { - //Looking up the destination entity type for the current Migration - $migration = Migration::currentMigration(); - $destination = $migration->getDestination(); - $entity_type = $destination->getEntityType(); - - if (($redirect_destination = entity_uri($entity_type, $entity)) && !empty($entity->migrate_redirects)) { + //We looked up the destination entity_type in the constructor + if (($redirect_destination = entity_uri($this->entity_type, $entity)) && !empty($entity->migrate_redirects)) { if (!is_array($entity->migrate_redirects)) { $entity->migrate_redirects = array($entity->migrate_redirects); } @@ -38,7 +75,11 @@ class MigrateRedirectEntityHandler extends MigrateDestinationHandler { $parsed = redirect_parse_url($path); $redirect->source = $parsed['path']; $redirect->source_options['query'] = $parsed['query']; - redirect_save($redirect); + + //Only save if the redirect is not already existing + if($this->redirect_validate($redirect)){ + redirect_save($redirect); + } } } } -- 1.7.4.1