--- loncom/interface/lonsearchcat.pm 2002/08/21 17:18:08 1.154 +++ loncom/interface/lonsearchcat.pm 2003/03/13 19:08:52 1.171 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # Search Catalog # -# $Id: lonsearchcat.pm,v 1.154 2002/08/21 17:18:08 www Exp $ +# $Id: lonsearchcat.pm,v 1.171 2003/03/13 19:08:52 matthew Exp $ # # Copyright Michigan State University Board of Trustees # @@ -74,12 +74,11 @@ package Apache::lonsearchcat; # ------------------------------------------------- modules used by this module use strict; -use Apache::Constants qw(:common); +use Apache::Constants qw(:common :http); use Apache::lonnet(); use Apache::File(); use CGI qw(:standard); use Text::Query; -use DBI; use GDBM_File; use Apache::loncommon(); use Apache::lonmysql(); @@ -115,6 +114,11 @@ that produces it. Adding a new view typ adding a line to the definition of this hash and making sure the function takes the proper parameters. +=item $bodytag + +LON-CAPA standard body tag, gotten from &Apache::lonnet::bodytag. +No title, no table, just a tag. + =back =cut @@ -136,6 +140,16 @@ my %Views = ("Detailed Citation View" => "Compact View" => \&compact_view); my %persistent_db; my $hidden_fields; +my $bodytag; + +# +# For course search +# +my %alreadyseen; +my $hashtied; +my %hash; +my $totalfound; + ###################################################################### ###################################################################### @@ -164,6 +178,10 @@ string that holds portions of the screen sub handler { my $r = shift; # + + my $loaderror=&Apache::lonnet::overloaderror($r); + if ($loaderror) { return $loaderror; } + my $closebutton; # button that closes the search window # This button is different for the RAT compared to # normal invocation. @@ -171,12 +189,17 @@ sub handler { $r->content_type('text/html'); $r->send_http_header; return OK if $r->header_only; + ## + ## Prevent caching of the search interface window. Hopefully this means + ## we will get the launch=1 passed in a little more. + &Apache::loncommon::no_cache($r); ## ## Pick up form fields passed in the links. ## &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'}, ['catalogmode','launch','acts','mode','form','element','pause', - 'phase','persistent_db_id','table','start','show']); + 'phase','persistent_db_id','table','start','show', + 'cleargroupsort']); ## ## The following is a trick - we wait a few seconds if asked to so ## the daemon running the search can get ahead of the daemon @@ -201,6 +224,7 @@ sub handler { ($ENV{'form.launch'} eq '1')) { $ENV{'form.persistent_db_id'} = time; } + $bodytag=&Apache::loncommon::bodytag(undef,undef,undef,1); my $persistent_db_file = "/home/httpd/perl/tmp/". &Apache::lonnet::escape($domain). '_'.&Apache::lonnet::escape($ENV{'user.name'}). @@ -213,7 +237,7 @@ sub handler { $r->print(< LON-CAPA Search Error - +$bodytag We were unable to retrieve data describing your search. This is a serious error and has been logged. Please alert your LON-CAPA administrator. @@ -226,15 +250,18 @@ END ## Clear out old values from groupsearch database ## untie %groupsearch_db if (tied(%groupsearch_db)); - if ($ENV{'form.launch'} eq '1' && - ($ENV{'form.catalogmode'} eq 'groupsearch') && - ($ENV{'form.phase'} eq 'results')) { + if (($ENV{'form.cleargroupsort'} eq '1') || + (($ENV{'form.launch'} eq '1') && + ($ENV{'form.catalogmode'} eq 'groupsearch'))) { if (tie(%groupsearch_db,'GDBM_File',$diropendb,&GDBM_WRCREAT(),0640)) { &start_fresh_session(); untie %groupsearch_db; + delete($ENV{'form.cleargroupsort'}); } else { - $r->print('Unable to tie hash to db '. - 'file'); + # This is a stupid error to give to the user. + # It really tells them nothing. + $r->print(''.$bodytag. + 'Unable to tie hash to db file'); return OK; } } @@ -319,6 +346,8 @@ END &run_search($r,$query,$customquery,$customshow, $libraries,$pretty_string); } + } elsif ($ENV{'form.phase'} eq 'course_search') { + &course_search($r); } elsif(($ENV{'form.phase'} eq 'basic_search') || ($ENV{'form.phase'} eq 'adv_search')) { $ENV{'form.searchmode'} = 'basic'; @@ -329,7 +358,7 @@ END if (! defined(&create_results_table())) { $r->print(<Search Error - +$bodytag Unable to create table in which to store search results. The search has been aborted. @@ -341,7 +370,7 @@ END if (! &make_form_data_persistent($r,$persistent_db_file)) { $r->print(<Search Error - +$bodytag Unable to properly store search information. The search has been aborted. @@ -377,6 +406,111 @@ END ###################################################################### ###################################################################### +sub course_search { + my $r=shift; + my $bodytag=&Apache::loncommon::bodytag('Course Search'); + my $pretty_search_string = ''.$ENV{'form.courseexp'}.''; + my $search_string = $ENV{'form.courseexp'}; + my @New_Words; + if ($ENV{'form.crsrelated'}) { + ($search_string,@New_Words) = &related_version($ENV{'form.courseexp'}); + if (@New_Words) { + $pretty_search_string .= " with related words: @New_Words."; + } else { + $pretty_search_string .= " with no related words."; + } + } + my $fulltext=$ENV{'form.crsfulltext'}; + my @allwords=($search_string,@New_Words); + $totalfound=0; + $r->print('LON-CAPA Course Search'. + $bodytag.$pretty_search_string); + $r->rflush(); +# ======================================================= Go through the course + $hashtied=0; + undef %alreadyseen; + %alreadyseen=(); + my $c=$r->connection; + &tiehash(); + foreach (keys %hash) { + if ($c->aborted()) { last; } + if (($_=~/^src\_(.+)$/) && (!$alreadyseen{$hash{$_}})) { + &checkonthis($r,$hash{$_},0,$hash{'title_'.$1},$fulltext,@allwords); + } + } + &untiehash(); + unless ($totalfound) { + $r->print('

