1: # The LearningOnline Network
2: # Search Catalog
3: #
4: # 03/08/2001 Scott Harrison
5: # Scott Harrison: 03/12/2001, 03/13/2001, 03/14/2001, 03/15/2001, 03/19/2001
6: # Scott Harrison: 03/20/2001, 03/21/2001, 03/22/2001, 03/26/2001, 03/27/2001
7: # Scott Harrison: 04/02/2001
8: #
9: # Functions
10: #
11: # handler(server reference) : interacts with the Apache server layer
12: # (for /adm/searchcat URLs)
13: # simpletextfield(name,value) : returns HTML formatted string for simple text
14: # field
15: # simplecheckbox(name,value) : returns HTML formatted string for simple
16: # checkbox
17: # searchphrasefield(title,name,value) : returns HTML formatted string for
18: # a search expression phrase field
19: # dateboxes(name, defaultmonth, defaultday, defaultyear) : returns HTML
20: # formatted string
21: # for a calendar date
22: # selectbox(title,name,value,%HASH=options) : returns HTML formatted string for
23: # a selection box field
24: # advancedsearch(server reference, environment reference) : perform a complex
25: # multi-field logical query
26: # filled(field) : determines whether a given field has been filled
27: # basicsearch(server reference, environment reference) : perform a simple
28: # single-field logical query
29: # output_blank_field_error(server reference) : outputs a message saying that
30: # more fields need to be filled in
31: # output_results(output mode,
32: # server reference,
33: # environment reference,
34: # reply list reference) : outputs results from search
35: # build_SQL_query(field name, logic) : builds a SQL query string from a
36: # logical expression with AND/OR keywords
37: # recursive_SQL_query_build(field name, reverse notation expression) :
38: # builds a SQL query string from a reverse notation expression
39: # logical expression with AND/OR keywords
40: # build_custommetadata_query(field_name, logic_statement) : builds a perl
41: # regular expression from a logical expression with AND/OR
42: # keywords
43: # detailed_citation_view, summary_view, fielded_format_view, xml_sgml_view:
44: # four different ways to view metadata records. Outputs a HTML-ified string.
45: # Input arguments are title, author, subject, url, keywords, version, notes,
46: # short abstract, mime, language, creation date, last revision date, owner,
47: # copyright, hostname, httphost, and extra custom metadata to show.
48: # build_date_queries(cmonth1, cday1, cyear1, cmonth2, cday2, cyear2,
49: # lmonth1, lday1, lyear1, lmonth2, lday2, lyear2) :
50: # Builds a SQL logic query to check time/date entries.
51: # output_date_error(server reference, error message) : outputs
52: # an error message specific to bad date format.
53: # make_persistent() : makes a set of hidden HTML fields to make
54: # SQL search interface information to be persistent
55:
56: package Apache::lonsearchcat;
57:
58: use strict;
59: use Apache::Constants qw(:common);
60: use Apache::lonnet();
61: use Apache::File();
62: use CGI qw(:standard);
63: use Text::Query;
64:
65: # ---------------------------------------- variables used throughout the module
66: my %language;
67: my $scrout;
68: my %metadatafields;
69: my %cprtag;
70: my %mimetag;
71: my $closebutton;
72:
73: # ------ form selection elements that allow for choosing different output views
74: # Detailed Citation View ---> sub detailed_citationview
75: # Summary View ---> sub summary_view
76: # Fielded Format ---> sub fielded_format_view
77: # XML/SGML ---> sub xml_sgml_view
78: my $basicviewselect=<<END;
79: <select name='basicviewselect'>
80: <option value='Detailed Citation View'>Detailed Citation View</option>
81: <option value='Summary View'>Summary View</option>
82: <option value='Fielded Format'>Fielded Format</option>
83: <option value='XML/SGML'>XML/SGML</option>
84: </select>
85: END
86: my $advancedviewselect=<<END;
87: <select name='advancedviewselect'>
88: <option value='Detailed Citation View'>Detailed Citation View</option>
89: <option value='Summary View'>Summary View</option>
90: <option value='Fielded Format'>Fielded Format</option>
91: <option value='XML/SGML'>XML/SGML</option>
92: </select>
93: END
94:
95: # ----------------------------- Handling routine called via Apache and mod_perl
96: sub handler {
97: my $r = shift;
98:
99: # -------------------------------------- see if called from an interactive mode
100: map {
101: my ($name, $value) = split(/=/,$_);
102: $value =~ tr/+/ /;
103: $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
104: if ($name eq 'catalogmode') {
105: $ENV{'form.'.$name}=$value;
106: }
107: } (split(/&/,$ENV{'QUERY_STRING'}));
108:
109: $r->content_type('text/html');
110: $r->send_http_header;
111: return OK if $r->header_only;
112:
113: %metadatafields=();
114:
115: my $hidden='';
116: if ($ENV{'form.catalogmode'} eq 'interactive') {
117: $hidden=<<END;
118: <input type='hidden' name='catalogmode' value='interactive'>
119: END
120: $closebutton=<<END;
121: <input type="button" name="close" value='CLOSE' onClick="self.close()">
122: END
123: }
124:
125: # ------------------------------------------------ First, check out environment
126: $metadatafields{'owner'}=$ENV{'user.name'}.'@'.$ENV{'user.domain'};
127:
128: # --------------------------------- Compute various listings of metadata values
129:
130: %language=();
131: $language{'any'}='Any language';
132: {
133: my $fh=Apache::File->new($r->dir_config('lonTabDir').'/language.tab');
134: map {
135: $_=~/(\w+)\s+([\w\s\-]+)/; chomp;
136: $language{$1}=$2;
137: } <$fh>;
138: }
139:
140: %cprtag=();
141: $cprtag{'any'}='Any copyright/distribution';
142: {
143: my $fh=Apache::File->new($r->dir_config('lonIncludes').'/copyright.tab');
144: map {
145: $_=~/(\w+)\s+([\w\s\-]+)/; chomp;
146: $cprtag{$1}=$2;
147: } <$fh>;
148: }
149:
150: %mimetag=();
151: $mimetag{'any'}='Any type';
152: {
153: my $fh=Apache::File->new($r->dir_config('lonTabDir').'/filetypes.tab');
154: map {
155: $_=~/(\w+)\s+(\w+)\s+([\w\s\-]+)/; chomp;
156: $mimetag{$1}=".$1 $3";
157: } <$fh>;
158: }
159:
160: # ----------------------------------- See if a search invocation should be done
161: if ($ENV{'form.basicsubmit'} eq 'SEARCH') {
162: return &basicsearch($r,\%ENV);
163: }
164: elsif ($ENV{'form.advancedsubmit'} eq 'SEARCH') {
165: return &advancedsearch($r,\%ENV);
166: }
167:
168: # ----------------------------- Else, begin building search interface to output
169: $scrout=''; # building a part of screen output
170: $scrout.=&searchphrasefield('Limit by title','title',
171: $ENV{'form.title'});
172:
173: $scrout.=&searchphrasefield('Limit by author','author',
174: $ENV{'form.author'});
175:
176: $scrout.=&searchphrasefield('Limit by subject','subject',
177: $ENV{'form.subject'});
178:
179: $scrout.=&searchphrasefield('Limit by keywords','keywords',
180: $ENV{'form.keywords'});
181:
182: $scrout.=&searchphrasefield('Limit by URL','url',
183: $ENV{'form.url'});
184:
185: $scrout.=&searchphrasefield('Limit by version','version',
186: $ENV{'form.version'});
187:
188: $scrout.=&searchphrasefield('Limit by notes','notes',
189: $ENV{'form.notes'});
190:
191: $scrout.=&searchphrasefield('Limit by abstract','abstract',
192: $ENV{'form.abstract'});
193:
194: $ENV{'form.mime'}='notxxx' unless length($ENV{'form.mime'});
195: $scrout.=&selectbox('Limit by MIME type','mime',
196: $ENV{'form.mime'},%mimetag);
197:
198: $ENV{'form.language'}='any' unless length($ENV{'form.language'});
199:
200: $scrout.=&selectbox('Limit by language','language',
201: $ENV{'form.language'},%language);
202:
203:
204: # ------------------------------------------------ Compute date selection boxes
205: $scrout.=<<CREATIONDATESTART;
206: <p>
207: <font color="#800000" face="helvetica"><b>LIMIT BY CREATION DATE RANGE:</b>
208: </font>
209: <br>
210: between:
211: CREATIONDATESTART
212: $scrout.=&dateboxes('creationdatestart',1,1,1976,
213: $ENV{'form.creationdatestart_month'},
214: $ENV{'form.creationdatestart_day'},
215: $ENV{'form.creationdatestart_year'},
216: );
217: $scrout.=<<CREATIONDATEEND;
218: and:
219: CREATIONDATEEND
220: $scrout.=&dateboxes('creationdateend',12,31,2051,
221: $ENV{'form.creationdateend_month'},
222: $ENV{'form.creationdateend_day'},
223: $ENV{'form.creationdateend_year'},
224: );
225: $scrout.="</p>";
226:
227: $scrout.=<<LASTREVISIONDATESTART;
228: <p>
229: <font color="#800000" face="helvetica"><b>LIMIT BY LAST REVISION DATE RANGE:
230: </b></font>
231: <br>between:
232: LASTREVISIONDATESTART
233: $scrout.=&dateboxes('lastrevisiondatestart',1,1,1976,
234: $ENV{'form.lastrevisiondatestart_month'},
235: $ENV{'form.lastrevisiondatestart_day'},
236: $ENV{'form.lastrevisiondatestart_year'},
237: );
238: $scrout.=<<LASTREVISIONDATEEND;
239: and:
240: LASTREVISIONDATEEND
241: $scrout.=&dateboxes('lastrevisiondateend',12,31,2051,
242: $ENV{'form.lastrevisiondateend_month'},
243: $ENV{'form.lastrevisiondateend_day'},
244: $ENV{'form.lastrevisiondateend_year'},
245: );
246: $scrout.='</p>';
247:
248: $scrout.=&searchphrasefield('Limit by publisher/owner','owner',
249: $ENV{'form.owner'});
250: # $metadatafields{'owner'});
251:
252: $ENV{'form.copyright'}='any' unless length($ENV{'form.copyright'});
253: $scrout.=&selectbox('Limit by copyright/distribution','copyright',
254: $ENV{'form.copyright'},%cprtag);
255:
256: # ------------------------------------------- Compute customized metadata field
257: $scrout.=<<CUSTOMMETADATA;
258: <p>
259: <font color="#800000" face="helvetica"><b>LIMIT BY SPECIAL METADATA FIELDS:</b>
260: </font>
261: For resource-specific metadata, enter in an expression in the form of
262: <i>key</i>=<i>value</i> separated by operators such as AND or OR.<br>
263: <b>Example:</b> grandmother=75 OR grandfather=85
264: <br>
265: CUSTOMMETADATA
266: $scrout.=&simpletextfield('custommetadata',$ENV{'form.custommetadata'});
267: $scrout.=' <i>initial users of this system do not need to worry about this option</i>';
268:
269: $scrout.=<<CUSTOMSHOW;
270: <p>
271: <font color="#800000" face="helvetica"><b>SHOW SPECIAL METADATA FIELDS:</b>
272: </font>
273: Enter in a space-separated list of special metadata fields to show
274: in a fielded listing for each record result.
275: <br>
276: CUSTOMSHOW
277: $scrout.=&simpletextfield('customshow',$ENV{'form.customshow'});
278: $scrout.=' <i>initial users of this system do not need to worry about this option</i>';
279:
280: # ---------------------------------------------------------------- Print screen
281: $r->print(<<ENDDOCUMENT);
282: <html>
283: <head>
284: <title>The LearningOnline Network with CAPA</title>
285: </head>
286: <body bgcolor="#FFFFFF">
287: <img align=right src=/adm/lonIcons/lonlogos.gif>
288: <h1>Search Catalog</h1>
289: <form method="post" action="/adm/searchcat">
290: $hidden
291: <hr>
292: <h3>Basic Search</h3>
293: <p>
294: Enter terms or phrases separated by search operators
295: such as AND or OR then press SEARCH below. Terms should be specific
296: to the title, author, subject, notes, or abstract information associated
297: with a resource.
298: <br>
299: ENDDOCUMENT
300: $r->print(&simpletextfield('basicexp',$ENV{'form.basicexp'}));
301: $r->print(' ');
302: $r->print(&simplecheckbox('titleonly',$ENV{'form.titleonly'}));
303: $r->print('<font color="#800000">Title only</font> ');
304: $r->print(&simplecheckbox('allversions',$ENV{'form.allversions'}));
305: $r->print(<<ENDDOCUMENT);
306: <font color="#800000">Search historic archives</font>
307: <br>
308: <input type="submit" name="basicsubmit" value='SEARCH' />
309: <input type="reset" name="reset" value='RESET' />
310: $closebutton
311: $basicviewselect
312: </p>
313: <hr>
314: <h3>Advanced Search</h3>
315: $scrout
316: <p>
317: <input type="submit" name="advancedsubmit" value='SEARCH' />
318: <input type="reset" name="reset" value='RESET' />
319: $closebutton
320: $advancedviewselect
321: </p>
322: </form>
323: </body>
324: </html>
325: ENDDOCUMENT
326: return OK;
327: }
328:
329: # --------------------------------------------------------- Various form fields
330:
331: sub simpletextfield {
332: my ($name,$value)=@_;
333: return '<input type=text name=\''.$name.
334: '\' size=20 value=\''.$value.'\' />';
335: }
336:
337: sub simplecheckbox {
338: my ($name,$value)=@_;
339: my $checked='';
340: $checked="CHECKED" if $value eq 'on';
341: return '<input type=checkbox name=\''.$name.'\' '. $checked . '>';
342: }
343:
344: sub searchphrasefield {
345: my ($title,$name,$value)=@_;
346: my $instruction=<<END;
347: Enter terms or phrases separated by search operators such
348: as AND or OR.
349: END
350: my $uctitle=uc($title);
351: return "\n<p><font color=\"#800000\" face=\"helvetica\"><b>$uctitle:</b>".
352: "</FONT> $instruction<br>".
353: '<input type=text name="'.$name.'" size=80 value=\''.$value.'\'>';
354: }
355:
356: sub dateboxes {
357: my ($name,$defaultmonth,$defaultday,$defaultyear,
358: $currentmonth,$currentday,$currentyear)=@_;
359: ($defaultmonth,$defaultday,$defaultyear)=('','','');
360: my $month=<<END;
361: <select name="${name}_month">
362: <option value='$defaultmonth'> </option>
363: <option value="1">January</option>
364: <option value="2">February</option>
365: <option value="3">March</option>
366: <option value="4">April</option>
367: <option value="5">May</option>
368: <option value="6">June</option>
369: <option value="7">July</option>
370: <option value="8">August</option>
371: <option value="9">September</option>
372: <option value="10">October</option>
373: <option value="11">November</option>
374: <option value="12">December</option>
375: </select>
376: END
377: $month=~s/(\"$currentmonth\")/$1 SELECTED/ if length($currentmonth);
378: my $day=<<END;
379: <select name="${name}_day">
380: <option value='$defaultday'> </option>
381: <option value="1">1</option>
382: <option value="2">2</option>
383: <option value="3">3</option>
384: <option value="4">4</option>
385: <option value="5">5</option>
386: <option value="6">6</option>
387: <option value="7">7</option>
388: <option value="8">8</option>
389: <option value="9">9</option>
390: <option value="10">10</option>
391: <option value="11">11</option>
392: <option value="12">12</option>
393: <option value="13">13</option>
394: <option value="14">14</option>
395: <option value="15">15</option>
396: <option value="16">16</option>
397: <option value="17">17</option>
398: <option value="18">18</option>
399: <option value="19">19</option>
400: <option value="20">20</option>
401: <option value="21">21</option>
402: <option value="22">22</option>
403: <option value="23">23</option>
404: <option value="24">24</option>
405: <option value="25">25</option>
406: <option value="26">26</option>
407: <option value="27">27</option>
408: <option value="28">28</option>
409: <option value="29">29</option>
410: <option value="30">30</option>
411: <option value="31">31</option>
412: </select>
413: END
414: $day=~s/(\"$currentday\")/$1 SELECTED/ if length($currentday);
415: my $year=<<END;
416: <select name="${name}_year">
417: <option value='$defaultyear'> </option>
418: <option value="1976">1976</option>
419: <option value="1977">1977</option>
420: <option value="1978">1978</option>
421: <option value="1979">1979</option>
422: <option value="1980">1980</option>
423: <option value="1981">1981</option>
424: <option value="1982">1982</option>
425: <option value="1983">1983</option>
426: <option value="1984">1984</option>
427: <option value="1985">1985</option>
428: <option value="1986">1986</option>
429: <option value="1987">1987</option>
430: <option value="1988">1988</option>
431: <option value="1989">1989</option>
432: <option value="1990">1990</option>
433: <option value="1991">1991</option>
434: <option value="1992">1992</option>
435: <option value="1993">1993</option>
436: <option value="1994">1994</option>
437: <option value="1995">1995</option>
438: <option value="1996">1996</option>
439: <option value="1997">1997</option>
440: <option value="1998">1998</option>
441: <option value="1999">1999</option>
442: <option value="2000">2000</option>
443: <option value="2001">2001</option>
444: <option value="2002">2002</option>
445: <option value="2003">2003</option>
446: <option value="2004">2004</option>
447: <option value="2005">2005</option>
448: <option value="2006">2006</option>
449: <option value="2007">2007</option>
450: <option value="2008">2008</option>
451: <option value="2009">2009</option>
452: <option value="2010">2010</option>
453: <option value="2011">2011</option>
454: <option value="2012">2012</option>
455: <option value="2013">2013</option>
456: <option value="2014">2014</option>
457: <option value="2015">2015</option>
458: <option value="2016">2016</option>
459: <option value="2017">2017</option>
460: <option value="2018">2018</option>
461: <option value="2019">2019</option>
462: <option value="2020">2020</option>
463: <option value="2021">2021</option>
464: <option value="2022">2022</option>
465: <option value="2023">2023</option>
466: <option value="2024">2024</option>
467: <option value="2025">2025</option>
468: <option value="2026">2026</option>
469: <option value="2027">2027</option>
470: <option value="2028">2028</option>
471: <option value="2029">2029</option>
472: <option value="2030">2030</option>
473: <option value="2031">2031</option>
474: <option value="2032">2032</option>
475: <option value="2033">2033</option>
476: <option value="2034">2034</option>
477: <option value="2035">2035</option>
478: <option value="2036">2036</option>
479: <option value="2037">2037</option>
480: <option value="2038">2038</option>
481: <option value="2039">2039</option>
482: <option value="2040">2040</option>
483: <option value="2041">2041</option>
484: <option value="2042">2042</option>
485: <option value="2043">2043</option>
486: <option value="2044">2044</option>
487: <option value="2045">2045</option>
488: <option value="2046">2046</option>
489: <option value="2047">2047</option>
490: <option value="2048">2048</option>
491: <option value="2049">2049</option>
492: <option value="2050">2050</option>
493: <option value="2051">2051</option>
494: </select>
495: END
496: $year=~s/(\"$currentyear\")/$1 SELECTED/ if length($currentyear);
497: return "$month$day$year";
498: }
499:
500: sub selectbox {
501: my ($title,$name,$value,%options)=@_;
502: my $uctitle=uc($title);
503: my $selout="\n<p><font color=\"#800000\" face=\"helvetica\"><b>$uctitle:".
504: "</b></font><br>".'<select name="'.$name.'">';
505: map {
506: $selout.='<option value=\''.$_.'\'';
507: if ($_ eq $value) { $selout.=' selected'; }
508: $selout.='>'.$options{$_}.'</option>';
509: } sort keys %options;
510: return $selout.'</select>';
511: }
512:
513: # ----------------------------------------------- Performing an advanced search
514: sub advancedsearch {
515: my ($r,$envhash)=@_;
516: my %ENV=%{$envhash};
517:
518: my $fillflag=0;
519: # Clean up fields for safety
520: for my $field ('title','author','subject','keywords','url','version',
521: 'creationdatestart_month','creationdatestart_day',
522: 'creationdatestart_year','creationdateend_month',
523: 'creationdateend_day','creationdateend_year',
524: 'lastrevisiondatestart_month','lastrevisiondatestart_day',
525: 'lastrevisiondatestart_year','lastrevisiondateend_month',
526: 'lastrevisiondateend_day','lastrevisiondateend_year',
527: 'notes','abstract','mime','language','owner',
528: 'custommetadata','customshow') {
529: $ENV{"form.$field"}=~s/[^\w\s\(\)\=\-\"\']//g;
530: }
531:
532: # Check to see if enough information was filled in
533: for my $field ('title','author','subject','keywords','url','version',
534: 'notes','abstract','mime','language','owner',
535: 'custommetadata') {
536: if (&filled($ENV{"form.$field"})) {
537: $fillflag++;
538: }
539: }
540: unless ($fillflag) {
541: &output_blank_field_error($r);
542: return OK;
543: }
544:
545:
546: # Turn the form input into a SQL-based query
547: my $query='';
548:
549: my @queries;
550: # Evaluate logical expression AND/OR/NOT phrase fields.
551: foreach my $field ('title','author','subject','notes','abstract','url',
552: 'keywords','version','owner') {
553: if ($ENV{'form.'.$field}) {
554: push @queries,&build_SQL_query($field,$ENV{'form.'.$field});
555: }
556: }
557: # Evaluate option lists
558: if ($ENV{'form.language'} and $ENV{'form.language'} ne 'any') {
559: push @queries,"(language like \"$ENV{'form.language'}\")";
560: }
561: if ($ENV{'form.mime'} and $ENV{'form.mime'} ne 'any') {
562: push @queries,"(mime like \"$ENV{'form.mime'}\")";
563: }
564: if ($ENV{'form.copyright'} and $ENV{'form.copyright'} ne 'any') {
565: push @queries,"(copyright like \"$ENV{'form.copyright'}\")";
566: }
567: # Evaluate date windows
568: my $datequery=&build_date_queries(
569: $ENV{'form.creationdatestart_month'},
570: $ENV{'form.creationdatestart_day'},
571: $ENV{'form.creationdatestart_year'},
572: $ENV{'form.creationdateend_month'},
573: $ENV{'form.creationdateend_day'},
574: $ENV{'form.creationdateend_year'},
575: $ENV{'form.lastrevisiondatestart_month'},
576: $ENV{'form.lastrevisiondatestart_day'},
577: $ENV{'form.lastrevisiondatestart_year'},
578: $ENV{'form.lastrevisiondateend_month'},
579: $ENV{'form.lastrevisiondateend_day'},
580: $ENV{'form.lastrevisiondateend_year'},
581: );
582: # Test to see if date windows are legitimate
583: if ($datequery=~/^Incorrect/) {
584: &output_date_error($r,$datequery);
585: return OK;
586: }
587: elsif ($datequery) {
588: push @queries,$datequery;
589: }
590:
591: # Process form information for custom metadata querying
592: my $customquery='';
593: if ($ENV{'form.custommetadata'}) {
594: $customquery=&build_custommetadata_query('custommetadata',
595: $ENV{'form.custommetadata'});
596: }
597: my $customshow='';
598: if ($ENV{'form.customshow'}) {
599: $customshow=$ENV{'form.customshow'};
600: $customshow=~s/[^\w\s]//g;
601: my @fields=split(/\s+/,$customshow);
602: $customshow=join(" ",@fields);
603: }
604: # Send query statements over the network to be processed by either the SQL
605: # database or a recursive scheme of 'grep'-like actions (for custom
606: # metadata).
607: if (@queries) {
608: $query=join(" AND ",@queries);
609: $query="select * from metadata where $query";
610: my $reply; # reply hash reference
611: unless ($customquery or $customshow) {
612: $reply=&Apache::lonnet::metadata_query($query);
613: }
614: else {
615: $reply=&Apache::lonnet::metadata_query($query,
616: $customquery,$customshow);
617: }
618: &output_results('Advanced',$r,$envhash,$customquery,$reply);
619: }
620: elsif ($customquery) {
621: my $reply; # reply hash reference
622: $reply=&Apache::lonnet::metadata_query('',
623: $customquery,$customshow);
624: &output_results('Advanced',$r,$envhash,$customquery,$reply);
625: }
626: $r->print(' '); # just in case.. hrrmm..
627: return OK;
628: }
629:
630: # ---------------------------------------------------- see if a field is filled
631: sub filled {
632: my ($field)=@_;
633: if ($field=~/\S/) {
634: return 1;
635: }
636: else {
637: return 0;
638: }
639: }
640:
641: # --------------------------------------------------- Performing a basic search
642: sub basicsearch {
643: my ($r,$envhash)=@_;
644: my %ENV=%{$envhash};
645:
646: # Clean up fields for safety
647: for my $field ('basicexp') {
648: $ENV{"form.$field"}=~s/[^\w\s\(\)\-]//g;
649: }
650:
651: # Check to see if enough is filled in
652: unless (&filled($ENV{'form.basicexp'})) {
653: &output_blank_field_error($r);
654: return OK;
655: }
656:
657: # Build SQL query string based on form page
658: my $query='';
659: my $concatarg=join('," ",',
660: ('title', 'author', 'subject', 'notes', 'abstract'));
661: $query='select * from metadata where concat(' . $concatarg . ') like %' .
662: $ENV{'form.basicexp'} . '%';
663:
664: # Get reply (either a hash reference to filehandles or bad connection)
665: my $reply=&Apache::lonnet::metadata_query($query);
666:
667: # Output search results
668: &output_results('Basic',$r,$envhash,$query,$reply);
669:
670: return OK;
671: }
672:
673: # ---------------- Message to output when there are not enough fields filled in
674: sub output_blank_field_error {
675: my ($r)=@_;
676: # make query information persistent to allow for subsequent revision
677: my $persistent=&make_persistent();
678:
679: $r->print(<<BEGINNING);
680: <html>
681: <head>
682: <title>The LearningOnline Network with CAPA</title>
683: BEGINNING
684: $r->print(<<RESULTS);
685: </head>
686: <body bgcolor="#ffffff">
687: <img align=right src=/adm/lonIcons/lonlogos.gif>
688: <h1>Search Catalog</h1>
689: <form method="post" action="/adm/searchcat">
690: $persistent
691: <input type='button' value='Revise search request'
692: onClick='this.form.submit();'>
693: $closebutton
694: <hr>
695: <h3>Helpful Message</h3>
696: <p>
697: Incorrect search query due to blank entry fields.
698: You need to fill in the relevant
699: fields on the search page in order for a query to be
700: processed.
701: </p>
702: </body>
703: </html>
704: RESULTS
705: }
706:
707: # ----------------------------- format and output results based on a reply list
708: sub output_results {
709: my ($mode,$r,$envhash,$query,@replylist)=@_;
710: my %ENV=%{$envhash};
711: my $compiledresult='';
712:
713: foreach my $reply (@replylist) {
714:
715: my @results;
716:
717: my $replyfile='';
718: $reply=~/^([\.\w]+)$/; # must do since 'use strict' checks for tainting
719: $replyfile=$r->dir_config('lonDaemons').'/tmp/'.$1;
720: $reply=~/(.*?)\_/;
721: my $hostname=$1;
722: sleep 3; # temporary fix, need to check for completion and status
723: {
724: while (1) {
725: last if -e $replyfile;
726: sleep 1;
727: }
728: # QUESTION: how should I handle this error condition..
729: # I'm sure there is syntax elsewhere I can use..
730: my $fh=Apache::File->new($replyfile) or
731: ($r->print('file cannot be opened') and return OK);
732: @results=<$fh>;
733: }
734:
735: my $customshow='';
736: my $extrashow='';
737: my @customfields;
738: if ($ENV{'form.customshow'}) {
739: $customshow=$ENV{'form.customshow'};
740: $customshow=~s/[^\w\s]//g;
741: my @fields=map {"<font color=\"#008000\">$_:</font><!-- $_ -->"}
742: split(/\s+/,$customshow);
743: @customfields=split(/\s+/,$customshow);
744: if ($customshow) {
745: $extrashow="<ul><li>".join("</li><li>",@fields)."</li></ul>\n";
746: }
747: }
748: my $customdata='';
749: my %customhash;
750: foreach my $result (@results) {
751: if ($result=~/^(custom\=.*)$/) { # grab all custom metadata
752: my $tmp=$result;
753: $tmp=~s/^custom\=//;
754: my ($k,$v)=map {&Apache::lonnet::unescape($_);
755: } split(/\,/,$tmp);
756: $customhash{$k}=$v;
757: }
758: }
759: foreach my $result (@results) {
760: next if $result=~/^custom\=/;
761: chomp $result;
762: next unless $result;
763: my @fields=map
764: {&Apache::lonnet::unescape($_)}
765: (split(/\,/,$result));
766: my ($title,$author,$subject,$url,$keywords,$version,
767: $notes,$abstract,$mime,$lang,
768: $creationdate,$lastrevisiondate,$owner,$copyright)=@fields;
769: my $shortabstract=$abstract;
770: $shortabstract=substr($abstract,0,200) if length($abstract)>200;
771: $fields[7]=$shortabstract;
772: my $extrashow2=$extrashow;
773: if ($extrashow) {
774: foreach my $field (@customfields) {
775: my $value='';
776: if ($customhash{$url}=~/\<${field}[^\>]*\>(.*?)\<\/${field}[^\>]*\>/s) {
777: $value=$1;
778: }
779: $extrashow2=~s/\<\!\-\- $field \-\-\>/ $value/g;
780: }
781: }
782:
783: $compiledresult.=<<END if $compiledresult;
784: <hr align='left' width='200' noshade />
785: END
786: $compiledresult.=<<END;
787: <p>
788: END
789: $compiledresult.=<<END if $ENV{'form.catalogmode'} eq 'interactive';
790: <font size='-1'><INPUT TYPE="button" NAME="returnvalues" VALUE="SELECT"
791: onClick="javascript:select_data('$title','$url')">
792: </font>
793: <br>
794: END
795: my $httphost=$ENV{'HTTP_HOST'};
796:
797: my $viewselect;
798: if ($mode eq 'Basic') {
799: $viewselect=$ENV{'form.basicviewselect'};
800: }
801: elsif ($mode eq 'Advanced') {
802: $viewselect=$ENV{'form.advancedviewselect'};
803: }
804:
805: if ($viewselect eq 'Detailed Citation View') {
806: $compiledresult.=&detailed_citation_view(@fields,
807: $hostname,$httphost,
808: $extrashow2);
809: }
810: elsif ($viewselect eq 'Summary View') {
811: $compiledresult.=&summary_view(@fields,$hostname,$httphost,
812: $extrashow2);
813: }
814: elsif ($viewselect eq 'Fielded Format') {
815: $compiledresult.=&fielded_format_view(@fields,$hostname,
816: $httphost,$extrashow2);
817: }
818: elsif ($viewselect eq 'XML/SGML') {
819: $compiledresult.=&xml_sgml_view(@fields,$hostname,$httphost,
820: $extrashow2);
821: }
822:
823: }
824:
825: unless ($compiledresult) {
826: $compiledresult="There were no results that matched your query";
827: }
828:
829: # make query information persistent to allow for subsequent revision
830: my $persistent=&make_persistent();
831:
832: $r->print(<<BEGINNING);
833: <html>
834: <head>
835: <title>The LearningOnline Network with CAPA</title>
836: BEGINNING
837: $r->print(<<SCRIPT) if $ENV{'form.catalogmode'} eq 'interactive';
838: <script>
839: function select_data(title,url) {
840: changeTitle(title);
841: changeURL(url);
842: }
843: function changeTitle(val) {
844: if (opener.inf.document.forms.resinfo.elements.t) {
845: opener.inf.document.forms.resinfo.elements.t.value=val;
846: }
847: }
848: function changeURL(val) {
849: if (opener.inf.document.forms.resinfo.elements.u) {
850: opener.inf.document.forms.resinfo.elements.u.value=val;
851: }
852: }
853: </script>
854: SCRIPT
855: $r->print(<<RESULTS);
856: </head>
857: <body bgcolor="#ffffff">
858: <img align=right src=/adm/lonIcons/lonlogos.gif>
859: <h1>Search Catalog</h1>
860: <form method="post" action="/adm/searchcat">
861: $customdata
862: <input type='button' value='Revise search request'
863: onClick='this.form.submit();'>
864: $closebutton
865: $persistent
866: <hr>
867: <h3>Search Query</h3>
868: RESULTS
869: if ($mode eq 'Basic') {
870: $r->print(<<RESULTS);
871: <p>
872: <b>Basic search:</b> $ENV{'form.basicexp'}
873: </p>
874: RESULTS
875: }
876: elsif ($mode eq 'Advanced') {
877: $r->print(<<RESULTS);
878: <p>
879: <b>Advanced search</b>
880: $query
881: </p>
882: RESULTS
883: }
884: $r->print(<<RESULTS);
885: <h3>Search Results</h3>
886: $compiledresult
887: </body>
888: </html>
889: RESULTS
890: }
891: }
892:
893: # ------------------------------------------------------------- build_SQL_query
894: sub build_SQL_query {
895: my ($field_name,$logic_statement)=@_;
896: my $q=new Text::Query('abc',
897: -parse => 'Text::Query::ParseAdvanced',
898: -build => 'Text::Query::Build');
899: $q->prepare($logic_statement);
900: my $matchexp=${$q}{'matchexp'}; chomp $matchexp;
901: my $sql_query=&recursive_SQL_query_build($field_name,$matchexp);
902: return $sql_query;
903: }
904:
905: # ------------------------------------------------- build custom metadata query
906: sub build_custommetadata_query {
907: my ($field_name,$logic_statement)=@_;
908: my $q=new Text::Query('abc',
909: -parse => 'Text::Query::ParseAdvanced',
910: -build => 'Text::Query::BuildAdvancedString');
911: $q->prepare($logic_statement);
912: my $matchexp=${$q}{'-parse'}{'-build'}{'matchstring'};
913: # quick fix to change literal into xml tag-matching
914: # will eventually have to write a separate builder module
915: my $oldmatchexp=$matchexp;
916: $matchexp=~s/(\w+)\\\=([\w\\\+]+)/\\\<$1\\\>\[\^\\\<\]\*$2\[\^\\\<\]\*\\\<\\\/$1\\\>/g;
917: return $matchexp;
918: }
919:
920: # - Recursively parse a reverse notation expression into a SQL query expression
921: sub recursive_SQL_query_build {
922: my ($dkey,$pattern)=@_;
923: my @matches=($pattern=~/(\[[^\]|\[]*\])/g);
924: return $pattern unless @matches;
925: foreach my $match (@matches) {
926: $match=~/\[ (\w+)\s(.*) \]/;
927: my ($key,$value)=($1,$2);
928: my $replacement='';
929: if ($key eq 'literal') {
930: $replacement="($dkey like \"\%$value\%\")";
931: }
932: elsif ($key eq 'and') {
933: $value=~/(.*[\"|\)]) ([|\(|\^].*)/;
934: $replacement="($1 AND $2)";
935: }
936: elsif ($key eq 'or') {
937: $value=~/(.*[\"|\)]) ([|\(|\^].*)/;
938: $replacement="($1 OR $2)";
939: }
940: substr($pattern,
941: index($pattern,$match),
942: length($match),
943: $replacement
944: );
945: }
946: &recursive_SQL_query_build($dkey,$pattern);
947: }
948:
949: # ------------------------------------------------------ Detailed Citation View
950: sub detailed_citation_view {
951: my ($title,$author,$subject,$url,$keywords,$version,
952: $notes,$shortabstract,$mime,$lang,
953: $creationdate,$lastrevisiondate,$owner,$copyright,
954: $hostname,$httphost,$extrashow)=@_;
955: my $result=<<END;
956: <i>$owner</i>, last revised $lastrevisiondate
957: <h3><A HREF="http://$httphost$url" TARGET='search_preview'>$title</A></h3>
958: <h3>$author</h3>
959: </p>
960: <p>
961: <b>Subject:</b> $subject<br>
962: <b>Keyword(s):</b> $keywords<br>
963: <b>Notes:</b> $notes<br>
964: <b>MIME Type:</b> $mimetag{$mime}<br>
965: <b>Language:</b> $language{$lang}<br>
966: <b>Copyright/Distribution:</b> $cprtag{$copyright}<br>
967: </p>
968: $extrashow
969: <p>
970: $shortabstract
971: </p>
972: END
973: return $result;
974: }
975:
976: # ---------------------------------------------------------------- Summary View
977: sub summary_view {
978: my ($title,$author,$subject,$url,$keywords,$version,
979: $notes,$shortabstract,$mime,$lang,
980: $creationdate,$lastrevisiondate,$owner,$copyright,
981: $hostname,$httphost,$extrashow)=@_;
982: my $result=<<END;
983: <a href="http://$httphost$url" TARGET='search_preview'>$author</a><br />
984: $title<br />
985: $owner -- $lastrevisiondate<br />
986: $cprtag{$copyright}<br />
987: $extrashow
988: </p>
989: END
990: return $result;
991: }
992:
993: # -------------------------------------------------------------- Fielded Format
994: sub fielded_format_view {
995: my ($title,$author,$subject,$url,$keywords,$version,
996: $notes,$shortabstract,$mime,$lang,
997: $creationdate,$lastrevisiondate,$owner,$copyright,
998: $hostname,$httphost,$extrashow)=@_;
999: my $result=<<END;
1000: <b>URL: </b> <A HREF="http://$httphost$url" TARGET='search_preview'>$url</A>
1001: <br />
1002: <b>Title:</b> $title<br />
1003: <b>Author(s):</b> $author<br />
1004: <b>Subject:</b> $subject<br />
1005: <b>Keyword(s):</b> $keywords<br />
1006: <b>Notes:</b> $notes<br />
1007: <b>MIME Type:</b> $mimetag{$mime}<br />
1008: <b>Language:</b> $language{$lang}<br />
1009: <b>Creation Date:</b> $creationdate<br />
1010: <b>Last Revision Date:</b> $lastrevisiondate<br />
1011: <b>Publisher/Owner:</b> $owner<br />
1012: <b>Copyright/Distribution:</b> $cprtag{$copyright}<br />
1013: <b>Repository Location:</b> $hostname<br />
1014: <b>Abstract:</b> $shortabstract<br />
1015: $extrashow
1016: </p>
1017: END
1018: return $result;
1019: }
1020:
1021: # -------------------------------------------------------------------- XML/SGML
1022: sub xml_sgml_view {
1023: my ($title,$author,$subject,$url,$keywords,$version,
1024: $notes,$shortabstract,$mime,$lang,
1025: $creationdate,$lastrevisiondate,$owner,$copyright,
1026: $hostname,$httphost,$extrashow)=@_;
1027: my $result=<<END;
1028: <pre>
1029: <LonCapaResource>
1030: <url>$url</url>
1031: <title>$title</title>
1032: <author>$author</author>
1033: <subject>$subject</subject>
1034: <keywords>$keywords</keywords>
1035: <notes>$notes</notes>
1036: <mimeInfo>
1037: <mime>$mime</mime>
1038: <mimetag>$mimetag{$mime}</mimetag>
1039: </mimeInfo>
1040: <languageInfo>
1041: <language>$lang</language>
1042: <languagetag>$language{$lang}</languagetag>
1043: </languageInfo>
1044: <creationdate>$creationdate</creationdate>
1045: <lastrevisiondate>$lastrevisiondate</lastrevisiondate>
1046: <owner>$owner</owner>
1047: <copyrightInfo>
1048: <copyright>$copyright</copyright>
1049: <copyrighttag>$cprtag{$copyright}</copyrighttag>
1050: </copyrightInfo>
1051: <repositoryLocation>$hostname</repositoryLocation>
1052: <shortabstract>$shortabstract</shortabstract>
1053: </LonCapaResource>
1054: </pre>
1055: $extrashow
1056: END
1057: return $result;
1058: }
1059:
1060: sub build_date_queries {
1061: my ($cmonth1,$cday1,$cyear1,$cmonth2,$cday2,$cyear2,
1062: $lmonth1,$lday1,$lyear1,$lmonth2,$lday2,$lyear2)=@_;
1063: my @queries;
1064: if ($cmonth1 or $cday1 or $cyear1 or $cmonth2 or $cday2 or $cyear2) {
1065: unless ($cmonth1 and $cday1 and $cyear1 and
1066: $cmonth2 and $cday2 and $cyear2) {
1067: return "Incorrect entry for the creation date. You must specify ".
1068: "a starting month, day, and year and an ending month, ".
1069: "day, and year.";
1070: }
1071: my $cnumeric1=sprintf("%d%2d%2d",$cyear1,$cmonth1,$cday1);
1072: $cnumeric1+=0;
1073: my $cnumeric2=sprintf("%d%2d%2d",$cyear2,$cmonth2,$cday2);
1074: $cnumeric2+=0;
1075: if ($cnumeric1>$cnumeric2) {
1076: return "Incorrect entry for the creation date. The starting ".
1077: "date must occur before the ending date.";
1078: }
1079: my $cquery="(creationdate BETWEEN '$cyear1-$cmonth1-$cday1' AND '".
1080: "$cyear2-$cmonth2-$cday2 23:59:59')";
1081: push @queries,$cquery;
1082: }
1083: if ($lmonth1 or $lday1 or $lyear1 or $lmonth2 or $lday2 or $lyear2) {
1084: unless ($lmonth1 and $lday1 and $lyear1 and
1085: $lmonth2 and $lday2 and $lyear2) {
1086: return "Incorrect entry for the last revision date. You must ".
1087: "specify a starting month, day, and year and an ending ".
1088: "month, day, and year.";
1089: }
1090: my $lnumeric1=sprintf("%d%2d%2d",$lyear1,$lmonth1,$lday1);
1091: $lnumeric1+=0;
1092: my $lnumeric2=sprintf("%d%2d%2d",$lyear2,$lmonth2,$lday2);
1093: $lnumeric2+=0;
1094: if ($lnumeric1>$lnumeric2) {
1095: return "Incorrect entry for the last revision date. The ".
1096: "starting date must occur before the ending date.";
1097: }
1098: my $lquery="(lastrevisiondate BETWEEN '$lyear1-$lmonth1-$lday1' AND '".
1099: "$lyear2-$lmonth2-$lday2 23:59:59')";
1100: push @queries,$lquery;
1101: }
1102: if (@queries) {
1103: return join(" AND ",@queries);
1104: }
1105: return '';
1106: }
1107:
1108: sub output_date_error {
1109: my ($r,$message)=@_;
1110: # make query information persistent to allow for subsequent revision
1111: my $persistent=&make_persistent();
1112:
1113: $r->print(<<BEGINNING);
1114: <html>
1115: <head>
1116: <title>The LearningOnline Network with CAPA</title>
1117: BEGINNING
1118: $r->print(<<RESULTS);
1119: </head>
1120: <body bgcolor="#ffffff">
1121: <img align=right src=/adm/lonIcons/lonlogos.gif>
1122: <h1>Search Catalog</h1>
1123: <form method="post" action="/adm/searchcat">
1124: $persistent
1125: <input type='button' value='Revise search request'
1126: onClick='this.form.submit();'>
1127: $closebutton
1128: <hr>
1129: <h3>Helpful Message</h3>
1130: <p>
1131: $message
1132: </p>
1133: </body>
1134: </html>
1135: RESULTS
1136: }
1137:
1138: sub make_persistent {
1139: my $persistent='';
1140:
1141: map {
1142: if (/^form\./ && !/submit/) {
1143: my $name=$_;
1144: my $key=$name;
1145: $ENV{$key}=~s/\'//g; # do not mess with html field syntax
1146: $name=~s/^form\.//;
1147: $persistent.=<<END;
1148: <input type='hidden' name='$name' value='$ENV{$key}' />
1149: END
1150: }
1151: } (keys %ENV);
1152: return $persistent;
1153: }
1154: 1;
1155: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>