diff --git a/migrate.inc b/migrate.inc new file mode 100644 index 0000000..ab9ef40 --- /dev/null +++ b/migrate.inc @@ -0,0 +1,87 @@ +registerTypes(array('entity')); + } + + 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 + * displayMessage functionality. + */ + private function redirectValidate($redirect) { + $redirect = (object) $redirect; + + // check that there there are no redirect loops + if (url($redirect->source) == url($redirect->redirect)) { + MigrationBase::displayMessage(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) { + MigrationBase::displayMessage(t('The source path is already being redirected.'), 'error'); + return FALSE; + } + } + + return TRUE; + } + + public function complete($entity, stdClass $row) { + + // Set current migration context one time. + if (!isset($this->destination)) { + $migration = Migration::currentMigration(); + $this->destination = $migration->getDestination(); + $this->entity_type = $this->destination->getEntityType(); + } + + // 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); + } + + 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 = $redirect_destination['path']; + $parsed = redirect_parse_url($path); + $redirect->source = $parsed['path']; + $redirect->source_options['query'] = $parsed['query']; + + // Only save if the redirect does not already exist. + if ($this->redirectValidate($redirect)) { + 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 dfbe029..d6e5207 100644 --- a/redirect.module +++ b/redirect.module @@ -375,6 +375,16 @@ function redirect_views_api() { ); } +/* + * Implements hook_migrate_api(). + */ +function redirect_migrate_api() { + $api = array( + 'api' => 2, + ); + return $api; +} + /** * Implements hook_page_build(). *