#!/home/rebelsky/perl/bin/perl
#This script grabs the instance of the annotated text when it is ambiguous 
#on the page and the user is asked to pick an instance.  It then stores 
#the context and the correct instance.
#(POD documentation at end)

##################
# Package Begins #
##################
#package declaration
package Number;
use Exporter ();
@ISA = (Exporter);
@EXPORT = qw(number_execute);

#Import module
use AFile; 
use Search;
use Network;
use HTTP::Request;
use HTTP::Daemon;

###########################################################################
# number_execute #
###########################################################################
# This subroutine gets the user information from ravel and then grabs the
# context from around the annotated text and sends the user a response
# saying that the annotation has been added.
# ARGUMENTS: The user's name in string form, the user information in
# string form, the client connection in order to send a response to the
# client, and the original client request.
# RETURNS: Returns 1 to show that it ran correctly.
###########################################################################
sub number_execute
  {
    #read arguments
    my $name = shift;           #id of the user for this request
    my $user_info = shift;      #a string with info on the user
    my $client = shift;         #client connection
    my $client_request = shift; #request from the client

    #variables
    my $query;                  #original query string
    my $num;                    #occurence number for the annotated text
    my $tempfile;               #file address for temporary annotation
    my $temphtml;               #file address for the temporary html file
    my $dir;                    #the directory for the annotation file
    my $temp;                   #contents of temp. html file in string form
    my $text;                   #annotated text
    my $i;                      #iteration variable
    my $data;                   #the data for annotation file
    my @keys;                   #a list of keys in the fields hash
    my $file;                   #file address for the new annotation file
    my $response;               #the response to send the client
    my $response_object;        #the client response object
    local(*fields);             #the hash filled in by AFile->readFile
    
    #read and process query string
    $query = $client_request->url;
    $query =~ /\?(.*)/i;
    $query = $1;
    $query =~ /(.*)\&(.*)\&(.*)\&(.*)/i;
    $num = $1;
    $tempfile = $2;
    $temphtml = $3;
    $dir = $4;
    
    #open temporary html file, remove data, close file, and delete file 
    $temp = readInFile($temphtml);
    delFile($temphtml);
    
    #Process the data currently stored in the annotation file (subroutine 
    #in AFile module)
    readFile(*fields,$tempfile);
    
    #get rid of this temporary annotation file
    delFile($tempfile);
    
    #get the keys from the hash
    @keys = keys(%fields);
    
    #chop off extra end and beginning spaces for all of the keys
    foreach $key (@keys)
      {
        $fields{$key} =~ s/^(\s)*//i;
        $fields{$key} =~ s/(\s)*$//i;
      }                         #foreach $key (@keys)
    
    #turn the annotated text pulled from the annotation file into a search 
    #string.
    $text = $fields{'annotated_text'};
    #called from Search->textToSearch
    $text = textToSearch($text);
    
    #find the correct instance of the text in the html source code
    $i = 1;
    while($i != $num)
      {
        $temp =~ /(?:.*?\W$text)((<|[^a-zA-Z_0-9<>][^>]*?<).*)/si;
        $temp = $1;
        $i = $i + 1;
      }                         #while($i != $num)
    
    
    #grab the context
    $temp =~ /(.{0,100}\W$text(<|[^a-zA-Z_0-9<>][^>]*?<).{0,100})/si;
    $fields{'context'} = $1;
    $fields{'context'} =~ /(.{0,100}\W$text\W.{0,100})/si;
    $fields{'context'} = $1;
    $fields{'context'} =~ s/\|/%7C/sig;
    
    #make sure there are no vertical bars in the hash before storing
    foreach $item (keys(%fields))
      {
        $fields{$item} =~ s/\|/%7C/sig;
      }                         #foreach $item (keys(%fields))
    
    #reprint the annotation file with the correct information
    $data = "$fields{'author'}\n$fields{'email'}\n$fields{'date'}\/$fields{'time'}\n$fields{'title'}\n\|\n$fields{'annotated_text'}\n\|\n$fields{'annotation'}\n\|\n$fields{'context'}\n\|\n$num\n\|\n$fields{'protection'}";
    
    #find a nonexistant file in the annotation dir and then store the 
    #annotation info in the file
    $i = 1;
    while (existsAndStore("$dir\/$i.txt",$data))
      {
        $i = $i + 1;
      }                         #while (fileExists("$dir$i.txt",$data))
    
    #this file is the new annotation file
    $file = "$dir\/$i.txt";
    
    #print confirmation message
    $response = <<"EOF"; 
<html>
<meta name=\"Ravel\" content=\"disallow: .*?-.*?-.*?-.*?\$\">
<head>
<script language=\"javascript1.1\">
function pageReload()
  {
    opener.location.reload(true)
  }
<\/script>
<title>Annotation Submitted<\/title>
<\/head>
<body bgcolor=\"white\" onLoad=\"pageReload()\">
<h1>Annotation Submitted<\/h1>
<p>
Your annotation has been submitted.
<\/p>
<form>
<input type=\"button\" onClick=\"self.close()\" name=\"OK\" value=\"OK\">
<\/form>
<\/body>
<\/html>
EOF

    #create a client response oject and send a response
    $response_object = HTTP::Response->new(200);
    $response_object->content($response);
    $client->send_response($response_object);

    #return the true value to show that it ran correctly
    return 1;
  }#sub number_execute

#return the true value to show that the package loaded correctly
return 1;

#####################
# Pod Documentation #
#####################
=pod

=head1 ID

=over 4

=item Package

Number.pm (Previously known as number.cgi)

=item Author

Rachel Heck

=item Description

This script grabs the instance of the annotated text when it is ambiguous 
on the page and the user is asked to pick an instance.  It then stores the 
context and the correct instance.

=back

=head1 Subroutines

=over 4

=item number_execute

This subroutine gets the user information from ravel and then grabs the 
context from around the annotated text and sends the user a response 
saying that the annotation has been added.

=back

=head1 History

=over 4

=item [9 July 1999]

The script runs correctly and is fully commented.

=item [12 July 1999]

This program was modified to grab up to 100 characters on either side of the 
annotated text for the context as opposed to only 50.

=item [13 July 1999]

Uses the networked database.

=item [14 July 1999]

Allows newlines in the context.  Gets rid of extra spaces in the fields 
hash.  Reads from the unique temporary html and annotation files that 
Ambiguity->whichOne creates.

=item [15 July 1999]

Fixed a bug in grabbing the right instances context.  It also now uses 
Network->existsAndStore instead of Network->fileExists followed by 
Network->writeFile.

=item [16 July 1999]

Fixed a bug so that the context could only be as large as the annotated 
text plus 101 characters on each side.  Pop-up annotation windows will not 
use plug-ins in the Ravel server anymore.  The package containing readFile 
is now called AFile instead of Annotation.

=item [23 July 1999]

When an ambiguous annotation is successfully added, the main browser window is reloaded.

=item [27 July 1999]

Edited the reload command so that it does an unconditional reload (it will not reload the page from cache).

=item [30 July 1999]

Vertical bars are escaped before storing the new file.

=item [4 August 1999]

Turned the script (number.cgi) into a package (Number.pm) in 
order to use Proxy URLs.  This adds security.  All cgi calls are now Proxy 
URLs.  The name, email, and groups information are no longer being passed 
to cgi scripts.

=item [5 August 1999]

The subroutine number_execute is working and is fully commented.

=back

=cut

