--- loncom/interface/lonsearchcat.pm 2002/06/20 19:43:50 1.125
+++ loncom/interface/lonsearchcat.pm 2002/06/25 15:08:59 1.129
@@ -1,7 +1,7 @@
# The LearningOnline Network with CAPA
# Search Catalog
#
-# $Id: lonsearchcat.pm,v 1.125 2002/06/20 19:43:50 matthew Exp $
+# $Id: lonsearchcat.pm,v 1.129 2002/06/25 15:08:59 matthew Exp $
#
# Copyright Michigan State University Board of Trustees
#
@@ -67,14 +67,14 @@ search (on a server basis) is displayed
###############################################################################
###############################################################################
+###############################################################################
## ##
## ORGANIZATION OF THIS PERL MODULE ##
## ##
## 1. Modules used by this module ##
-## 2. Choices for different output views (detailed, summary, xml, etc) ##
-## 3. BEGIN block (to be run once after compilation) ##
-## 4. Handling routine called via Apache and mod_perl ##
-## 5. Other subroutines ##
+## 2. Variables used throughout the module ##
+## 3. handler subroutine called via Apache and mod_perl ##
+## 4. Other subroutines ##
## ##
###############################################################################
@@ -101,18 +101,6 @@ use Apache::loncommon();
=over 4
-=item %hostdomains
-
-matches host name to host domain
-
-=item %hostips
-
-matches host name to host ip
-
-=item %hitcount
-
-stores number of hits per host
-
=item $closebutton
button that closes the search window
@@ -137,46 +125,13 @@ used in &handler() and is also used in &
######################################################################
######################################################################
-# -- information holders
-my %hostdomains; # matches host name to host domain
-my %hostips; # matches host name to host ip
-my %hitcount; # stores number of hits per host
-
# -- dynamically rendered interface components
my $closebutton; # button that closes the search window
my $importbutton; # button to take the selected results and go to group sorting
# -- miscellaneous variables
-my $yourself; # allows for quickly limiting to oneself
my %hash; # database hash
-
-# ------------------------------------------ choices for different output views
-# Detailed Citation View ---> sub detailed_citation_view
-# Summary View ---> sub summary_view
-# Fielded Format ---> sub fielded_format_view
-# XML/SGML ---> sub xml_sgml_view
-
-#------------------------------------------------------------- global variables
-my $diropendb = "";
-my $domain = "";
-
-# ----------------------------------------------------------------------- BEGIN
-
-=pod
-
-=item BEGIN block
-
-Load %hostdomains and %hostips with data from lonnet.pm. Only library
-servers are considered.
-
-=cut
-
-BEGIN {
- foreach (keys (%Apache::lonnet::libserv)) {
- $hostdomains{$_}=$Apache::lonnet::hostdom{$_};
- $hostips{$_}=$Apache::lonnet::hostip{$_};
- }
-}
+my $diropendb = ""; # db file
######################################################################
######################################################################
@@ -318,7 +273,8 @@ sub basic_search_form{
$hidden
Basic Search
-Enter terms or phrases separated by AND, OR, or NOT then press SEARCH below.
+Enter terms or quoted phrases separated by AND, OR, or NOT
+then press SEARCH below.
@@ -367,6 +323,23 @@ Returns a scalar which holds html for th
sub advanced_search_form{
my ($closebutton,$hidden) = @_;
+ my $advanced_buttons = <<"END";
+
+
+
+$closebutton
+
+
+
+
+
+END
my $scrout=<<"ENDHEADER";
@@ -381,34 +354,44 @@ sub advanced_search_form{
-
Search Catalog
-
@@ -515,7 +482,8 @@ ENDDOCUMENT
=item &make_persistent()
Returns a scalar which holds the current ENV{'form.*'} values in
-a 'hidden' html input tag.
+a 'hidden' html input tag. This allows search interface information
+to be somewhat persistent.
=cut
@@ -573,8 +541,52 @@ is where the $name and $value are used)
=item &dateboxes()
+Returns html selection form elements for the specification of
+the day, month, and year.
+
=item &selectbox()
+Returns a scalar containing an html ';
}
######################################################################
@@ -674,7 +684,9 @@ sub selectbox {
=pod
-=item &advancedsearch()
+=item &advancedsearch()
+
+Parse advanced search results.
=cut
@@ -719,7 +731,7 @@ sub advancedsearch {
my @queries;
# Evaluate logical expression AND/OR/NOT phrase fields.
foreach my $field ('title','author','subject','notes','abstract','url',
- 'keywords','version','owner') {
+ 'keywords','version','owner','mime') {
if ($ENV{'form.'.$field}) {
push @queries,&build_SQL_query($field,$ENV{'form.'.$field});
}
@@ -728,9 +740,9 @@ sub advancedsearch {
if ($ENV{'form.language'} and $ENV{'form.language'} ne 'any') {
push @queries,"(language like \"$ENV{'form.language'}\")";
}
- if ($ENV{'form.mime'} and $ENV{'form.mime'} ne 'any') {
- push @queries,"(mime like \"$ENV{'form.mime'}\")";
- }
+# if ($ENV{'form.mime'} and $ENV{'form.mime'} ne 'any') {
+# push @queries,"(mime like \"$ENV{'form.mime'}\")";
+# }
if ($ENV{'form.copyright'} and $ENV{'form.copyright'} ne 'any') {
push @queries,"(copyright like \"$ENV{'form.copyright'}\")";
}
@@ -785,12 +797,13 @@ sub advancedsearch {
$customquery,$customshow);
}
&output_results('Advanced',$r,$envhash,$customquery,$reply,$hidden);
- }
- elsif ($customquery) {
+ return OK;
+ } elsif ($customquery) {
my $reply; # reply hash reference
$reply=&Apache::lonnet::metadata_query('',
$customquery,$customshow);
&output_results('Advanced',$r,$envhash,$customquery,$reply,$hidden);
+ return OK;
}
# should not get to this point
return 'Error. Should not have gone to this point.';
@@ -803,6 +816,8 @@ sub advancedsearch {
=item &basicsearch()
+Parse basic search form.
+
=cut
######################################################################
@@ -837,8 +852,9 @@ sub basicsearch {
$query=&build_SQL_query('concat('.$concatarg.')',$ENV{'form.'.'basicexp'});
# Get reply (either a hash reference to filehandles or bad connection)
+# &Apache::lonnet::logthis("metadata query started:".time);
my $reply=&Apache::lonnet::metadata_query('select * from metadata where '.$query);
-
+# &Apache::lonnet::logthis("metadata query finished:".time);
# Output search results
&output_results('Basic',$r,$envhash,$query,$reply,$hidden);
@@ -854,6 +870,9 @@ sub basicsearch {
=item &build_SQL_query()
+Builds a SQL query string from a logical expression with AND/OR keywords
+using Text::Query and &recursive_SQL_query_builder()
+
=cut
######################################################################
@@ -876,14 +895,15 @@ sub build_SQL_query {
=item &build_custommetadata_query()
+Constructs a custom metadata query using a rather heinous regular
+expression.
+
=cut
######################################################################
######################################################################
sub build_custommetadata_query {
my ($field_name,$logic_statement)=@_;
- &Apache::lonnet::logthis("Entered build_custommetadata_query:".
- $field_name.':'.$logic_statement);
my $q=new Text::Query('abc',
-parse => 'Text::Query::ParseAdvanced',
-build => 'Text::Query::BuildAdvancedString');
@@ -898,7 +918,6 @@ sub build_custommetadata_query {
\*$2\[\^\\<\]?# *wordtwo[^\<]
\*\\<\\\/$1\\>?# *\<\/wordone\>
/g;
- &Apache::lonnet::logthis("match expression: ".$matchexp);
return $matchexp;
}
@@ -909,6 +928,8 @@ sub build_custommetadata_query {
=item &recursive_SQL_query_build()
+Recursively constructs an SQL query. Takes as input $dkey and $pattern.
+
=cut
######################################################################
@@ -953,6 +974,9 @@ sub recursive_SQL_query_build {
=item &build_date_queries()
+Builds a SQL logic query to check time/date entries.
+Also reports errors (check for /^Incorrect/).
+
=cut
######################################################################
@@ -1023,6 +1047,7 @@ contacted, etc.)
######################################################################
######################################################################
sub output_results {
+# &Apache::lonnet::logthis("output_results:".time);
my $fnum; # search result counter
my ($mode,$r,$envhash,$query,$replyref,$hidden)=@_;
my %ENV=%{$envhash};
@@ -1045,8 +1070,14 @@ sub output_results {
Search Catalog
CATALOGBEGIN
- $r->print(<
+ my $action = "/adm/searchcat";
+ if ($mode eq 'Basic') {
+ $action .= "?reqinterface=basic";
+ } elsif ($mode eq 'Advanced') {
+ $action .= "?reqinterface=advanced";
+ }
+ $r->print(<
$hidden
dir_config('lonDaemons').'/tmp/'.$1;
$reply=~/(.*?)\_/;
- {
- my $temp=0;
- WLOOP: while (1) {
- if (-e $replyfile && $tflag) {
- &popwin_imgupdate($r,$rkey,"srvhalf.gif");
- &popwin_js($r,'popwin.hc["'.$rkey.'"]='.
- '"still transferring..."'.';');
- $tflag=0;
- }
- if (-e "$replyfile.end") {
- $serversleft--;
- delete $rhash{$rkey};
- if (-s $replyfile) {
- &popwin_imgupdate($r,$rkey,"srvgood.gif");
- my $fh=Apache::File->new($replyfile) or
- ($r->print('ERROR: file '.
- $replyfile.' cannot be opened') and
- return OK);
- @results=<$fh> if $fh;
- $hitcount{$rkey}=@results+0;
- &popwin_js($r,'popwin.hc["'.$rkey.'"]='.
- $hitcount{$rkey}.';');
- $hitcountsum+=$hitcount{$rkey};
- &popwin_js($r,'popwin.document.forms.popremain.'.
- 'numhits.value='.$hitcountsum.';');
- }
- else {
- &popwin_imgupdate($r,$rkey,"srvempty.gif");
- &popwin_js($r,'popwin.hc["'.$rkey.'"]=0;');
- }
- last WLOOP;
- } # end of if ( -e "$replyfile.end")
- last WLOOP unless $timeremain;
- sleep 1; # wait for daemons to write files?
- $timeremain--;
- $elapsetime++;
- last WLOOP if ($temp>1);
- &popwin_js($r,"popwin.document.popremain.".
- "elapsetime.value=$elapsetime;");
- $temp++;
- }
+ for (my $counter=0;$counter<2;$counter++) {
+ if (-e $replyfile && ! -e "$replyfile.end") {
+ &popwin_imgupdate($r,$rkey,"srvhalf.gif");
+ &popwin_js($r,'popwin.hc["'.$rkey.'"]='.
+ '"still transferring..."'.';');
+ }
+ # Are we finished transferring data?
+ if (-e "$replyfile.end") {
+ $serversleft--;
+ delete $rhash{$rkey};
+ if (-s $replyfile) {
+ &popwin_imgupdate($r,$rkey,"srvgood.gif");
+ my $fh;
+ unless ($fh=Apache::File->new($replyfile)){
+ # Is it really appropriate to die on this error?
+ $r->print('ERROR: file '.
+ $replyfile.' cannot be opened');
+ return OK;
+ }
+ @results=<$fh> if $fh;
+ my $hits =@results;
+ &popwin_js($r,'popwin.hc["'.$rkey.'"]='.
+ $hits.';');
+ $hitcountsum+=$hits;
+ &popwin_js($r,'popwin.document.forms.popremain.'.
+ 'numhits.value='.$hitcountsum.';');
+ } else {
+ &popwin_imgupdate($r,$rkey,"srvempty.gif");
+ &popwin_js($r,'popwin.hc["'.$rkey.'"]=0;');
+ }
+ last;
+ } # end of if ( -e "$replyfile.end")
+ last unless $timeremain;
+ sleep 1; # wait for daemons to write files?
+ $timeremain--;
+ $elapsetime++;
+ &popwin_js($r,"popwin.document.popremain.".
+ "elapsetime.value=$elapsetime;");
}
&popwin_js($r,'popwin.document.whirly.'.
'src="/adm/lonIcons/lonanimend.gif";');
} # end of if ($reply eq 'con_lost') else statement
- my $customshow='';
- my $extrashow='';
- my @customfields;
- if ($ENV{'form.customshow'}) {
- $customshow=$ENV{'form.customshow'};
- $customshow=~s/[^\w\s]//g;
- my @fields=map {"$_:"}
- split(/\s+/,$customshow);
- @customfields=split(/\s+/,$customshow);
- if ($customshow) {
- $extrashow="
".join("
",@fields)."
\n";
- }
- }
- my $customdata='';
- my %customhash;
- foreach my $result (@results) {
- if ($result=~/^(custom\=.*)$/) { # grab all custom metadata
- my $tmp=$result;
- $tmp=~s/^custom\=//;
- my ($k,$v)=map {&Apache::lonnet::unescape($_);
- } split(/\,/,$tmp);
- $customhash{$k}=$v;
- }
- }
- if (keys %hash) {
- untie %hash;
- }
- if (tie(%hash,'GDBM_File',$diropendb,&GDBM_WRCREAT,0640)) {
+ my %Fields = undef; # Holds the data to be sent to the various
+ # *_view routines.
+ my ($extrashow,$customfields,$customhash) = &handle_custom_fields(\@results);
+ my @customfields = @$customfields;
+ my %customhash = %$customhash;
+ untie %hash if (keys %hash);
+ #
+ if (! tie(%hash,'GDBM_File',$diropendb,&GDBM_WRCREAT,0640)) {
+ $r->print('Unable to tie hash to db '.
+ 'file');
+ } else {
if ($ENV{'form.launch'} eq '1') {
&start_fresh_session();
}
@@ -1183,63 +1193,33 @@ CATALOGCONTROLS
next if $result=~/^custom\=/;
chomp $result;
next unless $result;
- my @fields=map
- {&Apache::lonnet::unescape($_)}
- (split(/\,/,$result));
- my ($title,$author,$subject,$url,$keywords,$version,
- $notes,$abstract,$mime,$lang,
- $creationdate,$lastrevisiondate,$owner,$copyright)=@fields;
- unless ($title =~ /^\s*$/ ) { $title='Untitled'; }
- unless ($ENV{'user.adv'}) {
- $keywords='- not displayed -';
- $fields[4]=$keywords;
- $notes='- not displayed -';
- $fields[6]=$notes;
- $abstract='- not displayed -';
- $fields[7]=$abstract;
- $subject='- not displayed -';
- $fields[2]=$subject;
- }
-
- my $shortabstract=$abstract;
- $shortabstract=substr($abstract,0,200).'...' if length($abstract)>200;
- $fields[7]=$shortabstract;
- my $shortkeywords=$keywords;
- $shortkeywords=substr($keywords,0,200).'...' if length($keywords)>200;
- $fields[4]=$shortkeywords;
-
- my $extrashow2=$extrashow;
+ %Fields = &parse_raw_result($result,$rkey);
+ $Fields{'extrashow'}=$extrashow;
if ($extrashow) {
foreach my $field (@customfields) {
my $value='';
- if ($customhash{$url}=~/\<${field}[^\>]*\>(.*?)\<\/${field}[^\>]*\>/s) {
- $value=$1;
- }
- $extrashow2=~s/\<\!\-\- $field \-\-\>/ $value/g;
- }
+ $value = $1 if ($customhash{$Fields{'url'}}=~/\<{$field}[^\>]*\>(.*?)\<\/{$field}[^\>]*\>/s);
+ $Fields{'extrashow'}=~s/\<\!\-\- $field \-\-\>/ $value/g;
+ }
}
-
- $compiledresult.=<
-END
- $compiledresult.=<
-END
- if ($ENV{'form.catalogmode'} eq 'interactive') {
- my $titleesc=$title;
- $titleesc=~s/\'/\\'/; # '
-
- $compiledresult.=<";
+ }
+ $compiledresult.="\n