22
Project: search.cgi cs149 - PERL programming Submitted by: Sheshagiri Pai Instructor and Guide: Professor Jon Degallier Date: 05/22/2006

Project: search.cgi cs149 - PERL programming Submitted by: Sheshagiri Pai Instructor and Guide: Professor Jon Degallier Date: 05/22/2006

Embed Size (px)

Citation preview

Page 1: Project: search.cgi cs149 - PERL programming Submitted by: Sheshagiri Pai Instructor and Guide: Professor Jon Degallier Date: 05/22/2006

Project: search.cgics149 - PERL programming

Submitted by: Sheshagiri Pai

Instructor and Guide: Professor Jon Degallier

Date: 05/22/2006

Page 2: Project: search.cgi cs149 - PERL programming Submitted by: Sheshagiri Pai Instructor and Guide: Professor Jon Degallier Date: 05/22/2006

Executive Summary

• Fetch strings according to search criteria

• From the messages in the message board

• Display the search results

• Link the search results to the messages by message id

Page 3: Project: search.cgi cs149 - PERL programming Submitted by: Sheshagiri Pai Instructor and Guide: Professor Jon Degallier Date: 05/22/2006

Setup

• Place search.cgi file in the student home directory by ftp

• chmode the file to 755

• Login as user tt2 with password *********

• Go to discussion board

• Click on the Search menu item

• This should bring up The “SEARCH” page.

Page 4: Project: search.cgi cs149 - PERL programming Submitted by: Sheshagiri Pai Instructor and Guide: Professor Jon Degallier Date: 05/22/2006

Search page description

• Search Criteria:

• Any - any word present in the message

• All the words - in no particular order

• All the words - in order

• Expression - using AND or OR

• Search input - text box for the words to be searched

• Exact Match - if checked, else search is case-insensitive

• Search button - for carrying out the search and displaying the results

Page 5: Project: search.cgi cs149 - PERL programming Submitted by: Sheshagiri Pai Instructor and Guide: Professor Jon Degallier Date: 05/22/2006

Query string params

• When search menu item is clicked, the search.cgi page is invoked with the following parameters: querystring:

class=26930&&admin_id=jond&admin_name=Jon%20Degallier&board_name=26930&board_code=jond26930&name=Sheshagiri%20Pai&[email protected]&userid=spai&group=1&inst

=ohlone

Page 6: Project: search.cgi cs149 - PERL programming Submitted by: Sheshagiri Pai Instructor and Guide: Professor Jon Degallier Date: 05/22/2006

Main Algorithm• if (!param('search_data')) {

&create_form();}else

{&print_search_before_result();&get_n_prepare_data();foreach $topic(@topics)

{&get_messages($topic);foreach $message_file(@message_files)

{&separate_thread($message_file);foreach $message (@thread_array)

{ if(&find_match())

{&process_found_message();

&print_search_result(); }

} }}

&print_search_after_result(); }

Page 7: Project: search.cgi cs149 - PERL programming Submitted by: Sheshagiri Pai Instructor and Guide: Professor Jon Degallier Date: 05/22/2006

Search Form

Page 8: Project: search.cgi cs149 - PERL programming Submitted by: Sheshagiri Pai Instructor and Guide: Professor Jon Degallier Date: 05/22/2006

Search Form - contd..

Page 9: Project: search.cgi cs149 - PERL programming Submitted by: Sheshagiri Pai Instructor and Guide: Professor Jon Degallier Date: 05/22/2006

Search Results

Page 10: Project: search.cgi cs149 - PERL programming Submitted by: Sheshagiri Pai Instructor and Guide: Professor Jon Degallier Date: 05/22/2006

Search Results - Linked Message

Page 11: Project: search.cgi cs149 - PERL programming Submitted by: Sheshagiri Pai Instructor and Guide: Professor Jon Degallier Date: 05/22/2006

Create Form• The parameters from the query string are gathered and passed as hidden parameters• Also passed are the parameters for ‘search text’ and ‘exact match checkbox’ $userid = param('userid'); $board_code = param('board_code'); $admin_id = param('admin_id'); $class = param('class'); $board_name = param('board_name'); $topic_name = param('topic_name'); $name = param('name'); $group = param('group'); $email = param('email'); $admin_name = param('admin_name'); $admin_email = param('admin_email');

Page 12: Project: search.cgi cs149 - PERL programming Submitted by: Sheshagiri Pai Instructor and Guide: Professor Jon Degallier Date: 05/22/2006

Create Form - contd..• <html>

<body background = "http://207.62.192.204/~tt2/wbimages/back.gif" bgcolor="lightblue"> <br><font color=blue><h1>SEARCH</h4></font> <br> <h2>Enter the string to search in the message boards</h2>

<br> <form action="search.cgi?">

<p><input type="radio" name="option" value="1">Any <input type="radio" name="option" value="2">All NO order <input type="radio" name="option" value="3">All IN order <input type="radio" name="option" value="4">Expression <p>

Search String: <input type="text" name="search_data"/> Exact Match: <input type="checkbox" name="match"/>

<input type="submit" value="Search"/><input type="hidden" name="userid" value=$userid/><input type="hidden" name="board_name" value=$board_name/>

<input type="hidden" name="board_code" value=$board_code/> <input type="hidden" name="admin_id" value=$admin_id/> <input type="hidden" name="class" value=$class/> <input type="hidden" name="topic_name" value=$topic_name/> <input type="hidden" name="name" value=$name/> <input type="hidden" name="group" value=$group/> <input type="hidden" name="email" value=$email/> <input type="hidden" name="admin_name" value=$admin_name/> <input type="hidden" name="admin_email" value=$admin_email/>

</form> </body> </html>

