From ca550a7c3e08b5958dbc2952f2b24c06f8fdb5fc Mon Sep 17 00:00:00 2001 From: ozhozh Date: Fri, 9 Oct 2009 19:09:30 +0000 Subject: [PATCH] Add feature to disable stat logging, fixes issue 124. git-svn-id: http://yourls.googlecode.com/svn/trunk@220 12232710-3e20-11de-b438-597f59cd7555 --- includes/functions.php | 8 ++ readme.html | 2 + yourls-infos.php | 238 +++++++++++++++++++++-------------------- 3 files changed, 134 insertions(+), 114 deletions(-) diff --git a/includes/functions.php b/includes/functions.php index d3632de..cc147b7 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -968,6 +968,9 @@ function yourls_get_HTTP_status( $code ) { // Log a redirect (for stats) function yourls_log_redirect( $keyword ) { + if ( !yourls_do_log_redirect() ) + return true; + global $ydb; $table = YOURLS_DB_TABLE_LOG; @@ -980,6 +983,11 @@ function yourls_log_redirect( $keyword ) { return $ydb->query( "INSERT INTO `$table` VALUES ('', NOW(), '$keyword', '$referrer', '$ua', '$ip', '$location')" ); } +// Check if we want to not log redirects (for stats) +function yourls_do_log_redirect() { + return ( !defined('YOURLS_NOSTATS') || YOURLS_NOSTATS != true ); +} + // Converts an IP to a 2 letter country code, using GeoIP database if available in includes/geo/ function yourls_geo_ip_to_countrycode( $ip = '', $default = '' ) { if ( !file_exists( dirname(__FILE__).'/geo/GeoIP.dat') || !file_exists( dirname(__FILE__).'/geo/geoip.inc') ) diff --git a/readme.html b/readme.html index f125a42..3911879 100644 --- a/readme.html +++ b/readme.html @@ -346,6 +346,8 @@
  • YOURLS_PRIVATE_API
    If YOURLS_PRIVATE is set to true, you can still easily make your API public. To do so, override the global private setting with:
    define('YOURLS_PRIVATE_API', false); +
  • YOURLS_NOSTATS
    + If YOURLS_NOSTATS is set to true, redirects won't be logged and there will be not stats available.
  • diff --git a/yourls-infos.php b/yourls-infos.php index 38f1488..97e21be 100644 --- a/yourls-infos.php +++ b/yourls-infos.php @@ -1,4 +1,6 @@ get_results( "SELECT `shorturl`, `click_time`, `referrer`, `user_agent`, `country_code` FROM `$table` WHERE `shorturl` IN ( '$keywords' );" ); -} else { - // Fetch information for current keyword only - $hits = $ydb->get_results( "SELECT `click_time`, `referrer`, `user_agent`, `country_code` FROM `$table` WHERE `shorturl` = '$keyword';" ); -} - -$referrers = array(); -$direct = $notdirect = 0; -$countries = array(); -$dates = array(); -$list_of_days = array(); -$list_of_months = array(); -$list_of_years = array(); -$last_24h = array(); - -// Loop through all results and build list of referrers, countries and hits per day -foreach( (array)$hits as $hit ) { - extract( (array)$hit ); - - if ( isset( $country_code ) && $country_code ) { - if( !array_key_exists( $country_code, $countries ) ) - $countries[$country_code] = 0; - $countries[$country_code]++; - } - - if( isset( $referrer ) ) { - if ( $referrer == 'direct' ) { - $direct++; - } else { - $notdirect++; - $host = yourls_get_domain( $referrer ); - if( !array_key_exists( $host, $referrers ) ) - $referrers[$host] = array( ); - if( !array_key_exists( $referrer, $referrers[$host] ) ) - $referrers[$host][$referrer] = 0; - $referrers[$host][$referrer]++; - } + // Duplicate keywords, if applicable + $keyword_list = yourls_get_duplicate_keywords( $longurl ); + + // Fetch all information from the table log + $table = YOURLS_DB_TABLE_LOG; + + if( $aggregate ) { + $keywords = join( "', '", $keyword_list ); + // Fetch information for all keywords pointing to $longurl + $hits = $ydb->get_results( "SELECT `shorturl`, `click_time`, `referrer`, `user_agent`, `country_code` FROM `$table` WHERE `shorturl` IN ( '$keywords' );" ); + } else { + // Fetch information for current keyword only + $hits = $ydb->get_results( "SELECT `click_time`, `referrer`, `user_agent`, `country_code` FROM `$table` WHERE `shorturl` = '$keyword';" ); } - - if( isset( $click_time ) ) { - $now = intval( date('U') ); - preg_match('/(\d+)-(\d+)-(\d+)\s(\d+):(\d+):(\d+)/', $click_time, $matches); - list( $temp, $year, $month, $day, $hour, $min, $sec ) = $matches; - unset( $matches ); + $referrers = array(); + $direct = $notdirect = 0; + $countries = array(); + $dates = array(); + $list_of_days = array(); + $list_of_months = array(); + $list_of_years = array(); + $last_24h = array(); + + // Loop through all results and build list of referrers, countries and hits per day + foreach( (array)$hits as $hit ) { + extract( (array)$hit ); + + if ( isset( $country_code ) && $country_code ) { + if( !array_key_exists( $country_code, $countries ) ) + $countries[$country_code] = 0; + $countries[$country_code]++; + } - // Build array of $dates[$year][$month][$day] = number of clicks - if( !array_key_exists( $year, $dates ) ) - $dates[$year] = array(); - if( !array_key_exists( $month, $dates[$year] ) ) - $dates[$year][$month] = array(); - if( !array_key_exists( $day, $dates[$year][$month] ) ) - $dates[$year][$month][$day] = 0; - $dates[$year][$month][$day]++; + if( isset( $referrer ) ) { + if ( $referrer == 'direct' ) { + $direct++; + } else { + $notdirect++; + $host = yourls_get_domain( $referrer ); + if( !array_key_exists( $host, $referrers ) ) + $referrers[$host] = array( ); + if( !array_key_exists( $referrer, $referrers[$host] ) ) + $referrers[$host][$referrer] = 0; + $referrers[$host][$referrer]++; + } + } - // Build array of last 24 hours $last_24h[$hour] = number of click - $then = strtotime( $click_time); - if( ( $now >= $then ) && ( ( $now - $then ) < ( 24 * 60 * 60 ) ) ) { - $year = sprintf( "%02d", substr( $year, -1, 2 ) ); // 2009 -> 09 - $diff = $now - strtotime( $click_time); - if( !array_key_exists( "$year-$month-$day-$hour", $last_24h ) ) - $last_24h["$year-$month-$day-$hour"] = 0; - $last_24h["$year-$month-$day-$hour"]++; + if( isset( $click_time ) ) { + $now = intval( date('U') ); + + preg_match('/(\d+)-(\d+)-(\d+)\s(\d+):(\d+):(\d+)/', $click_time, $matches); + list( $temp, $year, $month, $day, $hour, $min, $sec ) = $matches; + unset( $matches ); + + // Build array of $dates[$year][$month][$day] = number of clicks + if( !array_key_exists( $year, $dates ) ) + $dates[$year] = array(); + if( !array_key_exists( $month, $dates[$year] ) ) + $dates[$year][$month] = array(); + if( !array_key_exists( $day, $dates[$year][$month] ) ) + $dates[$year][$month][$day] = 0; + $dates[$year][$month][$day]++; + + // Build array of last 24 hours $last_24h[$hour] = number of click + $then = strtotime( $click_time); + if( ( $now >= $then ) && ( ( $now - $then ) < ( 24 * 60 * 60 ) ) ) { + $year = sprintf( "%02d", substr( $year, -1, 2 ) ); // 2009 -> 09 + $diff = $now - strtotime( $click_time); + if( !array_key_exists( "$year-$month-$day-$hour", $last_24h ) ) + $last_24h["$year-$month-$day-$hour"] = 0; + $last_24h["$year-$month-$day-$hour"]++; + } } } -} -// Sort dates, chronologically from [2007][12][24] to [2009][02][19] -ksort( $dates ); -foreach( $dates as $year=>$months ) { - ksort( $dates[$year] ); - foreach( $months as $month=>$day ) { - ksort( $dates[$year][$month] ); + // Sort dates, chronologically from [2007][12][24] to [2009][02][19] + ksort( $dates ); + foreach( $dates as $year=>$months ) { + ksort( $dates[$year] ); + foreach( $months as $month=>$day ) { + ksort( $dates[$year][$month] ); + } } -} -// Get $list_of_days, $list_of_months, $list_of_years -if( $dates ) { - extract( yourls_build_list_of_days( $dates ) ); + // Get $list_of_days, $list_of_months, $list_of_years + if( $dates ) { + extract( yourls_build_list_of_days( $dates ) ); - // If the $last_24h doesn't have all the hours, insert missing hours with value 0 - for ($i = 23; $i >= 0; $i--) { - $h = date('y-m-d-H', $now - ($i * 60 * 60) ); - if( !array_key_exists( $h, $last_24h ) ) { - $last_24h[$h] = 0; + // If the $last_24h doesn't have all the hours, insert missing hours with value 0 + for ($i = 23; $i >= 0; $i--) { + $h = date('y-m-d-H', $now - ($i * 60 * 60) ); + if( !array_key_exists( $h, $last_24h ) ) { + $last_24h[$h] = 0; + } } + ksort( $last_24h ); } - ksort( $last_24h ); -} -// Sort countries, most frequent first -if ( $countries ) - arsort( $countries ); - -// Sort referrers. $referrer_sort is a array of most frequent domains -arsort( $referrers ); -$referrer_sort = array(); -$number_of_sites = count( array_keys( $referrers ) ); -foreach( $referrers as $site => $urls ) { - if( count($urls) > 1 || $number_of_sites == 1 ) - $referrer_sort[$site] = array_sum( $urls ); -} -arsort($referrer_sort); + // Sort countries, most frequent first + if ( $countries ) + arsort( $countries ); + + // Sort referrers. $referrer_sort is a array of most frequent domains + arsort( $referrers ); + $referrer_sort = array(); + $number_of_sites = count( array_keys( $referrers ) ); + foreach( $referrers as $site => $urls ) { + if( count($urls) > 1 || $number_of_sites == 1 ) + $referrer_sort[$site] = array_sum( $urls ); + } + arsort($referrer_sort); -/** -echo "
    ";
    -echo "referrers: "; print_r( $referrers );
    -echo "referrer sort: "; print_r( $referrer_sort );
    -echo "direct: $direct\n";
    -echo "notdirect: $notdirect\n";
    -echo "dates: "; print_r( $dates );
    -echo "list of days: "; print_r( $list_of_days );
    -echo "list_of_months: "; print_r( $list_of_months );
    -echo "list_of_years: "; print_r( $list_of_years );
    -echo "last_24h: "; print_r( $last_24h );
    -//echo "countries: "; print_r( $countries );
    +	/**
    +	echo "
    ";
    +	echo "referrers: "; print_r( $referrers );
    +	echo "referrer sort: "; print_r( $referrer_sort );
    +	echo "direct: $direct\n";
    +	echo "notdirect: $notdirect\n";
    +	echo "dates: "; print_r( $dates );
    +	echo "list of days: "; print_r( $list_of_days );
    +	echo "list_of_months: "; print_r( $list_of_months );
    +	echo "list_of_years: "; print_r( $list_of_years );
    +	echo "last_24h: "; print_r( $last_24h );
    +	//echo "countries: "; print_r( $countries );
     
    -die();
    -/**/
    +	die();
    +	/**/
     
    +}
     
     yourls_html_head( 'infos' );
     ?>
    @@ -161,7 +165,7 @@
     	YOURLS
     
     
    -

    Your are logged in as: . Logout

    +

    Your are logged in as: .

    Informations

    @@ -189,14 +193,17 @@
    +

    Traffic statistics

    @@ -509,6 +516,9 @@
    + + +

    Share

    -- 2.45.0