You are here

wordpress_migrate.install in WordPress Migrate 7.2

Same filename and directory in other branches
  1. 7 wordpress_migrate.install

WordPress migration module installation

File

wordpress_migrate.install
View source
<?php

/**
 * @file
 * WordPress migration module installation
 */
function wordpress_migrate_schema() {
  $schema = array();
  $schema['wordpress_migrate'] = wordpress_migrate_schema_wordpress();
  $schema['wordpress_migrate_attachment'] = wordpress_migrate_schema_attachment();
  $schema['wordpress_migrate_linked_authors'] = wordpress_migrate_schema_linked_authors();
  return $schema;
}
function wordpress_migrate_schema_wordpress() {
  return array(
    'description' => 'Mapping of machine name to filename for WordPress migrations',
    'fields' => array(
      'blog_id' => array(
        'type' => 'serial',
        'not null' => TRUE,
        'description' => 'Unique blog ID',
      ),
      'filename' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'description' => 'Filename of XML imported by this migration',
      ),
      'title' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'description' => 'Blog title (used to seed machine name)',
      ),
      'blog_url' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'description' => 'Original blog URL',
      ),
      'link' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'description' => 'Link value from XML',
      ),
      'uid' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'description' => "User ID of the account initiating the import",
      ),
      'wxr_version' => array(
        'type' => 'varchar',
        'length' => 10,
        'not null' => TRUE,
        'default' => '1.1',
        'description' => 'WXR version of the imported file',
      ),
    ),
    'primary key' => array(
      'blog_id',
    ),
    'unique keys' => array(
      'blog_url' => array(
        'blog_url',
      ),
    ),
  );
}
function wordpress_migrate_schema_attachment() {
  return array(
    'description' => 'Map original attachment URL to Drupal URI',
    'fields' => array(
      'blog_id' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => 'ID of parent blog',
      ),
      'original_url' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'description' => 'URL of attachment on WordPress',
      ),
      'new_uri' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'description' => 'URI of attachment on Drupal',
      ),
      'new_fid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => FALSE,
        'description' => 'File ID of the attachment',
      ),
      'parent_nid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => FALSE,
        'description' => 'Drupal ID of the node this is attached to',
      ),
    ),
    'primary key' => array(
      'blog_id',
      'original_url',
    ),
  );
}
function wordpress_migrate_schema_linked_authors() {
  return array(
    'description' => 'Track pre-existing user accounts',
    'fields' => array(
      'mail' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'description' => 'Email address of user account already in Drupal',
      ),
      'uid' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'description' => "User ID of the account in Drupal",
      ),
    ),
    'primary key' => array(
      'mail',
    ),
  );
}

/**
 * Implements hook_requirements().
 *
 * @param $phase
 *
 * @return array
 */
function wordpress_migrate_requirements($phase) {
  $requirements = array();
  $t = get_t();

  // Make sure the XMLReader extension is available.
  if (!extension_loaded('xmlreader')) {
    $requirements['wordpress_migrate_xmlreader']['severity'] = REQUIREMENT_ERROR;
    $requirements['wordpress_migrate_xmlreader']['title'] = $t('XMLReader extension');
    $requirements['wordpress_migrate_xmlreader']['description'] = $t('WordPress Migrate requires the PHP XMLReader extension.');
  }
  if (!function_exists('curl_init')) {
    $requirements['wordpress_migrate_curl'] = array(
      'title' => $t('cURL'),
      'value' => $t('Not found'),
      'severity' => REQUIREMENT_ERROR,
    );
    $requirements['wordpress_migrate_curl']['description'] = $t('Wordpress Migrate requires the <a href="@curl_url">php cURL extension</a>.', array(
      '@curl_url' => 'http://php.net/manual/en/curl.setup.php',
    ));
  }
  return $requirements;
}

/**
 * Implements hook_update_dependencies().
 */
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;
}

/**
 * Add uid field to wordpress_migrate table.
 */
