? ok_statistics Index: statistics =================================================================== RCS file: /cvsroot/twiki/twiki/bin/statistics,v retrieving revision 1.30 diff -c -r1.30 statistics *** statistics 20 Oct 2002 07:57:47 -0000 1.30 --- statistics 2 Dec 2002 22:10:20 -0000 *************** *** 40,57 **** # FIXME: Beware global $| in mod_perl! Should use 'local' $| = 1; # no buffering &main(); # ========================= sub main { - # initialize variables - my $cgiQuery = undef; - my $tmp = ""; - my $theTopic = ""; - my $thePathInfo = ""; - my $theRemoteUser = ""; my $logDate = ""; # determine at runtime if script is called by browser or cron job if( $ENV{'DOCUMENT_ROOT'} ) { --- 40,58 ---- # FIXME: Beware global $| in mod_perl! Should use 'local' $| = 1; # no buffering + # initialize variables + my $cgiQuery = undef; + my $theTopic = ""; + my $thePathInfo = ""; + my $theRemoteUser = ""; + &main(); # ========================= sub main { my $logDate = ""; + my $tmp = ""; # determine at runtime if script is called by browser or cron job if( $ENV{'DOCUMENT_ROOT'} ) { *************** *** 92,101 **** $mon = $mon+1; $logDate = sprintf("%.4u%.2u", $year, $mon); } my $logMonth; my $logYear; ! $tmp = $logDate; $tmp =~ s/([0-9]{4})(.*)/$2/g; if( $tmp && $tmp < 13 ) { $logMonth = $TWiki::isoMonth[$tmp-1]; --- 93,109 ---- $mon = $mon+1; $logDate = sprintf("%.4u%.2u", $year, $mon); } + doMonthsLog( $logDate ); + } + + # ======================================== + sub doMonthsLog + { + my ($logDate) = @_; my $logMonth; my $logYear; ! my $tmp = $logDate; $tmp =~ s/([0-9]{4})(.*)/$2/g; if( $tmp && $tmp < 13 ) { $logMonth = $TWiki::isoMonth[$tmp-1]; *************** *** 107,115 **** --- 115,133 ---- my $logMonthYear = "$logMonth $logYear"; printMsg( "* Statistics for $logMonthYear", $cgiQuery ); + #SVEN - work out the previous + my $prevMon = $tmp - 1; + my $prevYear = $logYear; + if ( $prevMon <= 0 ) { + $prevMon = 12; + $prevYear = $prevYear - 1; + } + my $prevLogDate = sprintf("%.4u%.2u", $prevYear, $prevMon); + my $logFile = $TWiki::logFilename; $logFile =~ s/%DATE%/$logDate/g; + my $newLines = ""; if( -e $logFile ) { # Copy the log file to temp file, since analysis could take some time *************** *** 159,165 **** # Generate WebStatistics topic update for one or more webs if ( $thePathInfo ) { # do a particular web: ! processWeb( $thePathInfo, $theRemoteUser, $theTopic, $logMonthYear, $viewRef, $contribRef, $statViewsRef, $statSavesRef, $statUploadsRef, $cgiQuery ); } else { --- 177,183 ---- # Generate WebStatistics topic update for one or more webs if ( $thePathInfo ) { # do a particular web: ! $newLines = processWeb( $thePathInfo, $theRemoteUser, $theTopic, $logMonthYear, $prevLogDate, $viewRef, $contribRef, $statViewsRef, $statSavesRef, $statUploadsRef, $cgiQuery ); } else { *************** *** 174,180 **** } foreach my $web ( @weblist ) { if( -d "$dataDir/$web" ) { ! processWeb( "/$web", $theRemoteUser, $theTopic, $logMonthYear, $viewRef, $contribRef, $statViewsRef, $statSavesRef, $statUploadsRef, $cgiQuery ); } } --- 192,198 ---- } foreach my $web ( @weblist ) { if( -d "$dataDir/$web" ) { ! $newLines = processWeb( "/$web", $theRemoteUser, $theTopic, $logMonthYear, $prevLogDate, $viewRef, $contribRef, $statViewsRef, $statSavesRef, $statUploadsRef, $cgiQuery ); } } *************** *** 195,200 **** --- 213,219 ---- } else { printMsg( "End creating usage statistics", $cgiQuery ); } + return $newLines; } # Debug only *************** *** 353,360 **** # ========================= sub processWeb { ! my( $thePathInfo, $theRemoteUser, $theTopic, $theLogMonthYear, $viewRef, $contribRef, ! $statViewsRef, $statSavesRef, $statUploadsRef, $cgiQuery ) = @_; my ( $topic, $webName, $dummy, $userName, $dataDir ) = &TWiki::initialize( $thePathInfo, $theRemoteUser, $theTopic, "", $cgiQuery ); --- 372,380 ---- # ========================= sub processWeb { ! my( $thePathInfo, $theRemoteUser, $theTopic, $theLogMonthYear, $prevLogDate, ! $viewRef, $contribRef, $statViewsRef, $statSavesRef, $statUploadsRef, ! $cgiQuery ) = @_; my ( $topic, $webName, $dummy, $userName, $dataDir ) = &TWiki::initialize( $thePathInfo, $theRemoteUser, $theTopic, "", $cgiQuery ); *************** *** 395,400 **** --- 415,421 ---- # Update the WebStatistics topic + my $newLines = ""; my $tmp; my $statsTopic = $TWiki::statisticsTopicname; # DEBUG *************** *** 402,415 **** --- 423,455 ---- if( &TWiki::Store::topicExists( $webName, $statsTopic ) ) { my( $meta, $text ) = &TWiki::Store::readTopic( $webName, $statsTopic ); my @lines = split( /\n/, $text ); + + + + my $prevLogMonth; + my $prevLogYear; + my $tmp = $prevLogDate; + $tmp =~ s/([0-9]{4})(.*)/$2/g; + if( $tmp && $tmp < 13 ) { + $prevLogMonth = $TWiki::isoMonth[$tmp-1]; + } else { + $prevLogMonth = "Date error"; + } + $prevLogYear = $prevLogDate; + $prevLogYear =~ s/([0-9]{4})(.*)/$1/g; + my $prevLogMonthYear = "$prevLogMonth $prevLogYear"; + my $statLine; my $idxStat = -1; + my $idxprevStat = -1; my $idxTmpl = -1; for( my $x = 0; $x < @lines; $x++ ) { $tmp = $lines[$x]; # Check for existing line for this month+year if( $tmp =~ /$theLogMonthYear/ ) { $idxStat = $x; + } elsif( $tmp =~ /$prevLogMonthYear/ ) { + $idxprevStat = $x; } elsif( $tmp =~ /<\!\-\-statDate\-\->/ ) { $statLine = $_; $idxTmpl = $x; *************** *** 427,442 **** if( $idxStat >= 0 ) { # entry already exists, need to update $lines[$idxStat] = $statLine; - - } elsif( $idxTmpl >= 0 ) { - # entry does not exist, add after line - $lines[$idxTmpl] = "$lines[$idxTmpl]\n$statLine"; - } else { ! # entry does not exist, add at the end ! $lines[@lines] = $statLine; } $text = join( "\n", @lines ); $text .= "\n"; --- 467,497 ---- if( $idxStat >= 0 ) { # entry already exists, need to update + $newLines = $statLine; $lines[$idxStat] = $statLine; } else { ! #if this month isn't there make sure last month was completely finished ! my $otherLine = doMonthsLog( $prevLogDate ); ! if ( $otherLine ) { ! if( $idxprevStat >=0 ) { ! $lines[$idxprevStat] = $otherLine; ! $otherLine = ""; ! } else { ! $otherLine = "\n$otherLine"; ! } ! } ! if( $idxTmpl >= 0 ) { ! # entry does not exist, add after line ! $newLines = $statLine.$otherLine; ! $lines[$idxTmpl] = "$lines[$idxTmpl]\n$newLines"; ! } else { ! # entry does not exist, add at the end ! $newLines = $statLine.$otherLine; ! $lines[@lines] = $newLines; ! } } + + $text = join( "\n", @lines ); $text .= "\n"; *************** *** 446,451 **** --- 501,507 ---- } else { printMsg( " *** Warning: No updates done, topic $webName.$statsTopic does not exist", $cgiQuery ); } + return $newLines; } # =========================