--- loncom/interface/lonsearchcat.pm 2002/08/01 14:11:57 1.150 +++ loncom/interface/lonsearchcat.pm 2002/08/16 13:52:51 1.153 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # Search Catalog # -# $Id: lonsearchcat.pm,v 1.150 2002/08/01 14:11:57 matthew Exp $ +# $Id: lonsearchcat.pm,v 1.153 2002/08/16 13:52:51 matthew Exp $ # # Copyright Michigan State University Board of Trustees # @@ -206,7 +206,7 @@ sub handler { '_'.&Apache::lonnet::escape($ENV{'user.name'}). '_'.$ENV{'form.persistent_db_id'}.'_persistent_search.db'; ## - if (! &get_persistent_form_data($r,$persistent_db_file)) { + if (! &get_persistent_form_data($persistent_db_file)) { if ($ENV{'form.phase'} =~ /(run_search|results)/) { &Apache::lonnet::logthis("lonsearchcat:Unable to recover data ". "from $persistent_db_file"); @@ -298,6 +298,7 @@ END } } $ENV{'form.phase'} = 'disp_basic' if (! exists($ENV{'form.phase'})); + $ENV{'form.show'} = 20 if (! exists($ENV{'form.show'})); ## ## Switch on the phase ## @@ -307,15 +308,23 @@ END &print_advanced_search_form($r,$closebutton); } elsif ($ENV{'form.phase'} eq 'results') { &display_results($r,$importbutton,$closebutton); - } elsif($ENV{'form.phase'} eq 'run_search') { + } elsif ($ENV{'form.phase'} =~ /^(sort|run_search)$/) { my ($query,$customquery,$customshow,$libraries,$pretty_string) = &get_persistent_data($persistent_db_file, ['query','customquery','customshow', 'libraries','pretty_string']); - &run_search($r,$query,$customquery,$customshow, - $libraries,$pretty_string); + if ($ENV{'form.phase'} eq 'sort') { + &print_sort_form($r,$pretty_string); + } elsif ($ENV{'form.phase'} eq 'run_search') { + &run_search($r,$query,$customquery,$customshow, + $libraries,$pretty_string); + } } elsif(($ENV{'form.phase'} eq 'basic_search') || ($ENV{'form.phase'} eq 'adv_search')) { + $ENV{'form.searchmode'} = 'basic'; + if ($ENV{'form.phase'} eq 'adv_search') { + $ENV{'form.searchmode'} = 'advanced'; + } # Set up table if (! defined(&create_results_table())) { $r->print(< $query, + &make_persistent({ query => $query, customquery => $customquery, customshow => $customshow, libraries => $libraries, @@ -425,8 +433,12 @@ END $ENV{'form.viewselect'}, undef,undef,undef, sort(keys(%Views))); + $scrout.=&selectbox(undef,'show', + $ENV{'form.show'}, + undef,undef,undef, + (10,20,50,100)); $scrout.=< +per page.