function wordpress_migrate_update_7001() {
  db_add_field('wordpress_migrate', 'uid', array(
    'type' => 'int',
    'not null' => TRUE,
    'default' => 0,
    'description' => "User ID of the account initiating the import",
  ));
  return t('Added uid column to wordpress_migrate table');
}

/**
 * Add the wordpress_migrate_attachment table.
 */
function wordpress_migrate_update_7002() {
  db_create_table('wordpress_migrate_attachment', wordpress_migrate_schema_attachment());
  return t('Added the wordpress_migrate_attachment table');
}

/**
 * Add blog_url field to wordpress_migrate table.
 */
function wordpress_migrate_update_7003() {
  db_add_field('wordpress_migrate', 'blog_url', array(
    'type' => 'varchar',
    'length' => 255,
    'not null' => TRUE,
    'description' => 'Original blog URL',
  ));
  return t('Added blog_url column to wordpress_migrate table');
}

/**
 * Add link field to wordpress_migrate table.
 */
function wordpress_migrate_update_7004() {
  if (!db_field_exists('wordpress_migrate', 'link')) {
    db_add_field('wordpress_migrate', 'link', array(
      'type' => 'varchar',
      'length' => 255,
      'not null' => TRUE,
      'description' => 'Link value from XML',
    ));
  }
  return t('Added link column to wordpress_migrate table');
}

/**
 * Fix any malformed YouTube embeds.
 */
function wordpress_migrate_update_7005() {
  $results = db_select('file_managed', 'f')
    ->fields('f', array(
    'fid',
    'uri',
  ))
    ->condition(db_or()
    ->condition('f.uri', 'youtube://%?%', 'LIKE')
    ->condition('f.uri', 'youtube://%&%', 'LIKE'))
    ->execute();
  foreach ($results as $result) {
    $original = $result->uri;
    preg_match('@(youtube://[^\\?\\&]+)@i', $result->uri, $matches);
    $result->uri = $matches[1];
    list(, , , $result->filename) = explode('/', $result->uri);
    $check_for_duplicates = db_select('file_managed', 'f')
      ->fields('f', array(
      'fid',
      'uri',
    ))
      ->condition('f.uri', $result->uri)
      ->execute()
      ->fetchObject();
    if (!$check_for_duplicates) {
      drupal_write_record('file_managed', $result, 'fid');
    }
    else {

      // The uri column is unique. Throw an error, but run through the rest first.
      if (!isset($errors)) {
        $errors = array(
          $original,
          $result,
          $check_for_duplicates,
        );
      }
    }
  }
  if (isset($errors)) {
    throw new DrupalUpdateException(t('Unable to fix uri: @media_1 (fid: @fid_1), as it conflicts with uri: @media_2 (fid: @fid_2).', array(
      '@media_1' => $errors[0],
      '@media_2' => $errors[2]->uri,
      '@fid_1' => $errors[1]->fid,
      '@fid_2' => $errors[2]->fid,
    )));
  }
  return t('Fixed all malformed YouTube embeds.');
}

/**
 * Add the wordpress_migrate_attachment table and wxr_version column..
 */
function wordpress_migrate_update_7006() {
  if (!db_field_exists('wordpress_migrate', 'wxr_version')) {
    db_add_field('wordpress_migrate', 'wxr_version', array(
      'type' => 'varchar',
      'length' => 10,
      'not null' => TRUE,
      'default' => '1.1',
      'description' => 'WXR version of the imported file',
    ));
  }
  if (!db_table_exists('wordpress_migrate_linked_authors')) {
    db_create_table('wordpress_migrate_linked_authors', wordpress_migrate_schema_linked_authors());
  }
  return t('Added the wordpress_migrate_linked_authors table and wxr_version column');
}

/**
 * Key blogs with a serial field; add parent_nid tracking for attachments.
 */
