--- loncom/interface/lonsearchcat.pm 2003/03/08 01:43:12 1.167 +++ loncom/interface/lonsearchcat.pm 2003/12/16 15:00:56 1.195 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # Search Catalog # -# $Id: lonsearchcat.pm,v 1.167 2003/03/08 01:43:12 www Exp $ +# $Id: lonsearchcat.pm,v 1.195 2003/12/16 15:00:56 matthew Exp $ # # Copyright Michigan State University Board of Trustees # @@ -82,6 +82,7 @@ use Text::Query; use GDBM_File; use Apache::loncommon(); use Apache::lonmysql(); +use Apache::lonlocal; # ---------------------------------------- variables used throughout the module @@ -142,9 +143,13 @@ my %persistent_db; my $hidden_fields; my $bodytag; +# +# For course search +# my %alreadyseen; my $hashtied; my %hash; +my $totalfound; ###################################################################### ###################################################################### @@ -182,7 +187,7 @@ sub handler { # This button is different for the RAT compared to # normal invocation. # - $r->content_type('text/html'); + &Apache::loncommon::content_type($r,'text/html'); $r->send_http_header; return OK if $r->header_only; ## @@ -195,7 +200,7 @@ sub handler { &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'}, ['catalogmode','launch','acts','mode','form','element','pause', 'phase','persistent_db_id','table','start','show', - 'cleargroupsort']); + 'cleargroupsort','titleelement']); ## ## 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 @@ -203,15 +208,14 @@ sub handler { ## this once, so the pause indicator is deleted ## if (exists($ENV{'form.pause'})) { - sleep(3); + sleep(1); delete($ENV{'form.pause'}); } ## ## Initialize global variables ## my $domain = $r->dir_config('lonDefDomain'); - $diropendb= "/home/httpd/perl/tmp/".&Apache::lonnet::escape($domain). - "\_".&Apache::lonnet::escape($ENV{'user.name'})."_searchcat.db"; + $diropendb= "/home/httpd/perl/tmp/$ENV{'user.domain'}_$ENV{'user.name'}_searchcat.db"; # # set the name of the persistent database # $ENV{'form.persistent_db_id'} can only have digits in it. @@ -278,6 +282,10 @@ END $hidden_fields .= ''."\n"; } + if (exists($ENV{'form.titleelement'})) { + $hidden_fields .= ''."\n"; + } if (exists($ENV{'form.mode'})) { $hidden_fields .= ''."\n"; @@ -379,7 +387,8 @@ END (undef,undef,undef,undef); my $pretty_string; if ($ENV{'form.phase'} eq 'basic_search') { - ($query,$pretty_string) = &parse_basic_search($r,$closebutton); + ($query,$pretty_string,$libraries) = + &parse_basic_search($r,$closebutton); } else { # Advanced search ($query,$customquery,$customshow,$libraries,$pretty_string) = &parse_advanced_search($r,$closebutton); @@ -418,6 +427,7 @@ sub course_search { } 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(); @@ -425,13 +435,18 @@ sub course_search { $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},@allwords,$fulltext); + &checkonthis($r,$hash{$_},0,$hash{'title_'.$1},$fulltext,@allwords); } } &untiehash(); + unless ($totalfound) { + $r->print('

'.&mt('No resources found').'.

'); + } # =================================================== Done going through course $r->print(''); } @@ -456,7 +471,7 @@ sub untiehash { # =============================== This pulls up a resource and its dependencies sub checkonthis { - my ($r,$url,$level,$title,@allwords,$fulltext)=@_; + my ($r,$url,$level,$title,$fulltext,@allwords)=@_; $alreadyseen{$url}=1; $r->rflush(); my $result=&Apache::lonnet::metadata($url,'title').' '. @@ -464,7 +479,7 @@ sub checkonthis { &Apache::lonnet::metadata($url,'abstract').' '. &Apache::lonnet::metadata($url,'keywords'); if (($url) && ($fulltext)) { - $result.=&Apache::lonnet::ssibody($url); + $result.=&Apache::lonnet::ssi_body($url); } $result=~s/\s+/ /gs; my $applies=0; @@ -482,15 +497,18 @@ sub checkonthis { $r->print(' '); } $r->print(''. - ($title?$title:$url).''); - $r->rflush(); + ($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,'',@allwords,$fulltext); + &checkonthis($r,$_,$level+1,'',$fulltext,@allwords); } } } @@ -527,14 +545,15 @@ sub print_basic_search_form{ $bodytag ENDDOCUMENT if (&Apache::lonnet::allowed('bre',$ENV{'request.role.domain'})) { + my $CatalogSearch=&mt('Catalog Search'); + my $Statement=&mt('Enter terms or phrases separated by AND, OR, or NOT then press SEARCH below'); $scrout.=(<Catalog Search -
+

