You are here

function spambot_delete_nodes_and_comments in Spambot 7

Delete nodes and comments for user accounts which are found to be spammers.

Parameters

int $uid: User account ID.

string $action: Action to take with nodes and comments: 'unpublish_content', 'delete_content'.

2 calls to spambot_delete_nodes_and_comments()
spambot_cron in ./spambot.module
Implements hook_cron().
_spambot_user_spam_admin_form_submit_action in ./spambot.pages.inc
Take action under this user account.

File

./spambot.module, line 716
Main module file.

Code

function spambot_delete_nodes_and_comments($uid, $action) {
  if ($action) {
    $comments_enabled = module_exists('comment');

    // Prepare some data.
    $nodes = db_select('node')
      ->fields('node', array(
      'nid',
    ))
      ->condition('uid', $uid, '=')
      ->orderBy('nid')
      ->execute()
      ->fetchCol();
    $comments = array();
    if ($comments_enabled) {
      $comments = db_select('comment')
        ->fields('comment', array(
        'cid',
      ))
        ->condition('uid', $uid, '=')
        ->orderBy('cid')
        ->execute()
        ->fetchCol();
    }
    switch ($action) {
      case 'unpublish_content':

        // Unpublish nodes and content.
        if ($nodes) {
          module_load_include('inc', 'node', 'node.admin');
          node_mass_update($nodes, array(
            'status' => 0,
          ));
        }
        if ($comments) {
          db_update('comment')
            ->fields(array(
            'status' => COMMENT_NOT_PUBLISHED,
          ))
            ->condition('uid', $uid)
            ->execute();
        }
        drupal_set_message(t('Nodes and comments have been unpublished.'));
        break;
      case 'delete_content':

        // Delete nodes and content.
        node_delete_multiple($nodes);
        if ($comments) {
          comment_delete_multiple($comments);
        }
        drupal_set_message(t('Nodes and comments have been deleted.'));
        break;
    }
  }
}