1: # The LearningOnline Network with CAPA
2: #
3: # Search Catalog
4: #
5: # YEAR=2001
6: # 3/8, 3/12, 3/13, 3/14, 3/15, 3/19 Scott Harrison
7: # 3/20, 3/21, 3/22, 3/26, 3/27, 4/2, 8/15, 8/24, 8/25 Scott Harrison
8: # 10/12,10/14,10/15,10/16,11/28 Scott Harrison
9: #
10: # $Id: lonsearchcat.pm,v 1.106 2001/11/28 19:26:20 harris41 Exp $
11: ###
12:
13: ###############################################################################
14: ## ##
15: ## ORGANIZATION OF THIS PERL MODULE ##
16: ## ##
17: ## 1. Modules used by this module ##
18: ## 2. Choices for different output views (detailed, summary, xml, etc) ##
19: ## 3. BEGIN block (to be run once after compilation) ##
20: ## 4. Handling routine called via Apache and mod_perl ##
21: ## 5. Other subroutines ##
22: ## ##
23: ###############################################################################
24:
25: package Apache::lonsearchcat;
26:
27: # ------------------------------------------------- modules used by this module
28: use strict;
29: use Apache::Constants qw(:common);
30: use Apache::lonnet();
31: use Apache::File();
32: use CGI qw(:standard);
33: use Text::Query;
34: use GDBM_File;
35:
36: # ---------------------------------------- variables used throughout the module
37:
38: # -- information holders
39: my %language; # holds contents of language.tab
40: my %cprtag; # holds contents of copyright.tab
41: my %mimetag; # holds contents of filetypes.tab
42: my %hostdomains; # matches host name to host domain
43: my %hostips; # matches host name to host ip
44: my %hitcount; # stores number of hits per host
45:
46: # -- dynamically rendered interface components
47: my $closebutton; # button that closes the search window
48: my $importbutton; # button to take the selected results and go to group sorting
49:
50: # -- miscellaneous variables
51: my $scrout; # string that holds portions of the screen output
52: my $yourself; # allows for quickly limiting to oneself
53: my %hash;
54:
55: # ------------------------------------------ choices for different output views
56: # Detailed Citation View ---> sub detailed_citation_view
57: # Summary View ---> sub summary_view
58: # Fielded Format ---> sub fielded_format_view
59: # XML/SGML ---> sub xml_sgml_view
60: my $basicviewselect=<<END;
61: <select name='basicviewselect'>
62: <option value='Detailed Citation View'>Detailed Citation View</option>
63: <option value='Summary View'>Summary View</option>
64: <option value='Fielded Format'>Fielded Format</option>
65: <option value='XML/SGML'>XML/SGML</option>
66: </select>
67: END
68: my $advancedviewselect=<<END;
69: <select name='advancedviewselect'>
70: <option value='Detailed Citation View'>Detailed Citation View</option>
71: <option value='Summary View'>Summary View</option>
72: <option value='Fielded Format'>Fielded Format</option>
73: <option value='XML/SGML'>XML/SGML</option>
74: </select>
75: END
76:
77: # ----------------------------------------------------------------------- BEGIN
78: sub BEGIN {
79: # --------------------------------- Compute various listings of metadata values
80: $language{'any'}='Any language';
81: {
82: my $fh=Apache::File->new($Apache::lonnet::perlvar{'lonTabDir'}.
83: '/language.tab');
84: map {
85: $_=~/(\w+)\s+([\w\s\-]+)/; chomp;
86: $language{$1}=$2;
87: } <$fh>;
88: }
89: $cprtag{'any'}='Any copyright/distribution';
90: {
91: my $fh=Apache::File->new($Apache::lonnet::perlvar{'lonIncludes'}.
92: '/copyright.tab');
93: map {
94: $_=~/(\w+)\s+([\w\s\-]+)/; chomp;
95: $cprtag{$1}=$2;
96: } <$fh>;
97: }
98: $mimetag{'any'}='Any type';
99: {
100: my $fh=Apache::File->new($Apache::lonnet::perlvar{'lonTabDir'}.
101: '/filetypes.tab');
102: map {
103: $_=~/(\w+)\s+(\w+)\s+([\w\s\-]+)/; chomp;
104: $mimetag{$1}=".$1 $3";
105: } <$fh>;
106: }
107: {
108: my $fh=Apache::File->new($Apache::lonnet::perlvar{'lonTabDir'}.
109: '/hosts.tab');
110: map {
111: $_=~/(\w+?)\:(\w+?)\:(\w+?)\:(.*)/; chomp;
112: if ($3 eq 'library') {
113: $hostdomains{$1}=$2;
114: $hostips{$1}=$4;
115: }
116: } <$fh>;
117: }
118: }
119:
120: my $diropendb = "";
121: my $domain = "";
122:
123: # ----------------------------- Handling routine called via Apache and mod_perl
124: sub handler {
125: my $r = shift;
126: untie %hash;
127: &get_unprocessed_cgi();
128:
129: $r->content_type('text/html');
130: $r->send_http_header;
131: return OK if $r->header_only;
132:
133: $domain = $r->dir_config('lonDefDomain');
134:
135: $diropendb= "/home/httpd/perl/tmp/$domain\_$ENV{'user.name'}_searchcat.db";
136:
137: if ($ENV{'form.launch'} eq '1') {
138: if (tie(%hash,'GDBM_File',$diropendb,&GDBM_WRCREAT,0640)) {
139: &start_fresh_session();
140: untie %hash;
141: }
142: else {
143: $r->print('<html><head></head><body>Unable to tie hash to db '.
144: 'file</body></html>');
145: return OK;
146: }
147: }
148:
149: # ----------------------------------- configure dynamic components of interface
150: my $hidden='';
151: if ($ENV{'form.catalogmode'} eq 'interactive') {
152: $hidden="<input type='hidden' name='catalogmode' value='interactive'>".
153: "\n";
154: $closebutton="<input type='button' name='close' value='CLOSE' ".
155: "onClick='self.close()'>"."\n";
156: }
157: elsif ($ENV{'form.catalogmode'} eq 'groupsearch') {
158: $hidden=<<END;
159: <input type='hidden' name='catalogmode' value='groupsearch'>
160: END
161: $closebutton=<<END;
162: <input type='button' name='close' value='CLOSE' onClick='self.close()'>
163: END
164: $importbutton=<<END;
165: <input type='button' name='import' value='IMPORT'
166: onClick='javascript:select_group()'>
167: END
168: }
169:
170: # ------------------------------------------------------ Determine current user
171: $yourself=$ENV{'user.name'}.'@'.$ENV{'user.domain'};
172:
173: # --- Now, depending on the interface actions, do one of three things here:
174: # --- 1. a basic search
175: # --- 2. an advanced search
176: # --- 3. output a search interface
177:
178: # ----------------------------------- See if a search invocation should be done
179: if ($ENV{'form.basicsubmit'} eq 'SEARCH') {
180: untie %hash; return &basicsearch($r,\%ENV);
181: }
182: elsif ($ENV{'form.advancedsubmit'} eq 'SEARCH') {
183: untie %hash; return &advancedsearch($r,\%ENV);
184: }
185:
186: # ----------------------------- Else, begin building search interface to output
187: $scrout=''; # building a part of screen output
188: $scrout.=&searchphrasefield('Limit by title','title',
189: $ENV{'form.title'});
190:
191: $scrout.=&searchphrasefield('Limit by author','author',
192: $ENV{'form.author'});
193:
194: $scrout.=&searchphrasefield('Limit by subject','subject',
195: $ENV{'form.subject'});
196:
197: $scrout.=&searchphrasefield('Limit by keywords','keywords',
198: $ENV{'form.keywords'});
199:
200: $scrout.=&searchphrasefield('Limit by URL','url',
201: $ENV{'form.url'});
202:
203: # $scrout.=&searchphrasefield('Limit by version','version',
204: # $ENV{'form.version'});
205:
206: $scrout.=&searchphrasefield('Limit by notes','notes',
207: $ENV{'form.notes'});
208:
209: $scrout.=&searchphrasefield('Limit by abstract','abstract',
210: $ENV{'form.abstract'});
211:
212: $ENV{'form.mime'}='notxxx' unless length($ENV{'form.mime'});
213: $scrout.=&selectbox('Limit by MIME type','mime',
214: $ENV{'form.mime'},%mimetag);
215:
216: $ENV{'form.language'}='any' unless length($ENV{'form.language'});
217:
218: $scrout.=&selectbox('Limit by language','language',
219: $ENV{'form.language'},%language);
220:
221:
222: # ------------------------------------------------ Compute date selection boxes
223: $scrout.=<<CREATIONDATESTART;
224: <p>
225: <font color="#800000" face="helvetica"><b>LIMIT BY CREATION DATE RANGE:</b>
226: </font>
227: <br />
228: between:
229: CREATIONDATESTART
230: $scrout.=&dateboxes('creationdatestart',1,1,1976,
231: $ENV{'form.creationdatestart_month'},
232: $ENV{'form.creationdatestart_day'},
233: $ENV{'form.creationdatestart_year'},
234: );
235: $scrout.=<<CREATIONDATEEND;
236: and:
237: CREATIONDATEEND
238: $scrout.=&dateboxes('creationdateend',12,31,2051,
239: $ENV{'form.creationdateend_month'},
240: $ENV{'form.creationdateend_day'},
241: $ENV{'form.creationdateend_year'},
242: );
243: $scrout.="</p>";
244:
245: $scrout.=<<LASTREVISIONDATESTART;
246: <p>
247: <font color="#800000" face="helvetica"><b>LIMIT BY LAST REVISION DATE RANGE:
248: </b></font>
249: <br />between:
250: LASTREVISIONDATESTART
251: $scrout.=&dateboxes('lastrevisiondatestart',1,1,1976,
252: $ENV{'form.lastrevisiondatestart_month'},
253: $ENV{'form.lastrevisiondatestart_day'},
254: $ENV{'form.lastrevisiondatestart_year'},
255: );
256: $scrout.=<<LASTREVISIONDATEEND;
257: and:
258: LASTREVISIONDATEEND
259: $scrout.=&dateboxes('lastrevisiondateend',12,31,2051,
260: $ENV{'form.lastrevisiondateend_month'},
261: $ENV{'form.lastrevisiondateend_day'},
262: $ENV{'form.lastrevisiondateend_year'},
263: );
264: $scrout.='</p>';
265:
266: $scrout.=&searchphrasefield('Limit by publisher/owner','owner',
267: $ENV{'form.owner'});
268:
269: $ENV{'form.copyright'}='any' unless length($ENV{'form.copyright'});
270: $scrout.=&selectbox('Limit by copyright/distribution','copyright',
271: $ENV{'form.copyright'},%cprtag);
272:
273: # ------------------------------------------- Compute customized metadata field
274: $scrout.=<<CUSTOMMETADATA;
275: <p>
276: <font color="#800000" face="helvetica"><b>LIMIT BY SPECIAL METADATA FIELDS:</b>
277: </font>
278: For resource-specific metadata, enter in an expression in the form of
279: <i>key</i>=<i>value</i> separated by operators such as AND, OR or NOT.<br />
280: <b>Example:</b> grandmother=75 OR grandfather=85
281: <br />
282: CUSTOMMETADATA
283: $scrout.=&simpletextfield('custommetadata',$ENV{'form.custommetadata'});
284: $scrout.=' <i>initial users of this system do not need to worry about this option</i>';
285:
286: $scrout.=<<CUSTOMSHOW;
287: <p>
288: <font color="#800000" face="helvetica"><b>SHOW SPECIAL METADATA FIELDS:</b>
289: </font>
290: Enter in a space-separated list of special metadata fields to show
291: in a fielded listing for each record result.
292: <br />
293: CUSTOMSHOW
294: $scrout.=&simpletextfield('customshow',$ENV{'form.customshow'});
295: $scrout.=' <i>initial users of this system do not need to worry about this option</i>';
296:
297: # ---------------------------------------------------------------- Print screen
298: $r->print(<<ENDDOCUMENT);
299: <html>
300: <head>
301: <title>The LearningOnline Network with CAPA</title>
302: <script type="text/javascript">
303: function openhelp(val) {
304: openhelpwin=open('/adm/help/searchcat.html','helpscreen',
305: 'scrollbars=1,width=400,height=300');
306: openhelpwin.focus();
307: }
308: </script>
309: </head>
310: <body bgcolor="#FFFFFF">
311: <img align='right' src='/adm/lonIcons/lonlogos.gif' />
312: <h1>Search Catalog</h1>
313: <form method="post" action="/adm/searchcat">
314: $hidden
315: <hr />
316: <h3>Basic Search</h3>
317: <p>
318: Enter terms or phrases separated by search operators
319: such as AND, OR, or NOT then press SEARCH below. Terms should be specific
320: to the title, author, subject, notes, or abstract information associated
321: with a resource.
322: <br />
323: ENDDOCUMENT
324: $r->print(&simpletextfield('basicexp',$ENV{'form.basicexp'}));
325: $r->print(' ');
326: $r->print(&simplecheckbox('titleonly',$ENV{'form.titleonly'}));
327: $r->print('<font color="#800000">Title only</font> ');
328: # $r->print(&simplecheckbox('allversions',$ENV{'form.allversions'}));
329: # <font color="#800000">Search historic archives</font>
330: $r->print(<<ENDDOCUMENT);
331: <br />
332: <input type="submit" name="basicsubmit" value='SEARCH' />
333: <input type="reset" name="reset" value='RESET' />
334: $closebutton
335: $basicviewselect
336: <input type="button" value="HELP" onClick="openhelp()" />
337: </p>
338: <hr />
339: <h3>Advanced Search</h3>
340: $scrout
341: <p>
342: <input type="submit" name="advancedsubmit" value='SEARCH' />
343: <input type="reset" name="reset" value='RESET' />
344: $closebutton
345: $advancedviewselect
346: <input type="button" value="HELP" onClick="openhelp()" />
347: </p>
348: </form>
349: </body>
350: </html>
351: ENDDOCUMENT
352: return OK;
353: }
354:
355: # ----------- grab unprocessed CGI variables that may have been appended to URL
356: sub get_unprocessed_cgi {
357: map {
358: my ($name, $value) = split(/=/,$_);
359: $value =~ tr/+/ /;
360: $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
361: if ($name eq 'catalogmode' or $name eq 'launch' or $name eq 'acts') {
362: $ENV{'form.'.$name}=$value;
363: }
364: } (split(/&/,$ENV{'QUERY_STRING'}));
365: }
366:
367: # ------------------------------------------------------------- make persistent
368: sub make_persistent {
369: my $persistent='';
370:
371: map {
372: if (/^form\./ && !/submit/) {
373: my $name=$_;
374: my $key=$name;
375: $ENV{$key}=~s/\'//g; # do not mess with html field syntax
376: $name=~s/^form\.//;
377: $persistent.=<<END;
378: <input type='hidden' name='$name' value='$ENV{$key}' />
379: END
380: }
381: } (keys %ENV);
382: return $persistent;
383: }
384:
385: # --------------------------------------------------------- Various form fields
386:
387: sub simpletextfield {
388: my ($name,$value)=@_;
389: return '<input type=text name=\''.$name.
390: '\' size=20 value=\''.$value.'\' />';
391: }
392:
393: sub simplecheckbox {
394: my ($name,$value)=@_;
395: my $checked='';
396: $checked="CHECKED" if $value eq 'on';
397: return '<input type=checkbox name=\''.$name.'\' '. $checked . '>';
398: }
399:
400: sub searchphrasefield {
401: my ($title,$name,$value)=@_;
402: my $instruction=<<END;
403: Enter terms or phrases separated by search operators such
404: as AND, OR, or NOT.
405: END
406: my $uctitle=uc($title);
407: return "\n<p><font color=\"#800000\" face=\"helvetica\"><b>$uctitle:</b>".
408: "</FONT> $instruction<br />".
409: '<input type=text name="'.$name.'" size=80 value=\''.$value.'\'>';
410: }
411:
412: sub dateboxes {
413: my ($name,$defaultmonth,$defaultday,$defaultyear,
414: $currentmonth,$currentday,$currentyear)=@_;
415: ($defaultmonth,$defaultday,$defaultyear)=('','','');
416: my $month=<<END;
417: <select name="${name}_month">
418: <option value='$defaultmonth'> </option>
419: <option value="1">January</option>
420: <option value="2">February</option>
421: <option value="3">March</option>
422: <option value="4">April</option>
423: <option value="5">May</option>
424: <option value="6">June</option>
425: <option value="7">July</option>
426: <option value="8">August</option>
427: <option value="9">September</option>
428: <option value="10">October</option>
429: <option value="11">November</option>
430: <option value="12">December</option>
431: </select>
432: END
433: $month=~s/(\"$currentmonth\")/$1 SELECTED/ if length($currentmonth);
434: my $day=<<END;
435: <select name="${name}_day">
436: <option value='$defaultday'> </option>
437: <option value="1">1</option>
438: <option value="2">2</option>
439: <option value="3">3</option>
440: <option value="4">4</option>
441: <option value="5">5</option>
442: <option value="6">6</option>
443: <option value="7">7</option>
444: <option value="8">8</option>
445: <option value="9">9</option>
446: <option value="10">10</option>
447: <option value="11">11</option>
448: <option value="12">12</option>
449: <option value="13">13</option>
450: <option value="14">14</option>
451: <option value="15">15</option>
452: <option value="16">16</option>
453: <option value="17">17</option>
454: <option value="18">18</option>
455: <option value="19">19</option>
456: <option value="20">20</option>
457: <option value="21">21</option>
458: <option value="22">22</option>
459: <option value="23">23</option>
460: <option value="24">24</option>
461: <option value="25">25</option>
462: <option value="26">26</option>
463: <option value="27">27</option>
464: <option value="28">28</option>
465: <option value="29">29</option>
466: <option value="30">30</option>
467: <option value="31">31</option>
468: </select>
469: END
470: $day=~s/(\"$currentday\")/$1 SELECTED/ if length($currentday);
471: my $year=<<END;
472: <select name="${name}_year">
473: <option value='$defaultyear'> </option>
474: <option value="1976">1976</option>
475: <option value="1977">1977</option>
476: <option value="1978">1978</option>
477: <option value="1979">1979</option>
478: <option value="1980">1980</option>
479: <option value="1981">1981</option>
480: <option value="1982">1982</option>
481: <option value="1983">1983</option>
482: <option value="1984">1984</option>
483: <option value="1985">1985</option>
484: <option value="1986">1986</option>
485: <option value="1987">1987</option>
486: <option value="1988">1988</option>
487: <option value="1989">1989</option>
488: <option value="1990">1990</option>
489: <option value="1991">1991</option>
490: <option value="1992">1992</option>
491: <option value="1993">1993</option>
492: <option value="1994">1994</option>
493: <option value="1995">1995</option>
494: <option value="1996">1996</option>
495: <option value="1997">1997</option>
496: <option value="1998">1998</option>
497: <option value="1999">1999</option>
498: <option value="2000">2000</option>
499: <option value="2001">2001</option>
500: <option value="2002">2002</option>
501: <option value="2003">2003</option>
502: <option value="2004">2004</option>
503: <option value="2005">2005</option>
504: <option value="2006">2006</option>
505: <option value="2007">2007</option>
506: <option value="2008">2008</option>
507: <option value="2009">2009</option>
508: <option value="2010">2010</option>
509: <option value="2011">2011</option>
510: <option value="2012">2012</option>
511: <option value="2013">2013</option>
512: <option value="2014">2014</option>
513: <option value="2015">2015</option>
514: <option value="2016">2016</option>
515: <option value="2017">2017</option>
516: <option value="2018">2018</option>
517: <option value="2019">2019</option>
518: <option value="2020">2020</option>
519: <option value="2021">2021</option>
520: <option value="2022">2022</option>
521: <option value="2023">2023</option>
522: <option value="2024">2024</option>
523: <option value="2025">2025</option>
524: <option value="2026">2026</option>
525: <option value="2027">2027</option>
526: <option value="2028">2028</option>
527: <option value="2029">2029</option>
528: <option value="2030">2030</option>
529: <option value="2031">2031</option>
530: <option value="2032">2032</option>
531: <option value="2033">2033</option>
532: <option value="2034">2034</option>
533: <option value="2035">2035</option>
534: <option value="2036">2036</option>
535: <option value="2037">2037</option>
536: <option value="2038">2038</option>
537: <option value="2039">2039</option>
538: <option value="2040">2040</option>
539: <option value="2041">2041</option>
540: <option value="2042">2042</option>
541: <option value="2043">2043</option>
542: <option value="2044">2044</option>
543: <option value="2045">2045</option>
544: <option value="2046">2046</option>
545: <option value="2047">2047</option>
546: <option value="2048">2048</option>
547: <option value="2049">2049</option>
548: <option value="2050">2050</option>
549: <option value="2051">2051</option>
550: </select>
551: END
552: $year=~s/(\"$currentyear\")/$1 SELECTED/ if length($currentyear);
553: return "$month$day$year";
554: }
555:
556: sub selectbox {
557: my ($title,$name,$value,%options)=@_;
558: my $uctitle=uc($title);
559: my $selout="\n<p><font color=\"#800000\" face=\"helvetica\"><b>$uctitle:".
560: "</b></font><br />".'<select name="'.$name.'">';
561: map {
562: $selout.='<option value=\''.$_.'\'';
563: if ($_ eq $value) { $selout.=' selected'; }
564: $selout.='>'.$options{$_}.'</option>';
565: } sort keys %options;
566: return $selout.'</select>';
567: }
568:
569: # ----------------------------------------------- Performing an advanced search
570: sub advancedsearch {
571: my ($r,$envhash)=@_;
572: my %ENV=%{$envhash};
573:
574: my $fillflag=0;
575: # Clean up fields for safety
576: for my $field ('title','author','subject','keywords','url','version',
577: 'creationdatestart_month','creationdatestart_day',
578: 'creationdatestart_year','creationdateend_month',
579: 'creationdateend_day','creationdateend_year',
580: 'lastrevisiondatestart_month','lastrevisiondatestart_day',
581: 'lastrevisiondatestart_year','lastrevisiondateend_month',
582: 'lastrevisiondateend_day','lastrevisiondateend_year',
583: 'notes','abstract','mime','language','owner',
584: 'custommetadata','customshow') {
585: $ENV{"form.$field"}=~s/[^\w\/\s\(\)\=\-\"\']//g;
586: }
587:
588: # Check to see if enough information was filled in
589: for my $field ('title','author','subject','keywords','url','version',
590: 'notes','abstract','mime','language','owner',
591: 'custommetadata') {
592: if (&filled($ENV{"form.$field"})) {
593: $fillflag++;
594: }
595: }
596: unless ($fillflag) {
597: &output_blank_field_error($r);
598: return OK;
599: }
600:
601:
602: # Turn the form input into a SQL-based query
603: my $query='';
604:
605: my @queries;
606: # Evaluate logical expression AND/OR/NOT phrase fields.
607: foreach my $field ('title','author','subject','notes','abstract','url',
608: 'keywords','version','owner') {
609: if ($ENV{'form.'.$field}) {
610: push @queries,&build_SQL_query($field,$ENV{'form.'.$field});
611: }
612: }
613: # Evaluate option lists
614: if ($ENV{'form.language'} and $ENV{'form.language'} ne 'any') {
615: push @queries,"(language like \"$ENV{'form.language'}\")";
616: }
617: if ($ENV{'form.mime'} and $ENV{'form.mime'} ne 'any') {
618: push @queries,"(mime like \"$ENV{'form.mime'}\")";
619: }
620: if ($ENV{'form.copyright'} and $ENV{'form.copyright'} ne 'any') {
621: push @queries,"(copyright like \"$ENV{'form.copyright'}\")";
622: }
623: # Evaluate date windows
624: my $datequery=&build_date_queries(
625: $ENV{'form.creationdatestart_month'},
626: $ENV{'form.creationdatestart_day'},
627: $ENV{'form.creationdatestart_year'},
628: $ENV{'form.creationdateend_month'},
629: $ENV{'form.creationdateend_day'},
630: $ENV{'form.creationdateend_year'},
631: $ENV{'form.lastrevisiondatestart_month'},
632: $ENV{'form.lastrevisiondatestart_day'},
633: $ENV{'form.lastrevisiondatestart_year'},
634: $ENV{'form.lastrevisiondateend_month'},
635: $ENV{'form.lastrevisiondateend_day'},
636: $ENV{'form.lastrevisiondateend_year'},
637: );
638: # Test to see if date windows are legitimate
639: if ($datequery=~/^Incorrect/) {
640: &output_date_error($r,$datequery);
641: return OK;
642: }
643: elsif ($datequery) {
644: push @queries,$datequery;
645: }
646:
647: # Process form information for custom metadata querying
648: my $customquery='';
649: if ($ENV{'form.custommetadata'}) {
650: $customquery=&build_custommetadata_query('custommetadata',
651: $ENV{'form.custommetadata'});
652: }
653: my $customshow='';
654: if ($ENV{'form.customshow'}) {
655: $customshow=$ENV{'form.customshow'};
656: $customshow=~s/[^\w\s]//g;
657: my @fields=split(/\s+/,$customshow);
658: $customshow=join(" ",@fields);
659: }
660: # Send query statements over the network to be processed by either the SQL
661: # database or a recursive scheme of 'grep'-like actions (for custom
662: # metadata).
663: if (@queries) {
664: $query=join(" AND ",@queries);
665: $query="select * from metadata where $query";
666: my $reply; # reply hash reference
667: unless ($customquery or $customshow) {
668: $reply=&Apache::lonnet::metadata_query($query);
669: }
670: else {
671: $reply=&Apache::lonnet::metadata_query($query,
672: $customquery,$customshow);
673: }
674: &output_results('Advanced',$r,$envhash,$customquery,$reply);
675: }
676: elsif ($customquery) {
677: my $reply; # reply hash reference
678: $reply=&Apache::lonnet::metadata_query('',
679: $customquery,$customshow);
680: &output_results('Advanced',$r,$envhash,$customquery,$reply);
681: }
682: # should not get to this point
683: return 'Error. Should not have gone to this point.';
684: }
685:
686: # --------------------------------------------------- Performing a basic search
687: sub basicsearch {
688: my ($r,$envhash)=@_;
689: my %ENV=%{$envhash};
690: # Clean up fields for safety
691: for my $field ('basicexp') {
692: $ENV{"form.$field"}=~s/[^\w\s\(\)\-]//g;
693: }
694:
695: # Check to see if enough is filled in
696: unless (&filled($ENV{'form.basicexp'})) {
697: &output_blank_field_error($r);
698: return OK;
699: }
700:
701: # Build SQL query string based on form page
702: my $query='';
703: my $concatarg=join('," ",',
704: ('title', 'author', 'subject', 'notes', 'abstract'));
705: $concatarg='title' if $ENV{'form.titleonly'};
706:
707: $query=&build_SQL_query('concat('.$concatarg.')',$ENV{'form.'.'basicexp'});
708:
709: # Get reply (either a hash reference to filehandles or bad connection)
710: my $reply=&Apache::lonnet::metadata_query('select * from metadata where '.$query);
711:
712: # Output search results
713:
714: &output_results('Basic',$r,$envhash,$query,$reply);
715:
716: return OK;
717: }
718:
719: # ------------------------------------------------------------- build_SQL_query
720: sub build_SQL_query {
721: my ($field_name,$logic_statement)=@_;
722: my $q=new Text::Query('abc',
723: -parse => 'Text::Query::ParseAdvanced',
724: -build => 'Text::Query::Build');
725: $q->prepare($logic_statement);
726: my $matchexp=${$q}{'matchexp'}; chomp $matchexp;
727: my $sql_query=&recursive_SQL_query_build($field_name,$matchexp);
728: return $sql_query;
729: }
730:
731: # ------------------------------------------------- build custom metadata query
732: sub build_custommetadata_query {
733: my ($field_name,$logic_statement)=@_;
734: my $q=new Text::Query('abc',
735: -parse => 'Text::Query::ParseAdvanced',
736: -build => 'Text::Query::BuildAdvancedString');
737: $q->prepare($logic_statement);
738: my $matchexp=${$q}{'-parse'}{'-build'}{'matchstring'};
739: # quick fix to change literal into xml tag-matching
740: # will eventually have to write a separate builder module
741: my $oldmatchexp=$matchexp;
742: $matchexp=~s/(\w+)\\\=([\w\\\+]+)/\\\<$1\\\>\[\^\\\<\]\*$2\[\^\\\<\]\*\\\<\\\/$1\\\>/g;
743: return $matchexp;
744: }
745:
746: # - Recursively parse a reverse notation expression into a SQL query expression
747: sub recursive_SQL_query_build {
748: my ($dkey,$pattern)=@_;
749: my @matches=($pattern=~/(\[[^\]|\[]*\])/g);
750: return $pattern unless @matches;
751: foreach my $match (@matches) {
752: $match=~/\[ (\w+)\s(.*) \]/;
753: my ($key,$value)=($1,$2);
754: my $replacement='';
755: if ($key eq 'literal') {
756: $replacement="($dkey like \"\%$value\%\")";
757: }
758: elsif ($key eq 'not') {
759: $value=~s/like/not like/;
760: # $replacement="($dkey not like $value)";
761: $replacement="$value";
762: }
763: elsif ($key eq 'and') {
764: $value=~/(.*[\"|\)]) ([|\(|\^].*)/;
765: $replacement="($1 AND $2)";
766: }
767: elsif ($key eq 'or') {
768: $value=~/(.*[\"|\)]) ([|\(|\^].*)/;
769: $replacement="($1 OR $2)";
770: }
771: substr($pattern,
772: index($pattern,$match),
773: length($match),
774: $replacement
775: );
776: }
777: &recursive_SQL_query_build($dkey,$pattern);
778: }
779:
780: # ------------------------------------------------------------ Build date query
781: sub build_date_queries {
782: my ($cmonth1,$cday1,$cyear1,$cmonth2,$cday2,$cyear2,
783: $lmonth1,$lday1,$lyear1,$lmonth2,$lday2,$lyear2)=@_;
784: my @queries;
785: if ($cmonth1 or $cday1 or $cyear1 or $cmonth2 or $cday2 or $cyear2) {
786: unless ($cmonth1 and $cday1 and $cyear1 and
787: $cmonth2 and $cday2 and $cyear2) {
788: return "Incorrect entry for the creation date. You must specify ".
789: "a starting month, day, and year and an ending month, ".
790: "day, and year.";
791: }
792: my $cnumeric1=sprintf("%d%2d%2d",$cyear1,$cmonth1,$cday1);
793: $cnumeric1+=0;
794: my $cnumeric2=sprintf("%d%2d%2d",$cyear2,$cmonth2,$cday2);
795: $cnumeric2+=0;
796: if ($cnumeric1>$cnumeric2) {
797: return "Incorrect entry for the creation date. The starting ".
798: "date must occur before the ending date.";
799: }
800: my $cquery="(creationdate BETWEEN '$cyear1-$cmonth1-$cday1' AND '".
801: "$cyear2-$cmonth2-$cday2 23:59:59')";
802: push @queries,$cquery;
803: }
804: if ($lmonth1 or $lday1 or $lyear1 or $lmonth2 or $lday2 or $lyear2) {
805: unless ($lmonth1 and $lday1 and $lyear1 and
806: $lmonth2 and $lday2 and $lyear2) {
807: return "Incorrect entry for the last revision date. You must ".
808: "specify a starting month, day, and year and an ending ".
809: "month, day, and year.";
810: }
811: my $lnumeric1=sprintf("%d%2d%2d",$lyear1,$lmonth1,$lday1);
812: $lnumeric1+=0;
813: my $lnumeric2=sprintf("%d%2d%2d",$lyear2,$lmonth2,$lday2);
814: $lnumeric2+=0;
815: if ($lnumeric1>$lnumeric2) {
816: return "Incorrect entry for the last revision date. The ".
817: "starting date must occur before the ending date.";
818: }
819: my $lquery="(lastrevisiondate BETWEEN '$lyear1-$lmonth1-$lday1' AND '".
820: "$lyear2-$lmonth2-$lday2 23:59:59')";
821: push @queries,$lquery;
822: }
823: if (@queries) {
824: return join(" AND ",@queries);
825: }
826: return '';
827: }
828:
829: # ----------------------------- format and output results based on a reply list
830: # There are two windows that this function writes to. The main search
831: # window ("srch") has a listing of the results. A secondary window ("popwin")
832: # gives the status of the network search (time elapsed, number of machines
833: # contacted, etc.)
834: sub output_results {
835: my $fnum; # search result counter
836: my ($mode,$r,$envhash,$query,$replyref)=@_;
837: my %ENV=%{$envhash};
838: my %rhash=%{$replyref};
839: my $compiledresult='';
840: my $timeremain=300;
841: my $elapsetime=0;
842: my $resultflag=0;
843: my $tflag=1;
844:
845: # make query information persistent to allow for subsequent revision
846: my $persistent=&make_persistent();
847:
848: # output beginning of search page
849: $r->print(<<BEGINNING);
850: <html>
851: <head>
852: <title>The LearningOnline Network with CAPA</title>
853: BEGINNING
854:
855: # conditional output of script functions dependent on the mode in
856: # which the search was invoked
857: $r->print(<<SCRIPT) if $ENV{'form.catalogmode'} eq 'interactive';
858: <script type="text/javascript">
859: function select_data(title,url) {
860: changeTitle(title);
861: changeURL(url);
862: self.close();
863: }
864: function changeTitle(val) {
865: if (opener.inf.document.forms.resinfo.elements.t) {
866: opener.inf.document.forms.resinfo.elements.t.value=val;
867: }
868: }
869: function changeURL(val) {
870: if (opener.inf.document.forms.resinfo.elements.u) {
871: opener.inf.document.forms.resinfo.elements.u.value=val;
872: }
873: }
874: </script>
875: SCRIPT
876: $r->print(<<SCRIPT) if $ENV{'form.catalogmode'} eq 'groupsearch';
877: <script type="text/javascript">
878: function select_data(title,url) {
879: // alert('DEBUG: Should be storing '+title+' and '+url);
880: }
881: function queue(val) {
882: if (eval("document.forms.results.returnvalues["+val+"].checked")) {
883: document.forms.results.acts.value+='1a'+val+'b';
884: }
885: else {
886: document.forms.results.acts.value+='0a'+val+'b';
887: }
888: }
889: function select_group() {
890: window.location="/adm/groupsort?catalogmode=groupsearch&acts="+
891: document.forms.results.acts.value;
892: }
893: </script>
894: SCRIPT
895: $r->print(<<SCRIPT);
896: <script type="text/javascript">
897: function displayinfo(val) {
898: popwin.document.forms.popremain.sdetails.value=val;
899: }
900: function openhelp(val) {
901: openhelpwin=open('/adm/help/searchcat.html','helpscreen',
902: 'scrollbars=1,width=400,height=300');
903: openhelpwin.focus();
904: }
905: function abortsearch(val) {
906: openhelpwin=open('/adm/help/searchcat.html','helpscreen',
907: 'scrollbars=1,width=400,height=300');
908: openhelpwin.focus();
909: }
910: </script>
911: SCRIPT
912: $r->rflush();
913:
914: # begin showing the cataloged results
915: $r->print(<<CATALOGBEGIN);
916: </head>
917: <body bgcolor="#ffffff">
918: <img align=right src=/adm/lonIcons/lonlogos.gif>
919: <h1>Search Catalog</h1>
920: CATALOGBEGIN
921: $r->print(<<CATALOGCONTROLS);
922: <form name='results' method="post" action="/adm/searchcat">
923: <input type='hidden' name='acts' value='' />
924: <input type='button' value='Revise search request'
925: onClick='this.form.submit();' />
926: $importbutton
927: $closebutton
928: $persistent
929: <hr />
930: <h3>Search Query</h3>
931: CATALOGCONTROLS
932: if ($mode eq 'Basic') {
933: $r->print(<<RESULTS);
934: <p>
935: <b>Basic search:</b> $ENV{'form.basicexp'}
936: </p>
937: RESULTS
938: }
939: elsif ($mode eq 'Advanced') {
940: $r->print(<<RESULTS);
941: <p>
942: <b>Advanced search</b>
943: $query
944: </p>
945: RESULTS
946: }
947: $r->print('<h3>Search Results</h3>');
948: $r->rflush();
949: my $servernum=(keys %rhash)+0;
950:
951: # define server grid (shows status of multiple machines)
952: my $hcinit;
953: my $grid="'<br />'+";
954: $grid.="\n";
955: my $sn=1;
956: for my $sk (sort keys %rhash) {
957: # '<a href="
958: $grid.="'<a href=\"";
959: # javascript:displayinfo('+
960: $grid.="javascript:opener.displayinfo('+";
961: # "'"+'key
962: $grid.="\"'\"+'";
963: $grid.=$sk;
964: my $hc;
965: if ($rhash{$sk} eq 'con_lost') {
966: $hc="!!!BAD CONNECTION, CONTACT SYSTEM ADMINISTRATOR!!!";
967: }
968: else {
969: $hc="'+\"'\"+\"+hc['$sk']+\"+\"'\"+'";
970: $hcinit.="hc[\"$sk\"]=\"not yet connected...\";";
971: }
972: $grid.=" hitcount=".$hc;
973: $grid.=" domain=".$hostdomains{$sk};
974: $grid.=" IP=".$hostips{$sk};
975: # '+"'"+'">'+
976: $grid.="'+\"'\"+')\">'+";
977: $grid.="\n";
978: $grid.="'<img border=\"0\" name=\"img".$sn."\"".
979: " src=\"/adm/lonIcons/srvnull.gif\" alt=\"".$sk."\" /></a>'+\n";
980: $grid.="'<br />'+\n" unless $sn%10;
981: $sn++;
982: }
983: $r->print(<<ENDPOP);
984: <script type="text/javascript">
985: popwin=open('','popwin','scrollbars=1,width=400,height=200');
986: popwin.focus();
987: popwin.document.writeln('<'+'html>');
988: popwin.document.writeln('<'+'head>');
989: popwin.document.writeln('<'+'script>');
990: popwin.document.writeln('hc=new Array();$hcinit');
991: popwin.document.writeln('<'+'/script>');
992: popwin.document.writeln('<'+'/head>'+
993: '<'+'body bgcolor="#FFFFFF">'+
994: '<'+'image name="whirly" align="right" src="/adm/lonIcons/'+
995: 'lonanim.gif" '+
996: 'alt="animated logo" />'+
997: '<'+'h3>Search Results Progress<'+'/h3>'+
998: '<'+'form name="popremain">'+
999: '<'+'tt>'+
1000: '<'+'br clear="all"/><i>PLEASE BE PATIENT</i>'+
1001: '<'+'br />SCANNING $servernum SERVERS'+
1002: '<'+'br clear="all" />Number of record hits found '+
1003: '<'+'input type="text" size="10" name="numhits"'+
1004: ' value="0" />'+
1005: '<'+'br clear="all" />Time elapsed '+
1006: '<'+'input type="text" size="10" name="elapsetime"'+
1007: ' value="0" />'+
1008: '<'+'br />'+
1009: 'SERVER GRID (click on any cell for details)'+
1010: $grid
1011: '<'+'br />'+
1012: 'Server details '+
1013: '<'+'input type="text" size="25" name="sdetails"'+
1014: ' value="" />'+
1015: '<'+'br />'+
1016: ' <'+'input type="button" name="button"'+
1017: ' value="abort search and view current results" '+
1018: ' onClick="javascript:opener.abortsearch()" />'+
1019: ' <'+'input type="button" name="button"'+
1020: ' value="help" onClick="javascript:opener.openhelp()" />'+
1021: '<'+'/tt>'+
1022: '<'+'/form>'+
1023: '<'+'/body><'+'/html>');
1024: popwin.document.close();
1025: </script>
1026: ENDPOP
1027: $r->rflush();
1028:
1029: my $servercount=0;
1030: my $hitcountsum=0;
1031: my $bloop=$servernum;
1032: my %orkey;
1033: BLOOP: while(1) {
1034: my $sn=0;
1035: last BLOOP unless $bloop;
1036: RLOOP: foreach my $rkey (sort keys %rhash) {
1037: $sn++;
1038: next RLOOP if $orkey{$rkey};
1039: $servercount++;
1040: $tflag=1;
1041: $compiledresult='';
1042: my $hostname=$rkey;
1043: my $reply=$rhash{$rkey};
1044: my @results;
1045:
1046: my $replyfile='';
1047:
1048: if ($reply eq 'con_lost') {
1049: $r->print('<script type="text/javascript">popwin.document.img'.
1050: $sn.'.'.
1051: 'src="/adm/lonIcons/srvbad.gif";</script>'.
1052: "\n");
1053: $r->rflush();
1054: $bloop--;
1055: $orkey{$rkey}=1;
1056: }
1057: else {
1058: $reply=~/^([\.\w]+)$/; # must do since 'use strict' checks for tainting
1059: $replyfile=$r->dir_config('lonDaemons').'/tmp/'.$1;
1060: $reply=~/(.*?)\_/;
1061: {
1062: my $temp=0;
1063: WLOOP: while (1) {
1064: if (-e $replyfile && $tflag) {
1065: $r->print('<script type="text/javascript">'.
1066: 'popwin.document.img'.$sn.'.'.
1067: 'src="/adm/lonIcons/srvhalf.gif";</script>'.
1068: "\n");
1069: $r->rflush();
1070: $r->print('<script type="text/javascript">'.
1071: 'popwin.hc["'.$rkey.'"]='.
1072: '"still transferring..."'.';</script>'.
1073: "\n");
1074: $r->rflush();
1075: $tflag=0;
1076: }
1077: if ($temp>1) {
1078: sleep 1;
1079: $timeremain--;
1080: $elapsetime++;
1081: last WLOOP;
1082: }
1083: if (-e "$replyfile.end") {
1084: $bloop--;
1085: $orkey{$rkey}=1;
1086: if (-s $replyfile) {
1087: $r->print('<script type="text/javascript">'.
1088: 'popwin.document.img'.$sn.'.'.
1089: 'src="/adm/lonIcons/srvgood.gif";'.
1090: '</script>'."\n");
1091: $r->rflush();
1092: my $fh=Apache::File->new($replyfile) or
1093: ($r->print('ERROR: file '.
1094: $replyfile.' cannot be opened') and
1095: return OK);
1096: @results=<$fh> if $fh;
1097: $hitcount{$rkey}=@results+0;
1098: $r->print('<script type="text/javascript">'.
1099: 'popwin.hc["'.$rkey.'"]='.
1100: $hitcount{$rkey}.';</script>'.
1101: "\n");
1102: $r->rflush();
1103: $hitcountsum+=$hitcount{$rkey};
1104: $r->print('<script type="text/javascript">'.
1105: 'popwin.document.forms.popremain.'.
1106: 'numhits.value='.$hitcountsum.
1107: ';</script>'.
1108: "\n");
1109: $r->rflush();
1110: }
1111: else {
1112: $r->print('<script type="text/javascript">'.
1113: 'popwin.document.img'.$sn.'.'.
1114: 'src="/adm/lonIcons/srvempty.gif";'.
1115: '</script>'.
1116: "\n");
1117: $r->rflush();
1118: $r->print('<script type="text/javascript">'.
1119: 'popwin.hc["'.$rkey.'"]=0'.
1120: ';</script>'.
1121: "\n");
1122: $r->rflush();
1123: }
1124: last WLOOP;
1125: }
1126: last WLOOP unless $timeremain;
1127: sleep 1;
1128: $timeremain--;
1129: $elapsetime++;
1130: $r->print('<script type="text/javascript">'.
1131: 'popwin.document.popremain.elapsetime.'.
1132: 'value="'.$elapsetime.'";</script>'."\n");
1133: $r->rflush();
1134: $temp++;
1135: }
1136: }
1137: $r->print('<script type="text/javascript">'.
1138: 'popwin.document.whirly.'.
1139: 'src="'.'/adm/lonIcons/lonanimend.gif'.
1140: '";</script>'."\n");
1141: $r->rflush();
1142: }
1143: my $customshow='';
1144: my $extrashow='';
1145: my @customfields;
1146: if ($ENV{'form.customshow'}) {
1147: $customshow=$ENV{'form.customshow'};
1148: $customshow=~s/[^\w\s]//g;
1149: my @fields=map {"<font color=\"#008000\">$_:</font><!-- $_ -->"}
1150: split(/\s+/,$customshow);
1151: @customfields=split(/\s+/,$customshow);
1152: if ($customshow) {
1153: $extrashow="<ul><li>".join("</li><li>",@fields)."</li></ul>\n";
1154: }
1155: }
1156: my $customdata='';
1157: my %customhash;
1158: foreach my $result (@results) {
1159: if ($result=~/^(custom\=.*)$/) { # grab all custom metadata
1160: my $tmp=$result;
1161: $tmp=~s/^custom\=//;
1162: my ($k,$v)=map {&Apache::lonnet::unescape($_);
1163: } split(/\,/,$tmp);
1164: $customhash{$k}=$v;
1165: }
1166: }
1167: if (keys %hash) {
1168: untie %hash;
1169: }
1170: if (tie(%hash,'GDBM_File',$diropendb,&GDBM_WRCREAT,0640)) {
1171: if ($ENV{'form.launch'} eq '1') {
1172: &start_fresh_session();
1173: }
1174: foreach my $result (@results) {
1175: next if $result=~/^custom\=/;
1176: chomp $result;
1177: next unless $result;
1178: my @fields=map
1179: {&Apache::lonnet::unescape($_)}
1180: (split(/\,/,$result));
1181: my ($title,$author,$subject,$url,$keywords,$version,
1182: $notes,$abstract,$mime,$lang,
1183: $creationdate,$lastrevisiondate,$owner,$copyright)=@fields;
1184:
1185: unless ($ENV{'user.adv'}) {
1186: $keywords='<i>- not displayed -</i>';
1187: $fields[4]=$keywords;
1188: $notes='<i>- not displayed -</i>';
1189: $fields[6]=$notes;
1190: $abstract='<i>- not displayed -</i>';
1191: $fields[7]=$abstract;
1192: $subject='<i>- not displayed -</i>';
1193: $fields[2]=$subject;
1194: }
1195:
1196: my $shortabstract=$abstract;
1197: $shortabstract=substr($abstract,0,200).'...' if length($abstract)>200;
1198: $fields[7]=$shortabstract;
1199: my $shortkeywords=$keywords;
1200: $shortkeywords=substr($keywords,0,200).'...' if length($keywords)>200;
1201: $fields[4]=$shortkeywords;
1202:
1203: my $extrashow2=$extrashow;
1204: if ($extrashow) {
1205: foreach my $field (@customfields) {
1206: my $value='';
1207: if ($customhash{$url}=~/\<${field}[^\>]*\>(.*?)\<\/${field}[^\>]*\>/s) {
1208: $value=$1;
1209: }
1210: $extrashow2=~s/\<\!\-\- $field \-\-\>/ $value/g;
1211: }
1212: }
1213:
1214: $compiledresult.=<<END if $compiledresult or $servercount!=$servernum;
1215: <hr align='left' width='200' noshade />
1216: END
1217: $compiledresult.=<<END;
1218: <p>
1219: END
1220: $compiledresult.=<<END if $ENV{'form.catalogmode'} eq 'interactive';
1221: <font size='-1'><INPUT TYPE="button" NAME="returnvalues" VALUE="SELECT"
1222: onClick="javascript:select_data('$title','$url')">
1223: </font>
1224: <br />
1225: END
1226: if ($ENV{'form.catalogmode'} eq 'groupsearch') {
1227: $fnum+=0;
1228: $hash{"pre_${fnum}_link"}=$url;
1229: $hash{"pre_${fnum}_title"}=$title;
1230: $compiledresult.=<<END;
1231: <font size='-1'><input type="checkbox" name="returnvalues" value="SELECT"
1232: onClick="javascript:queue($fnum)" />
1233: </font>
1234: <br />
1235: END
1236: # <input type="hidden" name="title$fnum" value="$title" />
1237: # <input type="hidden" name="url$fnum" value="$url" />
1238: $fnum++;
1239: }
1240: my $httphost=$ENV{'HTTP_HOST'};
1241:
1242: my $viewselect;
1243: if ($mode eq 'Basic') {
1244: $viewselect=$ENV{'form.basicviewselect'};
1245: }
1246: elsif ($mode eq 'Advanced') {
1247: $viewselect=$ENV{'form.advancedviewselect'};
1248: }
1249:
1250: if ($viewselect eq 'Detailed Citation View') {
1251: $compiledresult.=&detailed_citation_view(@fields,
1252: $hostname,$httphost,
1253: $extrashow2);
1254: }
1255: elsif ($viewselect eq 'Summary View') {
1256: $compiledresult.=&summary_view(@fields,$hostname,$httphost,
1257: $extrashow2);
1258: }
1259: elsif ($viewselect eq 'Fielded Format') {
1260: $compiledresult.=&fielded_format_view(@fields,$hostname,
1261: $httphost,$extrashow2);
1262: }
1263: elsif ($viewselect eq 'XML/SGML') {
1264: $compiledresult.=&xml_sgml_view(@fields,$hostname,$httphost,
1265: $extrashow2);
1266: }
1267:
1268: }
1269:
1270: untie %hash;
1271: }
1272: else {
1273: $r->print('<html><head></head><body>Unable to tie hash to db '.
1274: 'file</body></html>');
1275: }
1276: if ($compiledresult) {
1277: $resultflag=1;
1278: }
1279:
1280: $r->print(<<RESULTS);
1281: $compiledresult
1282: RESULTS
1283: my $percent=sprintf('%3.0f',($servercount/$servernum*100));
1284: }
1285: }
1286: unless ($resultflag) {
1287: $r->print("\nThere were no results that matched your query\n");
1288: }
1289: # $r->print('<script type="text/javascript">'.'popwin.close()</script>'."\n"); $r->rflush();
1290: $r->print(<<RESULTS);
1291: </body>
1292: </html>
1293: RESULTS
1294: }
1295:
1296: # ------------------------------------------------------ Detailed Citation View
1297: sub detailed_citation_view {
1298: my ($title,$author,$subject,$url,$keywords,$version,
1299: $notes,$shortabstract,$mime,$lang,
1300: $creationdate,$lastrevisiondate,$owner,$copyright,
1301: $hostname,$httphost,$extrashow)=@_;
1302: my $result=<<END;
1303: <i>$owner</i>, last revised $lastrevisiondate
1304: <h3><A HREF="http://$httphost$url" TARGET='search_preview'>$title</A></h3>
1305: <h3>$author</h3>
1306: </p>
1307: <p>
1308: <b>Subject:</b> $subject<br />
1309: <b>Keyword(s):</b> $keywords<br />
1310: <b>Notes:</b> $notes<br />
1311: <b>MIME Type:</b> $mimetag{$mime}<br />
1312: <b>Language:</b> $language{$lang}<br />
1313: <b>Copyright/Distribution:</b> $cprtag{$copyright}<br />
1314: </p>
1315: $extrashow
1316: <p>
1317: $shortabstract
1318: </p>
1319: END
1320: return $result;
1321: }
1322:
1323: # ---------------------------------------------------------------- Summary View
1324: sub summary_view {
1325: my ($title,$author,$subject,$url,$keywords,$version,
1326: $notes,$shortabstract,$mime,$lang,
1327: $creationdate,$lastrevisiondate,$owner,$copyright,
1328: $hostname,$httphost,$extrashow)=@_;
1329: my $result=<<END;
1330: <a href="http://$httphost$url" TARGET='search_preview'>$author</a><br />
1331: $title<br />
1332: $owner -- $lastrevisiondate<br />
1333: $cprtag{$copyright}<br />
1334: $extrashow
1335: </p>
1336: END
1337: return $result;
1338: }
1339:
1340: # -------------------------------------------------------------- Fielded Format
1341: sub fielded_format_view {
1342: my ($title,$author,$subject,$url,$keywords,$version,
1343: $notes,$shortabstract,$mime,$lang,
1344: $creationdate,$lastrevisiondate,$owner,$copyright,
1345: $hostname,$httphost,$extrashow)=@_;
1346: my $result=<<END;
1347: <b>URL: </b> <A HREF="http://$httphost$url" TARGET='search_preview'>$url</A>
1348: <br />
1349: <b>Title:</b> $title<br />
1350: <b>Author(s):</b> $author<br />
1351: <b>Subject:</b> $subject<br />
1352: <b>Keyword(s):</b> $keywords<br />
1353: <b>Notes:</b> $notes<br />
1354: <b>MIME Type:</b> $mimetag{$mime}<br />
1355: <b>Language:</b> $language{$lang}<br />
1356: <b>Creation Date:</b> $creationdate<br />
1357: <b>Last Revision Date:</b> $lastrevisiondate<br />
1358: <b>Publisher/Owner:</b> $owner<br />
1359: <b>Copyright/Distribution:</b> $cprtag{$copyright}<br />
1360: <b>Repository Location:</b> $hostname<br />
1361: <b>Abstract:</b> $shortabstract<br />
1362: $extrashow
1363: </p>
1364: END
1365: return $result;
1366: }
1367:
1368: # -------------------------------------------------------------------- XML/SGML
1369: sub xml_sgml_view {
1370: my ($title,$author,$subject,$url,$keywords,$version,
1371: $notes,$shortabstract,$mime,$lang,
1372: $creationdate,$lastrevisiondate,$owner,$copyright,
1373: $hostname,$httphost,$extrashow)=@_;
1374: my $result=<<END;
1375: <pre>
1376: <LonCapaResource>
1377: <url>$url</url>
1378: <title>$title</title>
1379: <author>$author</author>
1380: <subject>$subject</subject>
1381: <keywords>$keywords</keywords>
1382: <notes>$notes</notes>
1383: <mimeInfo>
1384: <mime>$mime</mime>
1385: <mimetag>$mimetag{$mime}</mimetag>
1386: </mimeInfo>
1387: <languageInfo>
1388: <language>$lang</language>
1389: <languagetag>$language{$lang}</languagetag>
1390: </languageInfo>
1391: <creationdate>$creationdate</creationdate>
1392: <lastrevisiondate>$lastrevisiondate</lastrevisiondate>
1393: <owner>$owner</owner>
1394: <copyrightInfo>
1395: <copyright>$copyright</copyright>
1396: <copyrighttag>$cprtag{$copyright}</copyrighttag>
1397: </copyrightInfo>
1398: <repositoryLocation>$hostname</repositoryLocation>
1399: <shortabstract>$shortabstract</shortabstract>
1400: </LonCapaResource>
1401: </pre>
1402: $extrashow
1403: END
1404: return $result;
1405: }
1406:
1407: # ---------------------------------------------------- see if a field is filled
1408: sub filled {
1409: my ($field)=@_;
1410: if ($field=~/\S/ && $field ne 'any') {
1411: return 1;
1412: }
1413: else {
1414: return 0;
1415: }
1416: }
1417:
1418: # ---------------- Message to output when there are not enough fields filled in
1419: sub output_blank_field_error {
1420: my ($r)=@_;
1421: # make query information persistent to allow for subsequent revision
1422: my $persistent=&make_persistent();
1423:
1424: $r->print(<<BEGINNING);
1425: <html>
1426: <head>
1427: <title>The LearningOnline Network with CAPA</title>
1428: BEGINNING
1429: $r->print(<<RESULTS);
1430: </head>
1431: <body bgcolor="#ffffff">
1432: <img align='right' src='/adm/lonIcons/lonlogos.gif' />
1433: <h1>Search Catalog</h1>
1434: <form method="post" action="/adm/searchcat">
1435: $persistent
1436: <input type='button' value='Revise search request'
1437: onClick='this.form.submit();' />
1438: $closebutton
1439: <hr />
1440: <h3>Helpful Message</h3>
1441: <p>
1442: Incorrect search query due to blank entry fields.
1443: You need to fill in the relevant
1444: fields on the search page in order for a query to be
1445: processed.
1446: </p>
1447: </body>
1448: </html>
1449: RESULTS
1450: }
1451:
1452: # ----------------------------------------------------------- Output date error
1453: sub output_date_error {
1454: my ($r,$message)=@_;
1455: # make query information persistent to allow for subsequent revision
1456: my $persistent=&make_persistent();
1457:
1458: $r->print(<<BEGINNING);
1459: <html>
1460: <head>
1461: <title>The LearningOnline Network with CAPA</title>
1462: BEGINNING
1463: $r->print(<<RESULTS);
1464: </head>
1465: <body bgcolor="#ffffff">
1466: <img align='right' src='/adm/lonIcons/lonlogos.gif' />
1467: <h1>Search Catalog</h1>
1468: <form method="post" action="/adm/searchcat">
1469: $persistent
1470: <input type='button' value='Revise search request'
1471: onClick='this.form.submit();' />
1472: $closebutton
1473: <hr />
1474: <h3>Helpful Message</h3>
1475: <p>
1476: $message
1477: </p>
1478: </body>
1479: </html>
1480: RESULTS
1481: }
1482:
1483: # --------- settings whenever the user causes the search window to be launched
1484: sub start_fresh_session {
1485: delete $hash{'mode_catalog'};
1486: map {
1487: if ($_ =~ /^pre_/) {
1488: delete $hash{$_};
1489: }
1490: if ($_ =~ /^store/) {
1491: delete $hash{$_};
1492: }
1493: } keys %hash;
1494: }
1495:
1496: 1;
1497:
1498: __END__
1499:
1500: =head1 NAME
1501:
1502: Apache::lonsearchcat - mod_perl module for handling a searchable catalog
1503:
1504: =head1 SYNOPSIS
1505:
1506: Invoked by /etc/httpd/conf/srm.conf:
1507:
1508: <Location /adm/searchcat>
1509: PerlAccessHandler Apache::lonacc
1510: SetHandler perl-script
1511: PerlHandler Apache::lonsearchcat
1512: ErrorDocument 403 /adm/login
1513: ErrorDocument 500 /adm/errorhandler
1514: </Location>
1515:
1516: =head1 INTRODUCTION
1517:
1518: This module enables searching for a distributed browseable catalog.
1519:
1520: This is part of the LearningOnline Network with CAPA project
1521: described at http://www.lon-capa.org.
1522:
1523: =head1 BEGIN SUBROUTINE
1524:
1525: This routine is only run once after compilation.
1526:
1527: =over 4
1528:
1529: =item *
1530:
1531: Initializes %language hash table.
1532:
1533: =item *
1534:
1535: Initializes %cprtag hash table (for copyright.tab).
1536:
1537: =item *
1538:
1539: Initializes %mimetag hash table (for filetypes.tab).
1540:
1541: =item *
1542:
1543: Initializes %hostdomains and hostips hash table (for hosts.tab).
1544:
1545: =back
1546:
1547: =head1 HANDLER SUBROUTINE
1548:
1549: This routine is called by Apache and mod_perl.
1550:
1551: =over 4
1552:
1553: =item *
1554:
1555: configure dynamic components of interface
1556:
1557: =item *
1558:
1559: determine current user
1560:
1561: =item *
1562:
1563: see if a search invocation should be done
1564:
1565: =item *
1566:
1567: else, begin building search interface to output
1568:
1569: =item *
1570:
1571: compute date selection boxes
1572:
1573: =item *
1574:
1575: compute customized metadata field
1576:
1577: =item *
1578:
1579: print screen
1580:
1581: =back
1582:
1583: =head1 OTHER SUBROUTINES
1584:
1585: =over 4
1586:
1587: =item *
1588:
1589: get_unprocessed_cgi() : reads in critical name/value pairs that may have not
1590: been processed and passed into %ENV by the web server
1591:
1592: =item *
1593:
1594: make_persistent() : makes a set of hidden HTML fields to make
1595: SQL search interface information to be persistent
1596:
1597: =back
1598:
1599: WEB INTERFACE COMPONENT FUNCTIONS
1600:
1601: =over 4
1602:
1603: =item *
1604:
1605: simpletextfield(name,value) : returns HTML formatted string for simple text
1606: field
1607:
1608: =item *
1609:
1610: simplecheckbox(name,value) : returns HTML formatted string for simple
1611: checkbox
1612:
1613: =item *
1614:
1615: searchphrasefield(title,name,value) : returns HTML formatted string for
1616: a search expression phrase field
1617:
1618: =item *
1619:
1620: dateboxes(name, defaultmonth, defaultday, defaultyear) : returns HTML
1621: formatted string for a calendar date
1622:
1623: =item *
1624:
1625: selectbox(title,name,value,%HASH=options) : returns HTML formatted string for
1626: a selection box field
1627:
1628: =back
1629:
1630: SEARCH FUNCTIONS
1631:
1632: =over 4
1633:
1634: =item *
1635:
1636: advancedsearch(server reference, environment reference) : perform a complex
1637: multi-field logical query
1638:
1639: =item *
1640:
1641: basicsearch(server reference, environment reference) : perform a simple
1642: single-field logical query
1643:
1644: =item *
1645:
1646: build_SQL_query(field name, logic) : builds a SQL query string from a
1647: logical expression with AND/OR keywords
1648:
1649: =item *
1650:
1651: build_custommetadata_query(field_name, logic_statement) : builds a perl
1652: regular expression from a logical expression with AND/OR keywords
1653:
1654: =item *
1655:
1656: recursive_SQL_query_build(field name, reverse notation expression) :
1657: builds a SQL query string from a reverse notation expression
1658: logical expression with AND/OR keywords
1659:
1660: =item *
1661:
1662: build_date_queries(cmonth1, cday1, cyear1, cmonth2, cday2, cyear2,
1663: lmonth1, lday1, lyear1, lmonth2, lday2, lyear2) :
1664: Builds a SQL logic query to check time/date entries.
1665:
1666: =back
1667:
1668: OUTPUTTING RESULTS FUNCTION
1669:
1670: =over 4
1671:
1672: =item *
1673:
1674: output_results(output mode, server reference, environment reference,
1675: reply list reference) : outputs results from search
1676:
1677: =back
1678:
1679: DIFFERENT WAYS TO VIEW METADATA RECORDS
1680:
1681: =over 4
1682:
1683: =item *
1684:
1685: detailed_citation_view(ORDERED METADATA LIST FOR A RESULT OBJECT INSTANCE) :
1686: see metadata viewing notes below
1687:
1688: =item *
1689:
1690: summary_view(ORDERED METADATA LIST FOR A RESULT OBJECT INSTANCE) :
1691: see metadata viewing notes below
1692:
1693: =item *
1694:
1695: fielded_format_view(ORDERED METADATA LIST FOR A RESULT OBJECT INSTANCE) :
1696: see metadata viewing notes below
1697:
1698: =item *
1699:
1700: xml_sgml_view(ORDERED METADATA LIST FOR A RESULT OBJECT INSTANCE) :
1701: see metadata viewing notes below
1702:
1703: =back
1704:
1705: _____________________________________________________________________
1706: | * Metadata viewing notes |
1707: | Output is a HTML-ified string. |
1708: | Input arguments are title, author, subject, url, keywords, version, |
1709: | notes, short abstract, mime, language, creation date, |
1710: | last revision date, owner, copyright, hostname, httphost, and |
1711: | extra custom metadata to show. |
1712: ---------------------------------------------------------------------
1713:
1714: TEST CONDITIONAL FUNCTIONS
1715:
1716: =over 4
1717:
1718: =item *
1719:
1720: filled(field) : determines whether a given field has been filled
1721:
1722: =back
1723:
1724: ERROR FUNCTIONS
1725:
1726: =over 4
1727:
1728: =item *
1729:
1730: output_blank_field_error(server reference) : outputs a message saying that
1731: more fields need to be filled in
1732:
1733: =item *
1734:
1735: output_date_error(server reference, error message) : outputs
1736: an error message specific to bad date format.
1737:
1738: =back
1739:
1740: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>