You are here

public function MigrateDestinationComment::import in Migrate 7.2

Same name and namespace in other branches
  1. 6.2 plugins/destinations/comment.inc \MigrateDestinationComment::import()

Import a single comment.

Parameters

$comment: Comment object to build. Prefilled with any fields mapped in the Migration.

$row: Raw source data object - passed through to prepare/complete handlers.

Return value

array Array of key fields (cid only in this case) of the comment that was saved if successful. FALSE on failure.

Overrides MigrateDestination::import

File

plugins/destinations/comment.inc, line 135
Support for comment destinations.

Class

MigrateDestinationComment
Destination class implementing migration into comments.

Code

public function import(stdClass $comment, stdClass $row) {
  $migration = Migration::currentMigration();

  // Updating previously-migrated content?
  if (isset($row->migrate_map_destid1)) {
    if (isset($comment->cid)) {
      if ($comment->cid != $row->migrate_map_destid1) {
        throw new MigrateException(t("Incoming cid !cid and map destination nid !destid1 don't match", array(
          '!cid' => $comment->cid,
          '!destid1' => $row->migrate_map_destid1,
        )));
      }
    }
    else {
      $comment->cid = $row->migrate_map_destid1;
    }
  }

  // Fix up timestamps
  if (isset($comment->created)) {
    $comment->created = MigrationBase::timestamp($comment->created);
  }
  if (isset($comment->changed)) {
    $comment->changed = MigrationBase::timestamp($comment->changed);
  }
  if (!isset($comment->node_type)) {
    $comment->node_type = $this->bundle;
  }
  if ($migration
    ->getSystemOfRecord() == Migration::DESTINATION) {
    if (!isset($comment->cid)) {
      throw new MigrateException(t('System-of-record is DESTINATION, but no destination cid provided'));
    }
    $rawcomment = $comment;
    $old_comment = comment_load($comment->cid);
    if (empty($old_comment)) {
      throw new MigrateException(t('System-of-record is DESTINATION, but commend !cid does not exist', array(
        '!cid' => $comment->cid,
      )));
    }
    if (!isset($comment->nid)) {
      $comment->nid = $old_comment->nid;
    }
    if (!isset($comment->created)) {
      $comment->created = $old_comment->created;
    }
    if (!isset($comment->changed)) {
      $comment->changed = $old_comment->changed;
    }
    $this
      ->prepare($comment, $row);
    foreach ($rawcomment as $field => $value) {
      $old_comment->{$field} = $comment->{$field};
    }
    $comment = $old_comment;
  }
  else {

    // Set some default properties.
    $defaults = array(
      'language' => $this->language,
      'node_type' => $this->bundle,
      'subject' => '',
      'status' => COMMENT_PUBLISHED,
      'uid' => 0,
      'cid' => 0,
      'pid' => 0,
    );
    foreach ($defaults as $field => $value) {
      if (!isset($comment->{$field})) {
        $comment->{$field} = $value;
      }
    }
    $this
      ->prepare($comment, $row);

    // Make sure we have a nid
    if (!isset($comment->nid) || !$comment->nid) {
      throw new MigrateException(t('No node ID provided for comment'));
    }

    // comment_save() hardcodes hostname, so if we're trying to set it we
    // need to save it and apply it after
    if (isset($comment->hostname)) {
      $hostname = $comment->hostname;
    }
  }
  if (isset($comment->cid) && $comment->cid) {
    $updating = TRUE;
  }
  else {
    $updating = FALSE;
  }

  // Validate field data prior to saving.
  MigrateDestinationEntity::fieldAttachValidate('comment', $comment);
  migrate_instrument_start('comment_save');
  comment_save($comment);
  migrate_instrument_stop('comment_save');
  if (isset($hostname) && isset($comment->cid) && $comment->cid > 0) {
    db_update('comment')
      ->fields(array(
      'hostname' => $hostname,
    ))
      ->condition('cid', $comment->cid)
      ->execute();
  }
  $this
    ->complete($comment, $row);
  if (isset($comment->cid) && $comment->cid > 0) {
    $return = array(
      $comment->cid,
    );
    if ($updating) {
      $this->numUpdated++;
    }
    else {
      $this->numCreated++;
    }
  }
  else {
    $return = FALSE;
  }
  return $return;
}