Skip to:
Content

bbPress.org

Changeset 7226


Ignore:
Timestamp:
11/29/2021 03:08:21 PM (3 years ago)
Author:
johnjamesjacoby
Message:

Akismet: improvements to clean-up routines, based on user feedback.

  • Use correct ID column for the posts database table
  • Use correct post_id column for the postmeta database table
  • Reduce row limit to 1000 from 100000 to avoid lengthy table locks in active forums
  • Remove usage of constant, that may be phased out eventually
  • Update related code docs
  • Introduce helper methods for applying dynamically named filters

In trunk for 2.7. See #3395.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/includes/extend/akismet.php

    r7206 r7226  
    967967
    968968    /**
    969      * Deletes old spam topics & replies from the queue after 15 days
    970      * (determined by `_bbp_akismet_delete_spam_interval` filter).
    971      *
    972      * @since 2.6.7 bbPress (r7203)
    973      *
    974      * @global wpdb $wpdb
    975      */
    976     public function delete_old_spam() {
    977         global $wpdb;
     969     * Get the number of rows to delete in a single clean-up query.
     970     *
     971     * @since 2.6.9 bbPress (r7225)
     972     *
     973     * @param string $filter The name of the filter to run.
     974     * @return int
     975     */
     976    public function get_delete_limit( $filter = '' ) {
     977
     978        // Default filter
     979        if ( empty( $filter ) ) {
     980            $filter = '_bbp_akismet_delete_spam_limit';
     981        }
    978982
    979983        /**
    980          * Determines how many posts will be deleted in each batch.
     984         * Determines how many rows will be deleted in each batch.
    981985         *
    982          * @param int The default as defined by AKISMET_DELETE_LIMIT (also used
    983          *            in Akismet WordPress plugin).
     986         * @param int The number of rows. Default 1000.
    984987         */
    985         $delete_limit = (int) apply_filters( '_bbp_akismet_delete_spam_limit',
    986             defined( 'AKISMET_DELETE_LIMIT' )
    987                 ? AKISMET_DELETE_LIMIT
    988                 : 10000
    989         );
    990 
    991         // Validate the deletion limit
    992         $delete_limit = max( 1, intval( $delete_limit ) );
     988        $delete_limit = (int) apply_filters( $filter, 1000 );
     989
     990        // Validate and return the deletion limit
     991        return max( 1, $delete_limit );
     992    }
     993
     994    /**
     995     * Get the interval (in days) for spam to remain in the queue.
     996     *
     997     * @since 2.6.9 bbPress (r7225)
     998     *
     999     * @param string $filter The name of the filter to run.
     1000     * @return int
     1001     */
     1002    public function get_delete_interval( $filter = '' ) {
     1003
     1004        // Default filter
     1005        if ( empty( $filter ) ) {
     1006            $filter = '_bbp_akismet_delete_spam_interval';
     1007        }
    9931008
    9941009        /**
     
    9961011         * queue before being deleted.
    9971012         *
    998          * @param int The default number of days.
     1013         * @param int The number of days. Default 15.
    9991014         */
    1000         $delete_interval = (int) apply_filters( '_bbp_akismet_delete_spam_interval', 15 );
    1001 
    1002         // Validate the deletion interval
    1003         $delete_interval = max( 1, intval( $delete_interval ) );
     1015        $delete_interval = (int) apply_filters( $filter, 15 );
     1016
     1017        // Validate and return the deletion interval
     1018        return max( 1, $delete_interval );
     1019    }
     1020
     1021    /**
     1022     * Deletes old spam topics & replies from the queue after 15 days
     1023     * (determined by `_bbp_akismet_delete_spam_interval` filter)
     1024     * since they are not useful in the long term.
     1025     *
     1026     * @since 2.6.7 bbPress (r7203)
     1027     *
     1028     * @global wpdb $wpdb
     1029     */
     1030    public function delete_old_spam() {
     1031        global $wpdb;
     1032
     1033        // Get the deletion limit & interval
     1034        $delete_limit    = $this->get_delete_limit( '_bbp_akismet_delete_spam_limit' );
     1035        $delete_interval = $this->get_delete_interval( '_bbp_akismet_delete_spam_interval' );
    10041036
    10051037        // Setup the query
     
    10351067            // Prepared as strings since id is an unsigned BIGINT, and using %
    10361068            // will constrain the value to the maximum signed BIGINT.
    1037             $format_string = implode( ", ", array_fill( 0, count( $spam_ids ), '%s' ) );
     1069            $format_string = implode( ', ', array_fill( 0, count( $spam_ids ), '%s' ) );
    10381070
    10391071            // Run the delete queries
    1040             $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->posts} WHERE post_id IN ( " . $format_string . " )", $spam_ids ) );
    1041             $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->postmeta} WHERE post_id IN ( " . $format_string . " )", $spam_ids ) );
     1072            $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->posts} WHERE ID IN ( {$format_string} )", $spam_ids ) );
     1073            $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->postmeta} WHERE post_id IN ( {$format_string} )", $spam_ids ) );
    10421074
    10431075            // Clean the post cache for these topics & replies
     
    10691101
    10701102    /**
    1071      * Deletes `_bbp_akismet_as_submitted` meta keys after 15 days, since they
    1072      * are large and not useful in the long term.
     1103     * Deletes `_bbp_akismet_as_submitted` meta keys after 15 days
     1104     * (determined by `_bbp_akismet_delete_spam_meta_interval` filter)
     1105     * since they are large and not useful in the long term.
    10731106     *
    10741107     * @since 2.6.7 bbPress (r7203)
     
    10791112        global $wpdb;
    10801113
    1081         /**
    1082          * Determines how many days a piece of spam will be left in the Spam
    1083          * queue before being deleted.
    1084          *
    1085          * @param int The default number of days.
    1086          */
    1087         $interval = (int) apply_filters( '_bbp_akismet_delete_spam_meta_interval', 15 );
    1088 
    1089         // Validate the deletion interval
    1090         $interval = max( 1, intval( $interval ) );
     1114        // Get the deletion limit & interval
     1115        $delete_limit    = $this->get_delete_limit( '_bbp_akismet_delete_spam_meta_limit' );
     1116        $delete_interval = $this->get_delete_interval( '_bbp_akismet_delete_spam_meta_interval' );
    10911117
    10921118        // Setup the query
    1093         $sql = "SELECT m.post_id FROM {$wpdb->postmeta} as m INNER JOIN {$wpdb->posts} as p ON m.post_id = p.id WHERE m.meta_key = '_bbp_akismet_as_submitted' AND DATE_SUB(NOW(), INTERVAL %d DAY) > p.post_date_gmt LIMIT 10000";
     1119        $sql = "SELECT m.post_id FROM {$wpdb->postmeta} as m INNER JOIN {$wpdb->posts} as p ON m.post_id = p.ID WHERE m.meta_key = '_bbp_akismet_as_submitted' AND DATE_SUB(NOW(), INTERVAL %d DAY) > p.post_date_gmt LIMIT %d";
    10941120
    10951121        // Query loop of topic & reply IDs
    1096         while ( $spam_ids = $wpdb->get_col( $wpdb->prepare( $sql, $interval ) ) ) {
     1122        while ( $spam_ids = $wpdb->get_col( $wpdb->prepare( $sql, $delete_interval, $delete_limit ) ) ) {
    10971123
    10981124            // Exit loop if no spam IDs
     
    11351161
    11361162    /**
    1137      * Clears post meta that no longer has corresponding posts in the database.
     1163     * Clears post meta that no longer has corresponding posts in the database
     1164     * (determined by `_bbp_akismet_delete_spam_orphaned_limit` filter)
     1165     * since it is not useful in the long term.
    11381166     *
    11391167     * @since 2.6.7 bbPress (r7203)
     
    11441172        global $wpdb;
    11451173
     1174        // Get the deletion limit
     1175        $delete_limit = $this->get_delete_limit( '_bbp_akismet_delete_spam_orphaned_limit' );
     1176
     1177        // Default last meta ID
    11461178        $last_meta_id = 0;
    11471179
     
    11551187
    11561188        // Setup the query
    1157         $sql = "SELECT m.meta_id, m.post_id, m.meta_key FROM {$wpdb->postmeta} as m LEFT JOIN {$wpdb->posts} as p ON m.post_id = p.id WHERE p.id IS NULL AND m.meta_id > %d ORDER BY m.meta_id LIMIT 1000";
     1189        $sql = "SELECT m.meta_id, m.post_id, m.meta_key FROM {$wpdb->postmeta} as m LEFT JOIN {$wpdb->posts} as p ON m.post_id = p.ID WHERE p.ID IS NULL AND m.meta_id > %d ORDER BY m.meta_id LIMIT %d";
    11581190
    11591191        // Query loop of topic & reply IDs
    1160         while ( $spam_meta_results = $wpdb->get_results( $wpdb->prepare( $sql, $last_meta_id ) ) ) {
     1192        while ( $spam_meta_results = $wpdb->get_results( $wpdb->prepare( $sql, $last_meta_id, $delete_limit ) ) ) {
    11611193
    11621194            // Exit loop if no spam IDs
Note: See TracChangeset for help on using the changeset viewer.