Skip to content

Instantly share code, notes, and snippets.

@amr
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amr/cc76bbfbac380996965f to your computer and use it in GitHub Desktop.
Save amr/cc76bbfbac380996965f to your computer and use it in GitHub Desktop.
Drupal 6 module to delete old unapproved comments.
name = Clean Comments
description = "Remove old unpublished comments"
core = 6.x
dependencies[] = comment
<?php
define('DEFAULT_CLEAN_COMMENTS_SIZE', '1000');
define('DEFAULT_CLEAN_COMMENTS_MIN_DAYS', '30');
/**
* Implements hook_menu().
*/
function clean_comments_menu() {
$items = array();
$items['admin/settings/clean_comments'] = array(
'title' => 'Clean comments',
'description' => 'Configure comments cleaning',
'page callback' => 'drupal_get_form',
'page arguments' => array('clean_comments_admin_settings'),
'access arguments' => array('administer clean comments'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Form builder; Global settings form.
*/
function clean_comments_admin_settings(&$form_state) {
$form['clean_comments_size'] = array(
'#type' => 'textfield',
'#title' => t("Number of comments to clean per cron run"),
'#default_value' => variable_get('clean_comments_size', DEFAULT_CLEAN_COMMENTS_SIZE),
);
$form['clean_comments_min_days'] = array(
'#type' => 'textfield',
'#title' => t("Comment minimum age"),
'#description' => t('Only delete unpublished comments older than this number of days'),
'#default_value' => variable_get('clean_comments_size', DEFAULT_CLEAN_COMMENTS_MIN_DAYS),
);
return system_settings_form($form);
}
/**
* Implementation of hook_cron().
*/
function clean_comments_cron() {
module_load_include('inc', 'comment', 'comment.admin');
// Find out the comment ids of the comments that are unpublished
// and are older than X days.
$min_age = strtotime('today - ' . variable_get('clean_comments_min_days', DEFAULT_CLEAN_COMMENTS_MIN_DAYS) . 'days');
$result = db_query('SELECT c.*, u.name AS registered_name FROM {comments} c INNER JOIN {users} u USING(uid) WHERE c.status = 0 AND c.timestamp < %d LIMIT %d',
$min_age, variable_get('clean_comments_size', DEFAULT_CLEAN_COMMENTS_SIZE));
if ($result) {
while (($comment = db_fetch_object($result)) !== false) {
$comment->name = $comment->uid ? $comment->registered_name : $comment->name;
_comment_delete_thread($comment);
_comment_update_node_statistics($comment->nid);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment