<?php
/*
Plugin Name: MostWanted
Plugin URI: http://www.boakes.org/most-wanted
Description: This plugin uses the StatTraq table to report on the most popular pages based on the number of times each has been viewed
Author: Rich Boakes
Author URI: http://boakes.org/
Version: 0.0.6
*/

/*
// release history
//
// version 0.0.1
//     original release
// version 0.0.2
//     improved code to allow the top "n" results to be displayed and
//     also to give the user control on how the text is trimmed. also
//     the number of views can optionally be displayed.
// version 0.0.3
//     tweaked "the top-two posts" issue
//     added a possible solution to the escaping problem (feedback invited)
// version 0.0.4
//     modified to use the $tableposts variable which may help users with 
//     multiple blogs through different users.
// version 0.0.5 (not released separately)
//     added the option to display the count in the tooltip.
// version 0.0.6
//     recognizes and omits entries with the spam flag in stattraq db
//     (disabled by default).
// version 0.0.7
//     added the fixStats method to repair any older statistics that do not
//     include the post ID.
//
// usage:
//   rjb_mostwanted($top_n, $trim, $trim_chars, $showviews)
//                        $top_n       the number of results to list
//                        $trim_chars  0 for no trimming, or the number of characters from
//                    each post title which shoudlbe displayed.
//                        $showviews   true if the number of times each post has been
//                    viewed should be included in the list.
//
// can be dropped into any ordered or unordered list, so for example...
//   <ul>
//   <?php rjb_mostwanted(); ?>
//   </ul>
//
// The example below would return a list of the top 10 entries
// with any titles longer than 30 characters trimmed to 27 in length and
// suffixed with "...".  The tooltip will still read the full title. The
// number of times the post has been viewed will be shown in brackets after
// the post title.
//   <ul>
//   <?php rjb_mostwanted(10, 30, true); ?>
//   </ul>
//
*/

$futureSpamOption "";
//$futureSpamOption = "and spam=0";

function rjb_mostwanted($top_n=5$trim_chars=0$showviews=false$show_views_in_tt=true){
        global 
$wpdb$tablestattraq$tableposts$user_level;

        
get_currentuserinfo();

        
$output $wpdb->get_results"SELECT p.post_title, st.article_id, COUNT( DISTINCT (st.ip_address) ) as cnt FROM $tablestattraq st, $tableposts p where p.ID=st.article_id ".$futureSpamOption." GROUP BY st.article_id ORDER BY cnt DESC LIMIT 0,$top_n" );

    if (isset(
$output)) {
            foreach (
$output as $line) {
                    if (
$showviews)        {  $views " (".($line->cnt).")";  }
                         if (
$show_views_in_tt) {  $ttviews " (".($line->cnt)." distinct viewers)";  }

                    
$short rjb_trim($line->post_title$trim_chars) ;
                    echo 
"<li><a title='"$line->post_title $ttviews "' href='/index.php-normal?p=" . ($line->article_id) . "'>" . ($short) . ($views) . "</a></li>";
            }
    } else {
        
_e("<li>No results available.</li>");
    }
   return;
}

function 
fixstats(){
    global 
$wpdb$tablestattraq$tableposts;
    
get_currentuserinfo();

    
// calculate duration
    
$sleep 1;
    
$output $wpdb->get_results"SELECT COUNT( DISTINCT (ID) ) as cnt FROM wp_posts;" );
    foreach (
$output as $line) {
            
$duration = ($sleep $line->cnt) /60;
    }
    
_e("<p>This will probably take around " $duration " minutes.  Your previous statistics are being ever-so-slightly-repaired.  Every record of a post that was accessed using a permalink is having it's post id added to it.</p><ol>");
    
$output $wpdb->get_results"SELECT id, post_name FROM wp_posts ORDER BY id DESC;" );

    foreach (
$output as $line) {
        
$q "UPDATE wp_stattraq SET article_id = '" $line->id "' WHERE (article_id = '0' and url like '%name=" $line->post_name "%');";
            
ob_end_flush();
        
_e("<li>Converting " $line->post_name "</li>");
        
$o2 $wpdb->get_results$q );
        
flush();
    
        
sleep($sleep);
    }
    
_e("</ol>");
   return;
}

function 
rjb_trim($trim_this$trim_chars=0) {

        if (
strlen($trim_this) > $trim_chars) {
                return 
substr($trim_this,0,($trim_chars-3)) . "...";
        }
        return 
$trim_this;
}
?>