You are here

comment_alter.install in Comment Alter 7

Same filename and directory in other branches
  1. 8 comment_alter.install

Un/install functions for Comment alter.

File

comment_alter.install
View source
<?php

/**
 * @file
 * Un/install functions for Comment alter.
 */

/**
 * Implements hook_schema().
 */
function comment_alter_schema() {
  $schema['comment_alter'] = array(
    'description' => 'Stores {node}.vid changes made by comments.',
    'fields' => array(
      'cid' => array(
        'description' => 'The {comment}.cid this change refers to.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'old_vid' => array(
        'description' => 'The old {node}.vid this change refers to.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'new_vid' => array(
        'description' => 'The new {node}.vid this change refers to.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'indexes' => array(
      'new_vid' => array(
        'new_vid',
      ),
      'old_vid' => array(
        'old_vid',
      ),
    ),
    'primary key' => array(
      'cid',
    ),
  );
  return $schema;
}

/**
 * Implements hook_uninstall().
 */
function comment_alter_uninstall() {
  $node_types = array_keys(node_type_get_types());
  foreach ($node_types as $node_type) {
    variable_del('comment_alter_' . $node_type);
    variable_del('comment_alter_diff_link_' . $node_type);
    variable_del('comment_alter_root_only_' . $node_type);
  }
}

/**
 * Remove {comment_alter} entries referencing deleted node revisions.
 */
function comment_alter_update_7000(&$sandbox) {

  // Prepare the batch process.
  if (!isset($sandbox['progress'])) {
    $sandbox['progress'] = 0;
    $sandbox['current_cid'] = 0;
    $query = db_select('comment_alter', 'ca');
    $old_revisions = $query
      ->leftJoin('node_revision', 'nr_old', 'ca.old_vid = %alias.vid');
    $new_revisions = $query
      ->leftJoin('node_revision', 'nr_new', 'ca.new_vid = %alias.vid');
    $sandbox['count'] = $query
      ->condition(db_or()
      ->isNull($old_revisions . '.nid')
      ->isNull($new_revisions . '.nid'))
      ->countQuery()
      ->execute()
      ->fetchField();
  }

  // Handle 25 orphaned entries in a go.
  $query = db_select('comment_alter', 'ca')
    ->fields('ca', array(
    'cid',
  ));
  $old_revisions = $query
    ->leftJoin('node_revision', 'nr_old', 'ca.old_vid = %alias.vid');
  $new_revisions = $query
    ->leftJoin('node_revision', 'nr_new', 'ca.new_vid = %alias.vid');
  $result = $query
    ->condition(db_or()
    ->isNull($old_revisions . '.nid')
    ->isNull($new_revisions . '.nid'))
    ->condition('cid', $sandbox['current_cid'], '>')
    ->range(0, 25)
    ->orderBy('cid', 'ASC')
    ->execute();
  $cids = array();
  foreach ($result as $comment) {
    $cids[$comment->cid] = $comment->cid;
    $sandbox['progress']++;
    $sandbox['current_cid'] = $comment->cid;
  }

  // Only run a DELETE query if there are entries to remove.
  if ($cids) {
    db_delete('comment_alter')
      ->condition('cid', $cids, 'IN')
      ->execute();
  }
  $sandbox['#finished'] = empty($sandbox['count']) ? 1 : $sandbox['progress'] / $sandbox['count'];
}

Functions

Namesort descending Description
comment_alter_schema Implements hook_schema().
comment_alter_uninstall Implements hook_uninstall().
comment_alter_update_7000 Remove {comment_alter} entries referencing deleted node revisions.