You are here

class MigrateDestinationComment in Migrate 6.2

Same name and namespace in other branches
  1. 7.2 plugins/destinations/comment.inc \MigrateDestinationComment

Destination class implementing migration into comments.

Hierarchy

Expanded class hierarchy of MigrateDestinationComment

File

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

View source
class MigrateDestinationComment extends MigrateDestinationEntity {
  public static function getKeySchema() {
    return array(
      'cid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'description' => 'ID of destination entity',
      ),
    );
  }

  /**
   * Return an options array for comment destinations.
   *
   * @param string $language
   *  Default language for comments created via this destination class.
   * @param string $text_format
   *  Default text format for comments created via this destination class.
   */
  public static function options($language, $text_format) {
    return compact('language', 'text_format');
  }

  /**
   * Basic initialization
   *
   * @param string $bundle
   *  A.k.a. the content type (page, article, etc.) of the ... comment?.
   * @param array $options
   *  Options applied to comments.
   */
  public function __construct($bundle, array $options = array()) {
    parent::__construct('comment', $bundle, $options);
  }

  /**
   * Returns a list of fields available to be mapped for comments attached to
   * a particular bundle (node type)
   *
   * @return array
   *  Keys: machine names of the fields (to be passed to addFieldMapping)
   *  Values: Human-friendly descriptions of the fields.
   */
  public function fields() {
    $fields = array();

    // First the core (comment table) properties
    $fields['cid'] = t('Comment: <a href="@doc">Existing comment ID</a>', array(
      '@doc' => 'http://drupal.org/node/1349714#cid',
    ));
    $fields['nid'] = t('Comment: <a href="@doc">Node (by Drupal ID)</a>', array(
      '@doc' => 'http://drupal.org/node/1349714#nid',
    ));
    $fields['uid'] = t('Comment: <a href="@doc">User (by Drupal ID)</a>', array(
      '@doc' => 'http://drupal.org/node/1349714#uid',
    ));
    $fields['pid'] = t('Comment: <a href="@doc">Parent (by Drupal ID)</a>', array(
      '@doc' => 'http://drupal.org/node/1349714#pid',
    ));
    $fields['subject'] = t('Comment: <a href="@doc">Subject</a>', array(
      '@doc' => 'http://drupal.org/node/1349714#subject',
    ));
    $fields['comment'] = t('Comment: <a href="@doc">Body</a>', array(
      '@doc' => 'http://drupal.org/node/1349714#comment',
    ));
    $fields['timestamp'] = t('Comment: <a href="@doc">Modified timestamp</a>', array(
      '@doc' => 'http://drupal.org/node/1349714#changed',
    ));
    $fields['status'] = t('Comment: <a href="@doc">Status</a>', array(
      '@doc' => 'http://drupal.org/node/1349714#status',
    ));
    $fields['hostname'] = t('Comment: <a href="@doc">Hostname/IP address</a>', array(
      '@doc' => 'http://drupal.org/node/1349714#hostname',
    ));
    $fields['name'] = t('Comment: <a href="@doc">User name (not username)</a>', array(
      '@doc' => 'http://drupal.org/node/1349714#name',
    ));
    $fields['mail'] = t('Comment: <a href="@doc">Email address</a>', array(
      '@doc' => 'http://drupal.org/node/1349714#mail',
    ));
    $fields['homepage'] = t('Comment: <a href="@doc">Homepage</a>', array(
      '@doc' => 'http://drupal.org/node/1349714#homepage',
    ));
    $fields['language'] = t('Comment: <a href="@doc">Language</a>', array(
      '@doc' => 'http://drupal.org/node/1349714#language',
    ));
    $fields['thread'] = t('Comment: <a href="@doc">Thread</a>', array(
      '@doc' => 'http://drupal.org/node/1349714#thread',
    ));

    // Then add in anything provided by handlers
    $fields += migrate_handler_invoke_all('Comment', 'fields', $this->entityType, $this->bundle);
    return $fields;
  }

  /**
   * Delete a comment
   *
   * @param $cid
   *  Array of comment ID fieldss to be deleted.
   */
  public function rollback(array $cid) {
    $path = drupal_get_path('module', 'comment') . '/comment.admin.inc';
    include_once $path;

    // Backdoor deletion - query stolen from comment_delete()
    $form = array();
    $form_state = array();
    $comment = db_fetch_object(db_query('SELECT c.*, u.name AS registered_name, u.uid
       FROM {comments} c
       LEFT JOIN {users} u ON u.uid = c.uid
       WHERE c.cid = %d', reset($cid)));

    // May have been deleted as the child of a comment previously deleted
    if ($comment) {
      $this
        ->prepareRollback($cid);
      $form['#comment'] = $comment;
      comment_confirm_delete_submit($form, $form_state);
      $this
        ->completeRollback($cid);
    }
  }

  /**
   * Import a single comment.
   *
   * @param $comment
   *  Comment object to build. Prefilled with any fields mapped in the Migration.
   * @param $row
   *  Raw source data object - passed through to prepare/complete handlers.
   * @return array
   *  Array of key fields (cid only in this case) of the comment that was saved if
   *  successful. FALSE on failure.
   */
  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->timestamp)) {
      $comment->timestamp = MigrationBase::timestamp($comment->timestamp);
    }
    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;
      }
      $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,
        'subject' => '',
        'status' => COMMENT_PUBLISHED,
        'uid' => 0,
        'cid' => 0,
        'pid' => 0,
        'format' => filter_fallback_format(),
      );
      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;
      }
    }

    // Convert to unix timestamp as needed
    if (isset($comment->timestamp)) {
      $comment->timestamp = MigrationBase::timestamp($comment->timestamp);
    }
    if (isset($comment->cid) && $comment->cid) {
      $updating = TRUE;
    }
    else {
      $updating = FALSE;
    }
    migrate_instrument_start('comment_save');
    $comment = (array) $comment;
    $comment['cid'] = comment_save($comment);
    $comment = (object) $comment;
    migrate_instrument_stop('comment_save');
    if (isset($hostname) && isset($comment->cid) && $comment->cid > 0) {
      db_update('comments')
        ->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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MigrateDestination::$numCreated protected property Maintain stats on the number of destination objects created or updated.
MigrateDestination::$numUpdated protected property
MigrateDestination::getCreated public function
MigrateDestination::getUpdated public function
MigrateDestination::resetStats public function Reset numCreated and numUpdated back to 0.
MigrateDestinationComment::fields public function Returns a list of fields available to be mapped for comments attached to a particular bundle (node type) Overrides MigrateDestination::fields
MigrateDestinationComment::getKeySchema public static function
MigrateDestinationComment::import public function Import a single comment. Overrides MigrateDestination::import
MigrateDestinationComment::options public static function Return an options array for comment destinations.
MigrateDestinationComment::rollback public function Delete a comment
MigrateDestinationComment::__construct public function Basic initialization Overrides MigrateDestinationEntity::__construct
MigrateDestinationEntity::$bundle protected property The bundle (node type, vocabulary, etc.) of the destination.
MigrateDestinationEntity::$entityType protected property The entity type (node, user, taxonomy_term, etc.) of the destination.
MigrateDestinationEntity::$language protected property Default language for text fields in this destination.
MigrateDestinationEntity::$textFormat protected property Default input format for text fields in this destination.
MigrateDestinationEntity::complete public function Give handlers a shot at modifying the object (or taking additional action) after saving it.
MigrateDestinationEntity::completeRollback public function Give handlers a shot at cleaning up after an entity has been rolled back.
MigrateDestinationEntity::getBundle public function
MigrateDestinationEntity::getEntityType public function
MigrateDestinationEntity::getLanguage public function
MigrateDestinationEntity::getTextFormat public function
MigrateDestinationEntity::prepare public function Give handlers a shot at modifying the object before saving it.
MigrateDestinationEntity::prepareRollback public function Give handlers a shot at cleaning up before an entity has been rolled back.
MigrateDestinationEntity::__toString public function Derived classes must implement __toString(). Overrides MigrateDestination::__toString