From 14ffd0723f4a98f46f490610d6872eecdad11bcc Mon Sep 17 00:00:00 2001 From: Matthew Thiffault Date: Tue, 10 Mar 2009 23:19:56 -0400 Subject: [PATCH 01/16] This is the library database search script that is destined for csclub.uwaterloo.ca/library. Along with it goes a .htaccess file to make sure that index.cgi works. There may be some updates to the script in the near future. --- library/.htaccess | 3 + library/index.cgi | 374 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 377 insertions(+) create mode 100755 library/.htaccess create mode 100755 library/index.cgi diff --git a/library/.htaccess b/library/.htaccess new file mode 100755 index 0000000..d54535d --- /dev/null +++ b/library/.htaccess @@ -0,0 +1,3 @@ +AddHandler cgi-script .cgi +Options +Indexes +ExecCGI +DirectoryIndex index.cgi diff --git a/library/index.cgi b/library/index.cgi new file mode 100755 index 0000000..1609553 --- /dev/null +++ b/library/index.cgi @@ -0,0 +1,374 @@ +#!/usr/bin/perl + +############################################### +# Postgres SQL Library Database Web Interface # +# Matt Thiffault (January 22,2009) # +# matt.thiffault@gmail.com # +############################################### + + +use DBI; + +print "Content-type: text/html\n\n"; + +$in = $ENV{'QUERY_STRING'}; + +@pairs = split(/[&=]/,$in); +%fields = @pairs; +chomp(%fields); +$webroot = "../../.."; + +print < + + + +Computer Science Club of the University of Waterloo: Home + + +
+ + +
+HOME +Services +Office +Events +Media +Wiki +Webmail +Gallery +Stats +


+ +END + +#display the search form +if($fields{'do'} eq "home" || $fields{'do'} eq ""){ + +#isbn,title,authors,year,edition,publisher + + print < +
+

Computer Science Library Collection Search

+
+
+ + + + + + + + + + + + + + + + + + + + + +
Title:
Publisher:
Edition:
Year:
ISBN:

+
+ + +END + +} + +#query the database +if($fields{'do'} eq "query"){ + + print "

Search Results

