From e91603ba42753579dde8e4bdf4a7b0de44ff50f0 Mon Sep 17 00:00:00 2001
From: Marco Villegas <marvil07@gmail.com>
Date: Fri, 15 Oct 2010 20:02:39 -0500
Subject: [PATCH] migrate_extras: Support for path_redirect

---
 .../modules/migrate_extras/migrate_extras.module   |    1 +
 .../migrate_extras/path_redirect.migrate.inc       |   97 ++++++++++++++++++++
 2 files changed, 98 insertions(+), 0 deletions(-)
 create mode 100644 sites/all/modules/migrate_extras/path_redirect.migrate.inc

diff --git sites/all/modules/migrate_extras/migrate_extras.module sites/all/modules/migrate_extras/migrate_extras.module
index 89c19fa..0d255ba 100644
--- sites/all/modules/migrate_extras/migrate_extras.module
+++ sites/all/modules/migrate_extras/migrate_extras.module
@@ -16,6 +16,7 @@ function migrate_extras_migrate_api() {
       'filefield' => array('status' => FALSE),
       'location' => array('status' => FALSE),
       'privatemsg' => array('status' => FALSE),
+      'path_redirect' => array('status' => FALSE),
     ),
   );
   return $api;
diff --git sites/all/modules/migrate_extras/path_redirect.migrate.inc sites/all/modules/migrate_extras/path_redirect.migrate.inc
new file mode 100644
index 0000000..ba1b97d
--- /dev/null
+++ sites/all/modules/migrate_extras/path_redirect.migrate.inc
@@ -0,0 +1,97 @@
+<?php
+// $Id$
+
+/**
+ * @file
+ * Integrates path_redirect module with the migrate module.
+ */
+
+/**
+ * Implementation of hook_migrate_types().
+ */
+function path_redirect_migrate_types() {
+  $types = array('redirect' => t('Path redirect'));
+  return $types;
+}
+
+/**
+ * Implementation of hook_migrate_fields_<type>().
+ */
+function path_redirect_migrate_fields_redirect() {
+  return array(
+    'rid' => t('Path redirect: Unique path redirect ID'),
+    'nid' => t('Path redirect: Node ID'),
+    'nodesourceid' => t('Path redirect: Node by migrate source id'),
+    'source' => t('Path redirect: Redirect source'),
+  );
+}
+
+/**
+ * Implementation of hook_migrate_delete_<type>().
+ */
+function path_redirect_migrate_delete_redirect($rid) {
+  path_redirect_delete($rid);
+}
+
+// $tblinfo contains the meta-information on the content set
+// $row is the source data for one object.
+// An array of messages is returned - use migrate_message() to generate a message.
+/**
+ * Implementation of hook_migrate_import_<type>().
+ */
+function path_redirect_migrate_import_redirect($tblinfo, $row) {
+  $errors = array();
+  $redirect = array();
+  // Handle the update case
+  if ($row->destid) {
+    $redirect['rid'] = $row->destid;
+  }
+
+  // For each destination field, populate it with the source value if present, and if
+  // if not use the specified default value
+  foreach ($tblinfo->fields as $destfield => $values) {
+    if ($values['srcfield'] && isset($row->$values['srcfield'])) {
+      $newvalue = $row->$values['srcfield'];
+    } else {
+      $newvalue = $values['default_value'];
+    }
+    $redirect[$destfield] = $newvalue;
+  }
+  // provide map to previous imported nodes
+  if (isset($redirect['nodesourceid']) && $redirect['nodesourceid'] && !isset($redirect['nid'])) {
+    $nid = _migrate_xlat_get_new_id('node', $redirect['nodesourceid']);
+    if ($nid) {
+      $redirect['nid'] = $nid;
+    }
+    else {
+      $errors[] = migrate_message(t('Could not determine node ID for !id',
+        array('!id' => $redirect['nodesourceid'])),
+        MIGRATE_MESSAGE_WARNING);
+    }
+    unset($redirect['nodesourceid']);
+  }
+
+  // now we should have a nid
+  if (isset($redirect['nid']) && $redirect['nid']) {
+    $node = node_load($redirect['nid']);
+    $redirect['redirect'] = $node->path;
+    $real_nid = $redirect['nid']; // save it to map later
+    unset($redirect['nid']);
+  }
+  else {
+    $errors[] = migrate_message(t('You need to provide a node source id or a node id to import path redirects. Node source ID is "!source_id" and Node ID is "!nid"',
+      array('!sourceid' => $redirect['nodesourceid'], '!nid' => $redirect['nid'])),
+    MIGRATE_MESSAGE_WARNING);
+  }
+
+  // Prepare the redirect for import.
+  $errors = array_merge($errors, migrate_destination_invoke_all('prepare_redirect', $redirect, $tblinfo, $row));
+
+  if (count($errors) == 0) {
+    path_redirect_save($redirect);
+    // Fill in the map table, so the migrate module knows this row is done
+    $sourcekey = $tblinfo->sourcekey;
+    migrate_add_mapping($tblinfo->mcsid, $row->$sourcekey, $real_nid);
+  }
+  return $errors;
+}
-- 
1.7.1

