format_date(time(), 'custom', 'H:i:s')));
// Expired content spam that we have to remove from each content repository.
$expired_content_spam = array('nids'=>array(), 'cids'=>array());
// Spam marks that we have to remove from the 'spam marks' table.
$obsolete_spam_marks = array('nids'=>array(), 'cids'=>array());
// Retrieve the list of expired content spam, based on the age specified in the settings panel.
$expire_spam_age = variable_get('akismet_remove_spam_age', 259200);
if ($expire_spam_age > 0) {
$result = db_result(db_query('SELECT content_type, content_id FROM {akismet_spam_marks} WHERE spam_created < %d', time() - $expire_spam_age));
while ($s = db_fetch_object($result)) {
$key = ($s->content_type == 'node' ? 'nids' : 'cids');
$expired_content_spam[$key][] = $s->content_id;
$obsolete_spam_marks[$key][] = $s->content_id;
}
}
// Deal with possible spam marks for content that have already been removed from database.
// Note: when Drupal deletes a node, all its comments are deleted, but no hook is invoked,
// so that may lead to orphans in the 'spam marks' table.
// This is why this cron task is being more complex that it could really be. Anyway, these
// queries shouldn't be too heavy.
$result = db_result(db_query('SELECT s.content_id FROM {akismet_spam_marks} s LEFT JOIN {node} n ON s.content_id = n.nid WHERE s.content_type = \'node\' AND n.nid IS NULL'));
while ($s = db_fetch_object($result)) {
if (!in_array($s->content_id, $obsolete_spam_marks['nids'])) {
$obsolete_spam_marks['nids'][] = $s->content_id;
}
}
$result = db_result(db_query('SELECT s.content_id FROM {akismet_spam_marks} s LEFT JOIN {comments} c ON s.content_id = c.cid WHERE s.content_type = \'comment\' AND c.cid IS NULL'));
while ($s = db_fetch_object($result)) {
if (!in_array($s->content_id, $obsolete_spam_marks['cids'])) {
$obsolete_spam_marks['cids'][] = $s->content_id;
}
}
// From this point on is where we really will delete stuff from database.
// Drupal cache will need to be cleared so anonymous users get updated views.
$clear_cache = FALSE;
// Remove expired spam from each content repository.
$expired_nids_removed = count($expired_content_spam['nids']);
$expired_cids_removed = count($expired_content_spam['cids']);
if ($expired_nids_removed > 0) {
$deleted_items = array();
$delete_count = 0;
foreach ($expired_content_spam['nids'] as $nid) {
if (akismet_content_delete('node', $nid)) {
$deleted_items[] = $nid;
$delete_count++;
}
}
if ($delete_count > 0) {
$message = t('Akismet housekeeping') .': '. format_plural($delete_count, '1 expired spam node removed from database', '%count expired spam nodes removed from database').
'
'.t('Node ID List: %nids', array('%nids' => implode(',', $deleted_items)));
watchdog('cron', $message);
$clear_cache = TRUE;
}
}
if ($expired_cids_removed > 0) {
$deleted_items = array();
$delete_count = 0;
foreach ($expired_content_spam['cids'] as $cid) {
if (akismet_content_delete('comment', $cid)) {
$deleted_items[] = $cid;
$delete_count++;
}
}
if ($delete_count > 0) {
$message = t('Akismet housekeeping') .': '. format_plural($delete_count, '1 expired spam comment removed from database', '%count expired spam comments removed from database').
'
'.t('Comment ID List: %cids', array('%cids' => implode(',', $deleted_items)));
watchdog('cron', $message);
$clear_cache = TRUE;
}
}
// Remove obsolete spam marks from database.
$spam_nids_removed = count($obsolete_spam_marks['nids']);
$spam_cids_removed = count($obsolete_spam_marks['cids']);
$spam_marks_removed = $spam_nids_removed + $spam_cids_removed;
if ($spam_nids_removed > 0) {
$spam_nids_list = implode(',', $obsolete_spam_marks['nids']);
db_query('DELETE FROM {akismet_spam_marks} WHERE content_type = \'node\' AND content_id IN (%s)', $spam_nids_list);
}
if ($spam_cids_removed > 0) {
$spam_cids_list = implode(',', $obsolete_spam_marks['cids']);
db_query('DELETE FROM {akismet_spam_marks} WHERE content_type = \'comment\' AND content_id IN (%s)', $spam_cids_list);
}
if ($spam_marks_removed > 0) {
$message = t('Akismet housekeeping') .': '. format_plural($spam_marks_removed, '1 spam mark removed from database', '%count spam marks removed from database');
if (isset($spam_nids_list)) {
$message .= '
'. t('Node ID List: %nids', array('%nids' => $spam_nids_list));
}
if (isset($spam_cids_list)) {
$message .= '
'. t('Comment ID List: %cids', array('%cids' => $spam_cids_list));
}
watchdog('cron', $message);
$clear_cache = TRUE;
}
// If anything was removed, then clear Drupal cache.
if ($clear_cache) {
akismet_clear_cache();
}
watchdog('cron', 'Akismet cron completed at %time.', array('%time' => format_date(time(), 'custom', 'H:i:s')));
}