Changeset 35682

Show
Ignore:
Timestamp:
03/21/08 11:25:50 (4 months ago)
Author:
ear1grey
Message:

1. Uses wp_cache extensively to reduce DB overhead.
2. The getCount functions are now based on analysis of cached DB results instead of running a separate DB query.
3. Total count is now correct and allows for overlap of different litmus tests.

Location:
worst-offenders/trunk
Files:
11 modified

Legend:

Unmodified
Added
Removed
  • worst-offenders/trunk/classes/all_litmus.php

    r35513 r35682  
    1919      class AllLitmus extends Litmus { 
    2020 
    21             function rollCall() { 
    22                   echo("<p>All Litmus</p>"); 
    23             } 
    24  
    25             function addIndex() { 
    26             } 
    27  
    2821            function getName() { 
    2922                  return "All"; 
     
    3427            } 
    3528 
    36             function addCount() { 
     29            public static function getMatches() { 
    3730            } 
     31             
     32            public static function tab() {       
     33                  global $wo3_title, $keys; 
    3834 
    39             function getMatches() { 
    40             } 
    41  
    42             public static function tab() {       
    43                   global $wo3_title; 
    4435                  parent::tab( 
    4536                        self::getName(), 
    46                         get_option( $keys['total_count'] ),  
     37                        wp_cache_get("current_count", $wo3_title), 
    4738                        self::isActive() 
    4839                  ); 
     
    7263                  } 
    7364            } 
     65 
    7466      } 
    7567} 
  • worst-offenders/trunk/classes/domain_litmus.php

    r35135 r35682  
    99*/ 
    1010 
    11 //drop function if exists test1; 
    12 //create FUNCTION address (url TEXT) 
    13 //RETURNS varchar(255) 
    14 //CONTAINS SQL DETERMINISTIC 
    15 //RETURN 
    16 //    SUBSTRING_INDEX( 
    17 //      TRIM( 
    18 //        LEADING "http://" FROM TRIM(url) 
    19 //      ), 
    20 //      "?", 
    21 //      1 
    22 //  ); 
    23 //SELECT address(" http://www.spoamtastic.com:80/blog/arse?123=456 ") AS subdomain; 
    24  
    25  
    2611if (class_exists('Litmus')) { 
     12      add_action('wo3_prep', array('DomainLitmus', 'getCount')); 
    2713      add_action('wo3_tabs', array('DomainLitmus', 'tab')); 
    2814      add_action('wo3_content', array('DomainLitmus', 'content')); 
    29       add_action('wo3_total_count', array('DomainLitmus', 'addCount')); 
    30       add_action('wo3_rollcall', array('DomainLitmus', 'rollCall')); 
    3115      add_action('wo3_add_index', array('DomainLitmus', 'addIndex')); 
    3216 
    3317      class DomainLitmus extends Litmus { 
    3418 
     19            function getName() { 
     20                  return "Domain"; 
     21            } 
     22 
     23            public static function getMatches() { 
     24                  global $keys; 
     25                  $lower_limit = get_option( $keys['ui_vis'] );  
     26                  return self::runCachedMatchesQuery( 
     27                        self::getName(), 
     28                        "select count(*) as num, address(comment_author_url) as comment_author_url_simplified, group_concat(comment_id separator ',') as comment_id_list from wp_comments where comment_approved='spam' and comment_author_url != '' group by comment_author_url_simplified having num >= $lower_limit order by num desc;" 
     29                  ); 
     30            } 
     31 
     32            function content() {     
     33                  if (DomainLitmus::isActive() || AllLitmus::isActive() ) { 
     34                        $comments = DomainLitmus::getMatches(); 
     35                        echo("<table>"); 
     36                        foreach($comments as $comment) { 
     37                              Litmus::wo3_show_row($comment->num, "comments from ".$comment->comment_author_url_simplified, $comment->comment_id_list); 
     38                        } 
     39                        echo("</table>"); 
     40                  } 
     41            } 
     42             
    3543            function addIndex() { 
    3644                  global $wpdb; 
     
    4351            } 
    4452 
    45             function rollCall() { 
    46                   echo("<p>Domain</p>"); 
     53 
     54            //============================================ 
     55            // common class methods for litmus tests   
     56            // (there's got to be a better way to do this! 
     57            //============================================ 
     58             
     59            function getCount() { 
     60                  global $wo3_title; 
     61                  self::getMatches(); 
     62                  return wp_cache_get(self::getName()."_count", $wo3_title); 
    4763            } 
    48  
    49             function getName() { 
    50                   return "Domain"; 
    51             } 
    52  
    53  
    54             function getCount() { 
    55                   global $wpdb, $keys; 
    56                   $lower_limit = get_option( $keys['ui_vis'] );  
    57                   $q = "select sum(x.num) from (select count(*) as num from wp_comments where comment_approved='spam' and address(comment_author_url) != '' group by address(comment_author_url) having num >= $lower_limit) as x;"; 
    58                   $num = $wpdb->get_var($q);                 
    59                   if ($num == null) return "0"; 
    60                   return $num; 
    61             } 
    62  
    63  
    64             function addCount() { 
    65                   global $keys; 
    66                   update_option( $keys['total_count'], DomainLitmus::getCount() + get_option( $keys['total_count'] ) ); 
    67             } 
    68  
    69  
    70             function getMatches() { 
    71                   global $wpdb, $keys; 
    72                   $lower_limit = get_option( $keys['ui_vis'] );  
    73                   $q= "select count(*) as num, address(comment_author_url) as comment_author_url_simplified, group_concat(comment_id separator ',') as comment_id_list from wp_comments where comment_approved='spam' and comment_author_url != '' group by comment_author_url_simplified having num >= $lower_limit order by num desc;"; 
    74                   return $wpdb->get_results($q); 
    75             } 
    76  
     64             
    7765            public static function tab() {       
    7866                  parent::tab(self::getName(), self::getCount(), self::isActive()); 
    7967            } 
    80  
    81  
     68             
    8269            function isActive() { 
    83                   return ($_GET['tab'] == DomainLitmus::getName() ) && parent::isActive(); 
     70                  return ($_GET['tab'] == self::getName() ) && parent::isActive(); 
    8471            } 
    8572 
    86  
    87             function content() {     
    88                   if (DomainLitmus::isActive() || AllLitmus::isActive() ) { 
    89                         $comments = DomainLitmus::getMatches(); 
    90                         echo("<table>"); 
    91                         foreach($comments as $comment) { 
    92                               Litmus::wo3_show_row($comment->num, "comments from ".$comment->comment_author_url_simplified, $comment->comment_id_list); 
    93                         } 
    94                         echo("</table>"); 
    95                   } 
    96  
    97  
    98             } 
    9973 
    10074      } 
  • worst-offenders/trunk/classes/email_litmus.php

    r35135 r35682  
    99*/ 
    1010 
    11 //drop function if exists test1; 
    12 //create FUNCTION address (url TEXT) 
    13 //RETURNS varchar(255) 
    14 //CONTAINS SQL DETERMINISTIC 
    15 //RETURN 
    16 //    SUBSTRING_INDEX( 
    17 //      TRIM( 
    18 //        LEADING "http://" FROM TRIM(url) 
    19 //      ), 
    20 //      "?", 
    21 //      1 
    22 //  ); 
    23 //SELECT address(" http://www.spoamtastic.com:80/blog/arse?123=456 ") AS subdomain; 
    24  
    25  
    2611if (class_exists('Litmus')) { 
     12      add_action('wo3_prep', array('EmailLitmus', 'getCount')); 
    2713      add_action('wo3_tabs', array('EmailLitmus', 'tab')); 
    2814      add_action('wo3_content', array('EmailLitmus', 'content')); 
    29       add_action('wo3_total_count', array('EmailLitmus', 'addCount')); 
    30       add_action('wo3_rollcall', array('EmailLitmus', 'rollCall')); 
    3115      add_action('wo3_add_index', array('EmailLitmus', 'addIndex')); 
    3216 
    3317      class EmailLitmus extends Litmus { 
    34  
    35             function addIndex() { 
    36                   global $wpdb; 
    37                   echo("<p>Adding index and fucntion for URL addresses.</p>"); 
    38                   $wpdb->query("ALTER TABLE 'wp_comments' ADD INDEX email_spotter'('comment_author_email');"); 
    39             } 
    40  
    41             function rollCall() { 
    42                   echo("<p>Email</p>"); 
    43             } 
    4418 
    4519            function getName() { 
     
    4721            } 
    4822 
    49             function getCount() { 
    50                   global $wpdb, $keys; 
     23            public static function getMatches() { 
     24                  global $keys; 
    5125                  $lower_limit = get_option( $keys['ui_vis'] );  
    52                   $q = "select sum(x.num) from (select count(*) as num from wp_comments where comment_approved='spam' and comment_author_email != '' group by comment_author_email having num >= $lower_limit) as x;"; 
    53                   $num = $wpdb->get_var($q);                 
    54                   if ($num == null) return "0"; 
    55                   return $num; 
    56             } 
    57  
    58             function addCount() { 
    59                   global $keys; 
    60                   update_option( $keys['total_count'], EmailLitmus::getCount() + get_option( $keys['total_count'] ) ); 
    61             } 
    62  
    63             function getMatches() { 
    64                   global $wpdb, $keys; 
    65                   $lower_limit = get_option( $keys['ui_vis'] );  
    66                   $q= "select count(*) as num, comment_author_email, group_concat(comment_id separator ',') as comment_id_list from wp_comments where comment_approved='spam' and comment_author_email != '' group by comment_author_email having num >= $lower_limit order by num desc;"; 
    67                   return $wpdb->get_results($q); 
    68             } 
    69  
    70             public static function tab() {       
    71                   parent::tab(self::getName(), self::getCount(), self::isActive()); 
    72             } 
    73  
    74  
    75             function isActive() { 
    76                   return ($_GET['tab'] == EmailLitmus::getName() ) && parent::isActive(); 
     26                  return self::runCachedMatchesQuery( 
     27                        self::getName(), 
     28                        "select count(*) as num, comment_author_email, group_concat(comment_id separator ',') as comment_id_list from wp_comments where comment_approved='spam' and comment_author_email != '' group by comment_author_email having num >= $lower_limit order by num desc;" 
     29                  ); 
    7730            } 
    7831 
    7932            function content() {     
    80  
    8133                  if ( EmailLitmus::isActive() || AllLitmus::isActive() ) { 
    8234                        $comments = EmailLitmus::getMatches(); 
     
    8739                        echo("</table>"); 
    8840                  } 
     41            } 
    8942 
     43            function addIndex() { 
     44                  global $wpdb; 
     45                  echo("<p>Adding index and fucntion for URL addresses.</p>"); 
     46                  $wpdb->query("ALTER TABLE 'wp_comments' ADD INDEX email_spotter'('comment_author_email');"); 
     47            } 
    9048 
     49            //============================================ 
     50            // common class methods for litmus tests   
     51            // (there's got to be a better way to do this! 
     52            //============================================ 
     53             
     54            function getCount() { 
     55                  global $wo3_title; 
     56                  self::getMatches(); 
     57                  return wp_cache_get(self::getName()."_count", $wo3_title); 
    9158            } 
     59             
     60            public static function tab() {       
     61                  parent::tab(self::getName(), self::getCount(), self::isActive()); 
     62            } 
     63             
     64            function isActive() { 
     65                  return ($_GET['tab'] == self::getName() ) && parent::isActive(); 
     66            } 
     67 
    9268 
    9369      } 
  • worst-offenders/trunk/classes/ip_litmus.php

    r35135 r35682  
    99*/ 
    1010 
    11 // very useful 
    12 // ALTER TABLE `wpdb`.`wp_comments` ADD INDEX `ip_spotter`(`comment_author_IP`); 
    13  
    14 // this one was less useful... 
    15 // ALTER TABLE `wpdb`.`wp_comments` ADD INDEX `comment_approved_txt`(`comment_approved`); 
    16  
    1711if (class_exists('Litmus')) { 
     12      add_action('wo3_prep', array('IPLitmus', 'getCount')); 
    1813      add_action('wo3_tabs', array('IPLitmus', 'tab')); 
    1914      add_action('wo3_content', array('IPLitmus', 'content')); 
    20       add_action('wo3_total_count', array('IPLitmus', 'addCount')); 
    21       add_action('wo3_rollcall', array('IPLitmus', 'rollCall')); 
    22 //    add_action('wo3_add_index', array('IPLitmus', 'addIndex')); 
     15      add_action('wo3_add_index', array('IPLitmus', 'addIndex')); 
    2316 
    2417      class IPLitmus extends Litmus { 
    25  
    26             function addIndex() { 
    27 //                global $wpdb; 
    28 //                $q = "ALTER TABLE 'wp_comments' ADD INDEX 'ip_spotter'('comment_author_IP');"; 
    29 //                $x = $wpdb->query($q); 
    30 //                echo("<p>Adding index for IP addresses: $x.</p>"); 
    31             } 
    32  
    33             function rollCall() { 
    34                   echo("<p>IP Litmus</p>"); 
    35             } 
    36  
    3718 
    3819            function getName() { 
    3920                  return "IP"; 
    4021            } 
    41  
    42  
    43             function getCount() { 
    44                   global $wpdb, $keys; 
     22             
     23            public static function getMatches() { 
     24                  global $keys; 
    4525                  $lower_limit = get_option( $keys['ui_vis'] );  
    46                   $q = "select sum(x.num) from (select count(*) as num from wp_comments where comment_approved='spam' group by comment_author_ip having num >= $lower_limit) as x;"; 
    47                   $num = $wpdb->get_var($q);                 
    48                   if ($num == null) return "0"; 
    49                   return $num; 
     26                  return self::runCachedMatchesQuery( 
     27                        self::getName(), 
     28                        "select comment_author_ip, count(comment_id) as num, group_concat(comment_ID separator ',') as comment_id_list from wp_comments where comment_approved='spam' group by comment_author_ip having num >= $lower_limit order by num;"               
     29                  ); 
    5030            } 
    5131 
    52  
    53             function addCount() { 
    54                   global $keys; 
    55                   update_option( $keys['total_count'], IPLitmus::getCount() + get_option( $keys['total_count'] ) ); 
    56             } 
    57  
    58  
    59             function getMatches() { 
    60                   global $wpdb, $keys; 
    61                   $lower_limit = get_option( $keys['ui_vis'] );  
    62                   $q = 
    63 "select comment_author_ip, count(comment_id) as num, group_concat(comment_ID separator ',') as comment_id_list 
    64 from wp_comments 
    65 where comment_approved='spam' 
    66 group by comment_author_ip 
    67 having num >= $lower_limit 
    68 order by num;"; 
    69  
    70                   return $wpdb->get_results($q); 
    71             } 
    72  
    73             public static function tab() {       
    74                   parent::tab(self::getName(), self::getCount(), self::isActive()); 
    75             } 
    76  
    77             function isActive() { 
    78                   return ($_GET['tab'] == IPLitmus::getName() ) && parent::isActive(); 
    79             } 
    80  
    81  
    8232            function content() {     
    83  
    8433                  if (IPLitmus::isActive() || AllLitmus::isActive()) { 
    8534                        $comments = IPLitmus::getMatches(); 
     
    9039                        echo("</table>"); 
    9140                  } 
     41            } 
     42 
     43            function addIndex() { 
     44                  global $wpdb; 
     45                  $q = "ALTER TABLE 'wp_comments' ADD INDEX 'ip_spotter'('comment_author_IP');"; 
     46                  $x = $wpdb->query($q); 
     47                  echo("<p>Adding index for IP addresses: $x.</p>"); 
     48            } 
    9249 
    9350 
     51            //============================================ 
     52            // common class methods for litmus tests   
     53            // (there's got to be a better way to do this! 
     54            //============================================ 
     55             
     56            function getCount() { 
     57                  global $wo3_title; 
     58                  self::getMatches(); 
     59                  return wp_cache_get(self::getName()."_count", $wo3_title); 
     60            } 
     61             
     62            public static function tab() {       
     63                  parent::tab(self::getName(), self::getCount(), self::isActive()); 
     64            } 
     65             
     66            function isActive() { 
     67                  return ($_GET['tab'] == self::getName() ) && parent::isActive(); 
    9468            } 
    9569 
  • worst-offenders/trunk/classes/litmus.php

    r35135 r35682  
    22 
    33abstract class Litmus { 
    4        
    5       var $classname = null; 
    64       
    75      // Override this method to name your litmus test 
     
    119      // Override this method to count the current number of items matched by your litmus test 
    1210      abstract function getCount(); 
    13  
    14       // Add any indexes required by this Litmus Test 
    15       abstract function addIndex(); 
    1611       
    17       // Override this method to add the result of getcount to $wo2_total_cpount 
    18       abstract function addCount() ; 
    19        
    20       // Override this method to retrieve matches to your litmus test 
    21       abstract function getMatches(); 
     12      abstract static function getMatches() ; 
    2213 
    2314      // This method should only be overridden if you want your tab to look different 
     
    2819      } 
    2920 
     21      public static function runCachedMatchesQuery($name, $q) { 
     22            global $wpdb, $wo3_title; 
     23 
     24            echo("\n<!-- Query is: $q -->\n"); 
     25 
     26            $result = wp_cache_get($name, $wo3_title); 
     27            if($result == false) { 
     28                  $result= $wpdb->get_results($q); 
     29                  echo("<!-- cache miss $name -->\n"); 
     30                  echo("<!-- query gave ".sizeof($result)." results -->\n"); 
     31                  wp_cache_add($name, $result, $wo3_title); 
     32            } else { 
     33                  echo("<!-- cache hit $name -->\n"); 
     34            } 
     35 
     36            self::updateCount($name, $result); 
     37 
     38            return $result; 
     39      } 
     40 
     41 
     42      private static function updateCount($name, $results) { 
     43            global $wo3_title; 
     44            echo("<!-- updating count from ".sizeof($results)." results -->\n"); 
     45            $ids = array(); 
     46            foreach($results as $result) { 
     47                  if (isset($result->comment_id_list)) { 
     48                        $ids = array_merge($ids, explode(",",$result->comment_id_list)); 
     49                        echo("<!-- its a list of ".sizeof($ids)." -->\n"); 
     50                  } 
     51                  if (isset($result->comment_id)) { 
     52                        echo("<!-- its an id -->\n"); 
     53                        $ids[] = $result->comment_id; 
     54                  } 
     55            } 
     56            $ids = array_unique($ids); 
     57            wp_cache_set($name."_count", sizeof($ids), $wo3_title); 
     58            echo("<!-- ".sizeof($results)." results gave ".sizeof($ids)." distinct ids -->\n"); 
     59 
     60            $current_ids = wp_cache_get("current_ids", $wo3_title); 
     61            if ($current_ids == false) $current_ids = array(); 
     62            $current_ids = array_merge($current_ids, $ids); 
     63            $current_ids = array_unique($current_ids); 
     64            wp_cache_set("current_ids", $current_ids, $wo3_title); 
     65            wp_cache_set("current_count", sizeof($current_ids), $wo3_title); 
     66            echo("<!-- current_count is now ".sizeof($current_ids)." -->\n"); 
     67 
     68      } 
    3069 
    3170      function isActive() { 
  • worst-offenders/trunk/classes/md5_litmus.php

    r35135 r35682  
    99*/ 
    1010 
    11 // ALTER TABLE `wpdb`.`wp_comments` ENGINE = MyISAM ROW_FORMAT = DYNAMIC; 
    12 // ALTER TABLE `wpdb`.`wp_comments` ADD FULLTEXT INDEX `content_fulltext`(`comment_content`); 
    13  
    14  
    15 // SELECT 
    16 //    p.id, 
    17 //    COUNT(IF(MATCH (p.name) AGAINST ('the colour purple'),1,NULL) AS name_score 
    18 //FROM 
    19 //    Product as p USE INDEX (name), ProductCategory as pc, Category as c 
    20 //WHERE 
    21  
    22 //CREATE FUNCTION wordcount2 ( a text, b VARCHAR(255) ) 
    23 //  RETURNS INTEGER 
    24 // CONTAINS SQL DETERMINISTIC 
    25 //RETURN (CHAR_LENGTH(a)-CHAR_LENGTH(REPLACE(a, b, '')))/CHAR_LENGTH(b); 
    26  
    27 //SELECT comment_id, wordcount2(comment_content, "http"), comment_content FROM wp_comments where //comment_approved="spam"; 
    28  
    29  
    30  
    3111if (class_exists('Litmus')) { 
     12      add_action('wo3_prep', array('MD5Litmus', 'getCount')); 
    3213      add_action('wo3_tabs', array('MD5Litmus', 'tab')); 
    3314      add_action('wo3_content', array('MD5Litmus', 'content')); 
    34       add_action('wo3_total_count', array('MD5Litmus', 'addCount')); 
    35       add_action('wo3_rollcall', array('MD5Litmus', 'rollCall')); 
    3615      add_action('wo3_add_index', array('MD5Litmus', 'addIndex')); 
    3716 
    3817      class MD5Litmus extends Litmus { 
     18 
     19            function getName() { 
     20                  return "MD5"; 
     21            } 
     22       
     23            public static function getMatches() { 
     24                  global $keys; 
     25                  $lower_limit = get_option( $keys['ui_vis'] );  
     26                  echo "\n<!-- ".self::getName().".getMatches() -->\n"; 
     27                  return self::runCachedMatchesQuery( 
     28                        self::getName(), 
     29                        "SELECT count(*) num, MD5(comment_content) as comment_content_md5, group_concat(comment_id separator ',') as comment_id_list FROM wp_comments where comment_approved='spam' group by comment_content_md5 having num >= $lower_limit order by num desc;" 
     30                  ); 
     31            } 
     32 
     33            function content() {     
     34                  if (self::isActive() || AllLitmus::isActive()) { 
     35                        $comments = self::getMatches(); 
     36                        echo("<table>"); 
     37                        if (!AllLitmus::isActive()) echo("<tr><th></th><th>Links</th><th>Message ID</th></tr>"); 
     38                        foreach($comments as $comment) { 
     39                              Litmus::wo3_show_row($comment->num, "messages match MD5(".$comment->comment_content_md5.")", $comment->comment_id_list); 
     40                        } 
     41                        echo("</table>"); 
     42                  } else { 
     43                        echo "\n<!-- ".self::getName()." is not active -->\n"; 
     44                  } 
     45            } 
    3946 
    4047            function addIndex() { 
     
    5158                  echo("<p>Adding index for MD5s.</p>"); 
    5259            } 
     60 
     61 
     62            //============================================ 
     63            // common class methods for litmus tests   
     64            // (there's got to be a better way to do this! 
     65            //============================================ 
    5366             
    54             function rollCall() { 
    55                   echo("<p>MD5 Litmus</p>"); 
     67            function getCount() { 
     68                  global $wo3_title; 
     69                  self::getMatches(); 
     70                  return wp_cache_get(self::getName()."_count", $wo3_title); 
    5671            } 
    57  
    58             function getName() { 
    59                   return "MD5"; 
    60             } 
    61  
    62             function getCount() { 
    63                   global $wpdb, $keys; 
    64                   $lower_limit = get_option( $keys['ui_vis'] );  
    65                   $q="select sum(x.num) from (select count(*) as num from wp_comments where comment_approved='spam' group by md5(comment_content) having num >= $lower_limit) as x;"; 
    66                   $num = $wpdb->get_var($q);                 
    67                   if ($num == null) return "0"; 
    68                   return $num; 
    69             } 
    70  
    71             function addCount() { 
    72                   global $keys; 
    73                   update_option( $keys['total_count'], self::getCount() + get_option( $keys['total_count'] ) ); 
    74             } 
    75  
    76             function getMatches() { 
    77                   global $wpdb, $keys; 
    78                   $lower_limit = get_option( $keys['ui_vis'] );  
    79                   $q = "SELECT count(*) num, MD5(comment_content) as comment_content_md5, group_concat(comment_id separator ',') as comment_id_list FROM wp_comments where comment_approved='spam' group by comment_content_md5 having num >= $lower_limit order by num desc;"; 
    80                   return $wpdb->get_results($q); 
    81             } 
    82  
     72             
    8373            public static function tab() {       
    8474                  parent::tab(self::getName(), self::getCount(), self::isActive()); 
    8575            } 
    86  
     76             
    8777            function isActive() { 
    88                   return $_GET['tab'] == self::getName() && parent::isActive(); 
     78                  return ($_GET['tab'] == self::getName() ) && parent::isActive(); 
    8979            } 
    9080 
    91  
    92             function content() {     
    93  
    94