@@ -492,6 +504,13 @@ ENDHEADER $ENV{'form.viewselect'}, undef,undef,undef, sort(keys(%Views))); + $scrout.=' '; + $scrout.=&selectbox(undef,'show', + $ENV{'form.show'}, + undef,undef,undef, + (10,20,50,100)); + $scrout.=' '. + 'Per Page'; $scrout.="Related
Words\n"; $scrout.=&searchphrasefield_with_related('title', 'title' , $ENV{'form.title'}); @@ -600,25 +619,25 @@ LASTREVISIONDATEEND (&Apache::loncommon::copyrightids), ); # ------------------------------------------- Compute customized metadata field - $scrout.=< -LIMIT BY SPECIAL METADATA FIELDS: - -For resource-specific metadata, enter in an expression in the form of -key=value separated by operators such as AND, OR or NOT.
-Example: grandmother=75 OR grandfather=85 -
-CUSTOMMETADATA - $scrout.=&simpletextfield('custommetadata',$ENV{'form.custommetadata'}); - $scrout.=< -SHOW SPECIAL METADATA FIELDS: - -Enter in a space-separated list of special metadata fields to show -in a fielded listing for each record result. -
-CUSTOMSHOW - $scrout.=&simpletextfield('customshow',$ENV{'form.customshow'}); +# $scrout.=< +#LIMIT BY SPECIAL METADATA FIELDS: +# +#For resource-specific metadata, enter in an expression in the form of +#key=value separated by operators such as AND, OR or NOT.
+#Example: grandmother=75 OR grandfather=85 +#
+#CUSTOMMETADATA +# $scrout.=&simpletextfield('custommetadata',$ENV{'form.custommetadata'}); +# $scrout.=< +#SHOW SPECIAL METADATA FIELDS: +# +#Enter in a space-separated list of special metadata fields to show +#in a fielded listing for each record result. +#
+#CUSTOMSHOW +# $scrout.=&simpletextfield('customshow',$ENV{'form.customshow'}); $scrout.=< @@ -651,7 +670,6 @@ to them. ###################################################################### ###################################################################### sub get_persistent_form_data { - my $r = shift; my $filename = shift; return 0 if (! -e $filename); return undef if (! tie(%persistent_db,'GDBM_File',$filename, @@ -663,24 +681,15 @@ sub get_persistent_form_data { # Loop through the keys, looking for 'form.' foreach my $name (keys(%persistent_db)) { next if ($name !~ /^form./); + next if (exists($ENV{$name})); my @values = map { &Apache::lonnet::unescape($_); } split(',',$persistent_db{$name}); next if (@values <1); - if (exists($ENV{$name})) { - if (ref($ENV{$name}) eq 'ARRAY') { - # If it is an array, tack @values on the end of it. - $ENV{$name} = [@$ENV{$name},@values]; - } elsif (! ref($ENV{$name}) && $arrays_allowed{$name}) { - # if arrays are allowed, turn it into one and add @values - $ENV{$name} = [$ENV{$name},@values]; - } # otherwise, assume the value in $ENV{$name} is better than ours. + if ($arrays_allowed{$name}) { + $ENV{$name} = [@values]; } else { - if ($arrays_allowed{$name}) { - $ENV{$name} = [@values]; - } else { - $ENV{$name} = $values[0] if ($values[0]); - } + $ENV{$name} = $values[0] if ($values[0]); } } untie (%persistent_db); @@ -721,7 +730,7 @@ sub get_persistent_data { my @values = map { &Apache::lonnet::unescape($_); } split(',',$persistent_db{$name}); - if (@values == 1) { + if (@values <= 1) { push @Values,$values[0]; } else { push @Values,\@values; @@ -749,14 +758,11 @@ elements escaped and concatenated in a c ###################################################################### ###################################################################### sub make_persistent { - my $r = shift; my %save = %{shift()}; my $filename = shift; return undef if (! tie(%persistent_db,'GDBM_File', $filename,&GDBM_WRCREAT(),0640)); foreach my $name (keys(%save)) { - next if (! exists($save{$name})); - next if (! defined($save{$name}) || $save{$name} eq ''); my @values = (ref($save{$name}) ? @{$save{$name}} : ($save{$name})); # We handle array references, but not recursively. my $store = join(',', map { &Apache::lonnet::escape($_); } @values ); @@ -791,7 +797,7 @@ sub make_form_data_persistent { next if (!/^form/ || /submit/); $save{$_} = $ENV{$_}; } - return &make_persistent($r,\%save,$filename); + return &make_persistent(\%save,$filename); } ###################################################################### @@ -1123,6 +1129,7 @@ sub parse_advanced_search { $ENV{"form.$_"}=~s/[^\w\/\s\(\)\=\-\"\']//g; } # Preprocess the category form element. + $ENV{'form.category'} = 'any' if (ref($ENV{'form.category'})); if ($ENV{'form.category'} ne 'any') { my @extensions = &Apache::loncommon::filecategorytypes ($ENV{'form.category'}); @@ -1137,7 +1144,7 @@ sub parse_advanced_search { } } unless ($fillflag) { - &output_blank_field_error($r,$closebutton); + &output_blank_field_error($r,$closebutton,'phase=disp_adv'); return ; } # Turn the form input into a SQL-based query @@ -1209,21 +1216,21 @@ sub parse_advanced_search { } # Process form information for custom metadata querying my $customquery=undef; - if ($ENV{'form.custommetadata'}) { - $pretty_search_string .=$font."Custom Metadata Search: ". - $ENV{'form.custommetadata'}."
\n"; - $customquery=&build_custommetadata_query('custommetadata', - $ENV{'form.custommetadata'}); - } +# if ($ENV{'form.custommetadata'}) { +# $pretty_search_string .=$font."Custom Metadata Search: ". +# $ENV{'form.custommetadata'}."
\n"; +# $customquery=&build_custommetadata_query('custommetadata', +# $ENV{'form.custommetadata'}); +# } my $customshow=undef; - if ($ENV{'form.customshow'}) { - $pretty_search_string .=$font."Custom Metadata Display: ". - $ENV{'form.customshow'}."
\n"; - $customshow=$ENV{'form.customshow'}; - $customshow=~s/[^\w\s]//g; - my @fields=split(/\s+/,$customshow); - $customshow=join(" ",@fields); - } +# if ($ENV{'form.customshow'}) { +# $pretty_search_string .=$font."Custom Metadata Display: ". +# $ENV{'form.customshow'}."
\n"; +# $customshow=$ENV{'form.customshow'}; +# $customshow=~s/[^\w\s]//g; +# my @fields=split(/\s+/,$customshow); +# $customshow=join(" ",@fields); +# } ## --------------------------------------------------------------- ## Deal with restrictions to given domains ## @@ -1238,15 +1245,15 @@ sub parse_advanced_search { $domain_hash{$_}++; } if ($domain_hash{'any'}) { - $pretty_domains_string = "Searching all domains."; + $pretty_domains_string = "In all LON-CAPA domains."; } else { if (@allowed_domains > 1) { - $pretty_domains_string = "Searching domains:"; + $pretty_domains_string = "In LON-CAPA domains:"; } else { - $pretty_domains_string = "Searching domain "; + $pretty_domains_string = "In LON-CAPA domain "; } foreach (sort @allowed_domains) { - $pretty_domains_string .= "$_ "; + $pretty_domains_string .= "".$_." "; } foreach (keys(%Apache::lonnet::libserv)) { if (exists($domain_hash{$Apache::lonnet::hostdom{$_}})) { @@ -1294,7 +1301,7 @@ sub parse_basic_search { # Check to see if enough is filled in unless (&filled($ENV{'form.basicexp'})) { - &output_blank_field_error($r,$closebutton); + &output_blank_field_error($r,$closebutton,'phase=disp_basic'); return OK; } my $pretty_search_string = ''.$ENV{'form.basicexp'}.''; @@ -1530,7 +1537,6 @@ sub build_date_queries { ###################################################################### ###################################################################### - sub copyright_check { my $Metadata = shift; # Check copyright tags and skip results the user cannot use @@ -1550,6 +1556,152 @@ sub copyright_check { return 1; } + +###################################################################### +###################################################################### + +=pod + +=item &ensure_db_and_table + +Ensure we can get lonmysql to connect to the database and the table we +need exists. + +Inputs: $r, table id + +Returns: undef on error, 1 if the table exists. + +=cut + +###################################################################### +###################################################################### +sub ensure_db_and_table { + my ($r,$table) = @_; + ## + ## Sanity check the table id. + ## + if (! defined($table) || $table eq '' || $table =~ /\D/ ) { + $r->print("Unable to retrieve search results. ". + "Unable to determine the table results were stored in. ". + ""); + return undef; + } + ## + ## Make sure we can connect and the table exists. + ## + my $connection_result = &Apache::lonmysql::connect_to_db(); + if (!defined($connection_result)) { + $r->print("Unable to connect to the MySQL database where your results". + " are stored. "); + &Apache::lonnet::logthis("lonsearchcat: unable to get lonmysql to". + " connect to database."); + &Apache::lonnet::logthis(&Apache::lonmysql::get_error()); + return undef; + } + my $table_check = &Apache::lonmysql::check_table($table); + if (! defined($table_check)) { + $r->print("A MySQL error has occurred."); + &Apache::lonnet::logthis("lonmysql was unable to determine the status". + " of table ".$table); + return undef; + } elsif (! $table_check) { + $r->print("The table of results could not be found."); + &Apache::lonnet::logthis("The user requested a table, ".$table. + ", that could not be found."); + return undef; + } + return 1; +} + +###################################################################### +###################################################################### + +=pod + +=item &print_sort_form + +=cut + +###################################################################### +###################################################################### +sub print_sort_form { + my ($r,$pretty_query_string) = @_; + ## + my %SortableFields = + (id => 'Default', + title => 'Title', + author => 'Author', + subject => 'Subject', + url => 'URL', + version => 'Version Number', + mime => 'Mime type', + lang => 'Language', + owner => 'Owner/Publisher', + copyright => 'Copyright', + hostname => 'Host', + creationdate => 'Creation Date', + lastrevisiondate => 'Revision Date', + ); + ## + my $table = $ENV{'form.table'}; + return if (! &ensure_db_and_table($r,$table)); + ## + ## Get the number of results + ## + my $total_results = &Apache::lonmysql::number_of_rows($table); + if (! defined($total_results)) { + $r->print("A MySQL error has occurred."); + &Apache::lonnet::logthis("lonmysql was unable to determine the number". + " of rows in table ".$table); + &Apache::lonnet::logthis(&Apache::lonmysql::get_error()); + return; + } + my $result; + $result.=< + + +Results + + +
+ +END + +#

