You are here

protected function MigrateDestinationComment::enableStatistics in Migrate 7.2

Re-enable and recompute node statistics after an import or rollback operation.

2 calls to MigrateDestinationComment::enableStatistics()
MigrateDestinationComment::postImport in plugins/destinations/comment.inc
Implements MigrateDestination::postImport().
MigrateDestinationComment::postRollback in plugins/destinations/comment.inc
Implements MigrateDestination::postRollback().

File

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

Class

MigrateDestinationComment
Destination class implementing migration into comments.

Code

protected function enableStatistics() {

  // If originally enabled, re-enable and rebuild the stats
  if ($this->maintainNodeStatistics) {
    $GLOBALS['conf']['comment_maintain_node_statistics'] = TRUE;

    // Copied from devel_rebuild_node_comment_statistics
    // Empty table
    db_truncate('node_comment_statistics')
      ->execute();

    // DBTNG. IGNORE keyword is not compatible with Postgres. SQLite?
    switch (db_driver()) {
      case 'pgsql':

        // We still want to run this under Postgres. On the very rare occasion
        // when we have 2 comments on the same node with the same timestamp
        // we will lose data.
        $sql = "\n            INSERT INTO {node_comment_statistics} (nid, cid, last_comment_timestamp, last_comment_name, last_comment_uid, comment_count) (\n              SELECT c.nid, c.cid, c.created, c.name, c.uid, c2.comment_count\n              FROM {comment} c\n              JOIN (\n                SELECT c.nid, MAX(c.created) AS created, COUNT(*) AS comment_count\n                FROM {comment} c\n                WHERE status=:published\n                GROUP BY c.nid\n              ) AS c2 ON c.nid = c2.nid AND c.created=c2.created\n            )";
        break;
      default:
        $sql = "\n            INSERT IGNORE INTO {node_comment_statistics} (nid, cid, last_comment_timestamp, last_comment_name, last_comment_uid, comment_count) (\n              SELECT c.nid, c.cid, c.created, c.name, c.uid, c2.comment_count\n              FROM {comment} c\n              JOIN (\n                SELECT c.nid, MAX(c.created) AS created, COUNT(*) AS comment_count\n                FROM {comment} c\n                WHERE status=:published\n                GROUP BY c.nid\n              ) AS c2 ON c.nid = c2.nid AND c.created=c2.created\n            )";
    }
    try {
      db_query($sql, array(
        ':published' => COMMENT_PUBLISHED,
      ));
    } catch (Exception $e) {

      // Our edge case has been hit. A Postgres migration has likely just
      // lost data. Let the user know.
      Migration::displayMessage(t('Failed to update node comment statistics: !message', array(
        '!message' => $e
          ->getMessage(),
      )));
    }

    // Insert records into the node_comment_statistics for nodes that are missing.
    $query = db_select('node', 'n');
    $query
      ->leftJoin('node_comment_statistics', 'ncs', 'ncs.nid = n.nid');
    $query
      ->addField('n', 'created', 'last_comment_timestamp');
    $query
      ->addField('n', 'uid', 'last_comment_uid');
    $query
      ->addField('n', 'nid');
    $query
      ->addExpression('0', 'comment_count');
    $query
      ->addExpression('NULL', 'last_comment_name');
    $query
      ->isNull('ncs.comment_count');
    db_insert('node_comment_statistics')
      ->from($query)
      ->execute();
  }
}