version 1.20, 2001/03/15 18:51:09
|
version 1.43, 2001/03/20 12:21:56
|
Line 2
|
Line 2
|
# Search Catalog |
# Search Catalog |
# |
# |
# 03/08/2001 Scott Harrison |
# 03/08/2001 Scott Harrison |
|
# Scott Harrison: 03/12/2001, 03/13/2001, 03/14/2001, 03/15/2001, 03/19/2001 |
|
# Scott Harrison: 03/20/2001 |
# |
# |
|
# Functions |
|
# |
|
# handler(server reference) : interacts with the Apache server layer |
|
# (for /adm/searchcat URLs) |
|
# simpletextfield(name,value) : returns HTML formatted string for simple text |
|
# field |
|
# simplecheckbox(name,value) : returns HTML formatted string for simple |
|
# checkbox |
|
# searchphrasefield(title,name,value) : returns HTML formatted string for |
|
# a search expression phrase field |
|
# dateboxes(name, defaultmonth, defaultday, defaultyear) : returns HTML |
|
# formatted string |
|
# for a calendar date |
|
# selectbox(title,name,value,%HASH=options) : returns HTML formatted string for |
|
# a selection box field |
|
# advancedsearch(server reference, environment reference) : perform a complex |
|
# multi-field logical query |
|
# filled(field) : determines whether a given field has been filled |
|
# basicsearch(server reference, environment reference) : perform a simple |
|
# single-field logical query |
|
# output_blank_field_error(server reference) : outputs a message saying that |
|
# more fields need to be filled in |
|
# output_results(output mode, |
|
# server reference, |
|
# environment reference, |
|
# reply list reference) : outputs results from search |
|
# build_SQL_query(field name, logic) : builds a SQL query string from a |
|
# logical expression with AND/OR keywords |
|
# recursive_SQL_query_build(field name, reverse notation expression) : |
|
# builds a SQL query string from a reverse notation expression |
|
# logical expression with AND/OR keywords |
|
|
package Apache::lonsearchcat; |
package Apache::lonsearchcat; |
|
|
use strict; |
use strict; |
Line 10 use Apache::Constants qw(:common);
|
Line 44 use Apache::Constants qw(:common);
|
use Apache::lonnet(); |
use Apache::lonnet(); |
use Apache::File(); |
use Apache::File(); |
use CGI qw(:standard); |
use CGI qw(:standard); |
|
use Text::Query; |
|
|
my %language; |
my %language; |
my $scrout; |
my $scrout; |
Line 232 ENDDOCUMENT
|
Line 267 ENDDOCUMENT
|
|
|
# --------------------------------------------------------- Various form fields |
# --------------------------------------------------------- Various form fields |
|
|
sub textfield { |
|
my ($title,$name,$value)=@_; |
|
return "\n<p><b>$title:</b><br>". |
|
'<input type=text name="'.$name.'" size=80 value="'.$value.'">'; |
|
} |
|
|
|
sub simpletextfield { |
sub simpletextfield { |
my ($name,$value)=@_; |
my ($name,$value)=@_; |
return '<input type=text name="'.$name.'" size=20 value="'.$value.'">'; |
return '<input type=text name="'.$name.'" size=20 value="'.$value.'">'; |
Line 424 sub advancedsearch {
|
Line 453 sub advancedsearch {
|
my ($r,$envhash)=@_; |
my ($r,$envhash)=@_; |
my %ENV=%{$envhash}; |
my %ENV=%{$envhash}; |
|
|
|
my $fillflag=0; |
|
for my $field ('title','author','subject','keywords','url','version', |
|
'notes','abstract','mime','language','owner', |
|
'custommetadata') { |
|
if (&filled($ENV{"form.$field"})) { |
|
$fillflag++; |
|
} |
|
} |
|
|
|
unless ($fillflag) { |
|
&output_blank_field_error($r); |
|
return OK; |
|
} |
|
|
|
my $query=''; |
|
# my $concatarg=join('," ",', |
|
# ('title', 'author', 'subject', 'notes', 'abstract')); |
|
|
|
$query="select * from metadata where concat(title) like '\%$ENV{'form.title'}\%'"; |
|
my $reply=&Apache::lonnet::metadata_query($query); |
|
|
|
&output_results('Advanced',$r,$envhash,$reply); |
|
return OK; |
|
} |
|
|
|
# ---------------------------------------------------- see if a field is filled |
|
sub filled { |
|
my ($field)=@_; |
|
if ($field=~/\S/) { |
|
return 1; |
|
} |
|
else { |
|
return 0; |
|
} |
} |
} |
|
|
# --------------------------------------------------- Performing a basic search |
# --------------------------------------------------- Performing a basic search |
Line 431 sub basicsearch {
|
Line 494 sub basicsearch {
|
my ($r,$envhash)=@_; |
my ($r,$envhash)=@_; |
my %ENV=%{$envhash}; |
my %ENV=%{$envhash}; |
|
|
my $query=$ENV{'form.basicexp'}; |
unless (&filled($ENV{'form.basicexp'})) { |
$query="select * from metadata where concat(title,\" \",author) like '\%$ENV{'form.basicexp'}\%'"; |
&output_blank_field_error($r); |
|
return OK; |
|
} |
|
|
|
my $query=''; |
|
my $concatarg=join('," ",', |
|
('title', 'author', 'subject', 'notes', 'abstract')); |
|
|
|
$query="select * from metadata where concat($concatarg) like '\%$ENV{'form.basicexp'}\%'"; |
my $reply=&Apache::lonnet::metadata_query($query); |
my $reply=&Apache::lonnet::metadata_query($query); |
&output_results($r,$envhash,$reply); |
&output_results('Basic',$r,$envhash,$reply); |
return OK; |
return OK; |
} |
} |
|
|
|
sub output_blank_field_error { |
|
my ($r)=@_; |
|
# make query information persistent to allow for subsequent revision |
|
my $persistent=''; |
|
map { |
|
if (/^form\./ && !/submit/) { |
|
my $name=$_; |
|
my $key=$name; |
|
$name=~s/^form\.//; |
|
$persistent.=<<END; |
|
<INPUT TYPE='hidden' NAME='$name' VALUE='$ENV{$key}'> |
|
END |
|
} |
|
} (keys %ENV); |
|
|
|
$r->print(<<BEGINNING); |
|
<html> |
|
<head> |
|
<title>The LearningOnline Network with CAPA</title> |
|
BEGINNING |
|
$r->print(<<RESULTS); |
|
</head> |
|
<body bgcolor="#ffffff"> |
|
<img align=right src=/adm/lonIcons/lonlogos.gif> |
|
<h1>Search Catalog</h1> |
|
<form method="post" action="/adm/searchcat"> |
|
$persistent |
|
<input type='button' value='Revise search request' |
|
onClick='this.form.submit();'> |
|
<input type='button' value='CLOSE' |
|
onClick='self.close();'> |
|
<hr> |
|
<h3>Helpful Message</h3> |
|
<p> |
|
Incorrect search query due to blank entry fields. |
|
You need to fill in the relevant |
|
fields on the search page in order for a query to be |
|
processed. |
|
</p> |
|
</body> |
|
</html> |
|
RESULTS |
|
} |
|
|
# ----------------------------- format and output results based on a reply list |
# ----------------------------- format and output results based on a reply list |
sub output_results { |
sub output_results { |
my ($r,$envhash,@replylist)=@_; |
my ($mode,$r,$envhash,@replylist)=@_; |
my %ENV=%{$envhash}; |
my %ENV=%{$envhash}; |
foreach my $reply (@replylist) { |
foreach my $reply (@replylist) { |
|
|
Line 483 onClick="javascript:select_data('$title'
|
Line 598 onClick="javascript:select_data('$title'
|
<br> |
<br> |
END |
END |
$compiledresult.=<<END; |
$compiledresult.=<<END; |
<b>URL: </b> <A HREF="http://$ENV{'HTTP_HOST'}/$url" TARGET='search_preview'>$url</A> |
<b>URL: </b> <A HREF="http://$ENV{'HTTP_HOST'}$url" TARGET='search_preview'>$url</A> |
<br> |
<br> |
<b>Title:</b> $title<br> |
<b>Title:</b> $title<br> |
<b>Author(s):</b> $author<br> |
<b>Author(s):</b> $author<br> |
Line 555 onClick='self.close();'>
|
Line 670 onClick='self.close();'>
|
$persistent |
$persistent |
<hr> |
<hr> |
<h3>Search Query</h3> |
<h3>Search Query</h3> |
|
RESULTS |
|
if ($mode eq 'Basic') { |
|
$r->print(<<RESULTS); |
<p> |
<p> |
<b>Basic search:</b> $ENV{'form.basicexp'} |
<b>Basic search:</b> $ENV{'form.basicexp'} |
</p> |
</p> |
|
RESULTS |
|
elsif ($mode eq 'Advanced') { |
|
$r->print(<<RESULTS); |
|
<p> |
|
<b>Advanced search</b> |
|
</p> |
|
RESULTS |
|
} |
<h3>Search Results</h3> |
<h3>Search Results</h3> |
$compiledresult |
$compiledresult |
</body> |
</body> |
Line 566 RESULTS
|
Line 692 RESULTS
|
} |
} |
} |
} |
|
|
|
# ------------------------------------------------------------- build_SQL_query |
|
sub build_SQL_query { |
|
my ($field_name,$logic_statement)=@_; |
|
my $q=new Text::Query('abc', |
|
-parse => 'Text::Query::ParseAdvanced', |
|
-build => 'Text::Query::Build'); |
|
$q->prepare($statement); |
|
my $matchexp=${$q}{'matchexp'}; chomp $matchexp; |
|
my $sql_query=&recursive_SQL_query_build($field_name,$matchexp); |
|
} |
|
|
|
# - Recursively parse a reverse notation expression into a SQL query expression |
|
sub recursive_SQL_query_build { |
|
my ($dkey,$pattern)=@_; |
|
my @matches=($pattern=~/(\[[^\]|\[]*\])/g); |
|
return $pattern unless @matches; |
|
foreach my $match (@matches) { |
|
$match=~/\[ (\w+)\s(.*) \]/; |
|
($key,$value)=($1,$2); |
|
my $replacement=''; |
|
if ($key eq 'literal') { |
|
$replacement="($dkey like \"\%$value\%\")"; |
|
} |
|
elsif ($key eq 'and') { |
|
$value=~/(.*[\"|\)]) ([|\(|\^].*)/; |
|
$replacement="($1 AND $2)"; |
|
} |
|
elsif ($key eq 'or') { |
|
$value=~/(.*[\"|\)]) ([|\(|\^].*)/; |
|
$replacement="($1 OR $2)"; |
|
} |
|
substr($pattern, |
|
index($pattern,$match), |
|
length($match), |
|
$replacement |
|
); |
|
} |
|
&recursive_SQL_query_build($dkey,$pattern); |
|
} |
|
|
1; |
1; |
__END__ |
__END__ |