[phpmix-cvs] drupal/modules/akismet akismet.module,1.6,1.7
Status: Pre-Alpha
Brought to you by:
markus_petrux
From: <php...@li...> - 2006-06-07 12:34:25
|
Update of /cvsroot/phpmix/drupal/modules/akismet In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv23478 Modified Files: akismet.module Log Message: Previous implementation of hook_cron was not really complete. Index: akismet.module =================================================================== RCS file: /cvsroot/phpmix/drupal/modules/akismet/akismet.module,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** akismet.module 6 Jun 2006 00:08:35 -0000 1.6 --- akismet.module 7 Jun 2006 12:33:44 -0000 1.7 *************** *** 35,39 **** break; case 'admin/help/akismet': ! $output = t('<p>The <a href="%akismet-module-home">akismet module</a> for <a href="%drupal">Drupal</a> allows you to use the <a href="%akismet">Akismet Service</a> to protect your site from being spammed.</p>', array( '%akismet-module-home' => 'http://www.phpmix.org/projects/drupal', --- 35,41 ---- break; case 'admin/help/akismet': ! case 'admin/settings/akismet': ! $output = '<p>'; ! $output .= t('<p>The <a href="%akismet-module-home">akismet module</a> for <a href="%drupal">Drupal</a> allows you to use the <a href="%akismet">Akismet Service</a> to protect your site from being spammed.</p>', array( '%akismet-module-home' => 'http://www.phpmix.org/projects/drupal', *************** *** 41,49 **** '%akismet' => 'http://akismet.com' )); break; case 'admin/help#akismet': $output = t('<p>In order to use the <a href="%akismet">Akismet Service</a>, you need a <a href="%wpapikey">WordPress.com API key</a>. If you don\'t have one already, you can get it by simply signing up for a free account at <a href="%wordpress-com">wordpress.com</a>. Please, consult the <a href="%akismet-faq">Akismet FAQ</a> for further information.</p> <p>The <em>akismet module</em> may automatically check for spam posted in content (nodes and/or comments) by any user, except node or comment administrators respectively. It is also possible, from the <a href="%access-control">access control</a> panel, to grant <em>%no-check-perm</em> permission to <em>user roles</em> of your choice.</p> ! <p>Content marked as <em>spam</em> is still saved into database so it can be reviewed by content administrators. There is <a href="%akismet-settings">an option</a> that allows you to specify how long this information will be kept in the database. <em>Spam</em> older than a specified interval will be automatically removed. Requires crontab.</p> <p>Automatic spam detection can be enabled or disabled by content type and/or comments. In addition to this, the <em>akismet module</em> makes it easy for <em>content administrators</em> to manually <em>publish</em>/<em>unpublish</em> content and <em>mark</em>/<em>unmark</em> content as spam, from links available at the bottom of content.</p> <p></p>', --- 43,55 ---- '%akismet' => 'http://akismet.com' )); + $output .= t('<p>Akismet has caught <strong>%count spam</strong> for you since <strong>%since</strong>.</p>', array('%count' => variable_get('akismet_counter_spam', 0), '%since' => akismet_get_counting_since())); + if ($section == 'admin/settings/akismet') { + $output .= theme('more_help_link', url('admin/help/akismet')); + } break; case 'admin/help#akismet': $output = t('<p>In order to use the <a href="%akismet">Akismet Service</a>, you need a <a href="%wpapikey">WordPress.com API key</a>. If you don\'t have one already, you can get it by simply signing up for a free account at <a href="%wordpress-com">wordpress.com</a>. Please, consult the <a href="%akismet-faq">Akismet FAQ</a> for further information.</p> <p>The <em>akismet module</em> may automatically check for spam posted in content (nodes and/or comments) by any user, except node or comment administrators respectively. It is also possible, from the <a href="%access-control">access control</a> panel, to grant <em>%no-check-perm</em> permission to <em>user roles</em> of your choice.</p> ! <p>Content marked as <em>spam</em> is still saved into database so it can be reviewed by content administrators. There is <a href="%akismet-settings">an option</a> that allows you to specify how long this information will be kept in the database. <em>Spam</em> older than a specified age will be automatically removed. Requires crontab.</p> <p>Automatic spam detection can be enabled or disabled by content type and/or comments. In addition to this, the <em>akismet module</em> makes it easy for <em>content administrators</em> to manually <em>publish</em>/<em>unpublish</em> content and <em>mark</em>/<em>unmark</em> content as spam, from links available at the bottom of content.</p> <p></p>', *************** *** 58,71 **** )); break; - case 'admin/settings/akismet': - $output = t('<p>The <a href="%akismet-module-home">akismet module</a> for <a href="%drupal">Drupal</a> allows you to use the <a href="%akismet">Akismet Service</a> to protect your site from being spammed.</p> - <p>This panel allows you to configure all <em>akismet module</em> options. For further information, please consult the <a href="%help-page">help page</a> of the module.</p>', - array( - '%akismet-module-home' => 'http://www.phpmix.org/projects/drupal', - '%drupal' => 'http://drupal.org', - '%akismet' => 'http://akismet.com', - '%help-page' => url('admin/help/akismet') - )); - break; } return $output; --- 64,67 ---- *************** *** 83,88 **** */ function akismet_cron() { ! // Remove spam older than... ! db_query('DELETE FROM {akismet_spam_marks} WHERE spam_created < %d', time() - variable_get('akismet_remove_spam_interval', 259200)); } --- 79,163 ---- */ function akismet_cron() { ! // 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_query('SELECT content_type, content_id FROM {akismet_spam_marks} WHERE spam_created < %d', time() - $expire_spam_age); ! if (db_num_rows($result)) { ! 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_query("SELECT s.content_id FROM {akismet_spam_marks} s LEFT JOIN {node} n ON s.content_type = 'node' AND s.content_id = n.nid WHERE n.nid IS NULL"); ! if (db_num_rows($result)) { ! 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_query("SELECT s.content_id FROM {akismet_spam_marks} s LEFT JOIN {comments} c ON s.content_type = 'comment' AND s.content_id = c.cid WHERE c.cid IS NULL"); ! if (db_num_rows($result)) { ! 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) { ! foreach ($expired_content_spam['nids'] as $nid) { ! akismet_content_delete('node', $nid); ! } ! watchdog('cron', t('Akismet housekeeping') .': '. format_plural($expired_nids_removed, '1 expired spam node removed from database', '%count expired spam nodes removed from database')); ! $clear_cache = TRUE; ! } ! if ($expired_cids_removed > 0) { ! foreach ($expired_content_spam['cids'] as $cid) { ! akismet_content_delete('comment', $cid); ! } ! watchdog('cron', t('Akismet housekeeping') .': '. format_plural($expired_cids_removed, '1 expired spam comment removed from database', '%count expired spam comments removed from database')); ! $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) { ! db_query("DELETE FROM {akismet_spam_marks} WHERE content_type = 'node' AND content_id IN (%s)", implode(',', $obsolete_spam_marks['nids'])); ! } ! if ($spam_cids_removed > 0) { ! db_query("DELETE FROM {akismet_spam_marks} WHERE content_type = 'comment' AND content_id IN (%s)", implode(',', $obsolete_spam_marks['cids'])); ! } ! if ($spam_marks_removed > 0) { ! watchdog('cron', t('Akismet housekeeping') .': '. format_plural($spam_marks_removed, '1 spam mark removed from database', '%count spam marks removed from database')); ! $clear_cache = TRUE; ! } ! ! // If anything was removed, then clear Drupal cache. ! if ($clear_cache) { ! akismet_clear_cache(); ! } } *************** *** 146,156 **** '#description' => t('This option allows you to specify the connection timeout in seconds that is used for real time Akismet connections.') ); ! $interval_options = drupal_map_assoc(array(0, 86400, 172800, 259200, 604800, 1209600, 2419200, 4838400, 9676800), 'format_interval'); ! $interval_options[0] = t('never'); ! $form['general']['akismet_remove_spam_interval'] = array( '#type' => 'select', '#title' => t('Remove spam older than'), ! '#default_value' => variable_get('akismet_remove_spam_interval', 259200), ! '#options' => $interval_options, ! '#description' => t('Content marked as <em>spam</em> is still saved into database so it can be reviewed by content administrators. This option allows you to specify how long this information will be kept in the database. <em>Spam</em> older than the interval specified here will be automatically removed. Requires crontab.') ); --- 221,237 ---- '#description' => t('This option allows you to specify the connection timeout in seconds that is used for real time Akismet connections.') ); ! $age_options = drupal_map_assoc(array(0, 3600, 10800, 21600, 32400, 43200, 86400, 172800, 259200, 604800, 1209600, 1814400), 'format_interval'); ! $age_options[0] = t('never'); ! $age_options[2592000] = t('1 month'); ! $age_options[5184000] = t('2 months'); ! $age_options[7776000] = t('3 months'); ! $age_options[10368000] = t('4 months'); ! $age_options[15768000] = t('6 months'); ! $age_options[31536000] = t('1 year'); ! $form['general']['akismet_remove_spam_age'] = array( '#type' => 'select', '#title' => t('Remove spam older than'), ! '#default_value' => variable_get('akismet_remove_spam_age', 259200), ! '#options' => $age_options, ! '#description' => t('Content marked as <em>spam</em> is still saved into database so it can be reviewed by content administrators. This option allows you to specify how long this information will be kept in the database. <em>Spam</em> older than the age specified here will be automatically removed. Requires crontab.') ); *************** *** 276,279 **** --- 357,379 ---- /** + * Format the 'Counting since' date. + * + * @return string Counting since date formatted according to module settings. + */ + function akismet_get_counting_since() { + $since = variable_get('akismet_counter_since', array('day' => date('j'), 'month' => date('n'), 'year' => date('Y'))); + $format = variable_get('akismet_counter_date_format', 'F j, Y'); + $replace = array( + 'd' => sprintf('%02d', $since['day']), + 'j' => $since['day'], + 'm' => sprintf('%02d', $since['month']), + 'M' => format_date(gmmktime(0, 0, 0, $since['month'], 2, 1970), 'custom', 'M', 0), + 'F' => format_date(gmmktime(0, 0, 0, $since['month'], 2, 1970), 'custom', 'F', 0), + 'Y' => $since['year'] + ); + return strtr($format, $replace); + } + + /** * Implementation of hook_menu(). */ *************** *** 471,475 **** drupal_set_message(t('Your %content-type-name has been queued for moderation by site administrators and will be published after approval.', array('%content-type-name' => $content_type_name))); ! // Add an entry to the watchdog log. if ($akismet_api_result == AKISMET_API_RESULT_ERROR) { watchdog('content', t('Akismet service seems to be down, %content-type-name queued for manual approval: %title', array('%content-type-name' => $content_type_name, '%title' => theme('placeholder', $node->title))), WATCHDOG_WARNING, l(t('view'), "node/$node->nid")); --- 571,575 ---- drupal_set_message(t('Your %content-type-name has been queued for moderation by site administrators and will be published after approval.', array('%content-type-name' => $content_type_name))); ! // Record the event to watchdog. if ($akismet_api_result == AKISMET_API_RESULT_ERROR) { watchdog('content', t('Akismet service seems to be down, %content-type-name queued for manual approval: %title', array('%content-type-name' => $content_type_name, '%title' => theme('placeholder', $node->title))), WATCHDOG_WARNING, l(t('view'), "node/$node->nid")); *************** *** 578,582 **** drupal_set_message(t('Your comment has been queued for moderation by site administrators and will be published after approval.')); ! // Add an entry to the watchdog log. if ($akismet_api_result == AKISMET_API_RESULT_ERROR) { watchdog('content', t('Akismet service seems to be down, comment queued for manual approval: %subject', array('%subject' => theme('placeholder', $comment->subject))), WATCHDOG_WARNING, l(t('view'), "node/$comment->nid", NULL, NULL, "comment-$comment->cid")); --- 678,682 ---- drupal_set_message(t('Your comment has been queued for moderation by site administrators and will be published after approval.')); ! // Record the event to watchdog. if ($akismet_api_result == AKISMET_API_RESULT_ERROR) { watchdog('content', t('Akismet service seems to be down, comment queued for manual approval: %subject', array('%subject' => theme('placeholder', $comment->subject))), WATCHDOG_WARNING, l(t('view'), "node/$comment->nid", NULL, NULL, "comment-$comment->cid")); *************** *** 637,641 **** else if ($content_type == 'comment') { $content = db_fetch_object(db_query(db_rewrite_sql('SELECT c.*, u.name AS registered_name FROM {comments} c INNER JOIN {users} u ON u.uid = c.uid WHERE c.cid = %d', 'c'), $content_id)); ! if ($content->uid) { $content->name = $content->registered_name; } --- 737,744 ---- else if ($content_type == 'comment') { $content = db_fetch_object(db_query(db_rewrite_sql('SELECT c.*, u.name AS registered_name FROM {comments} c INNER JOIN {users} u ON u.uid = c.uid WHERE c.cid = %d', 'c'), $content_id)); ! if (empty($content->cid)) { ! $content = FALSE; ! } ! else if ($content->uid) { $content->name = $content->registered_name; } *************** *** 648,651 **** --- 751,810 ---- /** + * Delete a node or a comment. + * + * @param string Content type; can be either 'node' or 'comment'. + * @param integer Content ID; can be either a nid or a cid. + */ + function akismet_content_delete($content_type, $content_id) { + if ($content_type == 'node') { + $node = akismet_content_load($content_type, $content_id); + if ($node) { + // Sadly, we cannot invoke node_delete() because it checks for the + // user to have delete permission on nodes, and this function may + // be invoked from a cron task, which is run as anonymous user, so + // we have to hardcode the low level parts here. + + // Code based on node.module::node_delete(). + + db_query('DELETE FROM {node} WHERE nid = %d', $node->nid); + db_query('DELETE FROM {node_revisions} WHERE nid = %d', $node->nid); + + // Call the node-specific callback (if any): + node_invoke($node, 'delete'); + node_invoke_nodeapi($node, 'delete'); + + // Remove this node from the search index if needed. + if (function_exists('search_wipe')) { + search_wipe($node->nid, 'node'); + } + + // Record the event to watchdog. + watchdog('content', t('%type: deleted %title.', array('%type' => theme('placeholder', t($node->type)), '%title' => theme('placeholder', $node->title)))); + } + } + else if ($content_type == 'comment') { + $comment = akismet_content_load($content_type, $content_id); + if ($comment) { + _comment_delete_thread($comment); + _comment_update_node_statistics($comment->cid); + } + } + } + + /** + * Clear Drupal cache. + * + * This function needs to be called so anonymous users get updated content + * when certain operations have been executed. + */ + function akismet_clear_cache() { + static $already_done = FALSE; + if (!$already_done) { + cache_clear_all(); + $already_done = TRUE; + } + } + + /** * Mark content as spam or remove the mark. * *************** *** 693,696 **** --- 852,856 ---- watchdog('content', t('%action: %title', array('%action' => $action, '%title' => theme('placeholder', $content_title))), WATCHDOG_NOTICE, $content_link); } + akismet_clear_cache(); } *************** *** 716,720 **** } } ! else if ($content_type == 'comment') { // This code snippet is based on comment.module::comment_admin_overview_submit() --- 876,880 ---- } } ! else { // comment // This code snippet is based on comment.module::comment_admin_overview_submit() *************** *** 740,743 **** --- 900,905 ---- } } + + akismet_clear_cache(); } |