Sort Results

+#Sort by: }; + return $result; +} + +###################################################################### +###################################################################### + +=pod + =item &run_search =cut @@ -1703,19 +1882,36 @@ sub run_search { # Timing variables # my $starttime = time; - my $max_time = 120; # seconds for the search to complete + my $max_time = 30; # seconds for the search to complete # # Print run_search header # - $r->print("Search Status"); - $r->print("Search: ".$pretty_string."\n"); + $r->print(< +Search Status + + + +END + # Check to see if $pretty_string has more than one carriage return. + # Assume \n s are following
s and truncate the value. + # (there is probably a better way)... + my @Lines = split /
/,$pretty_string; + if (@Lines > 2) { + $pretty_string = join '
',(@Lines[0..2],'....
'); + } + $r->print("Search: ".$pretty_string); $r->rflush(); # # Determine the servers we need to contact. # my @Servers_to_contact; if (defined($serverlist)) { - @Servers_to_contact = @$serverlist; + if (ref($serverlist) eq 'ARRAY') { + @Servers_to_contact = @$serverlist; + } else { + @Servers_to_contact = ($serverlist); + } } else { @Servers_to_contact = sort(keys(%Apache::lonnet::libserv)); } @@ -1746,15 +1942,15 @@ sub run_search { my $hitcountsum; my $server; my $status; + my $revise = &revise_button(); $r->print(< - - + +
StatusTotal MatchesTime Remaining
StatusTotal MatchesTime Remaining
$revise
@@ -1851,18 +2047,18 @@ END } } &update_status($r,'Search Complete'.$server); + &update_seconds($r,0); &Apache::lonmysql::disconnect_from_db(); - # Let the user know - # # We have run out of time or run out of servers to talk to and # results to get. - $r->print("Search Completed.  "); - if ($hitcountsum) { - $r->print($hitcountsum." matches were found."); - } else { - $r->print("There were no successful matches to your query."); - } $r->print(""); + if ($ENV{'form.catalogmode'} ne 'groupsearch') { + $r->print(""); + } return; } @@ -1883,7 +2079,7 @@ sub prev_next_buttons { ## ## Prev my $prev_min = $current_min - $show; - $prev_min = 0 if $prev_min < 0; + $prev_min = 1 if $prev_min < 1; if ($prev_min < $current_min) { $links .= qq{ prev @@ -1953,22 +2149,7 @@ sub display_results { ## Prepare the table for querying ## my $table = $ENV{'form.table'}; - my $connection_result = &Apache::lonmysql::connect_to_db(); - if (!defined($connection_result)) { - $r->print(&Apache::lonmysql::get_error()); - } - my $table_check = &Apache::lonmysql::check_table($table); - if (! defined($table_check)) { - $r->print("A MySQL error has occurred."); - &Apache::lonnet::logthis("lonmysql was unable to determine the status". - " of table ".$table); - return; - } elsif (! $table_check) { - $r->print("The table of results could not be found."); - &Apache::lonnet::logthis("The user requested a table, ".$table. - ", that could not be found."); - return; - } + return if (! &ensure_db_and_table($r,$table)); ## ## Get the number of results ## @@ -1978,21 +2159,19 @@ sub display_results { &Apache::lonnet::logthis("lonmysql was unable to determine the number". " of rows in table ".$table); &Apache::lonnet::logthis(&Apache::lonmysql::get_error()); - &Apache::lonnet::logthis(&Apache::lonmysql::get_debug()); return; } ## ## Determine how many results we need to get ## - $ENV{'form.show'} = 20; - $ENV{'form.start'} = 0 if (! exists($ENV{'form.start'})); - $ENV{'form.show'} = 'all' if (! exists($ENV{'form.show'})); + $ENV{'form.start'} = 1 if (! exists($ENV{'form.start'})); + $ENV{'form.show'} = 'all' if (! exists($ENV{'form.show'})); my $min = $ENV{'form.start'}; my $max; if ($ENV{'form.show'} eq 'all') { $max = $total_results ; } else { - $max = $min + $ENV{'form.show'}; + $max = $min + $ENV{'form.show'} - 1; $max = $total_results if ($max > $total_results); } ## @@ -2018,7 +2197,7 @@ sub display_results { ## Get results from MySQL table ## my @Results = &Apache::lonmysql::get_rows($table, - 'id>'.$min.' AND id<='.$max); + 'id>='.$min.' AND id<='.$max); ## ## Loop through the results and output them. ## @@ -2312,13 +2491,13 @@ sub search_results_header { parent.close(); } function changeTitle(val) { - if (opener.inf.document.forms.resinfo.elements.t) { - opener.inf.document.forms.resinfo.elements.t.value=val; + if (parent.opener.inf.document.forms.resinfo.elements.t) { + parent.opener.inf.document.forms.resinfo.elements.t.value=val; } } function changeURL(val) { - if (opener.inf.document.forms.resinfo.elements.u) { - opener.inf.document.forms.resinfo.elements.u.value=val; + if (parent.opener.inf.document.forms.resinfo.elements.u) { + parent.opener.inf.document.forms.resinfo.elements.u.value=val; } } @@ -2386,7 +2565,7 @@ sub results_link { my $basic_link = "/adm/searchcat?"."&table=".$ENV{'form.table'}. "&persistent_db_id=".$ENV{'form.persistent_db_id'}; my $results_link = $basic_link."&phase=results". - "&pause=10"."&start=0"."&show=20"; + "&pause=1"."&start=1"; return $results_link; } @@ -2625,12 +2804,21 @@ sub filled { =item &output_blank_field_error() +Output a complete page that indicates the user has not filled in enough +information to do a search. + +Inputs: $r (Apache request handle), $closebutton, $parms. + +Returns: nothing + +$parms is extra information to include in the 'Revise search request' link. + =cut ###################################################################### ###################################################################### sub output_blank_field_error { - my ($r,$closebutton)=@_; + my ($r,$closebutton,$parms)=@_; # make query information persistent to allow for subsequent revision $r->print(< @@ -2644,16 +2832,15 @@ BEGINNING

Search Catalog

$hidden_fields -Revise search request  $closebutton
-

Helpful Message

+

Unactionable search query.

-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. +You did not fill in enough information for the search to be started. +You need to fill in relevant fields on the search page in order +for a query to be processed.

@@ -2696,7 +2883,7 @@ $hidden_fields onClick='this.form.submit();' /> $closebutton
-

Helpful Message

+

Error

$message