home -- outline -- lectures -- assignments -- discussion -- tips -- links --



Assignment 8: a Virtual Museum

In this homework you will implement a virtual museum. That is you will use cgi's to dynamically create pages with paintings, painters and more information. This assignment will be worth 20 credit points. (You can get an additional 20 points in extra credit.)

The canvasses

You do not have to worry about getting the data. All the data is in the directory /usr/local/classes/CS101//html/assignments/VM. You don't even have to copy the data from this place into your own directories. Just point all your links there.
 
Warning: for your cgi's this means you have to use absolute url's when you are searching a file in directory url. For example use
 
open(FILE, "/usr/local/classes/CS101/html/assignments/VM/painters/bio1.txt");

to open the first biography for reading. To make your life easier you should define a variable at the beginning of your cgi's which contains the path information that does not change, e.g.
 
$VM = "/usr/local/classes/CS101/html/assignments/VM/"
 
Then the code for opening the file can be shortened to:
 
open(FILE, "$VM/painters/bio1.txt");

 
Before we describe the assignment in detail let us first see what kind of data you will be dealing with. There are two directories in the VM directory: painters and paintings one containing information about painters, the other about paintings. In the painter directory you will find six files painter1.txt to painter6.txt and bio1.txt up to bio6.txt. Files with the same number belong together. The painter files contains information about the painter, for example painter1.txt looks like this:
 
Bocklin:Arnold:1827:1901:Swiss

The format is last name : first name(s) : date of birth : date of death : nationality followed by a newline. In case any of the data is not known, the corresponding field will be empty. The bio files contain several paragraphs of text containing a biography of the painter. Hence bio1.txt for example contains a biography of Arnold Bocklin. The paintings directory contains data about paintings. There are three types of files: title files, picture files and thumb files. The title files contain information about a certain painting as follows:
 
last name : first name : title of painting : style of painting : date of painting

For example title4.txt looks like this:
 
Caravaggio:Michelangelo Merisi:Narcissus:oil on canvas:

Note that there is no date for this painting, the field is empty. As you probably guessed, thumb4.jpg and pict4.jpg contain a thumbnail of the painting and the painting itself.

The Museum

Now that you know what data there is for you to use, let us describe the functionaility your virtual museum should implement. You will create a main page for your museum which includes forms and links to cgi's which allow you to do the following: More specifically do the following:

painting.cgi

Write a cgi painting.cgi which takes as input a number (suppose the field with the number is called painting) of a painting (in our case a number from 1 to 10, since we only have ten paintings) and displays a dynamically generated page for the painting having this number. The page should contain the title of the painting, the date it was painted, the style, and finally the painting itself (if you think the paintings are too large for your layout, use the width (or height) attribute of <IMG> to resize them). Since the &param function works with both POST and GET you can use your cgi to run with forms using both methods, and have direct links to it, e.g.
<a href="painting.cgi?painting=4">Narcissus</a>.

painter.cgi

Write a cgi painter.cgi which takes as input the number of a painter (in our case a number from 1 to 6, since we only have six painters) and dynamically generates a web-page for this painter, containing The first two pieces of information you get out of the painter and biography files. For the list of paintings use a while or for loop to look at all the title files in the painting directory, and display the ones which were painted by the painter whose first and last name you extracted from the painter file. You don't have to do any pattern matching here, since the names in the painter and titles files are the same, so string comparison via eq is good enough.
 
The link to the page with the actual painting will be a link to painting.cgi with the number of the painting as a value for painting. For example if you just found out that Caravaggio painted Narcissus your cgi should generate a link like the following:
<a href="painting.cgi?painting=4">Narcissus</a>.

Title Search

Include a form on your main page with a text input field which allows you to specify parts of a title. The information should be submitted to a cgi, say, search_title.cgi. This cgi will look at all the title files and return a list of links (to painting.cgi !) with thumbnails and names of paintings that match the keywords you specified in the form.
 
There are several ways to implement the search. You might compare titles exactly as strings (using eq). Or you might consider the search title as a pattern and try to match it against the titles you find in the files. In the last case you should do some preprocessing on your search string, for example substitute multiplie spaces (which a user might have entered by accident) by a single space (hint: try $search_string =~ /\s+/ /g).

Painter Search

Include another form which (as in the title search) allows you to search for a painter. Again return a list, in this case of possible painters with links to the corresponding painter pages (generated by painting.cgi !). The strategies described above for title search also apply to painter search, but here you should have two fields: first name and last name. The user should be able to specify both, one, or none (in the last case he should get a listing of all painters).
 
(Extra credit: the more adventurous should try to find a solution to the following problem: suppose somebody entered Rembrandt. Should that be searched for in the first name field or the last name field? For that matter what about Caravaggio and Michelangelo? The people who know the answers will probably not be the people looking at our virtual museum web-site [they probably have expensive books with color reproductions at home], so we should adapt the search in such a way that Caravaggio would be found if it was entered in the first name or the last name field.)

Keyword search

Finally there is the keyword search. On the form the user should be able to enter any number of keywords into the text field and get a list of painter and painting links that match any of these keywords (so Caravaggio should get me a link to the painter page for Caravaggio and a link for each of his paintings).
 
How do we deal with several keywords? What does it mean if the user enters
 
France Italy

We'll use the following convention: a list of keywords will match anything that matches any of the kewyords. In the above example every link relating to either Italy or France should be returned.
 
How do we do this in Perl? The answer is: use the following piece of code (suppose the keywords we are searching for are in the variable $keywords and the text we are searching is in $text).
 
  $search = join("|",split(/\s+/,$keywords));
  if ($text =~ /$search/i) {
      whatever you want to do in case the
      search is successful
  };

 
Let's try to understand what this code does: it takes the variable $keywords and splits it up by whitespaces into several keywords and then joins them back together putting a bar (|) where the spaces were. So $search will contain a pattern that tries to match any of the keywords. The i after the pattern makes the pattern case insensitive.
 
The following piece of code will search all the biography files for the pattern $keyword.
$VM = "/usr/local/classes/CS101/html/assignments/VM";
foreach $counter (1,2,3,4,5,6) {
      open (FILE, "$VM/painters/bio$counter.txt");
            $_ = <FILE>;
            while (!eof && !($_ =~ /$keyword/)) {
               $_ = <FILE>; };
            if ($_ =~ $keyword) { print "FOUND KEYOWORD IN BIO$counter.txt"; };
      close FILE;
};
Note that we are testing for whether we have found the end of the file (eof). If so we have to stop searching (otherwise we would run into an infinite loop.

Extra credit

There are several things you can do to get extra credit in order of increasing difficulty:

Acknowledgemnt

The graphics and bios for the Virtual Museum are all taken from Carol Jackson's Virtual Art Museum.