You are here

function node_migrate_import_node in Migrate 6

Implementation of hook_migrate_import_node().

File

modules/node.migrate.inc, line 43
Implementation of node destination handling

Code

function node_migrate_import_node($tblinfo, $row) {
  $node = new stdClass();
  $node->type = $tblinfo->desttype;

  // Handle an update operation
  if ($row->destid) {
    $node->nid = $row->destid;

    // Need to have the current vid present
    $node->vid = db_result(db_query("SELECT vid FROM {node} WHERE nid=%d", $row->destid));
  }
  else {
    if (isset($tblinfo->fields['nid'])) {
      $nidname = $tblinfo->fields['nid']['srcfield'];
      $node = node_load($row->{$nidname});
      if (!$node) {
        $node = new stdClass();
        $node->type = $tblinfo->desttype;
      }
    }
  }

  // Data which might be useful for node hooks.
  $node->migrate_tblinfo = $tblinfo;

  // Make sure the default comment settings are applied first, so they
  // can be overridden
  $function = 'comment_nodeapi';
  if (function_exists($function)) {
    $function($node, "prepare");
  }
  foreach ($tblinfo->fields as $destfield => $values) {
    if ($values['srcfield'] && $row->{$values}['srcfield']) {
      $node->{$destfield} = $row->{$values}['srcfield'];
    }
    else {
      $node->{$destfield} = $values['default_value'];
    }
  }

  // Do default prep before letting other modules take a shot
  $type_info = node_get_types('type', $node->type);
  $errors = array();

  // User name and uid.
  global $user;
  if (isset($node->uid) && $node->uid > 0) {
    $account = user_load(array(
      'uid' => $node->uid,
    ));
    $node->name = $account->name;
  }
  elseif (isset($node->name)) {

    // We have a mapped username.
    if ($account = user_load(array(
      'name' => $node->name,
    ))) {
      $node->uid = $account->uid;
    }
    elseif ($account = user_load(array(
      'mail' => $node->name,
    ))) {
      $node->uid = $account->uid;
    }

    // If no valid incoming user, the node owner will end up being the current user
  }
  elseif ($user->uid > 0) {
    $node->uid = $user->uid;
    $node->name = $user->name;
  }
  else {

    // Default to admin user
    $node->uid = 1;
    $account = user_load(array(
      'uid' => $node->uid,
    ));
    $node->name = $account->name;
  }

  // Creation date.
  if (isset($node->created)) {

    // We have a mapped date.
    if (empty($node->created)) {
      unset($node->created);
    }
    elseif (($date = _migrate_valid_date($node->created)) > -1) {
      $node->created = $date;
    }
    else {
      migrate_message(t('The created date %date is not a valid date.', array(
        '%date' => $node->created,
      )));
      unset($node->created);
    }
  }

  // Last updated
  if (isset($node->changed)) {

    // We have a mapped changed date.
    if (empty($node->changed)) {
      unset($node->changed);
    }
    elseif (($date = _migrate_valid_date($node->changed)) > -1) {
      $node->changed = $date;
    }
    else {
      $errors[] = migrate_message(t('The changed date %date is not a valid date.', array(
        '%date' => theme('placeholder', $node->changed),
      )));
      unset($node->changed);
    }
  }

  // Options (published, promoted, sticky, moderated, new revision).
  if (!isset($options)) {
    $options = variable_get('node_options_' . $node->type, array(
      'status',
      'promote',
    ));
  }
  $all_options = array(
    'status',
    'moderate',
    'promote',
    'sticky',
    'revision',
  );
  foreach ($all_options as $key) {
    if (isset($node->{$key}) && drupal_strlen($node->{$key}) > 0) {

      // If the field was mapped, use that value.
      $node->{$key} = $node->{$key} ? 1 : 0;
    }
    else {

      // If not, use the global option.
      $node->{$key} = isset($options[$key]) ? $options[$key] : 0;
    }
  }

  // Title.
  if ($type_info->has_title && (!isset($node->title) || empty($node->title))) {
    $node->title = t('{Empty title}');
  }

  // Strip tags from the title
  if (isset($node->title)) {
    $node->title = strip_tags($node->title);
  }

  // Body/teaser
  if ($type_info->has_body) {

    // If incoming data has a teaser and no body, copy the teaser into the body
    if (isset($node->teaser)) {
      $node->teaser = trim($node->teaser);
    }
    else {
      $node->teaser = '';
    }
    if (isset($node->body)) {
      $node->body = trim($node->body);
    }
    else {
      $node->body = '';
    }

    // If a teaser and body, and 'show summary in full view' disabled, add in
    // <!--break--> delimiter.
    // TODO: When updating an existing node (nid in incoming content set), the
    // break is unnecessarily prepended. How to avoid?
    if ($node->teaser && $node->body && (!isset($node->teaser_include) || !$node->teaser_include)) {
      $node->body = "\n<!--break-->\n" . $node->body;
    }
    elseif ($node->teaser && !$node->body) {
      $node->body = $node->teaser;
    }
    elseif ($node->body && !$node->teaser) {

      // Teaser not automatically generated
      $node->teaser = node_teaser($node->body);
    }
  }

  // Prepare the node for import.
  $errors = migrate_destination_invoke_all('prepare_node', $node, $tblinfo, $row);
  $success = TRUE;
  foreach ($errors as $error) {
    if ($error['level'] != MIGRATE_MESSAGE_INFORMATIONAL) {
      $success = FALSE;
      break;
    }
  }
  if ($success) {
    timer_start('node_save');

    // node_save() unconditionally sets the changed timestamp to time() - we need
    // to force the migrated value if present
    if (isset($node->changed)) {
      $changed = $node->changed;
    }
    node_save($node);
    if (isset($changed)) {
      $node->changed = $changed;
      drupal_write_record('node', $node, 'nid');
    }
    timer_stop('node_save');

    // Call completion hooks, for any processing which needs to be done after node_save
    timer_start('node completion hooks');
    $errors = array_merge($errors, migrate_destination_invoke_all('complete_node', $node, $tblinfo, $row));
    timer_stop('node completion hooks');
    $sourcekey = $tblinfo->sourcekey;
    migrate_add_mapping($tblinfo->mcsid, $row->{$sourcekey}, $node->nid);
  }
  return $errors;
}