"; + + #build query string here, query and display. + my $dbh = DBI->connect("dbi:Pg:dbname=library;host=localhost", + 'librarian', + 'KJI3RVgszrxrxe6W4gX3', + {AutoCommit => 1}) + or print "Couldn't connect to database: " . DBI->errstr; + + $searchterm = undef; + $orterm = undef; + @results; + @oresults; + $title = $fields{'title'}; + $title =~ s/[';:"]/ /g; + $publisher = $fields{'publisher'}; + $publisher =~ s/[';:"]/ /g; + $edition = $fields{'edition'}; + $edition =~ s/[';:"]/ /g; + $year = $fields{'year'}; + $year =~ s/[';:"]/ /g; + $isbn = $fields{'isbn'}; + $isbn =~ s/[';:"]/ /g; + + if($title){ + + @tmp = split(/\+/,$title); + my @keywords; + my @orwords; + push @keywords,"'%$tmp[0]%'"; + push @orwords,"'%tmp[0]%'"; + shift @tmp; + + foreach $keyword (@tmp){ + push @keywords,"AND (title ILIKE '%$keyword%')"; + push @orwords,"OR (title ILIKE '%$keyword%')"; + } + + $searchterm = "(title ILIKE @keywords)"; + $orterm = "(title ILIKE @orwords)"; + + } + + if($publisher){ + + @tmp = split(/\+/,$publisher); + my @keywords; + my @orwords; + push @keywords,"'%$tmp[0]%'"; + shift @tmp; + + foreach $keyword (@tmp){ + push @keywords,"AND (publisher ILIKE '%$keyword%')"; + push @orwords,"OR (publisher ILIKE '%$keyword%')"; + } + + if($searchterm eq undef){ + $searchterm = "(publisher ILIKE @keywords)"; + $orterm = "(publisher ILIKE @orwords)"; + }else{ + $temp = $searchterm; + $searchterm = $temp." AND (publisher ILIKE @keywords)"; + $temp = $orterm; + $orterm = $temp." OR (publisher ILIKE @keywords)"; + } + + } + + if($edition){ + + @tmp = split(/\+/,$edition); + my @keywords; + my @orwords; + push @keywords,"'%$tmp[0]%'"; + push @orwords,"'%$tmp[0]%'"; + shift @tmp; + + foreach $keyword (@tmp){ + push @keywords,"AND (edition ILIKE '%$keyword%')"; + push @keywords,"OR (edition ILIKE '%$keyword%')"; + } + + if($searchterm eq undef){ + $searchterm = "(edition ILIKE @keywords)"; + $orterm = "(edition ILIKE @keywords)" + }else{ + $temp = $searchterm; + $searchterm = $temp." AND (edition ILIKE @keywords)"; + $temp = $orterm; + $orterm = $temp." OR (edition ILIKE @keywords)"; + } + + } + + if($year){ + + @tmp = split(/\+/,$year); + my @keywords; + my @orwords; + push @keywords,"'%$tmp[0]%'"; + push @orwords,"'%$tmp[0]%'"; + shift @tmp; + + foreach $keyword (@tmp){ + push @keywords,"AND (year ILIKE '%$keyword%')"; + push @orwords,"OR (year ILIKE '%$keyword%')"; + } + + if($searchterm eq undef){ + $searchterm = "(year ILIKE @keywords)"; + $orterm = "(year ILIKE $orwords)"; + }else{ + $temp = $searchterm; + $searchterm = $temp." AND (year ILIKE @keywords)"; + $temp = $orterm; + $orterm = $temp." OR (year ILIKE @orwords)"; + + } + + } + + if($isbn){ + + @tmp = split(/\+/,$isbn); + my @keywords; + my @orwords; + push @keywords,"'%$tmp[0]%'"; + push @orwords,"'%$tmp[0]%'"; + shift @tmp; + + foreach $keyword (@tmp){ + push @keywords,"AND (isbn ILIKE '%$keyword%')"; + push @orwords,"OR (isbn ILIKE '%keyword%')"; + } + + if($searchterm eq undef){ + $searchterm = "(isbn ILIKE @keywords)"; + $orterm = "(isbn ILIKE @keywords)"; + }else{ + $temp = $searchterm; + $searchterm = $temp." AND (isbn ILIKE @keywords)"; + $temp = $orterm; + $orterm = $temp." OR (year ILIKE @orwords)"; + } + + } + + if($searchterm){ + + my $sth = $dbh->prepare("SELECT title,isbn,year,publisher,edition FROM book WHERE $searchterm;") + or print "Couldn't prepare statement: " . $dbh->errstr; + + my $oth = $dbh->prepare("SELECT title,isbn,year,publisher,edition FROM book WHERE $orterm;") + or print "Couldn't prepare statement: " . $dbh->errstr; + + $sth->execute() # Execute the query + or print "Couldn't execute statement: " . $sth->errstr; + + while($data = $sth->fetchrow_arrayref()){ + push @results, [@{$data}]; + } + + $oth->execute() + or print "Couldn't execute statement: " . $oth->errstr; + + while($ordata = $oth->fetchrow_arrayref()){ + push @oresults, [@{$ordata}]; + } + + $sth->finish; + $oth->finish; + + + if ($sth->rows == 0 && $oth->rows == 0) { + print "
No names matched

"; + }else{ + print "
"; + print ""; + + for my $book (@results){ + print ""; + for my $field (@{$book}){ + print "\n"; + } + print ""; + } + + for my $book (@oresults){ + foreach $andbook (@results){ + if(@{$book}[0] eq @{$andbook}[0]){ + $book = undef; + } + } + + if($book){ + print ""; + for my $field (@{$book}){ + print "\n"; + } + print ""; + } + } + + print "
Title ISBN Year Publisher Edition
$field
$field
"; + } + + }else{ + print "
You did not enter any search terms.

"; + } +} + +print <
+ +
-HOME -Services -Office -Events -Media -Wiki -Webmail -Gallery -Stats -


END + +open(DIR,"/users/www/www/directory.xml") or print "Couldn't open file\n"; +@items = ; +close(MENU); + +shift @items; +shift @items; +shift @items; +shift @items; +shift @items; +$items[-1] = undef; + +foreach $item (@items){ + if($item){ + + $item =~ s/[<>]//g; + $item =~ s/\/$//g; + $item =~ s/menuitem//g; + $item =~ s/ //; + @pairs = split(/[ =]/,$item); + shift @pairs; + %fields = @pairs; + + $fields{'href'} =~ s/"//g; + + chomp($fields{'href'}); + + $fields{'title'} =~ s/"//g; + + print "$fields{'title'}\n"; + + } +} + +print "


\n"; + #display the search form if($fields{'do'} eq "home" || $fields{'do'} eq ""){ diff --git a/menu.xml b/menu.xml index f9d1600..031771d 100644 --- a/menu.xml +++ b/menu.xml @@ -10,5 +10,5 @@ - + From 9dbdedea0e9e0653db361e5da7b0d9e42941e812 Mon Sep 17 00:00:00 2001 From: Matthew Thiffault Date: Wed, 11 Mar 2009 00:54:50 -0400 Subject: [PATCH 14/16] Fixed a slight problem involving the script not searching for anything. --- library/index.cgi | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/library/index.cgi b/library/index.cgi index 94334d8..7c0e8d8 100755 --- a/library/index.cgi +++ b/library/index.cgi @@ -14,9 +14,9 @@ print "Content-type: text/html\n\n"; $in = $ENV{'QUERY_STRING'}; @pairs = split(/[&=]/,$in); -%fields = @pairs; -chomp(%fields); -$webroot = "../../.."; +%field = @pairs; +chomp(%field); +$webroot = ".."; print <

\n"; #display the search form -if($fields{'do'} eq "home" || $fields{'do'} eq ""){ +if($field{'do'} eq "home" || $field{'do'} eq ""){ #isbn,title,authors,year,edition,publisher @@ -116,7 +116,7 @@ END } #query the database -if($fields{'do'} eq "query"){ +if($field{'do'} eq "query"){ print "

Search Results

"; @@ -131,15 +131,15 @@ if($fields{'do'} eq "query"){ $orterm = undef; @results; @oresults; - $title = $fields{'title'}; + $title = $field{'title'}; $title =~ s/[';:"]/ /g; - $publisher = $fields{'publisher'}; + $publisher = $field{'publisher'}; $publisher =~ s/[';:"]/ /g; - $edition = $fields{'edition'}; + $edition = $field{'edition'}; $edition =~ s/[';:"]/ /g; - $year = $fields{'year'}; + $year = $field{'year'}; $year =~ s/[';:"]/ /g; - $isbn = $fields{'isbn'}; + $isbn = $field{'isbn'}; $isbn =~ s/[';:"]/ /g; if($title){ From e325bb8059ce64d22daee98c3a65e86989f882fa Mon Sep 17 00:00:00 2001 From: Matthew Thiffault Date: Wed, 11 Mar 2009 00:59:53 -0400 Subject: [PATCH 15/16] Fixed broken links. --- library/index.cgi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/index.cgi b/library/index.cgi index 7c0e8d8..76bd6fc 100755 --- a/library/index.cgi +++ b/library/index.cgi @@ -68,7 +68,7 @@ foreach $item (@items){ $fields{'title'} =~ s/"//g; - print "$fields{'title'}\n"; + print "$fields{'title'}\n"; } } From dd1f2a234f637f900b39d82404258f62ce3ca15a Mon Sep 17 00:00:00 2001 From: Matthew Thiffault Date: Wed, 11 Mar 2009 01:13:55 -0400 Subject: [PATCH 16/16] Fixing more broken links. --- directory.xml | 9 +++++---- library/index.cgi | 11 +++++++---- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/directory.xml b/directory.xml index abf3bf2..f7e7a35 100644 --- a/directory.xml +++ b/directory.xml @@ -8,8 +8,9 @@ - - - - + + + + + diff --git a/library/index.cgi b/library/index.cgi index 76bd6fc..6195400 100755 --- a/library/index.cgi +++ b/library/index.cgi @@ -67,9 +67,12 @@ foreach $item (@items){ chomp($fields{'href'}); $fields{'title'} =~ s/"//g; - - print "$fields{'title'}\n"; - + + if($fields{'absolute'} =~ /true/){ + print "$fields{'title'}\n"; + }else{ + print "$fields{'title'}\n"; + } } } @@ -79,7 +82,7 @@ print "


\n"; if($field{'do'} eq "home" || $field{'do'} eq ""){ #isbn,title,authors,year,edition,publisher - + print <