Annotation of loncom/interface/lonsearchcat.pm, revision 1.10
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',
85: '');
86:
87: $scrout.=&searchphrasefield('Limit by author','author',
88: '');
89:
90: $scrout.=&searchphrasefield('Limit by subject','subject',
91: '');
92:
93: $scrout.=&searchphrasefield('Limit by notes','notes',
94: '');
95:
96: $scrout.=&searchphrasefield('Limit by abstract','abstract',
97: '');
98:
99: $scrout.=&selectbox('Limit by MIME type','mime',
100: 'notxxx',%mimetag);
101:
102: $scrout.=&selectbox('Limit by language','language',
103: 'any',%language);
104:
1.8 harris41 105:
106: # ------------------------------------------------ Compute date selection boxes
107: $scrout.=<<CREATIONDATESTART;
1.3 harris41 108: <p>
109: <font color="#800000" face="helvetica"><b>LIMIT BY CREATION DATE RANGE:</b>
110: </font>
111: <br>
1.8 harris41 112: between:
113: CREATIONDATESTART
1.9 harris41 114: $scrout.=&dateboxes('creationdatestart',1,1,1976);
1.8 harris41 115: $scrout.=<<CREATIONDATEEND;
116: and:
117: CREATIONDATEEND
1.9 harris41 118: $scrout.=&dateboxes('creationdateend',12,31,2051);
1.8 harris41 119: $scrout.="</p>";
120:
121: $scrout.=<<LASTREVISIONDATESTART;
122: <p>
123: <font color="#800000" face="helvetica"><b>LIMIT BY LAST REVISION DATE RANGE:
124: </b></font>
125: <br>between:
126: LASTREVISIONDATESTART
1.9 harris41 127: $scrout.=&dateboxes('lastrevisiondatestart',1,1,1976);
1.8 harris41 128: $scrout.=<<LASTREVISIONDATEEND;
129: and:
130: LASTREVISIONDATEEND
1.9 harris41 131: $scrout.=&dateboxes('lastrevisiondateend',12,31,2051);
1.8 harris41 132: $scrout.='</p>';
133:
134: $scrout.=&searchphrasefield('Limit by publisher/owner','owner',
135: $metadatafields{'owner'});
136:
137: $scrout.=&selectbox('Limit by copyright/distribution','copyright',
138: 'any',%cprtag);
139:
140: # ---------------------------------------------------------------- Print screen
141: $r->print(<<ENDDOCUMENT);
142: <html>
143: <head>
144: <title>The LearningOnline Network with CAPA</title>
145: </head>
146: <body bgcolor="#FFFFFF">
147: <img align=right src=/adm/lonIcons/lonlogos.gif>
148: <h1>Search Catalog</h1>
149: <form method="post" action="/adm/searchcat">
150: $hidden
151: <hr>
152: <h3>Basic Search</h3>
153: <p>
154: Enter terms or phrases separated by search operators
155: such as AND or OR then press SEARCH below. Terms should be specific
156: to the title, author, subject, notes, or abstract information associated
157: with a resource.
158: <br>
159: <input type="text" name="basicexp">
160: <input type="checkbox" name="titleonly">
161: <font color="#800000">Title only</font>
162: <br>
163: <input type="submit" name="basicsubmit" value="SEARCH">
164: <input type="reset" name="reset" value="RESET">
165: <input type="button" name="close" value="CLOSE" onClick="self.close()">
166: </p>
167: <hr>
168: <h3>Advanced Search</h3>
169: $scrout
170: <p>
171: <input type="submit" name="advancedsubmit" value="SEARCH">
172: <input type="reset" name="reset" value="RESET">
173: <input type="button" name="close" value="CLOSE" onClick="self.close()">
1.3 harris41 174: </p>
1.8 harris41 175: </form>
176: </body>
177: </html>
178: ENDDOCUMENT
179: return OK;
180: }
181:
182: # --------------------------------------------------------- Various form fields
183:
184: sub textfield {
185: my ($title,$name,$value)=@_;
186: return "\n<p><b>$title:</b><br>".
187: '<input type=text name="'.$name.'" size=80 value="'.$value.'">';
188: }
189:
190: sub searchphrasefield {
191: my ($title,$name,$value)=@_;
192: my $instruction=<<END;
193: Enter terms or phrases separated by search operators such
194: as AND or OR.
195: END
196: my $uctitle=uc($title);
197: return "\n<p><font color=\"#800000\" face=\"helvetica\"><b>$uctitle:</b>".
198: "</FONT> $instruction<br>".
199: '<input type=text name="'.$name.'" size=80 value="'.$value.'">';
200: }
1.3 harris41 201:
1.8 harris41 202: sub dateboxes {
1.9 harris41 203: my ($name,$defaultmonth,$defaultday,$defaultyear)=@_;
1.8 harris41 204: return<<END;
205: <select name="${name}_month">
1.9 harris41 206: <option value="$defaultmonth"> </option>
1.3 harris41 207: <option value="01">January</option>
208: <option value="02">February</option>
209: <option value="03">March</option>
210: <option value="04">April</option>
211: <option value="05">May</option>
212: <option value="06">June</option>
213: <option value="07">July</option>
214: <option value="08">August</option>
215: <option value="09">September</option>
216: <option value="10">October</option>
217: <option value="11">November</option>
218: <option value="12">December</option>
219: </select>
1.8 harris41 220: <select name="${name}_day">
1.9 harris41 221: <option value="$defaultday"> </option>
1.3 harris41 222: <option value=1>1</option>
223: <option value=2>2</option>
224: <option value=3>3</option>
225: <option value=4>4</option>
226: <option value=5>5</option>
227: <option value=6>6</option>
228: <option value=7>7</option>
229: <option value=8>8</option>
230: <option value=9>9</option>
231: <option value=10>10</option>
232: <option value=11>11</option>
233: <option value=12>12</option>
234: <option value=13>13</option>
235: <option value=14>14</option>
236: <option value=15>15</option>
237: <option value=16>16</option>
238: <option value=17>17</option>
239: <option value=18>18</option>
240: <option value=19>19</option>
241: <option value=20>20</option>
242: <option value=21>21</option>
243: <option value=22>22</option>
244: <option value=23>23</option>
245: <option value=24>24</option>
246: <option value=25>25</option>
247: <option value=26>26</option>
248: <option value=27>27</option>
249: <option value=28>28</option>
250: <option value=29>29</option>
251: <option value=30>30</option>
252: <option value=31>31</option>
253: </select>
1.8 harris41 254: <select name="${name}_year">
1.9 harris41 255: <option value="$defaultyear"> </option>
1.3 harris41 256: <option value=1976>1976</option>
257: <option value=1977>1977</option>
258: <option value=1978>1978</option>
259: <option value=1979>1979</option>
260: <option value=1980>1980</option>
261: <option value=1981>1981</option>
262: <option value=1982>1982</option>
263: <option value=1983>1983</option>
264: <option value=1984>1984</option>
265: <option value=1985>1985</option>
266: <option value=1986>1986</option>
267: <option value=1987>1987</option>
268: <option value=1988>1988</option>
269: <option value=1989>1989</option>
270: <option value=1990>1990</option>
271: <option value=1991>1991</option>
272: <option value=1992>1992</option>
273: <option value=1993>1993</option>
274: <option value=1994>1994</option>
275: <option value=1995>1995</option>
276: <option value=1996>1996</option>
277: <option value=1997>1997</option>
278: <option value=1998>1998</option>
279: <option value=1999>1999</option>
280: <option value=2000>2000</option>
281: <option value=2001>2001</option>
282: <option value=2002>2002</option>
283: <option value=2003>2003</option>
284: <option value=2004>2004</option>
285: <option value=2005>2005</option>
286: <option value=2006>2006</option>
287: <option value=2007>2007</option>
288: <option value=2008>2008</option>
289: <option value=2009>2009</option>
290: <option value=2010>2010</option>
291: <option value=2011>2011</option>
292: <option value=2012>2012</option>
293: <option value=2013>2013</option>
294: <option value=2014>2014</option>
295: <option value=2015>2015</option>
296: <option value=2016>2016</option>
297: <option value=2017>2017</option>
298: <option value=2018>2018</option>
299: <option value=2019>2019</option>
300: <option value=2020>2020</option>
301: <option value=2021>2021</option>
302: <option value=2022>2022</option>
303: <option value=2023>2023</option>
304: <option value=2024>2024</option>
305: <option value=2025>2025</option>
306: <option value=2026>2026</option>
307: <option value=2027>2027</option>
308: <option value=2028>2028</option>
309: <option value=2029>2029</option>
310: <option value=2030>2030</option>
311: <option value=2031>2031</option>
312: <option value=2032>2032</option>
313: <option value=2033>2033</option>
314: <option value=2034>2034</option>
315: <option value=2035>2035</option>
316: <option value=2036>2036</option>
317: <option value=2037>2037</option>
318: <option value=2038>2038</option>
319: <option value=2039>2039</option>
320: <option value=2040>2040</option>
321: <option value=2041>2041</option>
322: <option value=2042>2042</option>
323: <option value=2043>2043</option>
324: <option value=2044>2044</option>
325: <option value=2045>2045</option>
326: <option value=2046>2046</option>
327: <option value=2047>2047</option>
328: <option value=2048>2048</option>
329: <option value=2049>2049</option>
330: <option value=2050>2050</option>
331: <option value=2051>2051</option>
332: </select>
333: END
334: }
335:
336: sub selectbox {
337: my ($title,$name,$value,%options)=@_;
338: my $uctitle=uc($title);
339: my $selout="\n<p><font color=\"#800000\" face=\"helvetica\"><b>$uctitle:".
340: "</b></font><br>".'<select name="'.$name.'">';
341: map {
342: $selout.='<option value="'.$_.'"';
343: if ($_ eq $value) { $selout.=' selected'; }
344: $selout.='>'.$options{$_}.'</option>';
345: } sort keys %options;
346: return $selout.'</select>';
1.6 harris41 347: }
348:
349: # --------------------------------------------------- Performing a basic search
350: sub basicsearch {
351: my ($r,$expression)=@_;
352:
353: my $query=$expression;
354: $query="select * from metadata where concat(title,\" \",author) like '\%$expression\%'";
355: my $reply=&Apache::lonnet::reply("querysend:$query",'msul3');
356:
357: my @results;
358:
359: my $replyfile='';
360: $reply=~/^([\.\w]+)$/; # must do since 'use strict' checks for tainting
361: $replyfile=$r->dir_config('lonDaemons').'/tmp/'.$1;
362: $reply=~/(.*?)\_/;
363: my $hostname=$1;
364:
365: {
366: while (1) {
367: last if -e $replyfile;
368: sleep 1;
369: }
370: # QUESTION: how should I handle this error condition..
371: # I'm sure there is syntax elsewhere I can use..
372: my $fh=Apache::File->new($replyfile) or
373: ($r->print('file cannot be opened') and return OK);
374: @results=<$fh>;
375: }
376:
377: my $compiledresult='';
378:
379: foreach my $result (@results) {
1.9 harris41 380: my ($title,$author,$subject,$url,$keywords,$version,
381: $notes,$abstract,$mime,$lang,
1.6 harris41 382: $creationdate,$lastrevisiondate,$owner,$copyright
383: )=map {&Apache::lonnet::unescape($_)} (split(/\,/,$result));
384: my $shortabstract=$abstract;
385: $shortabstract=substr($abstract,0,200) if length($abstract)>200;
1.8 harris41 386: $compiledresult.=<<END;
1.6 harris41 387: <p>
1.8 harris41 388: END
389: $compiledresult.=<<END if $ENV{'form.catalogmode'} eq 'interactive';
390: <font size='-1'><INPUT TYPE="button" NAME="returnvalues" VALUE="SELECT"
1.10 ! harris41 391: onClick="javascript:select_data('$title','$url')">
1.8 harris41 392: </font>
393: <br>
394: END
395: $compiledresult.=<<END;
1.9 harris41 396: <b>URL: </b> $url<br>
1.6 harris41 397: <b>Title:</b> $title<br>
398: <b>Author(s):</b> $author<br>
399: <b>Subject:</b> $subject<br>
1.9 harris41 400: <b>Keyword(s):</b> $keywords<br>
1.6 harris41 401: <b>Notes:</b> $notes<br>
402: <b>Abstract:</b> $shortabstract<br>
403: <b>MIME Type:</b> $mimetag{$mime}<br>
404: <b>Language:</b> $language{$lang}<br>
405: <b>Creation Date:</b> $creationdate<br>
406: <b>Last Revision Date:</b> $lastrevisiondate<br>
407: <b>Publisher/Owner:</b> $owner<br>
408: <b>Copyright/Distribution:</b> $copyright<br>
409: <b>Repository Location:</b> $hostname
410: </p>
411: END
412: }
413:
414: unless ($compiledresult) {
415: $compiledresult="There were no results that matched your query";
416: }
417:
1.9 harris41 418: # make query information persistent to allow for subsequent revision
419: my $persistent='';
420: map {
421: if (/^form\./ && !/submit/) {
422: my $name=$_;
423: my $key=$name;
424: $name=~s/^form\.//;
425: $persistent.=<<END;
426: <INPUT TYPE='hidden' NAME='$name' VALUE='$ENV{$key}'>
427: END
428: }
429: } (keys %ENV);
430:
1.8 harris41 431: $r->print(<<BEGINNING);
1.6 harris41 432: <html>
433: <head>
434: <title>The LearningOnline Network with CAPA</title>
1.8 harris41 435: BEGINNING
436: $r->print(<<SCRIPT) if $ENV{'form.catalogmode'} eq 'interactive';
437: <script>
438: function select_data(title,url) {
439: changeTitle(title);
440: changeURL(url);
441: }
442: function changeTitle(val) {
443: if (opener.inf.document.forms.resinfo.elements.t) {
444: opener.inf.document.forms.resinfo.elements.t.value=val;
445: }
446: }
447: function changeURL(val) {
448: if (opener.inf.document.forms.resinfo.elements.u) {
449: opener.inf.document.forms.resinfo.elements.u.value=val;
450: }
451: }
452: </script>
453: SCRIPT
454: $r->print(<<RESULTS);
1.6 harris41 455: </head>
456: <body bgcolor="#ffffff">
457: <img align=right src=/adm/lonIcons/lonlogos.gif>
458: <h1>Search Catalog</h1>
459: <form method="post" action="/adm/searchcat">
1.9 harris41 460: <input type='button' value='Revise search request'
461: onClick='this.form.submit();'>
462: $persistent
1.6 harris41 463: <hr>
464: <h3>Search Query</h3>
465: <p>
1.8 harris41 466: <b>Basic search:</b> $expression
1.6 harris41 467: </p>
468: <h3>Search Results</h3>
469: $compiledresult
470: </body>
471: </html>
472: RESULTS
473:
474: return OK;
1.3 harris41 475: }
1.1 www 476:
477: 1;
478: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>