No resources found.

'); + } +# =================================================== Done going through course + $r->print(''); +} + +# ---------------------------------------------------------------- tie the hash + +sub tiehash { + $hashtied=0; + if ($ENV{'request.course.fn'}) { + if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.".db", + &GDBM_READER(),0640)) { + $hashtied=1; + } + } +} + +sub untiehash { + if ($hashtied) { untie %hash; } + $hashtied=0; +} + +# =============================== This pulls up a resource and its dependencies + +sub checkonthis { + my ($r,$url,$level,$title,$fulltext,@allwords)=@_; + $alreadyseen{$url}=1; + $r->rflush(); + my $result=&Apache::lonnet::metadata($url,'title').' '. + &Apache::lonnet::metadata($url,'subject').' '. + &Apache::lonnet::metadata($url,'abstract').' '. + &Apache::lonnet::metadata($url,'keywords'); + if (($url) && ($fulltext)) { + $result.=&Apache::lonnet::ssi_body($url); + } + $result=~s/\s+/ /gs; + my $applies=0; + foreach (@allwords) { + if ($_=~/\w/) { + if ($result=~/$_/si) { + $applies++; + } + } + } +# Does this resource apply? + if ($applies) { + $r->print('
'); + for (my $i=0;$i<=$level*5;$i++) { + $r->print(' '); + } + $r->print(''. + ($title?$title:$url).'
'); + $totalfound++; + } elsif ($fulltext) { + $r->print(' .'); + } + $r->rflush(); +# Check also the dependencies of this one + my $dependencies= + &Apache::lonnet::metadata($url,'dependencies'); + foreach (split(/\,/,$dependencies)) { + if (($_=~/^\/res\//) && (!$alreadyseen{$_})) { + &checkonthis($r,$_,$level+1,'',$fulltext,@allwords); + } + } +} + +###################################################################### +###################################################################### + =pod =item &print_basic_search_form() @@ -390,7 +524,7 @@ Returns a scalar which holds html for th sub print_basic_search_form{ my ($r,$closebutton) = @_; - my $bodytag=&Apache::loncommon::bodytag('Catalog Search'); + my $bodytag=&Apache::loncommon::bodytag('Search'); my $scrout=<<"ENDDOCUMENT"; @@ -404,6 +538,10 @@ sub print_basic_search_form{ $bodytag +ENDDOCUMENT +if (&Apache::lonnet::allowed('bre',$ENV{'request.role.domain'})) { + $scrout.=(<Catalog Search
$hidden_fields @@ -419,7 +557,9 @@ ENDDOCUMENT ' '; my $checkbox = &simplecheckbox('related',$ENV{'form.related'}); $scrout.=<Advanced Search +Advanced Search $checkbox use related words

@@ -439,6 +579,35 @@ END per page.

