#!/usr/bin/perl # zoom.cgi - a simple Web-based Z39.59 client # Eric Lease Morgan # 2007-06-15 - first investigations; based on zoom.pl # define use constant RESULTS => './etc/results.txt'; use constant SERVER => 'localhost:9999/catalog'; use constant TEMPLATE => './etc/template.txt'; # require/include use CGI; use CGI::Carp qw( fatalsToBrowser ); use MARC::Record; use strict; use ZOOM; # initialize my $cgi = CGI->new; my $html = ''; # get the command my $cmd = $cgi->param( 'cmd' ); if ( ! $cmd ) { # create the home page my $html = &slurp( TEMPLATE ); $html =~ s/##CONTENT##//e; $html =~ s/##QUERY##//e; # done &gracefulExit ( $html ); } elsif ( $cmd eq 'search' ) { # initialize my $query = $cgi->param( 'query' ); my $hitlist; # create a connection and search my $connection = new ZOOM::Connection( SERVER, 0, count => 1, preferredRecordSyntax => "usmarc" ); my $results = $connection->search_pqf( qq["$query"] ); my $hits = $results->size; # loop through (some of) the hits for ( my $i = 0; $i < $hits; $i++ ) { # get the record my $record = $results->record( $i )->raw; my $marc = MARC::Record->new_from_usmarc( $record ); # extract the subjects my $subjects; foreach my $subject_field ( $marc->field( '6..' )) { $subjects .= $subject_field->as_string . '; ' } # create an item display my $item = $cgi->li( 'author: ' . $marc->author ); $item .= $cgi->li( 'date: ' . $marc->publication_date ); $item .= $cgi->li( 'subjects: ' . $subjects ); $item = $cgi->ul( $item ); # add it and the title to the hit list $hitlist .= $cgi->li( $marc->title_proper . $item ); } # finish the list $hitlist = $cgi->ol( $hitlist ); # create the results page my $html = &slurp( TEMPLATE ); $html =~ s/##CONTENT##/&slurp( RESULTS )/e; $html =~ s/##QUERY##/$query/e; $html =~ s/##HITLIST##/$hitlist/e; # done &gracefulExit ( $html ); } else { # unknown command &error( "Error: unknown value for cmd ($cmd). Call Eric." ); } ############# # subroutines sub slurp { # open a file named by the input and return its contents my $f = @_[0]; my $r; open (F, "< $f"); while () { $r .= $_ } close F; return $r; } # build an error screen sub error { my $msg = shift; $msg = $cgi->p( $msg ); my $html = &slurp( TEMPLATE ); $html =~ s/##CONTENT##/$msg/; &gracefulExit ( $html ); } # quit the program sub gracefulExit { my $html = shift; print $cgi->header( -type=>'text/html', -charset => 'UTF-8' ); print $html; exit; }