function wordpress_migrate_update_7007() {

  // Add a serial ID field to be the PK for blogs
  if (!db_field_exists('wordpress_migrate', 'blog_id')) {
    db_drop_primary_key('wordpress_migrate');
    db_add_field('wordpress_migrate', 'blog_id', array(
      'type' => 'serial',
      'not null' => TRUE,
      'description' => 'Unique blog ID',
    ), array(
      'primary key' => array(
        'blog_id',
      ),
    ));
    db_add_unique_key('wordpress_migrate', 'blog_url', array(
      'blog_url',
    ));
  }

  // Attachments now will reference the integer ID instead of the filename.
  if (!db_field_exists('wordpress_migrate_attachment', 'blog_id')) {
    db_add_field('wordpress_migrate_attachment', 'blog_id', array(
      'type' => 'int',
      'unsigned' => TRUE,
      'not null' => TRUE,
      'default' => 0,
      'description' => 'ID of parent blog',
    ));
    $query = db_select('wordpress_migrate_attachment', 'a')
      ->fields('a', array(
      'filename',
    ));
    $query
      ->innerJoin('wordpress_migrate', 'wm', 'a.filename=wm.filename');
    $result = $query
      ->fields('wm', array(
      'blog_id',
    ))
      ->execute();
    foreach ($result as $row) {
      db_update('wordpress_migrate_attachment')
        ->fields(array(
        'blog_id' => $row->blog_id,
      ))
        ->condition('filename', $row->filename)
        ->execute();
    }
    db_drop_primary_key('wordpress_migrate_attachment');
    db_drop_field('wordpress_migrate_attachment', 'filename');
    db_add_primary_key('wordpress_migrate_attachment', array(
      'blog_id',
      'original_url',
    ));
  }

  // Track the attachment parent nodes
  if (!db_field_exists('wordpress_migrate_attachment', 'parent_nid')) {
    db_add_field('wordpress_migrate_attachment', 'parent_nid', array(
      'type' => 'int',
      'unsigned' => TRUE,
      'not null' => FALSE,
      'description' => 'ID of parent blog',
    ));
  }
  return t('Added blog_id key, parent node tracking for attachments.');
}

/**
 * Track the fid of each attachment, for rewriting to media tags.
 */
function wordpress_migrate_update_7008() {
  if (!db_field_exists('wordpress_migrate_attachment', 'new_fid')) {
    db_add_field('wordpress_migrate_attachment', 'new_fid', array(
      'type' => 'int',
      'unsigned' => TRUE,
      'not null' => FALSE,
      'description' => 'File ID of the attachment',
    ));
  }
}

/**
 * Reset default import method to Immediate.
 */
function wordpress_migrate_update_7009() {
  variable_set('wordpress_migrate_import_method', 0);
}

/**
 * Fix blog_url index name to make sense.
 */
function wordpress_migrate_update_7010() {
  if (db_index_exists('wordpress_migrate', 'filename')) {
    db_drop_index('wordpress_migrate', 'filename');
    db_add_unique_key('wordpress_migrate', 'blog_url', array(
      'blog_url',
    ));
  }
}

/**
 * Get import options out of variables and into arguments.
 */
