You are here

public function WordPressItemMigration::prepare in WordPress Migrate 7.2

Same name and namespace in other branches
  1. 7 wordpress_item.inc \WordPressItemMigration::prepare()

Prepare node - called just before node_save().

Parameters

stdClass $node:

stdClass $row:

File

./wordpress_item.inc, line 738
Support for migrating posts and pages from a WordPress blog into Drupal.

Class

WordPressItemMigration
Intermediate Migration class, implementing behavior common across different types (post_type) of items.

Code

public function prepare(stdClass $node, stdClass $row) {

  // With WXR version 1.0, match creator username to Drupal username if
  // possible; otherwise, use the user that initiated the import. With later
  // versions, we've already got the right uid via the author migration.
  if ($this->blog
    ->getWxrVersion() == '1.0') {
    static $drupal_static_fast;
    if (!isset($drupal_static_fast)) {
      $drupal_static_fast['user_map'] =& drupal_static(__FUNCTION__);
      $drupal_static_fast['default_user'] =& drupal_static(__FUNCTION__ . 'DefaultUser');
    }
    $user_map =& $drupal_static_fast['user_map'];
    if (!isset($user_map[$row->creator])) {
      $user_map[$row->creator] = db_select('users', 'u')
        ->fields('u', array(
        'uid',
      ))
        ->condition('name', $row->creator)
        ->execute()
        ->fetchField();
      if (!$user_map[$row->creator]) {
        $default_user =& $drupal_static_fast['default_user'];
        if (!isset($default_user)) {
          $default_user = db_select('wordpress_migrate', 'wpm')
            ->fields('wpm', array(
            'uid',
          ))
            ->condition('filename', $this->wxrFile)
            ->execute()
            ->fetchField();
        }
        $user_map[$row->creator] = $default_user;
      }
    }
    $node->uid = $user_map[$row->creator];
  }

  // If any term relationships were unresolved, create them the hard way
  foreach (array(
    'tag',
    'category',
  ) as $term_type) {
    $meta_field_name = $term_type . 'Field';
    if ($this->{$meta_field_name}) {
      $field_name = $this->{$meta_field_name};
      $value_name = $term_type . '_value';

      // Shortcut - if the counts match, don't need to dig deeper
      $field_values = field_get_items('node', $node, $field_name);
      if (!empty($field_values)) {
        $node_count = count($field_values);
      }
      else {
        $node_count = 0;
      }
      if (isset($row->{$term_type})) {
        $row_count = count($row->{$term_type});
      }
      else {
        $row_count = 0;
      }
      if ($node_count != $row_count) {
        $field_info = field_info_field($field_name);
        $vocabulary_name = $field_info['settings']['allowed_values'][0]['vocabulary'];
        $vocabulary = taxonomy_vocabulary_machine_name_load($vocabulary_name);
        $vid = $vocabulary->vid;

        // Get any terms already in the field
        $done_terms = array();
        if (is_array($field_values)) {
          foreach ($field_values as $value_array) {
            $terms = taxonomy_term_load_multiple($value_array);
            foreach ($terms as $term) {
              $done_terms[] = $term->name;
            }
          }
        }
        if (isset($row->{$value_name})) {
          if (!is_array($row->{$value_name})) {
            $row->{$value_name} = array(
              $row->{$value_name},
            );
          }
          $values = array();
          foreach ($row->{$value_name} as $value) {
            $values[] = html_entity_decode($value);
          }
          $diff = array_diff($values, $done_terms);
          $field_language = field_language('node', $node, $field_name);
          foreach ($diff as $new_term_name) {

            // Let's see if the term already exists
            $matches = taxonomy_term_load_multiple(array(), array(
              'name' => trim($new_term_name),
              'vid' => $vid,
            ));
            if ($matches) {
              $node->{$field_name}[$field_language][] = array(
                'tid' => key($matches),
              );
            }
            else {
              $term = new stdClass();
              $term->name = $new_term_name;
              $term->vid = $vid;
              taxonomy_term_save($term);
              $node->{$field_name}[$field_language][] = array(
                'tid' => $term->tid,
              );
            }
          }
        }
      }
    }
  }

  // If we have an attached podcast, replace [powerpress] in the body with
  // a link to the audio file.
  $podcast_field = $this->arguments['podcast_field'];
  if ($podcast_field && isset($node->{$podcast_field})) {
    $podcast_fid = $node->{$podcast_field}[LANGUAGE_NONE][0]['fid'];
    if ($podcast_fid) {
      $podcast = file_load($podcast_fid);
      if ($podcast) {

        // file_create_url() gives us a full URL, which when running from
        // drush will often start with http://default/, which is not what we
        // want. Strip the prefix for a relative link.
        $url = file_create_url($podcast->uri);
        $url = str_replace($GLOBALS['base_url'] . '/', '/', $url);
        $link = '<p><a href="' . $url . '"><img src="/modules/file/icons/audio-x-generic.png" /></a></p>';
        $body_language = field_language('node', $node, 'body');
        $node->body[$body_language][0]['value'] = str_replace('[powerpress]', $link, $node->body[$body_language][0]['value']);
      }
    }
  }
}