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