function wordpress_migrate_update_7011() {
  $arguments = array();
  $arguments['post_type'] = variable_get('wordpress_migrate_post_type', 'blog');
  $arguments['import_users'] = variable_get('wordpress_migrate_create_new_users', 1);
  $arguments['default_author_uid'] = variable_get('wordpress_migrate_default_author_uid', 1);
  $arguments['attachment_field'] = variable_get('wordpress_migrate_attachment_field', '');
  $arguments['podcast_field'] = variable_get('wordpress_migrate_podcast_field', '');
  $arguments['text_format'] = variable_get('wordpress_migrate_text_format', 'filtered_html');
  $arguments['text_format_comment'] = variable_get('wordpress_migrate_text_format_comment', 'filtered_html');
  $arguments['page_type'] = variable_get('wordpress_migrate_page_type', 'page');
  $arguments['tag_vocabulary'] = variable_get('wordpress_migrate_tag_vocabulary', 'tags');
  $arguments['category_vocabulary'] = variable_get('wordpress_migrate_category_vocabulary', '');
  $arguments['path_action'] = variable_get('wordpress_migrate_path_action', 1);
  $arguments['generate_redirects'] = variable_get('wordpress_migrate_generate_redirects', 0);
  $result = db_select('migrate_status', 'ms')
    ->fields('ms', array(
    'machine_name',
    'arguments',
  ))
    ->condition('class_name', 'WordPress%', 'LIKE')
    ->execute();
  foreach ($result as $row) {
    $original_arguments = unserialize($row->arguments);
    $arguments += $original_arguments;
    db_update('migrate_status')
      ->fields(array(
      'arguments' => serialize($arguments),
    ))
      ->condition('machine_name', $row->machine_name)
      ->execute();
  }
}

/**
 * Drush support has moved into core, remove the variables we were using.
 */
function wordpress_migrate_update_7012() {
  variable_del('wordpress_migrate_drush');
  variable_del('wordpress_migrate_import_method');
  variable_del('wordpress_migrate_force_drush');
  variable_del('wordpress_migrate_notification');
  variable_del('wordpress_migrate_notification_subject');
  variable_del('wordpress_migrate_notification_body');
}

/**
 * Rewrite argument names for vocabulary references.
 */
function wordpress_migrate_update_7013() {
  $query = db_select('migrate_status', 'ms')
    ->fields('ms', array(
    'machine_name',
    'arguments',
  ));
  $query
    ->innerJoin('wordpress_migrate', 'wm', 'ms.group_name = wm.title');
  $result = $query
    ->execute();
  foreach ($result as $row) {
    $arguments = unserialize($row->arguments);
    if (isset($arguments['tag_vocabulary'])) {
      $arguments['tag_field'] = $arguments['tag_vocabulary'];
      unset($arguments['tag_vocabulary']);
    }
    if (isset($arguments['category_vocabulary'])) {
      $arguments['category_field'] = $arguments['category_vocabulary'];
      unset($arguments['category_vocabulary']);
    }
    db_update('migrate_status')
      ->fields(array(
      'arguments' => serialize($arguments),
    ))
      ->condition('machine_name', $row->machine_name)
      ->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();
  }
}

Functions

Namesort descending Description
wordpress_migrate_requirements Implements hook_requirements().
wordpress_migrate_schema @file WordPress migration module installation
wordpress_migrate_schema_attachment
wordpress_migrate_schema_linked_authors
wordpress_migrate_schema_wordpress
wordpress_migrate_update_7001 Add uid field to wordpress_migrate table.
wordpress_migrate_update_7002 Add the wordpress_migrate_attachment table.
wordpress_migrate_update_7003 Add blog_url field to wordpress_migrate table.
wordpress_migrate_update_7004 Add link field to wordpress_migrate table.
wordpress_migrate_update_7005 Fix any malformed YouTube embeds.
wordpress_migrate_update_7006 Add the wordpress_migrate_attachment table and wxr_version column..
wordpress_migrate_update_7007 Key blogs with a serial field; add parent_nid tracking for attachments.
wordpress_migrate_update_7008 Track the fid of each attachment, for rewriting to media tags.
wordpress_migrate_update_7009 Reset default import method to Immediate.
wordpress_migrate_update_7010 Fix blog_url index name to make sense.
wordpress_migrate_update_7011 Get import options out of variables and into arguments.
wordpress_migrate_update_7012 Drush support has moved into core, remove the variables we were using.
wordpress_migrate_update_7013 Rewrite argument names for vocabulary references.
wordpress_migrate_update_7014 Remove no-longer-used variables.
wordpress_migrate_update_7015 Updates to legacy wordpress migration arguments.
wordpress_migrate_update_dependencies Implements hook_update_dependencies().