//
// $post this is a text string that is added to every line.
// default =
// $method there are two ways of identifying unique visitors,
// neither of which are perfect, the default is by
// recognizing only unique IP addresses, which means
// that if several people from one company visit, then
// they may show as a single user. Alternatively,
// using the session_id is not perfect because some
// users refuse to set cookies.
// default="ip", alternative="session"
// $as_percentage if set to true, then the number of hits for each
// page as a percentage of the site total is displayed
// instead of the hit count itself. This may be
// desirable if you want to show popularity without
// letting on how many hits you get for each story.
// default = false
// $timeout adjusts how long the most-wanted information is cached
// for before being replenished from the db. a value of
// 0 (zero) will result in fresh data being retrieved for
// every query, whereas a value of 3600 will only query
// the database once every hour, cacheing the output and
// thus reducing DB load.
// default = 1800
//
//
// can be dropped into any ordered or unordered list, so for example...
//
//
//
//
// 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.
//
MostWanted uses the database tables from StatTraq to provide a list of the most popular articles.
There is a small quirk in StatTraq that results in all page views showing up as "Multiple Posts" in cases where permalinks are used. These stats are not lost, they're just incorrectly recorded, and can be fixed.
Currently there are get_var( $q );
_e( $potential );
?> rows in your database that could be repaired.
Between each update, the fix mechanism can pause in order to give the database a rest. This is handy if you're running a live service and don't want the update to be noticed - a value of "0" can safely be used if you are the sole user of the DB and you just want to get the fix done.
", $post="", $method="ip", $as_percentage = false, $timeout=1800){
global $wpdb, $tablestattraq, $tableposts, $user_level, $mostwanted_ver, $total_hits, $cache_until, $cached_result;
$time_now = time();
if ($time_now > $cache_until) {
// the cache_until time has been passed so the cache is invalid.
// the content must therefore be regenerated.
$cache_until = $time_now+$timeout;
_e("");
get_currentuserinfo();
$dateOption = "";
$durationExplained = "";
if ($duration != "") {
$dateOption .= "AND DATE_SUB(CURDATE(),INTERVAL ".$duration." DAY) < st.access_time";
$durationExplained = " in the last ".$duration." days";
}
if ($method == "ip") {
$method="ip_address";
} else {
$method="session_id";
}
$distinct = ($as_percentage?" of":"")." distinct viewers";
$q = "SELECT p.post_title, st.article_id, COUNT( DISTINCT (st.$method) ) as cnt FROM $tablestattraq st, $tableposts p where p.ID=st.article_id AND p.post_status='publish' ".$futureSpamOption." AND st.user_agent_type='0' ".$dateOption." GROUP BY st.article_id ORDER BY cnt DESC LIMIT 0,$top_n";
$output = $wpdb->get_results( $q );
$cached_result="";
_e("");
if (isset($output)) {
foreach ($output as $line) {
if ($showviews) {
$views = " ".MostWanted::getHits($as_percentage, $line->cnt, $dateOption)."";
}
if ($show_views_in_tt) {
$ttviews = " (".MostWanted::getHits($as_percentage, $line->cnt, $dateOption).$distinct.$durationExplained.")".$append;
}
$short = MostWanted::curtail($line->post_title, $curtail) ;
$t= str_replace("'","'", $line->post_title);
$cached_result .= $pre . "" . ($short) . "".$post;
}
} else {
$cached_result .= $pre . "No results available.".$post;
}
} else {
_e("");
// the cache is still valid
// there no need to do anything
}
echo $cached_result;
// reset the hits value so it is checked once when
// the next page is loaded.
$total_hits = -1;
return;
}
function getHits($as_percentage, $count, $dateOption) {
global $total_hits, $wpdb;
// start percentage lookup
if ($as_percentage) {
// get the total number of hits if it's not known already
if ($total_hits == -1) {
$q = "SELECT COUNT( * ) as cnt FROM $tablestattraq st where st.user_agent_type='0'" . $futureSpamOption." ".$dateOption;
$total_hits = $wpdb->get_var( $q );
}
return "" . round((($count / $total_hits) * 100), 1) . "%";
} else {
return $count;
}
// end percentage lookup
}
// trims a message down so that it is shorter than the length
// specified in the $trim_chars argument.
function curtail($trim_this, $trim_chars=0) {
if ($trim_chars > 0 && strlen($trim_this) > $trim_chars) {
return substr($trim_this,0,($trim_chars-3)) . “…”;
}
return $trim_this;
}
// adds a signature to the end of your page describing the
// version of MostWanted, which is rather handy when debugging
// from afar.
function signature() {
global $mostwanted_ver;
echo "\n\n\n";
}
// This utility method is not used by the main plugin. It is as a
// repair tool for stattraq data which may be incorrectly recorded
// without an article_id, which means that the data can't show up
// in the MostWanted output.
//
// If you've not hacked your stattraq plugin to fix this, then
// a call to this method will repair all entries in your database.
function fixstats($sleep = 1){
global $wpdb, $tablestattraq, $tableposts;
get_currentuserinfo();
// calculate duration
$output = $wpdb->get_results( "SELECT COUNT( DISTINCT (ID) ) as cnt FROM $wpdb->posts;" );
foreach ($output as $line) {
$duration = ($sleep * $line->cnt) /60;
}
_e("
MostWanted is now updating your stattraq database as a LOW_PRIORITY task. 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.
");
_e("
This page shows the progress of that process. If you wander off to another page, the process will continue in the background, but you won't know precicely when it's complete.
");
$output = $wpdb->get_results( "SELECT id, post_name FROM $wpdb->posts where post_status = 'publish' ORDER BY id DESC;" );
foreach ($output as $line) {
$q = "SELECT count(*) from $tablestattraq WHERE (article_id = '0' and url like '%name=" . $line->post_name . "%');";
$changes = $wpdb->get_var( $q );
if ($changes > 0) {
$q = "UPDATE low_priority $tablestattraq SET article_id = '" . $line->id . "' WHERE (article_id = '0' and url like '%name=" . $line->post_name . "%');";
_e("
Skipping " . $line->post_name . " because no rows require update
");
}
ob_end_flush();
}
_e("");
_e("
Finished!
");
return;
}
}
add_action('wp_head', array('MostWanted', 'signature'));
add_action('admin_menu', array('MostWanted', 'config_page'));
// NOTE the ">" symbol in the following line must
// be the last character in the file - do not add
// any spaces, tabs or newlines after it, or you
// will get "header already sent" errors.
?>