$CatalogSearch

+ $hidden_fields

-Enter terms or phrases separated by AND, OR, or NOT -then press SEARCH below. +$Statement.

@@ -542,58 +561,72 @@ then press SEARCH below. ENDDOCUMENT $scrout.=' '.&simpletextfield('basicexp',$ENV{'form.basicexp'},40). ' '; - my $checkbox = &simplecheckbox('related',$ENV{'form.related'}); + my $relatedcheckbox = &simplecheckbox('related',$ENV{'form.related'}); + my $domain = $r->dir_config('lonDefDomain'); + my $domaincheckbox = &simplecheckbox('domains',$domain); + my $srch=&mt('Search'); + my $header=&mt('Advanced Search'); + my $userelatedwords=&mt('use related words'); + my $onlysearchdomain=&mt('only search domain'); $scrout.=< - +href="/adm/searchcat?phase=disp_adv&catalogmode=$ENV{'form.catalogmode'}&launch=$ENV{'form.launch'}&mode=$ENV{'form.mode'}" +>$header + +
Advanced Search
$checkbox use related words
$relatedcheckbox $userelatedwords$domaincheckbox $onlysearchdomain $domain

  +   $closebutton END - $scrout.=&selectbox(undef,'viewselect', + $scrout.=&selectbox('View','viewselect', $ENV{'form.viewselect'}, undef,undef,undef, sort(keys(%Views))); - $scrout.=&selectbox(undef,'show', + $scrout.=&selectbox('Per Page','show', $ENV{'form.show'}, undef,undef,undef, (10,20,50,100)); $scrout.=< ENDDOCUMENT - if ($ENV{'request.course.id'}) { $scrout.='


'; } -} -if ($ENV{'request.course.id'}) { - $scrout.=(<Course Search -
+ } + if ($ENV{'request.course.id'}) { + my %lt=&Apache::lonlocal::texthash( + 'srch' => 'Search', + 'header' => 'Course Search', + 'note' => 'Enter terms or phrases, then press "Search" below', + ); + $scrout.=(< +

$lt{'header'}

+ $hidden_fields

-Enter terms or phrases, then press SEARCH below. +$lt{'note'}.

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.=(< @@ -662,8 +695,8 @@ ENDHEADER undef,undef,undef, (10,20,50,100)); $scrout.=' '. - 'Per Page'; - $scrout.="Related
Words\n"; + ''.&mt('Per Page').''; + $scrout.="".&mt('Related').'
'.&mt('Words')."\n"; $scrout.=&searchphrasefield_with_related('title', 'title' , $ENV{'form.title'}); $scrout.=&searchphrasefield('author', 'author' ,$ENV{'form.author'}); @@ -707,7 +740,7 @@ ENDHEADER my $size = 4; $size = (scalar @domains < ($size - 1) ? scalar @domains + 1 : $size); $scrout.="\n".''. - 'DOMAINS
'. + 'DOMAINS TO BE SEARCHED
'. ''; } ############################################### @@ -1030,7 +1071,7 @@ field heading. ############################################### sub fieldtitle { - my $title = uc(shift()); + my $title = uc(&mt(shift())); return ''.$title. ': '; } @@ -1125,7 +1166,7 @@ END my $i = 1; foreach (qw/January February March April May June July August September October November December /){ - $month .="\n"; + $month .="\n"; $i++; } $month.="\n"; @@ -1202,6 +1243,7 @@ sub selectbox { my ($title,$name,$default,$anyvalue,$anytag,$functionref,@idlist)=@_; if (! defined($functionref)) { $functionref = sub { $_[0]}; } my $selout=''; + $title=&mt($title); if (defined($title)) { my $uctitle=uc($title); $selout="\n".'

'. @@ -1212,12 +1254,12 @@ sub selectbox { foreach (@idlist) { $selout.=''; } elsif ($_ eq $default and /^$anyvalue$/) { - $selout.=' selected >'.$anytag.''; + $selout.=' selected >'.&mt($anytag).''; } - else {$selout.='>'.&{$functionref}($_).'';} + else {$selout.='>'.&mt(&{$functionref}($_)).'';} } return $selout.''.(defined($title)?'

':' '); } @@ -1403,11 +1445,33 @@ sub parse_advanced_search { ## --------------------------------------------------------------- ## Deal with restrictions to given domains ## + my ($libraries_to_query,$pretty_domains_string) = + &parse_domain_restrictions(); + $pretty_search_string .= $pretty_domains_string."
\n"; + # + if (@queries) { + $query=join(" AND ",@queries); + $query="select * from metadata where $query"; + } elsif ($customquery) { + $query = ''; + } + return ($query,$customquery,$customshow,$libraries_to_query, + $pretty_search_string); +} + +sub parse_domain_restrictions { my $libraries_to_query = undef; # $ENV{'form.domains'} can be either a scalar or an array reference. # We need an array. - my @allowed_domains = (ref($ENV{'form.domains'}) ? @{$ENV{'form.domains'}} - : ($ENV{'form.domains'}) ); + if (! exists($ENV{'form.domains'})) { + return (undef,''); + } + my @allowed_domains; + if (ref($ENV{'form.domains'})) { + @allowed_domains = @{$ENV{'form.domains'}}; + } else { + @allowed_domains = ($ENV{'form.domains'}); + } my %domain_hash = (); my $pretty_domains_string; foreach (@allowed_domains) { @@ -1430,16 +1494,7 @@ sub parse_advanced_search { } } } - $pretty_search_string .= $pretty_domains_string."
\n"; - # - if (@queries) { - $query=join(" AND ",@queries); - $query="select * from metadata where $query"; - } elsif ($customquery) { - $query = ''; - } - return ($query,$customquery,$customshow,$libraries_to_query, - $pretty_search_string); + return ($libraries_to_query,$pretty_domains_string); } ###################################################################### @@ -1467,7 +1522,8 @@ sub parse_basic_search { $ENV{"form.$_"}=&Apache::lonnet::unescape($ENV{"form.$_"}); $ENV{"form.$_"}=~s/[^\w\/\s\(\)\=\-\"\']//g; } - + my ($libraries_to_query,$pretty_domains_string) = + &parse_domain_restrictions(); # Check to see if enough is filled in unless (&filled($ENV{'form.basicexp'})) { &output_blank_field_error($r,$closebutton,'phase=disp_basic'); @@ -1486,13 +1542,19 @@ sub parse_basic_search { } # Build SQL query string based on form page my $query=''; - my $concatarg=join('," ",', + my $concatarg=join(',', ('title', 'author', 'subject', 'notes', 'abstract', 'keywords')); $concatarg='title' if $ENV{'form.titleonly'}; - $query=&build_SQL_query('concat('.$concatarg.')',$search_string); + $query=&build_SQL_query('concat_ws(" ",'.$concatarg.')',$search_string); + if (defined($pretty_domains_string) && $pretty_domains_string ne '') { + $pretty_search_string .= ' '.$pretty_domains_string; + } $pretty_search_string .= "
\n"; - return 'select * from metadata where '.$query,$pretty_search_string; + my $final_query = 'SELECT * FROM metadata WHERE '.$query; +# &Apache::lonnet::logthis($final_query); + return ($final_query,$pretty_search_string, + $libraries_to_query); } @@ -1605,30 +1667,26 @@ sub recursive_SQL_query_build { my @matches=($pattern=~/(\[[^\]|\[]*\])/g); return $pattern unless @matches; foreach my $match (@matches) { - $match=~/\[ (\w+)\s(.*) \]/; - my ($key,$value)=($1,$2); - my $replacement=''; - if ($key eq 'literal') { - $replacement="($dkey like \"\%$value\%\")"; - } - elsif ($key eq 'not') { - $value=~s/like/not like/; -# $replacement="($dkey not like $value)"; - $replacement="$value"; - } - elsif ($key eq 'and') { - $value=~/(.*[\"|\)]) ([|\(|\^].*)/; - $replacement="($1 AND $2)"; - } - elsif ($key eq 'or') { - $value=~/(.*[\"|\)]) ([|\(|\^].*)/; - $replacement="($1 OR $2)"; + $match=~/\[ (\w+)\s(.*) \]/; + my ($key,$value)=($1,$2); + my $replacement=''; + if ($key eq 'literal') { + $replacement="($dkey LIKE \"\%$value\%\")"; + } elsif (lc($key) eq 'not') { + $value=~s/LIKE/NOT LIKE/; +# $replacement="($dkey not like $value)"; + $replacement="$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 - ); + index($pattern,$match), + length($match), + $replacement); } &recursive_SQL_query_build($dkey,$pattern); } @@ -1796,8 +1854,8 @@ sub ensure_db_and_table { sub print_sort_form { my ($r,$pretty_query_string) = @_; ## - my %SortableFields = - (id => 'Default', + my %SortableFields=&Apache::lonlocal::texthash( + id => 'Default', title => 'Title', author => 'Author', subject => 'Subject', @@ -1809,7 +1867,7 @@ sub print_sort_form { copyright => 'Copyright', hostname => 'Host', creationdate => 'Creation Date', - lastrevisiondate => 'Revision Date', + lastrevisiondate => 'Revision Date' ); ## my $table = $ENV{'form.table'}; @@ -1885,37 +1943,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'}, #-------------------------------------------------- ); @@ -1943,9 +1995,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; @@ -2070,7 +2121,7 @@ END if (@Lines > 2) { $pretty_string = join '
',(@Lines[0..2],'....
'); } - $r->print("Search: ".$pretty_string); + $r->print(&mt("Search").": ".$pretty_string); $r->rflush(); # # Determine the servers we need to contact. @@ -2129,28 +2180,30 @@ END my $time_remaining = $max_time - (time - $starttime) ; my $last_time = $time_remaining; &update_seconds($r,$time_remaining); + &update_status($r,'contacting '.$Servers_to_contact[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) { # Contact one server my $server = shift(@Servers_to_contact); + &update_status($r,&mt('contacting').' '.$server); my $reply=&Apache::lonnet::metadata_query($query,$customquery, $customshow,[$server]); ($server) = keys(%$reply); $Server_status{$server} = $reply->{$server}; - &update_status($r,'contacting '.$server); } else { # wait a sec. to give time for files to be written # This sleep statement is here instead of outside the else # block because we do not want to pause if we have servers # left to contact. + if (scalar (keys(%Server_status))) { + &update_status($r, + &mt('waiting on').' '.(join(' ',keys(%Server_status)))); + } sleep(1); } # - 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. @@ -2163,12 +2216,12 @@ END $status=~/^([\.\w]+)$/; my $datafile=$r->dir_config('lonDaemons').'/tmp/'.$1; if (-e $datafile && ! -e "$datafile.end") { - &update_status($r,'Receiving results from '.$server); + &update_status($r,&mt('Receiving results from').' '.$server); next; } last if ($connection->aborted()); if (-e "$datafile.end") { - &update_status($r,'Reading results from '.$server); + &update_status($r,&mt('Reading results from').' '.$server); if (-z "$datafile") { delete($Server_status{$server}); next; @@ -2223,7 +2276,7 @@ END &update_seconds($r,$time_remaining); } } - &update_status($r,'Search Complete'.$server); + &update_status($r,&mt('Search Complete').$server); &update_seconds($r,0); &Apache::lonmysql::disconnect_from_db(); # We have run out of time or run out of servers to talk to and @@ -2367,7 +2420,8 @@ sub display_results { ."\n" ); if ($total_results == 0) { - $r->print("There are currently no results.\n". + $r->print(''. + '

'.&mt('There are currently no results').'.

'. ""); return; } else { @@ -2398,7 +2452,7 @@ sub display_results { $r->rflush(); } if (@Results < 1) { - $r->print("There were no results matching your query"); + $r->print(&mt("There were no results matching your query")); } else { $r->print ('
'. @@ -2471,7 +2525,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'}); @@ -2641,8 +2695,16 @@ Checked for existance & 'edit' mode. =item 'form.form' +Contains the name of the form that has the input fields to set + =item 'form.element' +the name of the input field to put the URL into + +=item 'form.titleelement' + +the name of the input field to put the title into + =back =cut @@ -2679,14 +2741,31 @@ SCRIPT } elsif ($ENV{'form.mode'} eq 'edit') { my $form = $ENV{'form.form'}; my $element = $ENV{'form.element'}; + my $titleelement = $ENV{'form.titleelement'}; + my $changetitle; + if (!$titleelement) { + $changetitle='function changeTitle(val) {}'; + } else { + $changetitle=< function select_data(title,url) { changeURL(url); + changeTitle(title); parent.close(); } -function changeTitle(val) { -} +$changetitle function changeURL(val) { if (parent.targetwin.document) { parent.targetwin.document.forms["$form"].elements["$element"].value=val; @@ -2702,10 +2781,19 @@ SCRIPT $result.=<