| 1 |
<?php
|
| 2 |
// $Id: akismet_cron.inc,v 1.4 2007/06/01 21:41:17 drewish Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Shutdown function executed at cron time.
|
| 6 |
*/
|
| 7 |
function akismet_cron_shutdown() {
|
| 8 |
watchdog('cron', 'Akismet cron started at %time.', array('%time' => format_date(time(), 'custom', 'H:i:s')));
|
| 9 |
|
| 10 |
// Expired content spam that we have to remove from each content repository.
|
| 11 |
$expired_content_spam = array('nids'=>array(), 'cids'=>array());
|
| 12 |
// Spam marks that we have to remove from the 'spam marks' table.
|
| 13 |
$obsolete_spam_marks = array('nids'=>array(), 'cids'=>array());
|
| 14 |
|
| 15 |
// Retrieve the list of expired content spam, based on the age specified in the settings panel.
|
| 16 |
$expire_spam_age = variable_get('akismet_remove_spam_age', 259200);
|
| 17 |
if ($expire_spam_age > 0) {
|
| 18 |
$result = db_result(db_query('SELECT content_type, content_id FROM {akismet_spam_marks} WHERE spam_created < %d', time() - $expire_spam_age));
|
| 19 |
while ($s = db_fetch_object($result)) {
|
| 20 |
$key = ($s->content_type == 'node' ? 'nids' : 'cids');
|
| 21 |
$expired_content_spam[$key][] = $s->content_id;
|
| 22 |
$obsolete_spam_marks[$key][] = $s->content_id;
|
| 23 |
}
|
| 24 |
}
|
| 25 |
|
| 26 |
// Deal with possible spam marks for content that have already been removed from database.
|
| 27 |
// Note: when Drupal deletes a node, all its comments are deleted, but no hook is invoked,
|
| 28 |
// so that may lead to orphans in the 'spam marks' table.
|
| 29 |
// This is why this cron task is being more complex that it could really be. Anyway, these
|
| 30 |
// queries shouldn't be too heavy.
|
| 31 |
$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'));
|
| 32 |
while ($s = db_fetch_object($result)) {
|
| 33 |
if (!in_array($s->content_id, $obsolete_spam_marks['nids'])) {
|
| 34 |
$obsolete_spam_marks['nids'][] = $s->content_id;
|
| 35 |
}
|
| 36 |
}
|
| 37 |
$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'));
|
| 38 |
while ($s = db_fetch_object($result)) {
|
| 39 |
if (!in_array($s->content_id, $obsolete_spam_marks['cids'])) {
|
| 40 |
$obsolete_spam_marks['cids'][] = $s->content_id;
|
| 41 |
}
|
| 42 |
}
|
| 43 |
|
| 44 |
// From this point on is where we really will delete stuff from database.
|
| 45 |
// Drupal cache will need to be cleared so anonymous users get updated views.
|
| 46 |
$clear_cache = FALSE;
|
| 47 |
|
| 48 |
// Remove expired spam from each content repository.
|
| 49 |
$expired_nids_removed = count($expired_content_spam['nids']);
|
| 50 |
$expired_cids_removed = count($expired_content_spam['cids']);
|
| 51 |
if ($expired_nids_removed > 0) {
|
| 52 |
$deleted_items = array();
|
| 53 |
$delete_count = 0;
|
| 54 |
foreach ($expired_content_spam['nids'] as $nid) {
|
| 55 |
if (akismet_content_delete('node', $nid)) {
|
| 56 |
$deleted_items[] = $nid;
|
| 57 |
$delete_count++;
|
| 58 |
}
|
| 59 |
}
|
| 60 |
if ($delete_count > 0) {
|
| 61 |
$message = t('Akismet housekeeping') .': '. format_plural($delete_count, '1 expired spam node removed from database', '%count expired spam nodes removed from database').
|
| 62 |
'<br />'.t('Node ID List: %nids', array('%nids' => implode(',', $deleted_items)));
|
| 63 |
watchdog('cron', $message);
|
| 64 |
$clear_cache = TRUE;
|
| 65 |
}
|
| 66 |
}
|
| 67 |
if ($expired_cids_removed > 0) {
|
| 68 |
$deleted_items = array();
|
| 69 |
$delete_count = 0;
|
| 70 |
foreach ($expired_content_spam['cids'] as $cid) {
|
| 71 |
if (akismet_content_delete('comment', $cid)) {
|
| 72 |
$deleted_items[] = $cid;
|
| 73 |
$delete_count++;
|
| 74 |
}
|
| 75 |
}
|
| 76 |
if ($delete_count > 0) {
|
| 77 |
$message = t('Akismet housekeeping') .': '. format_plural($delete_count, '1 expired spam comment removed from database', '%count expired spam comments removed from database').
|
| 78 |
'<br />'.t('Comment ID List: %cids', array('%cids' => implode(',', $deleted_items)));
|
| 79 |
watchdog('cron', $message);
|
| 80 |
$clear_cache = TRUE;
|
| 81 |
}
|
| 82 |
}
|
| 83 |
|
| 84 |
// Remove obsolete spam marks from database.
|
| 85 |
$spam_nids_removed = count($obsolete_spam_marks['nids']);
|
| 86 |
$spam_cids_removed = count($obsolete_spam_marks['cids']);
|
| 87 |
$spam_marks_removed = $spam_nids_removed + $spam_cids_removed;
|
| 88 |
if ($spam_nids_removed > 0) {
|
| 89 |
$spam_nids_list = implode(',', $obsolete_spam_marks['nids']);
|
| 90 |
db_query('DELETE FROM {akismet_spam_marks} WHERE content_type = \'node\' AND content_id IN (%s)', $spam_nids_list);
|
| 91 |
}
|
| 92 |
if ($spam_cids_removed > 0) {
|
| 93 |
$spam_cids_list = implode(',', $obsolete_spam_marks['cids']);
|
| 94 |
db_query('DELETE FROM {akismet_spam_marks} WHERE content_type = \'comment\' AND content_id IN (%s)', $spam_cids_list);
|
| 95 |
}
|
| 96 |
if ($spam_marks_removed > 0) {
|
| 97 |
$message = t('Akismet housekeeping') .': '. format_plural($spam_marks_removed, '1 spam mark removed from database', '%count spam marks removed from database');
|
| 98 |
if (isset($spam_nids_list)) {
|
| 99 |
$message .= '<br />'. t('Node ID List: %nids', array('%nids' => $spam_nids_list));
|
| 100 |
}
|
| 101 |
if (isset($spam_cids_list)) {
|
| 102 |
$message .= '<br />'. t('Comment ID List: %cids', array('%cids' => $spam_cids_list));
|
| 103 |
}
|
| 104 |
watchdog('cron', $message);
|
| 105 |
$clear_cache = TRUE;
|
| 106 |
}
|
| 107 |
|
| 108 |
// If anything was removed, then clear Drupal cache.
|
| 109 |
if ($clear_cache) {
|
| 110 |
akismet_clear_cache();
|
| 111 |
}
|
| 112 |
watchdog('cron', 'Akismet cron completed at %time.', array('%time' => format_date(time(), 'custom', 'H:i:s')));
|
| 113 |
}
|