# ================================================================== # Links SQL - enhanced directory management system # # Website : http://gossamer-threads.com/ # Support : support@gossamer-threads.com # Revision : $Id: SearchLogger.pm,v 1.6 2001/01/26 01:05:56 alex Exp $ # # Copyright (c) 2000 Gossamer Threads Inc. All Rights Reserved. # Redistribution in part or in whole strictly prohibited. Please # see LICENSE file for full details. # ================================================================== package Plugins::SearchLogger; # ================================================================== use strict; use vars qw/$VERSION $DEBUG $NAME $FONT/; use GT::Plugins qw/STOP CONTINUE/; use Links qw/$IN $DB $CFG/; use GT::Date; $VERSION = '1.1'; $DEBUG = 0; $NAME = 'SearchLogger'; $FONT = 'font face="Tahoma,Arial,Helvetica" size="2"'; sub view { # ------------------------------------------------------------------- # This is the admin view, it lets people search on keywords # and purge old listings. # my $db = $DB->table('SearchLog'); my $days = $IN->param('days'); if (defined $days and $days =~ /^\d+$/) { Links::init_date(); my $date = GT::Date::date_get (time - 86400 * $days); if ($IN->param('total')) { $db->delete ( GT::SQL::Condition->new ( 'Last_Hit', '<', $date) ); } } my $nh = $IN->param('nh') || 1; my $mh = $IN->param('mh') || 25; my $sth = $db->query_sth ($IN); my $hits = $db->hits; my $tb = $DB->html($db,$IN)->toolbar ($nh, $mh, $hits, $IN->url); my $output = qq~
~; while (my $hit = $sth->fetchrow_hashref) { my $escaped = $IN->escape ($hit->{Term}); $output .= qq! !; } $output .= "
<$FONT>Term <$FONT>Count <$FONT>Results <$FONT>Last Searched
<$FONT>$hit->{Term}<$FONT>$hit->{HitCount}<$FONT>$hit->{Results}<$FONT>$hit->{Last_Hit}
"; print $IN->header(); print qq~ Links SQL: Keyword Searches.
~; print Links::header ('Keyword Searches', 'This screen shows you a list of keywords people have searched for', 0); print qq~

<$FONT>$output

~; ($tb) and print "

<$FONT>Pages: $tb

"; print qq~

<$FONT>Purge listings older than days old.
Purge Totals table