+ENDDOCUMENT + if ($ENV{'request.course.id'}) { $scrout.='
'; } +} +if ($ENV{'request.course.id'}) { + $scrout.=(<Course Search +
+ +$hidden_fields +

+Enter terms or phrases, then press SEARCH below. +

+

+ + + + +
+ENDCOURSESEARCH + $scrout.=' '.&simpletextfield('courseexp',$ENV{'form.courseexp'},40); + my $crscheckbox = &simplecheckbox('crsfulltext',$ENV{'form.crsfulltext'}); + my $relcheckbox = &simplecheckbox('crsrelated',$ENV{'form.crsrelated'}); +$scrout.=(<
$relcheckbox use related words
$crscheckbox fulltext search (time consuming)

+

+ENDENDCOURSE +} + $scrout.=(< ENDDOCUMENT @@ -522,13 +691,13 @@ ENDHEADER $ENV{'form.abstract'}); # Hack - an empty table row. $scrout.="   \n"; - $scrout.=&searchphrasefield('file
extension','mime', - $ENV{'form.mime'}); + $scrout.=&searchphrasefield('file
extension','extension', + $ENV{'form.extension'}); $scrout.="   \n"; $scrout.=&searchphrasefield('publisher
owner','owner', $ENV{'form.owner'}); $scrout.="\n"; - $ENV{'form.category'}='any' unless length($ENV{'form.category'}); + $ENV{'form.category'}='any' if (! defined($ENV{'form.category'})); $scrout.=&selectbox('File Category','category', $ENV{'form.category'}, 'any','Any category', @@ -549,7 +718,7 @@ ENDHEADER my @domains =&Apache::loncommon::get_domains(); # adjust the size of the select box my $size = 4; - my $size = (scalar @domains < ($size - 1) ? scalar @domains + 1 : $size); + $size = (scalar @domains < ($size - 1) ? scalar @domains + 1 : $size); $scrout.="\n".''. 'DOMAINS
'. ' END @@ -1712,37 +1898,31 @@ the following format: =cut -## -## Restrictions: -## columns of type 'text' and 'blob' cannot have defaults. -## columns of type 'enum' cannot be used for FULLTEXT. -## -my @DataOrder = qw/id title author subject url keywords version notes - abstract mime lang owner copyright creationdate lastrevisiondate hostname/; - -my %Datatypes = - ( id =>{ type => 'MEDIUMINT', - restrictions => 'UNSIGNED NOT NULL', - primary_key => 'yes', - auto_inc => 'yes' - }, - title =>{ type=>'TEXT'}, - author =>{ type=>'TEXT'}, - subject =>{ type=>'TEXT'}, - url =>{ type=>'TEXT', - restrictions => 'NOT NULL' }, - keywords =>{ type=>'TEXT'}, - version =>{ type=>'TEXT'}, - notes =>{ type=>'TEXT'}, - abstract =>{ type=>'TEXT'}, - mime =>{ type=>'TEXT'}, - lang =>{ type=>'TEXT'}, - owner =>{ type=>'TEXT'}, - copyright =>{ type=>'TEXT'}, - hostname =>{ type=>'TEXT'}, +##################################################################### +##################################################################### + +my @Datatypes = + ( { name => 'id', + type => 'MEDIUMINT', + restrictions => 'UNSIGNED NOT NULL', + primary_key => 'yes', + auto_inc => 'yes' }, + { name => 'title', type=>'TEXT'}, + { name => 'author', type=>'TEXT'}, + { name => 'subject', type=>'TEXT'}, + { name => 'url', type=>'TEXT', restrictions => 'NOT NULL' }, + { name => 'keywords', type=>'TEXT'}, + { name => 'version', type=>'TEXT'}, + { name => 'notes', type=>'TEXT'}, + { name => 'abstract', type=>'TEXT'}, + { name => 'mime', type=>'TEXT'}, + { name => 'lang', type=>'TEXT'}, + { name => 'owner', type=>'TEXT'}, + { name => 'copyright', type=>'TEXT'}, + { name => 'hostname', type=>'TEXT'}, #-------------------------------------------------- - creationdate =>{ type=>'DATETIME'}, - lastrevisiondate =>{ type=>'DATETIME'}, + { name => 'creationdate', type=>'DATETIME'}, + { name => 'lastrevisiondate', type=>'DATETIME'}, #-------------------------------------------------- ); @@ -1770,9 +1950,8 @@ Returns: the identifier of the table on ###################################################################### sub create_results_table { my $table = &Apache::lonmysql::create_table - ( { columns => \%Datatypes, - column_order => \@DataOrder, - fullindex => \@Fullindicies, + ( { columns => \@Datatypes, + FULLTEXT => {'columns' => \@Fullindicies}, } ); if (defined($table)) { $ENV{'form.table'} = $table; @@ -1854,6 +2033,7 @@ sub revise_button { $revise_phase = 'disp_adv' if ($ENV{'form.searchmode'} eq 'advanced'); my $newloc = '/adm/searchcat'. '?persistent_db_id='.$ENV{'form.persistent_db_id'}. + '&cleargroupsort=1'. '&phase='.$revise_phase; my $result = qq{ }; @@ -1885,7 +2065,7 @@ sub run_search { $r->print(< Search Status - +$bodytag END @@ -1955,7 +2135,7 @@ END my $time_remaining = $max_time - (time - $starttime) ; my $last_time = $time_remaining; &update_seconds($r,$time_remaining); - while (($time_remaining > 0) && + while (($time_remaining > 0) && ((@Servers_to_contact) || keys(%Server_status))) { # Send out a search request if it needs to be done. if (@Servers_to_contact) { @@ -1973,7 +2153,13 @@ END # left to contact. sleep(1); } - &update_status($r,'waiting on '.(join(' ',keys(%Server_status)))); + # + if (scalar (keys(%Server_status))) { + &update_status($r,'waiting on '.(join(' ',keys(%Server_status)))); + } + # + # Loop through the servers we have contacted but do not + # have results from yet, looking for results. while (my ($server,$status) = each(%Server_status)) { last if ($connection->aborted()); if ($status eq 'con_lost') { @@ -2036,6 +2222,7 @@ END } last if ($connection->aborted()); # Finished looping through the servers + $starttime = time if (@Servers_to_contact); $time_remaining = $max_time - (time - $starttime) ; if ($last_time - $time_remaining > 0) { $last_time = $time_remaining; @@ -2127,6 +2314,10 @@ sub display_results { return; } ## + ## $checkbox_num is a count of the number of checkboxes output on the + ## page this is used only during catalogmode=groupsearch. + my $checkbox_num = 0; + ## ## Get the catalog controls setup ## my $action = "/adm/searchcat?phase=results"; @@ -2199,13 +2390,13 @@ sub display_results { ## foreach my $row (@Results) { if ($connection->aborted()) { - untie %groupsearch_db if (tied(%groupsearch_db)); - &Apache::lonmysql::disconnect_from_db(); + &cleanup(); return; } my %Fields = %{&parse_row(@$row)}; my $output="

\n"; - my $prefix=&catalogmode_output($Fields{'title'},$Fields{'url'}); + my $prefix=&catalogmode_output($Fields{'title'},$Fields{'url'}, + $Fields{'id'},$checkbox_num++); # Render the result into html $output.= &$viewfunction($prefix,%Fields); # Print them out as they come in. @@ -2236,22 +2427,18 @@ sub display_results { =pod -=item &catalogmode_output($title,$url) +=item &catalogmode_output($title,$url,$fnum,$checkbox_num) Returns html needed for the various catalog modes. Gets inputs from -$ENV{'form.catalogmode'}. Stores data in %groupsearch_db and $fnum -(local variable). +$ENV{'form.catalogmode'}. Stores data in %groupsearch_db. =cut ###################################################################### ###################################################################### -{ -my $fnum = 0; - sub catalogmode_output { my $output = ''; - my ($title,$url) = @_; + my ($title,$url,$fnum,$checkbox_num) = @_; if ($ENV{'form.catalogmode'} eq 'interactive') { $title=~ s/\'/\\\'/g; if ($ENV{'form.catalogmode'} eq 'interactive') { @@ -2267,15 +2454,12 @@ END $output.=< +onClick="javascript:queue($checkbox_num,$fnum)" /> END - $fnum++; } return $output; } - -} ###################################################################### ###################################################################### @@ -2293,7 +2477,7 @@ sub parse_row { my @Row = @_; my %Fields; for (my $i=0;$i<=$#Row;$i++) { - $Fields{$DataOrder[$i]}=&Apache::lonnet::unescape($Row[$i]); + $Fields{$Datatypes[$i]->{'name'}}=&Apache::lonnet::unescape($Row[$i]); } $Fields{'language'} = &Apache::loncommon::languagedescription($Fields{'lang'}); @@ -2523,8 +2707,8 @@ SCRIPT } $result.=< LON-CAPA Digital Library Search Results @@ -2823,7 +3009,7 @@ sub output_blank_field_error { BEGINNING $r->print(< - +$bodytag

Search Catalog

@@ -2870,7 +3056,7 @@ sub output_date_error { The LearningOnline Network with CAPA - +$bodytag

Search Catalog

@@ -2916,6 +3102,17 @@ sub start_fresh_session { 1; +sub cleanup { + if (tied(%groupsearch_db)) { + &Apache::lonnet::logthis('Cleanup searchcat: groupsearch_db'); + unless (untie(%groupsearch_db)) { + &Apache::lonnet::logthis('Failed cleanup searchcat: groupsearch_db'); + } + } + &untiehash(); + &Apache::lonmysql::disconnect_from_db(); +} + __END__ =pod