Annotation of loncom/interface/lonsearchcat.pm, revision 1.16
1.1 www 1: # The LearningOnline Network
2: # Search Catalog
3: #
1.2 harris41 4: # 03/08/2001 Scott Harrison
1.1 www 5: #
6: package Apache::lonsearchcat;
7:
8: use strict;
9: use Apache::Constants qw(:common);
1.6 harris41 10: use Apache::lonnet();
11: use Apache::File();
1.7 harris41 12: use CGI qw(:standard);
1.1 www 13:
1.3 harris41 14: my %language;
15: my $scrout;
16: my %metadatafields;
17: my %cprtag;
18: my %mimetag;
19:
1.1 www 20: sub handler {
21: my $r = shift;
1.7 harris41 22:
23: # -------------------------------------- see if called from an interactive mode
24: map {
25: my ($name, $value) = split(/=/,$_);
26: $value =~ tr/+/ /;
27: $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
28: if ($name eq 'catalogmode') {
29: $ENV{'form.'.$name}=$value;
30: }
31: } (split(/&/,$ENV{'QUERY_STRING'}));
32:
1.1 www 33: $r->content_type('text/html');
34: $r->send_http_header;
35: return OK if $r->header_only;
36:
1.3 harris41 37: %metadatafields=();
38:
1.8 harris41 39: my $hidden='';
40: $hidden=<<END if $ENV{'form.catalogmode'} eq 'interactive';
41: <input type='hidden' name='catalogmode' value='interactive'>
42: END
43:
1.3 harris41 44: # ------------------------------------------------ First, check out environment
45: $metadatafields{'owner'}=$ENV{'user.name'}.'@'.$ENV{'user.domain'};
46:
1.8 harris41 47: # --------------------------------- Compute various listings of metadata values
1.3 harris41 48:
49: %language=();
50: $language{'any'}='Any language';
51: {
52: my $fh=Apache::File->new($r->dir_config('lonTabDir').'/language.tab');
53: map {
54: $_=~/(\w+)\s+([\w\s\-]+)/;
55: $language{$1}=$2;
56: } <$fh>;
57: }
58:
59: %cprtag=();
60: $cprtag{'any'}='Any copyright/distribution';
61: {
62: my $fh=Apache::File->new($r->dir_config('lonIncludes').'/copyright.tab');
63: map {
64: $_=~/(\w+)\s+([\w\s\-]+)/;
65: $cprtag{$1}=$2;
66: } <$fh>;
67: }
68:
69: %mimetag=();
70: $mimetag{'any'}='Any type';
71: {
72: my $fh=Apache::File->new($r->dir_config('lonTabDir').'/filetypes.tab');
73: map {
74: $_=~/(\w+)\s+(\w+)\s+([\w\s\-]+)/;
75: $mimetag{$1}=".$1 $3";
76: } <$fh>;
77: }
78:
1.6 harris41 79: if ($ENV{'form.basicsubmit'} eq 'SEARCH') {
80: return &basicsearch($r,$ENV{'form.basicexp'});
81: }
82:
1.8 harris41 83: $scrout=''; # building a part of screen output
1.3 harris41 84: $scrout.=&searchphrasefield('Limit by title','title',
1.11 harris41 85: $ENV{'form.title'});
1.3 harris41 86:
87: $scrout.=&searchphrasefield('Limit by author','author',
1.11 harris41 88: $ENV{'form.author'});
1.3 harris41 89:
90: $scrout.=&searchphrasefield('Limit by subject','subject',
1.11 harris41 91: $ENV{'form.subject'});
92:
93: $scrout.=&searchphrasefield('Limit by keywords','keywords',
94: $ENV{'form.keywords'});
95:
96: $scrout.=&searchphrasefield('Limit by URL','url',
97: $ENV{'form.url'});
98:
99: $scrout.=&searchphrasefield('Limit by version','version',
100: $ENV{'form.version'});
1.3 harris41 101:
102: $scrout.=&searchphrasefield('Limit by notes','notes',
1.11 harris41 103: $ENV{'form.notes'});
1.3 harris41 104:
105: $scrout.=&searchphrasefield('Limit by abstract','abstract',
1.11 harris41 106: $ENV{'form.abstract'});
1.3 harris41 107:
1.11 harris41 108: $ENV{'form.mime'}='notxxx' unless length($ENV{'form.mime'});
1.3 harris41 109: $scrout.=&selectbox('Limit by MIME type','mime',
1.11 harris41 110: $ENV{'form.mime'},%mimetag);
111:
112: $ENV{'form.language'}='any' unless length($ENV{'form.language'});
1.3 harris41 113:
114: $scrout.=&selectbox('Limit by language','language',
1.11 harris41 115: $ENV{'form.language'},%language);
1.3 harris41 116:
1.8 harris41 117:
118: # ------------------------------------------------ Compute date selection boxes
119: $scrout.=<<CREATIONDATESTART;
1.3 harris41 120: <p>
121: <font color="#800000" face="helvetica"><b>LIMIT BY CREATION DATE RANGE:</b>
122: </font>
123: <br>
1.8 harris41 124: between:
125: CREATIONDATESTART
1.11 harris41 126: $scrout.=&dateboxes('creationdatestart',1,1,1976,
127: $ENV{'form.creationdatestart_month'},
128: $ENV{'form.creationdatestart_day'},
129: $ENV{'form.creationdatestart_year'},
130: );
1.8 harris41 131: $scrout.=<<CREATIONDATEEND;
132: and:
133: CREATIONDATEEND
1.11 harris41 134: $scrout.=&dateboxes('creationdateend',12,31,2051,
135: $ENV{'form.creationdateend_month'},
136: $ENV{'form.creationdateend_day'},
137: $ENV{'form.creationdateend_year'},
138: );
1.8 harris41 139: $scrout.="</p>";
140:
141: $scrout.=<<LASTREVISIONDATESTART;
142: <p>
143: <font color="#800000" face="helvetica"><b>LIMIT BY LAST REVISION DATE RANGE:
144: </b></font>
145: <br>between:
146: LASTREVISIONDATESTART
1.11 harris41 147: $scrout.=&dateboxes('lastrevisiondatestart',1,1,1976,
148: $ENV{'form.lastrevisiondatestart_month'},
149: $ENV{'form.lastrevisiondatestart_day'},
150: $ENV{'form.lastrevisiondatestart_year'},
151: );
1.8 harris41 152: $scrout.=<<LASTREVISIONDATEEND;
153: and:
154: LASTREVISIONDATEEND
1.11 harris41 155: $scrout.=&dateboxes('lastrevisiondateend',12,31,2051,
156: $ENV{'form.lastrevisiondateend_month'},
157: $ENV{'form.lastrevisiondateend_day'},
158: $ENV{'form.lastrevisiondateend_year'},
159: );
1.8 harris41 160: $scrout.='</p>';
161:
162: $scrout.=&searchphrasefield('Limit by publisher/owner','owner',
1.11 harris41 163: $ENV{'form.owner'});
164: # $metadatafields{'owner'});
1.8 harris41 165:
1.11 harris41 166: $ENV{'form.copyright'}='any' unless length($ENV{'form.copyright'});
1.8 harris41 167: $scrout.=&selectbox('Limit by copyright/distribution','copyright',
1.11 harris41 168: $ENV{'form.copyright'},%cprtag);
1.8 harris41 169:
1.14 harris41 170: # ------------------------------------------- Compute customized metadata field
171: $scrout.=<<CUSTOMMETADATA;
172: <p>
173: <font color="#800000" face="helvetica"><b>LIMIT BY OTHER METADATA FIELDS:</b>
174: </font>
175: For author-specific metadata, enter in an expression in the form of
176: <i>key</i>=<i>value</i> separated by operators such as AND or OR.<br>
177: <b>Example:</b> grandmother=75 OR grandfather=85
178: <br>
179: CUSTOMMETADATA
180: $scrout.=&simpletextfield('custommetadata',$ENV{'form.custommetadata'});
1.15 harris41 181: $scrout.=' <i>initial users of this system do not need to worry about this option</i>';
1.14 harris41 182:
1.8 harris41 183: # ---------------------------------------------------------------- Print screen
184: $r->print(<<ENDDOCUMENT);
185: <html>
186: <head>
187: <title>The LearningOnline Network with CAPA</title>
188: </head>
189: <body bgcolor="#FFFFFF">
190: <img align=right src=/adm/lonIcons/lonlogos.gif>
191: <h1>Search Catalog</h1>
192: <form method="post" action="/adm/searchcat">
193: $hidden
194: <hr>
195: <h3>Basic Search</h3>
196: <p>
197: Enter terms or phrases separated by search operators
198: such as AND or OR then press SEARCH below. Terms should be specific
199: to the title, author, subject, notes, or abstract information associated
200: with a resource.
201: <br>
1.11 harris41 202: ENDDOCUMENT
203: $r->print(&simpletextfield('basicexp',$ENV{'form.basicexp'}));
204: $r->print(' ');
205: $r->print(&simplecheckbox('titleonly',$ENV{'form.titleonly'}));
206: $r->print('<font color="#800000">Title only</font> ');
207: $r->print(&simplecheckbox('allversions',$ENV{'form.allversions'}));
208: $r->print(<<ENDDOCUMENT);
209: <font color="#800000">Search historic archives</font>
1.8 harris41 210: <br>
211: <input type="submit" name="basicsubmit" value="SEARCH">
212: <input type="reset" name="reset" value="RESET">
213: <input type="button" name="close" value="CLOSE" onClick="self.close()">
214: </p>
215: <hr>
216: <h3>Advanced Search</h3>
217: $scrout
218: <p>
219: <input type="submit" name="advancedsubmit" value="SEARCH">
220: <input type="reset" name="reset" value="RESET">
221: <input type="button" name="close" value="CLOSE" onClick="self.close()">
1.3 harris41 222: </p>
1.8 harris41 223: </form>
224: </body>
225: </html>
226: ENDDOCUMENT
227: return OK;
228: }
229:
230: # --------------------------------------------------------- Various form fields
231:
232: sub textfield {
233: my ($title,$name,$value)=@_;
234: return "\n<p><b>$title:</b><br>".
235: '<input type=text name="'.$name.'" size=80 value="'.$value.'">';
236: }
237:
1.11 harris41 238: sub simpletextfield {
239: my ($name,$value)=@_;
240: return '<input type=text name="'.$name.'" size=20 value="'.$value.'">';
241: }
242:
243: sub simplecheckbox {
244: my ($name,$value)=@_;
245: my $checked='';
246: $checked="CHECKED" if $value eq 'on';
247: return '<input type=checkbox name="'.$name.'" '. $checked . '>';
248: }
249:
1.8 harris41 250: sub searchphrasefield {
251: my ($title,$name,$value)=@_;
252: my $instruction=<<END;
253: Enter terms or phrases separated by search operators such
254: as AND or OR.
255: END
256: my $uctitle=uc($title);
257: return "\n<p><font color=\"#800000\" face=\"helvetica\"><b>$uctitle:</b>".
258: "</FONT> $instruction<br>".
259: '<input type=text name="'.$name.'" size=80 value="'.$value.'">';
260: }
1.3 harris41 261:
1.8 harris41 262: sub dateboxes {
1.11 harris41 263: my ($name,$defaultmonth,$defaultday,$defaultyear,
264: $currentmonth,$currentday,$currentyear)=@_;
265: ($defaultmonth,$defaultday,$defaultyear)=('','','');
266: my $month=<<END;
1.8 harris41 267: <select name="${name}_month">
1.11 harris41 268: <option value='$defaultmonth'> </option>
269: <option value="1">January</option>
270: <option value="2">February</option>
271: <option value="3">March</option>
272: <option value="4">April</option>
273: <option value="5">May</option>
274: <option value="6">June</option>
275: <option value="7">July</option>
276: <option value="8">August</option>
277: <option value="9">September</option>
1.3 harris41 278: <option value="10">October</option>
279: <option value="11">November</option>
280: <option value="12">December</option>
281: </select>
1.11 harris41 282: END
283: $month=~s/(\"$currentmonth\")/$1 SELECTED/ if length($currentmonth);
284: my $day=<<END;
1.8 harris41 285: <select name="${name}_day">
1.11 harris41 286: <option value='$defaultday'> </option>
287: <option value="1">1</option>
288: <option value="2">2</option>
289: <option value="3">3</option>
290: <option value="4">4</option>
291: <option value="5">5</option>
292: <option value="6">6</option>
293: <option value="7">7</option>
294: <option value="8">8</option>
295: <option value="9">9</option>
296: <option value="10">10</option>
297: <option value="11">11</option>
298: <option value="12">12</option>
299: <option value="13">13</option>
300: <option value="14">14</option>
301: <option value="15">15</option>
302: <option value="16">16</option>
303: <option value="17">17</option>
304: <option value="18">18</option>
305: <option value="19">19</option>
306: <option value="20">20</option>
307: <option value="21">21</option>
308: <option value="22">22</option>
309: <option value="23">23</option>
310: <option value="24">24</option>
311: <option value="25">25</option>
312: <option value="26">26</option>
313: <option value="27">27</option>
314: <option value="28">28</option>
315: <option value="29">29</option>
316: <option value="30">30</option>
317: <option value="31">31</option>
1.3 harris41 318: </select>
1.11 harris41 319: END
320: $day=~s/(\"$currentday\")/$1 SELECTED/ if length($currentday);
321: my $year=<<END;
1.8 harris41 322: <select name="${name}_year">
1.11 harris41 323: <option value='$defaultyear'> </option>
324: <option value="1976">1976</option>
325: <option value="1977">1977</option>
326: <option value="1978">1978</option>
327: <option value="1979">1979</option>
328: <option value="1980">1980</option>
329: <option value="1981">1981</option>
330: <option value="1982">1982</option>
331: <option value="1983">1983</option>
332: <option value="1984">1984</option>
333: <option value="1985">1985</option>
334: <option value="1986">1986</option>
335: <option value="1987">1987</option>
336: <option value="1988">1988</option>
337: <option value="1989">1989</option>
338: <option value="1990">1990</option>
339: <option value="1991">1991</option>
340: <option value="1992">1992</option>
341: <option value="1993">1993</option>
342: <option value="1994">1994</option>
343: <option value="1995">1995</option>
344: <option value="1996">1996</option>
345: <option value="1997">1997</option>
346: <option value="1998">1998</option>
347: <option value="1999">1999</option>
348: <option value="2000">2000</option>
349: <option value="2001">2001</option>
350: <option value="2002">2002</option>
351: <option value="2003">2003</option>
352: <option value="2004">2004</option>
353: <option value="2005">2005</option>
354: <option value="2006">2006</option>
355: <option value="2007">2007</option>
356: <option value="2008">2008</option>
357: <option value="2009">2009</option>
358: <option value="2010">2010</option>
359: <option value="2011">2011</option>
360: <option value="2012">2012</option>
361: <option value="2013">2013</option>
362: <option value="2014">2014</option>
363: <option value="2015">2015</option>
364: <option value="2016">2016</option>
365: <option value="2017">2017</option>
366: <option value="2018">2018</option>
367: <option value="2019">2019</option>
368: <option value="2020">2020</option>
369: <option value="2021">2021</option>
370: <option value="2022">2022</option>
371: <option value="2023">2023</option>
372: <option value="2024">2024</option>
373: <option value="2025">2025</option>
374: <option value="2026">2026</option>
375: <option value="2027">2027</option>
376: <option value="2028">2028</option>
377: <option value="2029">2029</option>
378: <option value="2030">2030</option>
379: <option value="2031">2031</option>
380: <option value="2032">2032</option>
381: <option value="2033">2033</option>
382: <option value="2034">2034</option>
383: <option value="2035">2035</option>
384: <option value="2036">2036</option>
385: <option value="2037">2037</option>
386: <option value="2038">2038</option>
387: <option value="2039">2039</option>
388: <option value="2040">2040</option>
389: <option value="2041">2041</option>
390: <option value="2042">2042</option>
391: <option value="2043">2043</option>
392: <option value="2044">2044</option>
393: <option value="2045">2045</option>
394: <option value="2046">2046</option>
395: <option value="2047">2047</option>
396: <option value="2048">2048</option>
397: <option value="2049">2049</option>
398: <option value="2050">2050</option>
399: <option value="2051">2051</option>
1.3 harris41 400: </select>
401: END
1.11 harris41 402: $year=~s/(\"$currentyear\")/$1 SELECTED/ if length($currentyear);
403: return "$month$day$year";
1.3 harris41 404: }
405:
406: sub selectbox {
407: my ($title,$name,$value,%options)=@_;
408: my $uctitle=uc($title);
409: my $selout="\n<p><font color=\"#800000\" face=\"helvetica\"><b>$uctitle:".
410: "</b></font><br>".'<select name="'.$name.'">';
411: map {
412: $selout.='<option value="'.$_.'"';
413: if ($_ eq $value) { $selout.=' selected'; }
414: $selout.='>'.$options{$_}.'</option>';
415: } sort keys %options;
416: return $selout.'</select>';
1.6 harris41 417: }
418:
419: # --------------------------------------------------- Performing a basic search
420: sub basicsearch {
421: my ($r,$expression)=@_;
422:
423: my $query=$expression;
424: $query="select * from metadata where concat(title,\" \",author) like '\%$expression\%'";
1.12 harris41 425: # my $reply=&Apache::lonnet::reply("querysend:$query",'msul3');
1.13 harris41 426: my $reply=&Apache::lonnet::metadata_query($query);
1.6 harris41 427:
428: my @results;
429:
430: my $replyfile='';
431: $reply=~/^([\.\w]+)$/; # must do since 'use strict' checks for tainting
432: $replyfile=$r->dir_config('lonDaemons').'/tmp/'.$1;
433: $reply=~/(.*?)\_/;
434: my $hostname=$1;
435:
436: {
437: while (1) {
438: last if -e $replyfile;
439: sleep 1;
440: }
441: # QUESTION: how should I handle this error condition..
442: # I'm sure there is syntax elsewhere I can use..
443: my $fh=Apache::File->new($replyfile) or
444: ($r->print('file cannot be opened') and return OK);
445: @results=<$fh>;
446: }
447:
448: my $compiledresult='';
449:
450: foreach my $result (@results) {
1.9 harris41 451: my ($title,$author,$subject,$url,$keywords,$version,
452: $notes,$abstract,$mime,$lang,
1.6 harris41 453: $creationdate,$lastrevisiondate,$owner,$copyright
454: )=map {&Apache::lonnet::unescape($_)} (split(/\,/,$result));
455: my $shortabstract=$abstract;
456: $shortabstract=substr($abstract,0,200) if length($abstract)>200;
1.8 harris41 457: $compiledresult.=<<END;
1.6 harris41 458: <p>
1.8 harris41 459: END
460: $compiledresult.=<<END if $ENV{'form.catalogmode'} eq 'interactive';
461: <font size='-1'><INPUT TYPE="button" NAME="returnvalues" VALUE="SELECT"
1.10 harris41 462: onClick="javascript:select_data('$title','$url')">
1.8 harris41 463: </font>
464: <br>
465: END
466: $compiledresult.=<<END;
1.16 ! harris41 467: <b>URL: </b> <A HREF='http://spock.lite.msu.edu/res/msu/shh1/poobah1.html' TARGET='search_preview'>$url</A><br>
1.6 harris41 468: <b>Title:</b> $title<br>
469: <b>Author(s):</b> $author<br>
470: <b>Subject:</b> $subject<br>
1.9 harris41 471: <b>Keyword(s):</b> $keywords<br>
1.6 harris41 472: <b>Notes:</b> $notes<br>
473: <b>Abstract:</b> $shortabstract<br>
474: <b>MIME Type:</b> $mimetag{$mime}<br>
475: <b>Language:</b> $language{$lang}<br>
476: <b>Creation Date:</b> $creationdate<br>
477: <b>Last Revision Date:</b> $lastrevisiondate<br>
478: <b>Publisher/Owner:</b> $owner<br>
479: <b>Copyright/Distribution:</b> $copyright<br>
480: <b>Repository Location:</b> $hostname
481: </p>
482: END
483: }
484:
485: unless ($compiledresult) {
486: $compiledresult="There were no results that matched your query";
487: }
488:
1.9 harris41 489: # make query information persistent to allow for subsequent revision
490: my $persistent='';
491: map {
492: if (/^form\./ && !/submit/) {
493: my $name=$_;
494: my $key=$name;
495: $name=~s/^form\.//;
496: $persistent.=<<END;
497: <INPUT TYPE='hidden' NAME='$name' VALUE='$ENV{$key}'>
498: END
499: }
500: } (keys %ENV);
501:
1.8 harris41 502: $r->print(<<BEGINNING);
1.6 harris41 503: <html>
504: <head>
505: <title>The LearningOnline Network with CAPA</title>
1.8 harris41 506: BEGINNING
507: $r->print(<<SCRIPT) if $ENV{'form.catalogmode'} eq 'interactive';
508: <script>
509: function select_data(title,url) {
510: changeTitle(title);
511: changeURL(url);
512: }
513: function changeTitle(val) {
514: if (opener.inf.document.forms.resinfo.elements.t) {
515: opener.inf.document.forms.resinfo.elements.t.value=val;
516: }
517: }
518: function changeURL(val) {
519: if (opener.inf.document.forms.resinfo.elements.u) {
520: opener.inf.document.forms.resinfo.elements.u.value=val;
521: }
522: }
523: </script>
524: SCRIPT
525: $r->print(<<RESULTS);
1.6 harris41 526: </head>
527: <body bgcolor="#ffffff">
528: <img align=right src=/adm/lonIcons/lonlogos.gif>
529: <h1>Search Catalog</h1>
530: <form method="post" action="/adm/searchcat">
1.9 harris41 531: <input type='button' value='Revise search request'
532: onClick='this.form.submit();'>
1.11 harris41 533: <input type='button' value='CLOSE'
534: onClick='self.close();'>
1.9 harris41 535: $persistent
1.6 harris41 536: <hr>
537: <h3>Search Query</h3>
538: <p>
1.8 harris41 539: <b>Basic search:</b> $expression
1.6 harris41 540: </p>
541: <h3>Search Results</h3>
542: $compiledresult
543: </body>
544: </html>
545: RESULTS
546:
547: return OK;
1.3 harris41 548: }
1.1 www 549:
550: 1;
551: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>