Page 13: Project: search.cgi cs149 - PERL programming Submitted by: Sheshagiri Pai Instructor and Guide: Professor Jon Degallier Date: 05/22/2006

Highlights : Existing methods• get_n_prepare_data()•open( INPUT, "< ../../teachers/jond/boards/$board_name/topics.dat") or

die("could not open boards/$board_name/topics.dat\n</br>"); @topics = reverse(@topics);

• get_messages()@message_files = < ../../teachers/jond/boards/$board_name/$topic/*>;

• separate_thread() if($message_file =~ /\d$/) {

open(INPUT, "<$message_file") or die("cannot open $message_file\n</br>");

@thread = <INPUT>;close INPUT;

}# split the message by 'message_id::' to into a 'thread' array $thread = join("\n", @thread);@thread_array = split(/message_id:: /, $thread);

Page 14: Project: search.cgi cs149 - PERL programming Submitted by: Sheshagiri Pai Instructor and Guide: Professor Jon Degallier Date: 05/22/2006

Find Match• sub find_match() {

$menu_item = param('option');# Take action based on the user's choice

if ($menu_item == 1) { return &search_any(); } elsif ($menu_item == 2) { return &search_all_no_order(); } elsif ($menu_item == 3) { return &search_all_in_order();

} elsif ($menu_item == 4) { return &search_with_expression(); } else { print ”<br><font color=red>You have to select a

criteria\n</font><br>"; exit; } }

Page 15: Project: search.cgi cs149 - PERL programming Submitted by: Sheshagiri Pai Instructor and Guide: Professor Jon Degallier Date: 05/22/2006

Find Match - search_any()

sub search_any() {#print "Search Any\n";# method 1: find any of the terms in one message

# loop through search array and search message content $search = 0; @search_any_term = split(" ", "@search_terms"); foreach(@search_any_term) { if($match eq "on")

{ if($message =~ m/\b$_\b/) { $search = 1; last; } } else { if($message =~ m/\b$_\b/i) { $search = 1; last; }

} } return $search; }

Page 16: Project: search.cgi cs149 - PERL programming Submitted by: Sheshagiri Pai Instructor and Guide: Professor Jon Degallier Date: 05/22/2006

Find Match - search_all_no_order() sub search_all_no_order() {

#print "Search All in No order\n";# method 2: find all the terms in one message

$search = 0; @search_any_term = split(" ", "@search_terms"); foreach(@search_any_term) { if($match eq "on") { { $search = 1; } else { $search = 0; last; }

} else { if($message =~ m/\b$_\b/i) { $search = 1; } else { $search = 0; last; }

} } return $search; }

Page 17: Project: search.cgi cs149 - PERL programming Submitted by: Sheshagiri Pai Instructor and Guide: Professor Jon Degallier Date: 05/22/2006

Find Match - search_all_in_order()

•sub search_all_in_order() {

#print "Search All in order\n"; # method 3: find all the terms in order $search = 0; $search_all_order = "@search_terms"; if($match eq "on") { if($message =~ m/\b$search_all_order\b/) { $search = 1; } else { $search = 0; } } else { if($message =~ m/\b$search_all_order\b/i) { $search = 1; } else { $search = 0; } } return $search; }

Page 18: Project: search.cgi cs149 - PERL programming Submitted by: Sheshagiri Pai Instructor and Guide: Professor Jon Degallier Date: 05/22/2006

Find Match - search_with_expression()

• Show in actual code

Page 19: Project: search.cgi cs149 - PERL programming Submitted by: Sheshagiri Pai Instructor and Guide: Professor Jon Degallier Date: 05/22/2006

Process Found Message• sub process_found_message() {

# grab writer's name, message id, date, and subject# concatenate the above in one string separated by pipes(|)# push string to $found string

$message = "@thread_array";

&trim($message); #print($message);

$message_id_only = substr $message, 0, index($message, 'date_time::'); $message_id = "message_id:: "."$message_id_only"; #print("$message_id\n");

my $subject = substr $message, index($message, 'subject:: '), index($message, 'attach:: ')-index($message, 'subject:: ');#print("$subject\n");

$found = "$message_id"."|"."$date_time"."|"."$from"."|"."$subject";

#print("<br>$found<br>");}

Page 20: Project: search.cgi cs149 - PERL programming Submitted by: Sheshagiri Pai Instructor and Guide: Professor Jon Degallier Date: 05/22/2006

Print Search Result• sub print_search_result() {

#foreach $found_message(@thread_array) {# build a table of all found messages with links #containing all cgi variables in a string

$userid_new = substr($userid, 0, -1); $name_new = substr($name, 0, -1); $email_new = substr($email, 0, -1); $admin_id_new = substr($admin_id, 0, -1); $admin_name_new = substr($admin_name, 0, -1); $board_name_new = substr($board_name, 0, -1); $board_code_new = substr($board_code, 0, -1); $group_new = substr($group, 0, -1); $message_id_only =~ s/^\s+//; #print($message_id_only);

Page 21: Project: search.cgi cs149 - PERL programming Submitted by: Sheshagiri Pai Instructor and Guide: Professor Jon Degallier Date: 05/22/2006

Print Search Result - contd..• print("<tr><td><a href=\"new_message.cgi?userid=$userid_new&name=$name&email=$email_new&". "admin_id=$admin_id_new&admin_name=$admin_name_new&board_name=$board_name_new&board_code=$board_code_new&".

"group=$group_new&topic_name=$topic_path&message_id=$message_id_only\" target = \"_blank\">$found</a></td></tr>"); #} #end loop of found array }

Page 22: Project: search.cgi cs149 - PERL programming Submitted by: Sheshagiri Pai Instructor and Guide: Professor Jon Degallier Date: 05/22/2006

Thanks Q & A