From c0f62fa305a2ea22ed702c40d73fec4cca3b81da Mon Sep 17 00:00:00 2001
From: Mike Ryan <mike.ryan@acquia.com>
Date: Fri, 23 Jan 2015 10:11:21 -0600
Subject: [PATCH] Issue #2401145 by mikeryan: Fix upgrade path

---
 wordpress.inc             |  25 ++++++-
 wordpress_attachment.inc  |  17 ++++-
 wordpress_migrate.install | 139 +++++++++++++++++++++++++++++++++-----
 3 files changed, 159 insertions(+), 22 deletions(-)

diff --git a/wordpress.inc b/wordpress.inc
index 8c5b77f..e74744d 100644
--- a/wordpress.inc
+++ b/wordpress.inc
@@ -296,6 +296,11 @@ class WordPressBlog {
     );
   }
 
+  /**
+   * Get a list of all migrations in this blog.
+   *
+   * @return Migration[]
+   */
   public function migrations() {
     if (empty($this->migrations)) {
       $this->migrations = array();
@@ -312,6 +317,11 @@ class WordPressBlog {
     return $this->migrations;
   }
 
+  /**
+   * Get a list of all WordPress blogs.
+   *
+   * @return WordPressBlog[]
+   */
   static public function blogs() {
     $blogs = array();
     $result = db_select('wordpress_migrate', 'wm')
@@ -330,18 +340,23 @@ class WordPressBlog {
    * @param $sourcefile
    *  The raw WXR file as uploaded.
    * @param $destination
-   *  Filespec to which to write the cleaned-up WXR file.
+   *  Filespec to which to write the cleaned-up WXR file. Omit when
+   *  $namespaces_only == TRUE.
    * @param bool $unlink
    *  Indicates whether $sourcefile will be deleted after preprocessing.
+   * @param bool $namespaces_only
+   *  When TRUE, do not rewrite the file, simply gather and return the namespaces.
    *
    * @return array
    *  List of referenced namespaces, keyed by prefix.
    */
-  static public function preprocessFile($sourcefile, $destination, $unlink = TRUE) {
+  static public function preprocessFile($sourcefile, $destination, $unlink = TRUE, $namespaces_only = FALSE) {
     // Cleanup some stuff in the process of moving the file to its final
     // destination
     $source_handle = fopen($sourcefile, 'r');
-    $dest_handle = fopen($destination, 'w');
+    if (!$namespaces_only) {
+      $dest_handle = fopen($destination, 'w');
+    }
 
     // First, get the header (everything before the <channel> element) to
     // rewrite the namespaces (skipping any empty lines).
@@ -376,6 +391,10 @@ class WordPressBlog {
       $namespaces[$match[1]] = $match[2];
     }
 
+    if ($namespaces_only) {
+      return $namespaces;
+    }
+
     // Replace HTML entities with XML entities
     $header = strtr($header, self::$entityReplacements);
     fputs($dest_handle, $header);
diff --git a/wordpress_attachment.inc b/wordpress_attachment.inc
index 83f298e..ec2ec13 100644
--- a/wordpress_attachment.inc
+++ b/wordpress_attachment.inc
@@ -188,15 +188,28 @@ class WordPressAttachment extends WordPressMigration {
     // Populate the parent node's attachment field, if applicable
     if ($row->{'wp:post_parent'}) {
       // Which migration did it come from, posts or pages?
-      $post_migration = Migration::getInstance($this->group->getName() . 'BlogPost');
+      $post_migration_name = $this->group->getName() . 'BlogPost';
+      $exists = db_select('migrate_status', 'ms')
+        ->fields('ms', array('machine_name'))
+        ->condition('machine_name', $post_migration_name)
+        ->execute()
+        ->fetchField();
+      if (!$exists) {
+        // Legacy migrations used BlogEntry.
+        $post_migration_name = $this->group->getName() . 'BlogEntry';
+      }
+      /** @var Migration $post_migration */
+      $post_migration = Migration::getInstance($post_migration_name);
       $post_map_row = $post_migration->getMap()->getRowByDestination(array($row->{'wp:post_parent'}));
 
       if (!empty($post_map_row)) {
         $attachment_field = $this->arguments['blog_attachment_field'];
       }
       else {
+        /** @var Migration $page_migration */
         $page_migration = Migration::getInstance($this->group->getName() . 'Page');
-        $page_map_row = $page_migration->getMap()->getRowByDestination(array($row->{'wp:post_parent'}));
+        $page_map_row = $page_migration->getMap()
+                                       ->getRowByDestination(array($row->{'wp:post_parent'}));
         if (!empty($page_map_row)) {
           $attachment_field = $this->arguments['page_attachment_field'];
         }
diff --git a/wordpress_migrate.install b/wordpress_migrate.install
index bd5646e..51c261b 100644
--- a/wordpress_migrate.install
+++ b/wordpress_migrate.install
@@ -159,24 +159,15 @@ function wordpress_migrate_requirements($phase) {
 }
 
 /**
- * Implements hook_uninstall().
+ * Implements hook_update_dependencies().
  */
-function wordpress_migrate_uninstall() {
-  variable_del('wordpress_migrate_attachment_field');
-  variable_del('wordpress_migrate_category_vocabulary');
-  variable_del('wordpress_migrate_generate_redirects');
-  variable_del('wordpress_migrate_import_method');
-  variable_del('wordpress_migrate_notification');
-  variable_del('wordpress_migrate_notification_body');
-  variable_del('wordpress_migrate_notification_failure_body');
-  variable_del('wordpress_migrate_notification_subject');
-  variable_del('wordpress_migrate_page_type');
-  variable_del('wordpress_migrate_path_action');
-  variable_del('wordpress_migrate_podcast_field');
-  variable_del('wordpress_migrate_post_type');
-  variable_del('wordpress_migrate_tag_vocabulary');
-  variable_del('wordpress_migrate_text_format');
-  variable_del('wordpress_migrate_text_format_comment');
+function wordpress_migrate_update_dependencies() {
+  // Make sure migrate_ui uses our drush variables before we delete them.
+  $dependencies['wordpress_migrate'][7012] = array('migrate_ui' => 7202);
+
+  // Migrate must set up the new group support before we can reference it.
+  $dependencies['wordpress_migrate'][7013] = array('migrate' => 7203);
+  return $dependencies;
 }
 
 /**
@@ -448,3 +439,117 @@ function wordpress_migrate_update_7013() {
       ->execute();
   }
 }
+
+/**
+ * Remove no-longer-used variables.
+ */
+function wordpress_migrate_update_7014() {
+  variable_del('wordpress_migrate_attachment_field');
+  variable_del('wordpress_migrate_category_vocabulary');
+  variable_del('wordpress_migrate_create_new_users');
+  variable_del('wordpress_migrate_default_author_uid');
+  variable_del('wordpress_migrate_generate_redirects');
+  variable_del('wordpress_migrate_page_type');
+  variable_del('wordpress_migrate_path_action');
+  variable_del('wordpress_migrate_podcast_field');
+  variable_del('wordpress_migrate_post_type');
+  variable_del('wordpress_migrate_tag_vocabulary');
+  variable_del('wordpress_migrate_text_format');
+  variable_del('wordpress_migrate_text_format_comment');
+}
+
+/**
+ * Updates to legacy wordpress migration arguments.
+ */
+function wordpress_migrate_update_7015() {
+  // We must force the module_implements cache to regenerate so destination
+  // handlers are registered and we can identify the destination fields below.
+  module_implements('migrate_api', FALSE, TRUE);
+
+  foreach (WordPressBlog::blogs() as $blog) {
+    $migrations = $blog->migrations();
+    $title = $blog->getTitle();
+    $group_created = FALSE;
+
+    foreach ($migrations as $migration) {
+      // We use the lack of namespaces to identify old blogs.
+      $group_arguments = $migration->getGroup()->getArguments();
+      if (!empty($group_arguments['namespaces'])) {
+        break;
+      }
+
+      // Set up the group properly, giving it the right name if it was 'default'.
+      $group_name = $migration->getGroup()->getName();
+      if ($group_name == 'default') {
+        $group_name = $title;
+      }
+      $arguments = $migration->getArguments();
+      $arguments['group_name'] = $group_name;
+      $arguments['post_type'] = $arguments['bundle'];
+      unset($arguments['bundle']);
+      if (!$group_created) {
+        $group_title = $group_name . ' (' . $blog->getBlogUrl() . ')';
+        $group_arguments['namespaces'] = WordPressBlog::preprocessFile(
+          $blog->getFilename(), NULL, FALSE, TRUE);
+        $group_arguments['source_system'] = 'WordPress';
+        $group_arguments['filename'] = $blog->getFilename();
+        MigrateGroup::register($group_name, $group_title, $group_arguments);
+        $group_created = TRUE;
+      }
+
+      if (is_a($migration, 'WordPressItemMigration')) {
+        // From tag_vocabulary/category_vocabulary, derive tag_field/category_field.
+        foreach (array('tag', 'category') as $vocab_type) {
+          $vocab = $vocab_type . '_vocabulary';
+          $field = $vocab_type . '_field';
+          if (!empty($arguments[$vocab])) {
+            foreach ($migration->getDestination()->fields() as $machine_name => $description) {
+              $field_info = field_info_field($machine_name);
+              if (isset($field_info['settings']['allowed_values'][0]['vocabulary']) &&
+                  $arguments[$vocab] == $field_info['settings']['allowed_values'][0]['vocabulary']
+              ) {
+                $arguments[$field] = $machine_name;
+                $arguments[$vocab_type . '_migration'] = ucfirst($vocab_type);
+                $arguments['dependencies'][] = $title . ucfirst($vocab_type);
+                unset($arguments[$vocab]);
+              }
+            }
+            if (!isset($arguments[$field])) {
+              $arguments[$field] = '';
+            }
+          }
+        }
+        $arguments['dependencies'][] = $title . 'Author';
+        $arguments['author_migration'] = $title . 'Author';
+      }
+
+      // For comment migrations (which were only on blogs previously), set source_post_type.
+      if (is_a($migration, 'WordPressComment')) {
+        $arguments['post_type'] = 'blog';
+        $arguments['source_post_type'] = 'post';
+        $arguments['dependencies'][] = $title . 'BlogEntry';
+      }
+
+      if (is_a($migration, 'WordPressAttachment')) {
+        $arguments['dependencies'][] = $title . 'BlogEntry';
+        $arguments['dependencies'][] = $title . 'Page';
+      }
+
+      MigrationBase::registerMigration(get_class($migration), $migration->getMachineName(),
+        $arguments);
+    }
+  }
+
+  // If no migrations remain belonging to the default group, remove it.
+  $count = db_select('migrate_status', 'ms')
+    ->fields('ms', array('machine_name'))
+    ->condition('group_name', 'default')
+    ->countQuery()
+    ->execute()
+    ->fetchField();
+  if ($count == 0) {
+    db_delete('migrate_group')
+      ->condition('name', 'default')
+      ->execute();
+  }
+}
-- 
GitLab