Annotation of loncom/interface/lonsearchcat.pm, revision 1.88
1.1 www 1: # The LearningOnline Network
2: # Search Catalog
3: #
1.2 harris41 4: # 03/08/2001 Scott Harrison
1.42 harris41 5: # Scott Harrison: 03/12/2001, 03/13/2001, 03/14/2001, 03/15/2001, 03/19/2001
6: # Scott Harrison: 03/20/2001
1.1 www 7: #
1.41 harris41 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
1.42 harris41 23: # advancedsearch(server reference, environment reference) : perform a complex
24: # multi-field logical query
1.41 harris41 25: # filled(field) : determines whether a given field has been filled
1.42 harris41 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
1.43 harris41 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
1.41 harris41 39:
1.1 www 40: package Apache::lonsearchcat;
41:
42: use strict;
43: use Apache::Constants qw(:common);
1.6 harris41 44: use Apache::lonnet();
45: use Apache::File();
1.7 harris41 46: use CGI qw(:standard);
1.41 harris41 47: use Text::Query;
1.1 www 48:
1.3 harris41 49: my %language;
50: my $scrout;
51: my %metadatafields;
52: my %cprtag;
53: my %mimetag;
1.46 harris41 54: my $closebutton;
1.55 harris41 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'>
1.50 harris41 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>
1.46 harris41 69: </select>
70: END
1.3 harris41 71:
1.1 www 72: sub handler {
73: my $r = shift;
1.7 harris41 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:
1.1 www 85: $r->content_type('text/html');
86: $r->send_http_header;
87: return OK if $r->header_only;
88:
1.3 harris41 89: %metadatafields=();
90:
1.8 harris41 91: my $hidden='';
92: $hidden=<<END if $ENV{'form.catalogmode'} eq 'interactive';
93: <input type='hidden' name='catalogmode' value='interactive'>
94: END
95:
1.47 harris41 96: $closebutton=<<END if $ENV{'form.catalogmode'} eq 'interactive';
1.68 harris41 97: <input type="button" name="close" value='CLOSE' onClick="self.close()">
1.46 harris41 98: END
99:
1.3 harris41 100: # ------------------------------------------------ First, check out environment
101: $metadatafields{'owner'}=$ENV{'user.name'}.'@'.$ENV{'user.domain'};
102:
1.8 harris41 103: # --------------------------------- Compute various listings of metadata values
1.3 harris41 104:
105: %language=();
106: $language{'any'}='Any language';
107: {
108: my $fh=Apache::File->new($r->dir_config('lonTabDir').'/language.tab');
109: map {
1.57 harris41 110: $_=~/(\w+)\s+([\w\s\-]+)/; chomp;
1.3 harris41 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 {
1.57 harris41 120: $_=~/(\w+)\s+([\w\s\-]+)/; chomp;
1.3 harris41 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 {
1.57 harris41 130: $_=~/(\w+)\s+(\w+)\s+([\w\s\-]+)/; chomp;
1.3 harris41 131: $mimetag{$1}=".$1 $3";
132: } <$fh>;
133: }
134:
1.6 harris41 135: if ($ENV{'form.basicsubmit'} eq 'SEARCH') {
1.19 harris41 136: return &basicsearch($r,\%ENV);
1.6 harris41 137: }
1.18 harris41 138: elsif ($ENV{'form.advancedsubmit'} eq 'SEARCH') {
139: return &advancedsearch($r,\%ENV);
140: }
1.6 harris41 141:
1.8 harris41 142: $scrout=''; # building a part of screen output
1.3 harris41 143: $scrout.=&searchphrasefield('Limit by title','title',
1.11 harris41 144: $ENV{'form.title'});
1.3 harris41 145:
146: $scrout.=&searchphrasefield('Limit by author','author',
1.11 harris41 147: $ENV{'form.author'});
1.3 harris41 148:
149: $scrout.=&searchphrasefield('Limit by subject','subject',
1.11 harris41 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'});
1.3 harris41 160:
161: $scrout.=&searchphrasefield('Limit by notes','notes',
1.11 harris41 162: $ENV{'form.notes'});
1.3 harris41 163:
164: $scrout.=&searchphrasefield('Limit by abstract','abstract',
1.11 harris41 165: $ENV{'form.abstract'});
1.3 harris41 166:
1.11 harris41 167: $ENV{'form.mime'}='notxxx' unless length($ENV{'form.mime'});
1.3 harris41 168: $scrout.=&selectbox('Limit by MIME type','mime',
1.11 harris41 169: $ENV{'form.mime'},%mimetag);
170:
171: $ENV{'form.language'}='any' unless length($ENV{'form.language'});
1.3 harris41 172:
173: $scrout.=&selectbox('Limit by language','language',
1.11 harris41 174: $ENV{'form.language'},%language);
1.3 harris41 175:
1.8 harris41 176:
177: # ------------------------------------------------ Compute date selection boxes
178: $scrout.=<<CREATIONDATESTART;
1.3 harris41 179: <p>
180: <font color="#800000" face="helvetica"><b>LIMIT BY CREATION DATE RANGE:</b>
181: </font>
182: <br>
1.8 harris41 183: between:
184: CREATIONDATESTART
1.11 harris41 185: $scrout.=&dateboxes('creationdatestart',1,1,1976,
186: $ENV{'form.creationdatestart_month'},
187: $ENV{'form.creationdatestart_day'},
188: $ENV{'form.creationdatestart_year'},
189: );
1.8 harris41 190: $scrout.=<<CREATIONDATEEND;
191: and:
192: CREATIONDATEEND
1.11 harris41 193: $scrout.=&dateboxes('creationdateend',12,31,2051,
194: $ENV{'form.creationdateend_month'},
195: $ENV{'form.creationdateend_day'},
196: $ENV{'form.creationdateend_year'},
197: );
1.8 harris41 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
1.11 harris41 206: $scrout.=&dateboxes('lastrevisiondatestart',1,1,1976,
207: $ENV{'form.lastrevisiondatestart_month'},
208: $ENV{'form.lastrevisiondatestart_day'},
209: $ENV{'form.lastrevisiondatestart_year'},
210: );
1.8 harris41 211: $scrout.=<<LASTREVISIONDATEEND;
212: and:
213: LASTREVISIONDATEEND
1.11 harris41 214: $scrout.=&dateboxes('lastrevisiondateend',12,31,2051,
215: $ENV{'form.lastrevisiondateend_month'},
216: $ENV{'form.lastrevisiondateend_day'},
217: $ENV{'form.lastrevisiondateend_year'},
218: );
1.8 harris41 219: $scrout.='</p>';
220:
221: $scrout.=&searchphrasefield('Limit by publisher/owner','owner',
1.11 harris41 222: $ENV{'form.owner'});
223: # $metadatafields{'owner'});
1.8 harris41 224:
1.11 harris41 225: $ENV{'form.copyright'}='any' unless length($ENV{'form.copyright'});
1.8 harris41 226: $scrout.=&selectbox('Limit by copyright/distribution','copyright',
1.11 harris41 227: $ENV{'form.copyright'},%cprtag);
1.8 harris41 228:
1.14 harris41 229: # ------------------------------------------- Compute customized metadata field
230: $scrout.=<<CUSTOMMETADATA;
231: <p>
1.77 harris41 232: <font color="#800000" face="helvetica"><b>LIMIT BY SPECIAL METADATA FIELDS:</b>
1.14 harris41 233: </font>
1.77 harris41 234: For resource-specific metadata, enter in an expression in the form of
1.14 harris41 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'});
1.15 harris41 240: $scrout.=' <i>initial users of this system do not need to worry about this option</i>';
1.14 harris41 241:
1.77 harris41 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:
1.8 harris41 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>
1.11 harris41 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>
1.8 harris41 280: <br>
1.68 harris41 281: <input type="submit" name="basicsubmit" value='SEARCH' />
282: <input type="reset" name="reset" value='RESET' />
1.46 harris41 283: $closebutton
1.55 harris41 284: $basicviewselect
1.8 harris41 285: </p>
286: <hr>
287: <h3>Advanced Search</h3>
288: $scrout
289: <p>
1.68 harris41 290: <input type="submit" name="advancedsubmit" value='SEARCH' />
291: <input type="reset" name="reset" value='RESET' />
1.46 harris41 292: $closebutton
1.55 harris41 293: $advancedviewselect
1.3 harris41 294: </p>
1.8 harris41 295: </form>
296: </body>
297: </html>
298: ENDDOCUMENT
299: return OK;
300: }
301:
302: # --------------------------------------------------------- Various form fields
303:
1.11 harris41 304: sub simpletextfield {
305: my ($name,$value)=@_;
1.68 harris41 306: return '<input type=text name=\''.$name.
307: '\' size=20 value=\''.$value.'\' />';
1.11 harris41 308: }
309:
310: sub simplecheckbox {
311: my ($name,$value)=@_;
312: my $checked='';
313: $checked="CHECKED" if $value eq 'on';
1.68 harris41 314: return '<input type=checkbox name=\''.$name.'\' '. $checked . '>';
1.11 harris41 315: }
316:
1.8 harris41 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>".
1.68 harris41 326: '<input type=text name="'.$name.'" size=80 value=\''.$value.'\'>';
1.8 harris41 327: }
1.3 harris41 328:
1.8 harris41 329: sub dateboxes {
1.11 harris41 330: my ($name,$defaultmonth,$defaultday,$defaultyear,
331: $currentmonth,$currentday,$currentyear)=@_;
332: ($defaultmonth,$defaultday,$defaultyear)=('','','');
333: my $month=<<END;
1.8 harris41 334: <select name="${name}_month">
1.11 harris41 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>
1.3 harris41 345: <option value="10">October</option>
346: <option value="11">November</option>
347: <option value="12">December</option>
348: </select>
1.11 harris41 349: END
350: $month=~s/(\"$currentmonth\")/$1 SELECTED/ if length($currentmonth);
351: my $day=<<END;
1.8 harris41 352: <select name="${name}_day">
1.11 harris41 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>
1.3 harris41 385: </select>
1.11 harris41 386: END
387: $day=~s/(\"$currentday\")/$1 SELECTED/ if length($currentday);
388: my $year=<<END;
1.8 harris41 389: <select name="${name}_year">
1.11 harris41 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>
1.3 harris41 467: </select>
468: END
1.11 harris41 469: $year=~s/(\"$currentyear\")/$1 SELECTED/ if length($currentyear);
470: return "$month$day$year";
1.3 harris41 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 {
1.68 harris41 479: $selout.='<option value=\''.$_.'\'';
1.3 harris41 480: if ($_ eq $value) { $selout.=' selected'; }
481: $selout.='>'.$options{$_}.'</option>';
482: } sort keys %options;
483: return $selout.'</select>';
1.6 harris41 484: }
485:
1.45 harris41 486: # ----------------------------------------------- Performing an advanced search
1.18 harris41 487: sub advancedsearch {
488: my ($r,$envhash)=@_;
489: my %ENV=%{$envhash};
490:
1.32 harris41 491: my $fillflag=0;
1.64 harris41 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',
1.77 harris41 501: 'custommetadata','customshow') {
1.68 harris41 502: $ENV{"form.$field"}=~s/[^\w\s\(\)\=\-\"\']//g;
1.64 harris41 503: }
1.32 harris41 504: for my $field ('title','author','subject','keywords','url','version',
505: 'notes','abstract','mime','language','owner',
506: 'custommetadata') {
1.40 harris41 507: if (&filled($ENV{"form.$field"})) {
1.32 harris41 508: $fillflag++;
509: }
510: }
511:
512: unless ($fillflag) {
513: &output_blank_field_error($r);
514: return OK;
515: }
1.39 harris41 516:
517: my $query='';
1.44 harris41 518:
1.45 harris41 519: my @queries;
1.44 harris41 520: # Go through logical expression AND/OR/NOT phrase fields.
1.58 harris41 521:
522: foreach my $field ('title','author','subject','notes','abstract','url',
523: 'keywords','version','owner') {
1.44 harris41 524: if ($ENV{'form.'.$field}) {
1.45 harris41 525: push @queries,&build_SQL_query($field,$ENV{'form.'.$field});
1.44 harris41 526: }
527: }
1.58 harris41 528: if ($ENV{'form.language'} and $ENV{'form.language'} ne 'any') {
1.59 harris41 529: push @queries,"(language like \"\%$ENV{'form.language'}\%\")";
1.58 harris41 530: }
531: if ($ENV{'form.mime'} and $ENV{'form.mime'} ne 'any') {
1.59 harris41 532: push @queries,"(mime like \"\%$ENV{'form.mime'}\%\")";
1.58 harris41 533: }
534: if ($ENV{'form.copyright'} and $ENV{'form.copyright'} ne 'any') {
1.59 harris41 535: push @queries,"(copyright like \"\%$ENV{'form.copyright'}\%\")";
1.58 harris41 536: }
1.60 harris41 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: );
1.61 harris41 551: if ($datequery=~/^Incorrect/) {
552: &output_date_error($r,$datequery);
553: return OK;
554: }
555: elsif ($datequery) {
1.60 harris41 556: push @queries,$datequery;
557: }
1.76 harris41 558: my $customquery='';
1.64 harris41 559: if ($ENV{'form.custommetadata'}) {
560: $customquery=&build_custommetadata_query('custommetadata',
561: $ENV{'form.custommetadata'});
562: }
1.83 harris41 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: }
1.45 harris41 570: if (@queries) {
1.58 harris41 571: $query=join(" AND ",@queries);
1.46 harris41 572: $query="select * from metadata where $query";
1.76 harris41 573: my $reply='';
1.83 harris41 574: unless ($customquery or $customshow) {
1.76 harris41 575: $reply=&Apache::lonnet::metadata_query($query);
576: }
577: else {
1.83 harris41 578: $reply=&Apache::lonnet::metadata_query($query,
579: $customquery,$customshow);
1.76 harris41 580: }
1.64 harris41 581: &output_results('Advanced',$r,$envhash,$customquery,$reply);
1.45 harris41 582: }
1.86 harris41 583: elsif ($customquery) {
584: my $reply='';
585: $reply=&Apache::lonnet::metadata_query('',
586: $customquery,$customshow);
587: &output_results('Advanced',$r,$envhash,$customquery,$reply);
1.45 harris41 588: }
1.87 harris41 589: $r->print(' '); # just in case.. hrrmm..
1.24 harris41 590: return OK;
1.18 harris41 591: }
592:
1.26 harris41 593: # ---------------------------------------------------- see if a field is filled
594: sub filled {
1.31 harris41 595: my ($field)=@_;
596: if ($field=~/\S/) {
1.28 harris41 597: return 1;
598: }
599: else {
600: return 0;
601: }
1.26 harris41 602: }
603:
1.6 harris41 604: # --------------------------------------------------- Performing a basic search
605: sub basicsearch {
1.19 harris41 606: my ($r,$envhash)=@_;
607: my %ENV=%{$envhash};
1.6 harris41 608:
1.64 harris41 609: # Clean up fields for safety
610: for my $field ('basicexp') {
611: $ENV{"form.$field"}=~s/[^\w\s\(\)\-]//g;
612: }
613:
1.26 harris41 614: unless (&filled($ENV{'form.basicexp'})) {
1.24 harris41 615: &output_blank_field_error($r);
616: return OK;
617: }
1.22 harris41 618:
1.39 harris41 619: my $query='';
1.33 harris41 620: my $concatarg=join('," ",',
621: ('title', 'author', 'subject', 'notes', 'abstract'));
622:
623: $query="select * from metadata where concat($concatarg) like '\%$ENV{'form.basicexp'}\%'";
1.13 harris41 624: my $reply=&Apache::lonnet::metadata_query($query);
1.44 harris41 625: &output_results('Basic',$r,$envhash,$query,$reply);
1.18 harris41 626: return OK;
1.22 harris41 627: }
628:
1.44 harris41 629: # ---------------- Message to output when there are not enough fields filled in
1.22 harris41 630: sub output_blank_field_error {
631: my ($r)=@_;
632: # make query information persistent to allow for subsequent revision
1.65 harris41 633: my $persistent=&make_persistent();
1.22 harris41 634:
635: $r->print(<<BEGINNING);
636: <html>
637: <head>
638: <title>The LearningOnline Network with CAPA</title>
639: BEGINNING
640: $r->print(<<RESULTS);
641: </head>
642: <body bgcolor="#ffffff">
643: <img align=right src=/adm/lonIcons/lonlogos.gif>
644: <h1>Search Catalog</h1>
645: <form method="post" action="/adm/searchcat">
646: $persistent
647: <input type='button' value='Revise search request'
648: onClick='this.form.submit();'>
1.46 harris41 649: $closebutton
1.22 harris41 650: <hr>
651: <h3>Helpful Message</h3>
652: <p>
653: Incorrect search query due to blank entry fields.
654: You need to fill in the relevant
655: fields on the search page in order for a query to be
656: processed.
657: </p>
658: </body>
659: </html>
660: RESULTS
1.18 harris41 661: }
1.6 harris41 662:
1.18 harris41 663: # ----------------------------- format and output results based on a reply list
664: sub output_results {
1.44 harris41 665: my ($mode,$r,$envhash,$query,@replylist)=@_;
1.19 harris41 666: my %ENV=%{$envhash};
1.44 harris41 667: my $compiledresult='';
668:
1.18 harris41 669: foreach my $reply (@replylist) {
670:
671: my @results;
672:
673: my $replyfile='';
674: $reply=~/^([\.\w]+)$/; # must do since 'use strict' checks for tainting
675: $replyfile=$r->dir_config('lonDaemons').'/tmp/'.$1;
676: $reply=~/(.*?)\_/;
677: my $hostname=$1;
1.52 harris41 678: sleep 3; # temporary fix, need to check for completion and status
1.18 harris41 679: {
680: while (1) {
681: last if -e $replyfile;
682: sleep 1;
683: }
684: # QUESTION: how should I handle this error condition..
685: # I'm sure there is syntax elsewhere I can use..
686: my $fh=Apache::File->new($replyfile) or
687: ($r->print('file cannot be opened') and return OK);
688: @results=<$fh>;
1.6 harris41 689: }
690:
1.77 harris41 691: my $customshow='';
692: my $extrashow='';
1.87 harris41 693: my @customfields;
1.77 harris41 694: if ($ENV{'form.customshow'}) {
695: $customshow=$ENV{'form.customshow'};
696: $customshow=~s/[^\w\s]//g;
1.87 harris41 697: my @fields=map {"<font color=\"#008000\">$_:</font><!-- $_ -->"}
1.77 harris41 698: split(/\s+/,$customshow);
1.88 ! harris41 699: @customfields=split(/\s+/,$customshow);
1.81 harris41 700: if ($customshow) {
701: $extrashow="<ul><li>".join("</li><li>",@fields)."</li></ul>\n";
702: }
1.77 harris41 703: }
1.79 harris41 704: my $customdata='';
1.87 harris41 705: my %customhash;
1.79 harris41 706: foreach my $result (@results) {
1.82 harris41 707: if ($result=~/^(custom\=.*)$/) { # grab all custom metadata
1.87 harris41 708: my $tmp=$result;
709: $tmp=~s/^custom\=//;
710: my ($k,$v)=map {&Apache::lonnet::unescape($_);
711: } split(/\,/,$tmp);
712: $customhash{$k}=$v;
1.82 harris41 713: }
1.79 harris41 714: }
1.18 harris41 715: foreach my $result (@results) {
1.82 harris41 716: next if $result=~/^custom\=/;
717: chomp $result;
1.85 harris41 718: next unless $result;
1.50 harris41 719: my @fields=map
720: {&Apache::lonnet::unescape($_)}
721: (split(/\,/,$result));
1.18 harris41 722: my ($title,$author,$subject,$url,$keywords,$version,
723: $notes,$abstract,$mime,$lang,
1.50 harris41 724: $creationdate,$lastrevisiondate,$owner,$copyright)=@fields;
1.18 harris41 725: my $shortabstract=$abstract;
726: $shortabstract=substr($abstract,0,200) if length($abstract)>200;
1.51 harris41 727: $fields[7]=$shortabstract;
1.87 harris41 728: my $extrashow2=$extrashow;
729: if ($extrashow) {
730: foreach my $field (@customfields) {
731: my $value='';
732: if ($customhash{$url}=~/\<${field}[^\>]*\>(.*?)\<\/${field}[^\>]*\>/s) {
1.88 ! harris41 733: $value=$1;
1.87 harris41 734: }
1.88 ! harris41 735: $extrashow2=~s/\<\!\-\- $field \-\-\>/ $value/g;
1.87 harris41 736: }
737: }
1.18 harris41 738: $compiledresult.=<<END;
1.56 harris41 739: <p>
1.8 harris41 740: END
1.18 harris41 741: $compiledresult.=<<END if $ENV{'form.catalogmode'} eq 'interactive';
1.8 harris41 742: <font size='-1'><INPUT TYPE="button" NAME="returnvalues" VALUE="SELECT"
1.10 harris41 743: onClick="javascript:select_data('$title','$url')">
1.8 harris41 744: </font>
745: <br>
746: END
1.51 harris41 747: my $httphost=$ENV{'HTTP_HOST'};
1.53 harris41 748:
1.55 harris41 749: my $viewselect;
750: if ($mode eq 'Basic') {
751: $viewselect=$ENV{'form.basicviewselect'};
752: }
753: elsif ($mode eq 'Advanced') {
754: $viewselect=$ENV{'form.advancedviewselect'};
755: }
756:
757: if ($viewselect eq 'Detailed Citation View') {
1.51 harris41 758: $compiledresult.=&detailed_citation_view(@fields,
1.77 harris41 759: $hostname,$httphost,
1.87 harris41 760: $extrashow2);
1.50 harris41 761: }
1.55 harris41 762: elsif ($viewselect eq 'Summary View') {
1.77 harris41 763: $compiledresult.=&summary_view(@fields,$hostname,$httphost,
1.87 harris41 764: $extrashow2);
1.50 harris41 765: }
1.55 harris41 766: elsif ($viewselect eq 'Fielded Format') {
1.51 harris41 767: $compiledresult.=&fielded_format_view(@fields,$hostname,
1.87 harris41 768: $httphost,$extrashow2);
1.50 harris41 769: }
1.55 harris41 770: elsif ($viewselect eq 'XML/SGML') {
1.77 harris41 771: $compiledresult.=&xml_sgml_view(@fields,$hostname,$httphost,
1.87 harris41 772: $extrashow2);
1.50 harris41 773: }
774:
1.18 harris41 775: }
1.6 harris41 776:
1.18 harris41 777: unless ($compiledresult) {
778: $compiledresult="There were no results that matched your query";
779: }
1.6 harris41 780:
1.18 harris41 781: # make query information persistent to allow for subsequent revision
1.65 harris41 782: my $persistent=&make_persistent();
1.9 harris41 783:
1.18 harris41 784: $r->print(<<BEGINNING);
1.6 harris41 785: <html>
786: <head>
787: <title>The LearningOnline Network with CAPA</title>
1.8 harris41 788: BEGINNING
1.18 harris41 789: $r->print(<<SCRIPT) if $ENV{'form.catalogmode'} eq 'interactive';
1.8 harris41 790: <script>
791: function select_data(title,url) {
792: changeTitle(title);
793: changeURL(url);
794: }
795: function changeTitle(val) {
796: if (opener.inf.document.forms.resinfo.elements.t) {
797: opener.inf.document.forms.resinfo.elements.t.value=val;
798: }
799: }
800: function changeURL(val) {
801: if (opener.inf.document.forms.resinfo.elements.u) {
802: opener.inf.document.forms.resinfo.elements.u.value=val;
803: }
804: }
805: </script>
806: SCRIPT
1.18 harris41 807: $r->print(<<RESULTS);
1.6 harris41 808: </head>
809: <body bgcolor="#ffffff">
810: <img align=right src=/adm/lonIcons/lonlogos.gif>
811: <h1>Search Catalog</h1>
812: <form method="post" action="/adm/searchcat">
1.80 harris41 813: $customdata
1.9 harris41 814: <input type='button' value='Revise search request'
815: onClick='this.form.submit();'>
1.46 harris41 816: $closebutton
1.9 harris41 817: $persistent
1.6 harris41 818: <hr>
819: <h3>Search Query</h3>
1.43 harris41 820: RESULTS
821: if ($mode eq 'Basic') {
822: $r->print(<<RESULTS);
1.6 harris41 823: <p>
1.19 harris41 824: <b>Basic search:</b> $ENV{'form.basicexp'}
1.6 harris41 825: </p>
1.43 harris41 826: RESULTS
1.44 harris41 827: }
1.43 harris41 828: elsif ($mode eq 'Advanced') {
829: $r->print(<<RESULTS);
830: <p>
831: <b>Advanced search</b>
1.44 harris41 832: $query
1.43 harris41 833: </p>
834: RESULTS
835: }
1.44 harris41 836: $r->print(<<RESULTS);
1.6 harris41 837: <h3>Search Results</h3>
838: $compiledresult
839: </body>
840: </html>
841: RESULTS
1.18 harris41 842: }
1.41 harris41 843: }
844:
845: # ------------------------------------------------------------- build_SQL_query
846: sub build_SQL_query {
1.43 harris41 847: my ($field_name,$logic_statement)=@_;
848: my $q=new Text::Query('abc',
849: -parse => 'Text::Query::ParseAdvanced',
850: -build => 'Text::Query::Build');
1.44 harris41 851: $q->prepare($logic_statement);
1.43 harris41 852: my $matchexp=${$q}{'matchexp'}; chomp $matchexp;
853: my $sql_query=&recursive_SQL_query_build($field_name,$matchexp);
1.44 harris41 854: return $sql_query;
1.43 harris41 855: }
1.41 harris41 856:
1.64 harris41 857: # ------------------------------------------------- build custom metadata query
858: sub build_custommetadata_query {
859: my ($field_name,$logic_statement)=@_;
860: my $q=new Text::Query('abc',
861: -parse => 'Text::Query::ParseAdvanced',
862: -build => 'Text::Query::BuildAdvancedString');
863: $q->prepare($logic_statement);
864: my $matchexp=${$q}{'-parse'}{'-build'}{'matchstring'};
1.69 harris41 865: # quick fix to change literal into xml tag-matching
1.75 harris41 866: # will eventually have to write a separate builder module
1.72 harris41 867: my $oldmatchexp=$matchexp;
1.88 ! harris41 868: $matchexp=~s/(\w+)\\\=([\w\\\+]+)/\\\<$1\\\>\[\^\\\<\]\*$2\[\^\\\<\]\*\\\<\\\/$1\\\>/g;
1.76 harris41 869: return $matchexp;
1.64 harris41 870: }
871:
1.43 harris41 872: # - Recursively parse a reverse notation expression into a SQL query expression
873: sub recursive_SQL_query_build {
874: my ($dkey,$pattern)=@_;
875: my @matches=($pattern=~/(\[[^\]|\[]*\])/g);
876: return $pattern unless @matches;
877: foreach my $match (@matches) {
878: $match=~/\[ (\w+)\s(.*) \]/;
1.44 harris41 879: my ($key,$value)=($1,$2);
1.43 harris41 880: my $replacement='';
881: if ($key eq 'literal') {
882: $replacement="($dkey like \"\%$value\%\")";
883: }
884: elsif ($key eq 'and') {
885: $value=~/(.*[\"|\)]) ([|\(|\^].*)/;
886: $replacement="($1 AND $2)";
887: }
888: elsif ($key eq 'or') {
889: $value=~/(.*[\"|\)]) ([|\(|\^].*)/;
890: $replacement="($1 OR $2)";
891: }
892: substr($pattern,
893: index($pattern,$match),
894: length($match),
895: $replacement
896: );
897: }
898: &recursive_SQL_query_build($dkey,$pattern);
1.50 harris41 899: }
900:
901: # ------------------------------------------------------ Detailed Citation View
902: sub detailed_citation_view {
903: my ($title,$author,$subject,$url,$keywords,$version,
1.51 harris41 904: $notes,$shortabstract,$mime,$lang,
905: $creationdate,$lastrevisiondate,$owner,$copyright,
1.77 harris41 906: $hostname,$httphost,$extrashow)=@_;
1.50 harris41 907: my $result=<<END;
1.56 harris41 908: <i>$owner</i>, last revised $lastrevisiondate
909: <h3><A HREF="http://$httphost$url" TARGET='search_preview'>$title</A></h3>
910: <h3>$author</h3>
911: </p>
912: <p>
1.50 harris41 913: <b>Subject:</b> $subject<br>
914: <b>Keyword(s):</b> $keywords<br>
915: <b>Notes:</b> $notes<br>
916: <b>MIME Type:</b> $mimetag{$mime}<br>
917: <b>Language:</b> $language{$lang}<br>
1.56 harris41 918: <b>Copyright/Distribution:</b> $cprtag{$copyright}<br>
1.78 harris41 919: </p>
1.77 harris41 920: $extrashow
1.78 harris41 921: <p>
1.56 harris41 922: $shortabstract
1.50 harris41 923: </p>
924: END
925: return $result;
926: }
927:
928: # ---------------------------------------------------------------- Summary View
929: sub summary_view {
930: my ($title,$author,$subject,$url,$keywords,$version,
1.51 harris41 931: $notes,$shortabstract,$mime,$lang,
932: $creationdate,$lastrevisiondate,$owner,$copyright,
1.77 harris41 933: $hostname,$httphost,$extrashow)=@_;
1.50 harris41 934: my $result=<<END;
1.56 harris41 935: <a href="http://$httphost$url" TARGET='search_preview'>$author</a><br />
936: $title<br />
937: $owner -- $lastrevisiondate<br />
938: $cprtag{$copyright}<br />
1.77 harris41 939: $extrashow
1.50 harris41 940: </p>
941: END
942: return $result;
943: }
944:
945: # -------------------------------------------------------------- Fielded Format
946: sub fielded_format_view {
947: my ($title,$author,$subject,$url,$keywords,$version,
1.51 harris41 948: $notes,$shortabstract,$mime,$lang,
949: $creationdate,$lastrevisiondate,$owner,$copyright,
1.77 harris41 950: $hostname,$httphost,$extrashow)=@_;
1.50 harris41 951: my $result=<<END;
1.51 harris41 952: <b>URL: </b> <A HREF="http://$httphost$url" TARGET='search_preview'>$url</A>
1.56 harris41 953: <br />
954: <b>Title:</b> $title<br />
955: <b>Author(s):</b> $author<br />
956: <b>Subject:</b> $subject<br />
957: <b>Keyword(s):</b> $keywords<br />
958: <b>Notes:</b> $notes<br />
959: <b>MIME Type:</b> $mimetag{$mime}<br />
960: <b>Language:</b> $language{$lang}<br />
961: <b>Creation Date:</b> $creationdate<br />
962: <b>Last Revision Date:</b> $lastrevisiondate<br />
963: <b>Publisher/Owner:</b> $owner<br />
964: <b>Copyright/Distribution:</b> $cprtag{$copyright}<br />
965: <b>Repository Location:</b> $hostname<br />
966: <b>Abstract:</b> $shortabstract<br />
1.77 harris41 967: $extrashow
1.50 harris41 968: </p>
969: END
970: return $result;
971: }
972:
973: # -------------------------------------------------------------------- XML/SGML
974: sub xml_sgml_view {
975: my ($title,$author,$subject,$url,$keywords,$version,
1.51 harris41 976: $notes,$shortabstract,$mime,$lang,
977: $creationdate,$lastrevisiondate,$owner,$copyright,
1.77 harris41 978: $hostname,$httphost,$extrashow)=@_;
1.50 harris41 979: my $result=<<END;
1.56 harris41 980: <pre>
981: <LonCapaResource>
1.57 harris41 982: <url>$url</url>
1.56 harris41 983: <title>$title</title>
984: <author>$author</author>
985: <subject>$subject</subject>
986: <keywords>$keywords</keywords>
987: <notes>$notes</notes>
988: <mimeInfo>
989: <mime>$mime</mime>
990: <mimetag>$mimetag{$mime}</mimetag>
991: </mimeInfo>
992: <languageInfo>
993: <language>$lang</language>
994: <languagetag>$language{$lang}</languagetag>
995: </languageInfo>
996: <creationdate>$creationdate</creationdate>
997: <lastrevisiondate>$lastrevisiondate</lastrevisiondate>
998: <owner>$owner</owner>
999: <copyrightInfo>
1000: <copyright>$copyright</copyright>
1001: <copyrighttag>$cprtag{$copyright}</copyrighttag>
1002: </copyrightInfo>
1003: <repositoryLocation>$hostname</repositoryLocation>
1004: <shortabstract>$shortabstract</shortabstract>
1.57 harris41 1005: </LonCapaResource>
1.56 harris41 1006: </pre>
1.77 harris41 1007: $extrashow
1.50 harris41 1008: END
1009: return $result;
1.60 harris41 1010: }
1011:
1012: sub build_date_queries {
1013: my ($cmonth1,$cday1,$cyear1,$cmonth2,$cday2,$cyear2,
1014: $lmonth1,$lday1,$lyear1,$lmonth2,$lday2,$lyear2)=@_;
1.61 harris41 1015: my @queries;
1016: if ($cmonth1 or $cday1 or $cyear1 or $cmonth2 or $cday2 or $cyear2) {
1017: unless ($cmonth1 and $cday1 and $cyear1 and
1018: $cmonth2 and $cday2 and $cyear2) {
1019: return "Incorrect entry for the creation date. You must specify ".
1020: "a starting month, day, and year and an ending month, ".
1021: "day, and year.";
1022: }
1.63 harris41 1023: my $cnumeric1=sprintf("%d%2d%2d",$cyear1,$cmonth1,$cday1);
1.61 harris41 1024: $cnumeric1+=0;
1.63 harris41 1025: my $cnumeric2=sprintf("%d%2d%2d",$cyear2,$cmonth2,$cday2);
1.61 harris41 1026: $cnumeric2+=0;
1027: if ($cnumeric1>$cnumeric2) {
1028: return "Incorrect entry for the creation date. The starting ".
1029: "date must occur before the ending date.";
1030: }
1031: my $cquery="(creationdate BETWEEN '$cyear1-$cmonth1-$cday1' AND '".
1032: "$cyear2-$cmonth2-$cday2 23:59:59')";
1033: push @queries,$cquery;
1034: }
1035: if ($lmonth1 or $lday1 or $lyear1 or $lmonth2 or $lday2 or $lyear2) {
1036: unless ($lmonth1 and $lday1 and $lyear1 and
1037: $lmonth2 and $lday2 and $lyear2) {
1038: return "Incorrect entry for the last revision date. You must ".
1039: "specify a starting month, day, and year and an ending ".
1040: "month, day, and year.";
1.62 harris41 1041: }
1.63 harris41 1042: my $lnumeric1=sprintf("%d%2d%2d",$lyear1,$lmonth1,$lday1);
1.61 harris41 1043: $lnumeric1+=0;
1.63 harris41 1044: my $lnumeric2=sprintf("%d%2d%2d",$lyear2,$lmonth2,$lday2);
1.61 harris41 1045: $lnumeric2+=0;
1046: if ($lnumeric1>$lnumeric2) {
1047: return "Incorrect entry for the last revision date. The ".
1048: "starting date must occur before the ending date.";
1049: }
1050: my $lquery="(lastrevisiondate BETWEEN '$lyear1-$lmonth1-$lday1' AND '".
1051: "$lyear2-$lmonth2-$lday2 23:59:59')";
1052: push @queries,$lquery;
1053: }
1054: if (@queries) {
1055: return join(" AND ",@queries);
1056: }
1057: return '';
1.60 harris41 1058: }
1059:
1060: sub output_date_error {
1061: my ($r,$message)=@_;
1062: # make query information persistent to allow for subsequent revision
1.65 harris41 1063: my $persistent=&make_persistent();
1.60 harris41 1064:
1065: $r->print(<<BEGINNING);
1066: <html>
1067: <head>
1068: <title>The LearningOnline Network with CAPA</title>
1069: BEGINNING
1070: $r->print(<<RESULTS);
1071: </head>
1072: <body bgcolor="#ffffff">
1073: <img align=right src=/adm/lonIcons/lonlogos.gif>
1074: <h1>Search Catalog</h1>
1075: <form method="post" action="/adm/searchcat">
1076: $persistent
1077: <input type='button' value='Revise search request'
1078: onClick='this.form.submit();'>
1079: $closebutton
1080: <hr>
1081: <h3>Helpful Message</h3>
1082: <p>
1083: $message
1084: </p>
1085: </body>
1086: </html>
1087: RESULTS
1.3 harris41 1088: }
1.1 www 1089:
1.64 harris41 1090: sub make_persistent {
1.65 harris41 1091: my $persistent='';
1.66 harris41 1092:
1.65 harris41 1093: map {
1094: if (/^form\./ && !/submit/) {
1095: my $name=$_;
1096: my $key=$name;
1.69 harris41 1097: $ENV{$key}=~s/\'//g; # do not mess with html field syntax
1.65 harris41 1098: $name=~s/^form\.//;
1099: $persistent.=<<END;
1.68 harris41 1100: <input type='hidden' name='$name' value='$ENV{$key}' />
1.65 harris41 1101: END
1102: }
1103: } (keys %ENV);
1104: return $persistent;
1.64 harris41 1105: }
1.1 www 1106: 1;
1107: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>