#!/usr/bin/perl #----------------------------------------------------------------------------- # Export To CSV AWStats plugin # This plugin adds export to csv functionality for different stats data # # Copyright (c) 2005 Pim Snel for Lingewoud B.V. # This AWStats plugin is a free software distributed under the GNU General # Public License. #----------------------------------------------------------------------------- # Perl Required Modules: None #----------------------------------------------------------------------------- # This plugin does not work when Tooltips are loaded or any other plugin that # uses the debug function. # # TODO # 1. make all year work # 2. add more export types # 3. cleanup code # 4. fix htmlentities? # # $Id$ # <----- # ENTER HERE THE USE COMMAND FOR ALL REQUIRED PERL MODULES. # -----> use strict;no strict "refs"; use HTML::Entities; #----------------------------------------------------------------------------- # PLUGIN VARIABLES #----------------------------------------------------------------------------- # <----- # ENTER HERE THE MINIMUM AWSTATS VERSION REQUIRED BY YOUR PLUGIN # AND THE NAME OF ALL FUNCTIONS THE PLUGIN MANAGE. my $PluginNeedAWStatsVersion="6.9"; my $PluginHooksFunctions="BuildFullHTMLOutput TabHeadHTML"; # -----> # <----- # IF YOUR PLUGIN NEED GLOBAL VARIABLES, THEY MUST BE DECLARED HERE. use vars qw/ $fld_termd $fld_enclosed $fld_escaped $ln_termd $MAXLINE $max_v $max_p $max_h $max_k $total_u $total_v $total_p $total_h $total_k $average_nb $average_u $average_v $average_p $average_h $average_k $total_e $total_x $rest_p $rest_e $rest_k $rest_x $firstdaytoshowtime $lastdaytoshowtime $firstdaytocountaverage $lastdaytocountaverage /; # -----> $fld_termd=','; $fld_enclosed='"'; $fld_escaped='\\'; $ln_termd="\n"; #----------------------------------------------------------------------------- # PLUGIN FUNCTION: Init_pluginname #----------------------------------------------------------------------------- sub Init_export_to_csv { my $InitParams=shift; my $checkversion=&Check_Plugin_Version($PluginNeedAWStatsVersion); #$EXPORTCSVON=1; if ($QueryString =~ /exportcsv=([^&]+)/i) { print "Content-type: application/csv\n"; print "Content-disposition: attachment; filename=filex.csv\n"; print "\n"; $HeaderHTTPSent=1; %HTMLOutput=(); $NOHTML=1; } return ($checkversion?$checkversion:"$PluginHooksFunctions"); } #----------------------------------------------------------------------------- # PLUGIN FUNTION: AddHTMLBodyHeader_pluginname # UNIQUE: NO (Several plugins using this function can be loaded) # Function called to Add HTML code at beginning of BODY section. #----------------------------------------------------------------------------- sub AddHTMLBodyHeader_export_to_csv { return 1; } #----------------------------------------------------------------------------- # PLUGIN FUNTION: BuildFullHTMLOutput_pluginname # UNIQUE: NO (Several plugins using this function can be loaded) # Function called to output an HTML page completely built by plugin instead # of AWStats output #----------------------------------------------------------------------------- sub BuildFullHTMLOutput_export_to_csv { if ($QueryString =~ /exportcsv=([^&]+)/i) { my $exportaction=$1; #FIXME this must be done by awstats.pl %MonthNumLib = ("01","$Message[60]","02","$Message[61]","03","$Message[62]","04","$Message[63]","05","$Message[64]","06","$Message[65]","07","$Message[66]","08","$Message[67]","09","$Message[68]","10","$Message[69]","11","$Message[70]","12","$Message[71]"); # Check year and month parameters if ($QueryString =~ /(^|&)month=(year)/i) { error("month=year is a deprecated option. Use month=all instead."); } if ($QueryString =~ /(^|&)year=(\d\d\d\d)/i) { $YearRequired=sprintf("%04d",$2); } else { $YearRequired="$nowyear"; } if ($QueryString =~ /(^|&)month=(\d{1,2})/i) { $MonthRequired=sprintf("%02d",$2); } elsif ($QueryString =~ /(^|&)month=(all)/i) { $MonthRequired='all'; } else { $MonthRequired="$nowmonth"; } if($exportaction eq 'monthhistory') { &CSVmonths(); } elsif($exportaction eq 'pageurl') { &CSVpageurl(); } elsif($exportaction eq 'monthdays') { &CSVmonthdays(); } return 1; } } sub CSVmonthdays { &Read_History_With_TmpUpdate($YearRequired,$MonthRequired,0,0,"day"); # Read full history file # # Define firstdaytocountaverage, lastdaytocountaverage, firstdaytoshowtime, lastdaytoshowtime my $firstdaytocountaverage=$nowyear.$nowmonth."01"; # Set day cursor to 1st day of month my $firstdaytoshowtime=$nowyear.$nowmonth."01"; # Set day cursor to 1st day of month my $lastdaytocountaverage=$nowyear.$nowmonth.$nowday; # Set day cursor to today my $lastdaytoshowtime=$nowyear.$nowmonth."31"; # Set day cursor to last day of month if ($MonthRequired eq 'all') { $firstdaytocountaverage=$YearRequired."0101"; # Set day cursor to 1st day of the required year } if (($MonthRequired ne $nowmonth && $MonthRequired ne 'all') || $YearRequired ne $nowyear) { if ($MonthRequired eq 'all') { $firstdaytocountaverage=$YearRequired."0101"; # Set day cursor to 1st day of the required year $firstdaytoshowtime=$YearRequired."1201"; # Set day cursor to 1st day of last month of required year $lastdaytocountaverage=$YearRequired."1231"; # Set day cursor to last day of the required year $lastdaytoshowtime=$YearRequired."1231"; # Set day cursor to last day of last month of required year } else { $firstdaytocountaverage=$YearRequired.$MonthRequired."01"; # Set day cursor to 1st day of the required month $firstdaytoshowtime=$YearRequired.$MonthRequired."01"; # Set day cursor to 1st day of the required month $lastdaytocountaverage=$YearRequired.$MonthRequired."31"; # Set day cursor to last day of the required month $lastdaytoshowtime=$YearRequired.$MonthRequired."31"; # Set day cursor to last day of the required month } } # BY DAY OF MONTH #--------------------------------------------------------------------- if ($Debug) { debug("ShowDaysOfMonthStats",2); } my $title="$Message[138]"; $average_nb=$average_u=$average_v=$average_p=$average_h=$average_k=0; $total_u=$total_v=$total_p=$total_h=$total_k=0; # Define total and max $max_v=$max_h=$max_k=0; # Start from 0 because can be lower than 1 foreach my $daycursor ($firstdaytoshowtime..$lastdaytoshowtime) { $daycursor =~ /^(\d\d\d\d)(\d\d)(\d\d)/; my $year=$1; my $month=$2; my $day=$3; if (! DateIsValid($day,$month,$year)) { next; } # If not an existing day, go to next $total_v+=$DayVisits{$year.$month.$day}||0; $total_p+=$DayPages{$year.$month.$day}||0; $total_h+=$DayHits{$year.$month.$day}||0; $total_k+=$DayBytes{$year.$month.$day}||0; if (($DayVisits{$year.$month.$day}||0) > $max_v) { $max_v=$DayVisits{$year.$month.$day}; } #if (($DayPages{$year.$month.$day}||0) > $max_p) { $max_p=$DayPages{$year.$month.$day}; } if (($DayHits{$year.$month.$day}||0) > $max_h) { $max_h=$DayHits{$year.$month.$day}; } if (($DayBytes{$year.$month.$day}||0) > $max_k) { $max_k=$DayBytes{$year.$month.$day}; } } # Define average foreach my $daycursor ($firstdaytocountaverage..$lastdaytocountaverage) { $daycursor =~ /^(\d\d\d\d)(\d\d)(\d\d)/; my $year=$1; my $month=$2; my $day=$3; if (! DateIsValid($day,$month,$year)) { next; } # If not an existing day, go to next $average_nb++; # Increase number of day used to count $average_v+=($DayVisits{$daycursor}||0); $average_p+=($DayPages{$daycursor}||0); $average_h+=($DayHits{$daycursor}||0); $average_k+=($DayBytes{$daycursor}||0); } if ($average_nb) { $average_v=$average_v/$average_nb; $average_p=$average_p/$average_nb; $average_h=$average_h/$average_nb; $average_k=$average_k/$average_nb; if ($average_v > $max_v) { $max_v=$average_v; } #if ($average_p > $max_p) { $max_p=$average_p; } if ($average_h > $max_h) { $max_h=$average_h; } if ($average_k > $max_k) { $max_k=$average_k; } } else { $average_v="?"; $average_p="?"; $average_h="?"; $average_k="?"; } # # Show data array for days print "$Message[4]"; print $fld_termd; if ($ShowDaysOfMonthStats =~ /V/i) { print decode_entities($Message[10]); } print $fld_termd; if ($ShowDaysOfMonthStats =~ /P/i) { print decode_entities($Message[56]); } print $fld_termd; if ($ShowDaysOfMonthStats =~ /H/i) { print decode_entities($Message[57]); } print $fld_termd; if ($ShowDaysOfMonthStats =~ /B/i) { print decode_entities($Message[75]); } print $ln_termd; foreach my $daycursor ($firstdaytoshowtime..$lastdaytoshowtime) { $daycursor =~ /^(\d\d\d\d)(\d\d)(\d\d)/; my $year=$1; my $month=$2; my $day=$3; if (! DateIsValid($day,$month,$year)) { next; } # If not an existing day, go to next my $dayofweekcursor=DayOfWeek($day,$month,$year); print "".(! $StaticLinks && $day==$nowday && $month==$nowmonth && $year==$nowyear?'':''); print Format_Date("$year$month$day"."000000",2); print (! $StaticLinks && $day==$nowday && $month==$nowmonth && $year==$nowyear?'':''); print ""; print $fld_termd; if ($ShowDaysOfMonthStats =~ /V/i) { print "",$DayVisits{$year.$month.$day}?$DayVisits{$year.$month.$day}:"0",""; } print $fld_termd; if ($ShowDaysOfMonthStats =~ /P/i) { print "",$DayPages{$year.$month.$day}?$DayPages{$year.$month.$day}:"0",""; } print $fld_termd; if ($ShowDaysOfMonthStats =~ /H/i) { print "",$DayHits{$year.$month.$day}?$DayHits{$year.$month.$day}:"0",""; } print $fld_termd; if ($ShowDaysOfMonthStats =~ /B/i) { print "",Format_Bytes(int($DayBytes{$year.$month.$day}||0)),""; } print $ln_termd; } # # Average row # print "