~; } sub view_daily { # ------------------------------------------------------------------- # This is the admin view, it lets people search on keywords # and purge old listings. # my $db_daily = $DB->table('SearchLogDaily'); my $days = $IN->param('days'); if (defined $days and $days =~ /^\d+$/) { Links::init_date(); my $date = GT::Date::date_get (time - 86400 * $days); if ($IN->param('daily')) { $db_daily->delete ( GT::SQL::Condition->new ( 'Last_Hit', '<', $date) ); } } my $nh = $IN->param('nh') || 1; my $mh = $IN->param('mh') || 25; my $sth = $db_daily->query_sth ($IN); my $hits = $db_daily->hits; my $tb = $DB->html($db_daily,$IN)->toolbar ($nh, $mh, $hits, $IN->url); my $output = qq~
~; while (my $hit = $sth->fetchrow_hashref) { my $escaped = $IN->escape ($hit->{Term}); $output .= qq! !; } $output .= "
<$FONT>Term <$FONT>Count <$FONT>Results <$FONT>Last Searched <$FONT>Hit Date
<$FONT>$hit->{Term} <$FONT>$hit->{HitCount} <$FONT>$hit->{Results} <$FONT>$hit->{Last_Hit} <$FONT>$hit->{Hit_Date}
"; print $IN->header(); print qq~ Links SQL: Keyword Searches.
~; print Links::header ('Keyword Searches', 'This screen shows you a list of keywords people have searched for', 0); print qq~

<$FONT>$output

~; ($tb) and print "

<$FONT>Pages: $tb

"; print qq~

<$FONT>Purge listings older than days old.
Purge Daily table

~; } sub log_query { # ------------------------------------------------------------------- # This is the search hook, it happens after the search and the only # argument it gets is $opts which is what would get sent to the search # template. Hooks default to continue processing, if you wanted to not # run the main search, you should call GT::Plugins->action ( STOP ); # my $opts = shift; my $db = $DB->table ('SearchLog'); my $db_daily = $DB->table ('SearchLogDaily'); my $query = $IN->param('query'); my $results = defined $opts->{link_hits} ? int($opts->{link_hits}) : 0; $query =~ s/^\s*|\s*$//g; $query = lc $query; #$query = substr ($query, 0, 24); my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); if ($year >= 100) {$year = $year+1900}; ## correct for year 2000 $mon++; ## months are 0 based -- distance from begining of year if ($mon < 10) {$mon = "0$mon"}; #$mday = $mday+1; if ($mday < 10) {$mday = "0$mday"}; my $current_Tday = "$year-$mon-$mday"; ## update regular log table $db->select ( {Term => $query} ); if ($db->hits) { $db->update ( { HitCount => \"HitCount+1", Results => $results, Last_Hit => $current_Tday }, { Term => $query } ); } else { $db->insert ( { Term => $query, HitCount => 1, Results => $results, Last_Hit => $current_Tday }); $db->update ( { Last_Hit => $current_Tday }, { Term => $query } ); } ## update daily table $db_daily->select ( {Term => $query, Hit_Date => $current_Tday} ); if ($db_daily->hits) { $db_daily->update ( { HitCount => \"HitCount+1", Results => $results }, { Term => $query, Hit_Date => $current_Tday } ); } else { $db_daily->insert ( { Term => $query, HitCount => 1, Results => $results, Last_Hit => $current_Tday, Hit_Date => $current_Tday}); } return $opts; } sub Top_Keywords { # ----------------------------------------------- # Returns a list of the Top5 Searched terms. # my ($num, $type) = @_; my ($s_table, $s_order, $search_db, $sth, $s_cond, $output); ## define them all here to make it easier to read the code $output = ''; if ($type eq 'd') { $s_table = 'SearchLogDaily'; $s_order = 'HitCount'; GT::Date::date_set_format('%yyyy%-%mm%-%dd%'); my $today = GT::Date::date_get(); $s_cond = GT::SQL::Condition->new( 'Hit_Date', '=', $today ) # $s_cond = "Hit_Date => $today"; # $s_cond = ""; } elsif ($type eq 'r') { $s_table = 'SearchLog'; $s_order = 'TS'; $s_cond = ''; } else { $s_table = 'SearchLog'; $s_order = 'HitCount'; $s_cond = ''; } $search_db = $DB->table($s_table); ## connect to the table, grab a handle $search_db->select_options ("ORDER BY $s_order DESC", "LIMIT $num"); ## set the options for the select $sth = $search_db->select($s_cond); ## execute the select, assign to a statement handle if ($search_db->hits) { my $db_cgi_url = $CFG->{'db_cgi_url'}; while (my $row = $sth->fetchrow_hashref) { my $term = GT::CGI::unescape($row->{'Term'}); #unless (length $term <= 17) {$term = substr ($term, 0, 17);} my $term_q = $IN->escape($row->{'Term'}); ## $IN is a CGI object $output .= qq~
  • $term ~; } $output .= ''; } else { $output = "No-one has searched for anything yet."; } return $output; } sub about { # ------------------------------------------------------------------- # Displays a help screen. # print $IN->header; print <<'END_OF_HELP'; Plugins::SearchLogger
    Plugin: SearchLogger

    SearchLogger

    The SearchLogger plugin is a simple addition to your search engine. It will log all queries that visitors perform, and allow you to browse them from the admin.

    When you installed this plugin it created a new table called SearchLog that stores all queries. On your left should now be a new menu item called View Logs. Clicking on this will allow you to view what people have searched on. There is also an option to remove old search terms. Simply enter the number of days and hit enter.

    If you have questions about this plugin, please visit the support forum at:

    http://gossamer-threads.com/scripts/forum/

    END_OF_HELP } 1;