Annotation of loncom/interface/lonsearchcat.pm, revision 1.76
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>
232: <font color="#800000" face="helvetica"><b>LIMIT BY OTHER METADATA FIELDS:</b>
233: </font>
234: For author-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'});
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.8 harris41 242: # ---------------------------------------------------------------- Print screen
243: $r->print(<<ENDDOCUMENT);
244: <html>
245: <head>
246: <title>The LearningOnline Network with CAPA</title>
247: </head>
248: <body bgcolor="#FFFFFF">
249: <img align=right src=/adm/lonIcons/lonlogos.gif>
250: <h1>Search Catalog</h1>
251: <form method="post" action="/adm/searchcat">
252: $hidden
253: <hr>
254: <h3>Basic Search</h3>
255: <p>
256: Enter terms or phrases separated by search operators
257: such as AND or OR then press SEARCH below. Terms should be specific
258: to the title, author, subject, notes, or abstract information associated
259: with a resource.
260: <br>
1.11 harris41 261: ENDDOCUMENT
262: $r->print(&simpletextfield('basicexp',$ENV{'form.basicexp'}));
263: $r->print(' ');
264: $r->print(&simplecheckbox('titleonly',$ENV{'form.titleonly'}));
265: $r->print('<font color="#800000">Title only</font> ');
266: $r->print(&simplecheckbox('allversions',$ENV{'form.allversions'}));
267: $r->print(<<ENDDOCUMENT);
268: <font color="#800000">Search historic archives</font>
1.8 harris41 269: <br>
1.68 harris41 270: <input type="submit" name="basicsubmit" value='SEARCH' />
271: <input type="reset" name="reset" value='RESET' />
1.46 harris41 272: $closebutton
1.55 harris41 273: $basicviewselect
1.8 harris41 274: </p>
275: <hr>
276: <h3>Advanced Search</h3>
277: $scrout
278: <p>
1.68 harris41 279: <input type="submit" name="advancedsubmit" value='SEARCH' />
280: <input type="reset" name="reset" value='RESET' />
1.46 harris41 281: $closebutton
1.55 harris41 282: $advancedviewselect
1.3 harris41 283: </p>
1.8 harris41 284: </form>
285: </body>
286: </html>
287: ENDDOCUMENT
288: return OK;
289: }
290:
291: # --------------------------------------------------------- Various form fields
292:
1.11 harris41 293: sub simpletextfield {
294: my ($name,$value)=@_;
1.68 harris41 295: return '<input type=text name=\''.$name.
296: '\' size=20 value=\''.$value.'\' />';
1.11 harris41 297: }
298:
299: sub simplecheckbox {
300: my ($name,$value)=@_;
301: my $checked='';
302: $checked="CHECKED" if $value eq 'on';
1.68 harris41 303: return '<input type=checkbox name=\''.$name.'\' '. $checked . '>';
1.11 harris41 304: }
305:
1.8 harris41 306: sub searchphrasefield {
307: my ($title,$name,$value)=@_;
308: my $instruction=<<END;
309: Enter terms or phrases separated by search operators such
310: as AND or OR.
311: END
312: my $uctitle=uc($title);
313: return "\n<p><font color=\"#800000\" face=\"helvetica\"><b>$uctitle:</b>".
314: "</FONT> $instruction<br>".
1.68 harris41 315: '<input type=text name="'.$name.'" size=80 value=\''.$value.'\'>';
1.8 harris41 316: }
1.3 harris41 317:
1.8 harris41 318: sub dateboxes {
1.11 harris41 319: my ($name,$defaultmonth,$defaultday,$defaultyear,
320: $currentmonth,$currentday,$currentyear)=@_;
321: ($defaultmonth,$defaultday,$defaultyear)=('','','');
322: my $month=<<END;
1.8 harris41 323: <select name="${name}_month">
1.11 harris41 324: <option value='$defaultmonth'> </option>
325: <option value="1">January</option>
326: <option value="2">February</option>
327: <option value="3">March</option>
328: <option value="4">April</option>
329: <option value="5">May</option>
330: <option value="6">June</option>
331: <option value="7">July</option>
332: <option value="8">August</option>
333: <option value="9">September</option>
1.3 harris41 334: <option value="10">October</option>
335: <option value="11">November</option>
336: <option value="12">December</option>
337: </select>
1.11 harris41 338: END
339: $month=~s/(\"$currentmonth\")/$1 SELECTED/ if length($currentmonth);
340: my $day=<<END;
1.8 harris41 341: <select name="${name}_day">
1.11 harris41 342: <option value='$defaultday'> </option>
343: <option value="1">1</option>
344: <option value="2">2</option>
345: <option value="3">3</option>
346: <option value="4">4</option>
347: <option value="5">5</option>
348: <option value="6">6</option>
349: <option value="7">7</option>
350: <option value="8">8</option>
351: <option value="9">9</option>
352: <option value="10">10</option>
353: <option value="11">11</option>
354: <option value="12">12</option>
355: <option value="13">13</option>
356: <option value="14">14</option>
357: <option value="15">15</option>
358: <option value="16">16</option>
359: <option value="17">17</option>
360: <option value="18">18</option>
361: <option value="19">19</option>
362: <option value="20">20</option>
363: <option value="21">21</option>
364: <option value="22">22</option>
365: <option value="23">23</option>
366: <option value="24">24</option>
367: <option value="25">25</option>
368: <option value="26">26</option>
369: <option value="27">27</option>
370: <option value="28">28</option>
371: <option value="29">29</option>
372: <option value="30">30</option>
373: <option value="31">31</option>
1.3 harris41 374: </select>
1.11 harris41 375: END
376: $day=~s/(\"$currentday\")/$1 SELECTED/ if length($currentday);
377: my $year=<<END;
1.8 harris41 378: <select name="${name}_year">
1.11 harris41 379: <option value='$defaultyear'> </option>
380: <option value="1976">1976</option>
381: <option value="1977">1977</option>
382: <option value="1978">1978</option>
383: <option value="1979">1979</option>
384: <option value="1980">1980</option>
385: <option value="1981">1981</option>
386: <option value="1982">1982</option>
387: <option value="1983">1983</option>
388: <option value="1984">1984</option>
389: <option value="1985">1985</option>
390: <option value="1986">1986</option>
391: <option value="1987">1987</option>
392: <option value="1988">1988</option>
393: <option value="1989">1989</option>
394: <option value="1990">1990</option>
395: <option value="1991">1991</option>
396: <option value="1992">1992</option>
397: <option value="1993">1993</option>
398: <option value="1994">1994</option>
399: <option value="1995">1995</option>
400: <option value="1996">1996</option>
401: <option value="1997">1997</option>
402: <option value="1998">1998</option>
403: <option value="1999">1999</option>
404: <option value="2000">2000</option>
405: <option value="2001">2001</option>
406: <option value="2002">2002</option>
407: <option value="2003">2003</option>
408: <option value="2004">2004</option>
409: <option value="2005">2005</option>
410: <option value="2006">2006</option>
411: <option value="2007">2007</option>
412: <option value="2008">2008</option>
413: <option value="2009">2009</option>
414: <option value="2010">2010</option>
415: <option value="2011">2011</option>
416: <option value="2012">2012</option>
417: <option value="2013">2013</option>
418: <option value="2014">2014</option>
419: <option value="2015">2015</option>
420: <option value="2016">2016</option>
421: <option value="2017">2017</option>
422: <option value="2018">2018</option>
423: <option value="2019">2019</option>
424: <option value="2020">2020</option>
425: <option value="2021">2021</option>
426: <option value="2022">2022</option>
427: <option value="2023">2023</option>
428: <option value="2024">2024</option>
429: <option value="2025">2025</option>
430: <option value="2026">2026</option>
431: <option value="2027">2027</option>
432: <option value="2028">2028</option>
433: <option value="2029">2029</option>
434: <option value="2030">2030</option>
435: <option value="2031">2031</option>
436: <option value="2032">2032</option>
437: <option value="2033">2033</option>
438: <option value="2034">2034</option>
439: <option value="2035">2035</option>
440: <option value="2036">2036</option>
441: <option value="2037">2037</option>
442: <option value="2038">2038</option>
443: <option value="2039">2039</option>
444: <option value="2040">2040</option>
445: <option value="2041">2041</option>
446: <option value="2042">2042</option>
447: <option value="2043">2043</option>
448: <option value="2044">2044</option>
449: <option value="2045">2045</option>
450: <option value="2046">2046</option>
451: <option value="2047">2047</option>
452: <option value="2048">2048</option>
453: <option value="2049">2049</option>
454: <option value="2050">2050</option>
455: <option value="2051">2051</option>
1.3 harris41 456: </select>
457: END
1.11 harris41 458: $year=~s/(\"$currentyear\")/$1 SELECTED/ if length($currentyear);
459: return "$month$day$year";
1.3 harris41 460: }
461:
462: sub selectbox {
463: my ($title,$name,$value,%options)=@_;
464: my $uctitle=uc($title);
465: my $selout="\n<p><font color=\"#800000\" face=\"helvetica\"><b>$uctitle:".
466: "</b></font><br>".'<select name="'.$name.'">';
467: map {
1.68 harris41 468: $selout.='<option value=\''.$_.'\'';
1.3 harris41 469: if ($_ eq $value) { $selout.=' selected'; }
470: $selout.='>'.$options{$_}.'</option>';
471: } sort keys %options;
472: return $selout.'</select>';
1.6 harris41 473: }
474:
1.45 harris41 475: # ----------------------------------------------- Performing an advanced search
1.18 harris41 476: sub advancedsearch {
477: my ($r,$envhash)=@_;
478: my %ENV=%{$envhash};
479:
1.32 harris41 480: my $fillflag=0;
1.64 harris41 481: # Clean up fields for safety
482: for my $field ('title','author','subject','keywords','url','version',
483: 'creationdatestart_month','creationdatestart_day',
484: 'creationdatestart_year','creationdateend_month',
485: 'creationdateend_day','creationdateend_year',
486: 'lastrevisiondatestart_month','lastrevisiondatestart_day',
487: 'lastrevisiondatestart_year','lastrevisiondateend_month',
488: 'lastrevisiondateend_day','lastrevisiondateend_year',
489: 'notes','abstract','mime','language','owner',
490: 'custommetadata') {
1.68 harris41 491: $ENV{"form.$field"}=~s/[^\w\s\(\)\=\-\"\']//g;
1.64 harris41 492: }
1.32 harris41 493: for my $field ('title','author','subject','keywords','url','version',
494: 'notes','abstract','mime','language','owner',
495: 'custommetadata') {
1.40 harris41 496: if (&filled($ENV{"form.$field"})) {
1.32 harris41 497: $fillflag++;
498: }
499: }
500:
501: unless ($fillflag) {
502: &output_blank_field_error($r);
503: return OK;
504: }
1.39 harris41 505:
506: my $query='';
1.44 harris41 507:
1.45 harris41 508: my @queries;
1.44 harris41 509: # Go through logical expression AND/OR/NOT phrase fields.
1.58 harris41 510:
511: foreach my $field ('title','author','subject','notes','abstract','url',
512: 'keywords','version','owner') {
1.44 harris41 513: if ($ENV{'form.'.$field}) {
1.45 harris41 514: push @queries,&build_SQL_query($field,$ENV{'form.'.$field});
1.44 harris41 515: }
516: }
1.58 harris41 517: if ($ENV{'form.language'} and $ENV{'form.language'} ne 'any') {
1.59 harris41 518: push @queries,"(language like \"\%$ENV{'form.language'}\%\")";
1.58 harris41 519: }
520: if ($ENV{'form.mime'} and $ENV{'form.mime'} ne 'any') {
1.59 harris41 521: push @queries,"(mime like \"\%$ENV{'form.mime'}\%\")";
1.58 harris41 522: }
523: if ($ENV{'form.copyright'} and $ENV{'form.copyright'} ne 'any') {
1.59 harris41 524: push @queries,"(copyright like \"\%$ENV{'form.copyright'}\%\")";
1.58 harris41 525: }
1.60 harris41 526: my $datequery=&build_date_queries(
527: $ENV{'form.creationdatestart_month'},
528: $ENV{'form.creationdatestart_day'},
529: $ENV{'form.creationdatestart_year'},
530: $ENV{'form.creationdateend_month'},
531: $ENV{'form.creationdateend_day'},
532: $ENV{'form.creationdateend_year'},
533: $ENV{'form.lastrevisiondatestart_month'},
534: $ENV{'form.lastrevisiondatestart_day'},
535: $ENV{'form.lastrevisiondatestart_year'},
536: $ENV{'form.lastrevisiondateend_month'},
537: $ENV{'form.lastrevisiondateend_day'},
538: $ENV{'form.lastrevisiondateend_year'},
539: );
1.61 harris41 540: if ($datequery=~/^Incorrect/) {
541: &output_date_error($r,$datequery);
542: return OK;
543: }
544: elsif ($datequery) {
1.60 harris41 545: push @queries,$datequery;
546: }
1.76 ! harris41 547: my $customquery='';
1.64 harris41 548: if ($ENV{'form.custommetadata'}) {
549: $customquery=&build_custommetadata_query('custommetadata',
550: $ENV{'form.custommetadata'});
551: }
1.45 harris41 552: if (@queries) {
1.58 harris41 553: $query=join(" AND ",@queries);
1.46 harris41 554: $query="select * from metadata where $query";
1.76 ! harris41 555: my $reply='';
! 556: unless ($customquery) {
! 557: $reply=&Apache::lonnet::metadata_query($query);
! 558: }
! 559: else {
! 560: $reply=&Apache::lonnet::metadata_query($query,$customquery);
! 561: }
1.64 harris41 562: &output_results('Advanced',$r,$envhash,$customquery,$reply);
1.45 harris41 563: }
564: else {
565: &output_results('Advanced',$r,$envhash,$query);
566: }
1.24 harris41 567: return OK;
1.18 harris41 568: }
569:
1.26 harris41 570: # ---------------------------------------------------- see if a field is filled
571: sub filled {
1.31 harris41 572: my ($field)=@_;
573: if ($field=~/\S/) {
1.28 harris41 574: return 1;
575: }
576: else {
577: return 0;
578: }
1.26 harris41 579: }
580:
1.6 harris41 581: # --------------------------------------------------- Performing a basic search
582: sub basicsearch {
1.19 harris41 583: my ($r,$envhash)=@_;
584: my %ENV=%{$envhash};
1.6 harris41 585:
1.64 harris41 586: # Clean up fields for safety
587: for my $field ('basicexp') {
588: $ENV{"form.$field"}=~s/[^\w\s\(\)\-]//g;
589: }
590:
1.26 harris41 591: unless (&filled($ENV{'form.basicexp'})) {
1.24 harris41 592: &output_blank_field_error($r);
593: return OK;
594: }
1.22 harris41 595:
1.39 harris41 596: my $query='';
1.33 harris41 597: my $concatarg=join('," ",',
598: ('title', 'author', 'subject', 'notes', 'abstract'));
599:
600: $query="select * from metadata where concat($concatarg) like '\%$ENV{'form.basicexp'}\%'";
1.13 harris41 601: my $reply=&Apache::lonnet::metadata_query($query);
1.44 harris41 602: &output_results('Basic',$r,$envhash,$query,$reply);
1.18 harris41 603: return OK;
1.22 harris41 604: }
605:
1.44 harris41 606: # ---------------- Message to output when there are not enough fields filled in
1.22 harris41 607: sub output_blank_field_error {
608: my ($r)=@_;
609: # make query information persistent to allow for subsequent revision
1.65 harris41 610: my $persistent=&make_persistent();
1.22 harris41 611:
612: $r->print(<<BEGINNING);
613: <html>
614: <head>
615: <title>The LearningOnline Network with CAPA</title>
616: BEGINNING
617: $r->print(<<RESULTS);
618: </head>
619: <body bgcolor="#ffffff">
620: <img align=right src=/adm/lonIcons/lonlogos.gif>
621: <h1>Search Catalog</h1>
622: <form method="post" action="/adm/searchcat">
623: $persistent
624: <input type='button' value='Revise search request'
625: onClick='this.form.submit();'>
1.46 harris41 626: $closebutton
1.22 harris41 627: <hr>
628: <h3>Helpful Message</h3>
629: <p>
630: Incorrect search query due to blank entry fields.
631: You need to fill in the relevant
632: fields on the search page in order for a query to be
633: processed.
634: </p>
635: </body>
636: </html>
637: RESULTS
1.18 harris41 638: }
1.6 harris41 639:
1.18 harris41 640: # ----------------------------- format and output results based on a reply list
641: sub output_results {
1.44 harris41 642: my ($mode,$r,$envhash,$query,@replylist)=@_;
1.19 harris41 643: my %ENV=%{$envhash};
1.44 harris41 644: my $compiledresult='';
645:
1.18 harris41 646: foreach my $reply (@replylist) {
647:
648: my @results;
649:
650: my $replyfile='';
651: $reply=~/^([\.\w]+)$/; # must do since 'use strict' checks for tainting
652: $replyfile=$r->dir_config('lonDaemons').'/tmp/'.$1;
653: $reply=~/(.*?)\_/;
654: my $hostname=$1;
1.52 harris41 655: sleep 3; # temporary fix, need to check for completion and status
1.18 harris41 656: {
657: while (1) {
658: last if -e $replyfile;
659: sleep 1;
660: }
661: # QUESTION: how should I handle this error condition..
662: # I'm sure there is syntax elsewhere I can use..
663: my $fh=Apache::File->new($replyfile) or
664: ($r->print('file cannot be opened') and return OK);
665: @results=<$fh>;
1.6 harris41 666: }
667:
1.18 harris41 668: foreach my $result (@results) {
1.50 harris41 669: my @fields=map
670: {&Apache::lonnet::unescape($_)}
671: (split(/\,/,$result));
1.18 harris41 672: my ($title,$author,$subject,$url,$keywords,$version,
673: $notes,$abstract,$mime,$lang,
1.50 harris41 674: $creationdate,$lastrevisiondate,$owner,$copyright)=@fields;
1.18 harris41 675: my $shortabstract=$abstract;
676: $shortabstract=substr($abstract,0,200) if length($abstract)>200;
1.51 harris41 677: $fields[7]=$shortabstract;
1.18 harris41 678: $compiledresult.=<<END;
1.56 harris41 679: <p>
1.8 harris41 680: END
1.18 harris41 681: $compiledresult.=<<END if $ENV{'form.catalogmode'} eq 'interactive';
1.8 harris41 682: <font size='-1'><INPUT TYPE="button" NAME="returnvalues" VALUE="SELECT"
1.10 harris41 683: onClick="javascript:select_data('$title','$url')">
1.8 harris41 684: </font>
685: <br>
686: END
1.51 harris41 687: my $httphost=$ENV{'HTTP_HOST'};
1.53 harris41 688:
1.55 harris41 689: my $viewselect;
690: if ($mode eq 'Basic') {
691: $viewselect=$ENV{'form.basicviewselect'};
692: }
693: elsif ($mode eq 'Advanced') {
694: $viewselect=$ENV{'form.advancedviewselect'};
695: }
696:
697: if ($viewselect eq 'Detailed Citation View') {
1.51 harris41 698: $compiledresult.=&detailed_citation_view(@fields,
699: $hostname,$httphost);
1.50 harris41 700: }
1.55 harris41 701: elsif ($viewselect eq 'Summary View') {
1.51 harris41 702: $compiledresult.=&summary_view(@fields,$hostname,$httphost);
1.50 harris41 703: }
1.55 harris41 704: elsif ($viewselect eq 'Fielded Format') {
1.51 harris41 705: $compiledresult.=&fielded_format_view(@fields,$hostname,
706: $httphost);
1.50 harris41 707: }
1.55 harris41 708: elsif ($viewselect eq 'XML/SGML') {
1.51 harris41 709: $compiledresult.=&xml_sgml_view(@fields,$hostname,$httphost);
1.50 harris41 710: }
711:
1.18 harris41 712: }
1.6 harris41 713:
1.18 harris41 714: unless ($compiledresult) {
715: $compiledresult="There were no results that matched your query";
716: }
1.6 harris41 717:
1.18 harris41 718: # make query information persistent to allow for subsequent revision
1.65 harris41 719: my $persistent=&make_persistent();
1.9 harris41 720:
1.18 harris41 721: $r->print(<<BEGINNING);
1.6 harris41 722: <html>
723: <head>
724: <title>The LearningOnline Network with CAPA</title>
1.8 harris41 725: BEGINNING
1.18 harris41 726: $r->print(<<SCRIPT) if $ENV{'form.catalogmode'} eq 'interactive';
1.8 harris41 727: <script>
728: function select_data(title,url) {
729: changeTitle(title);
730: changeURL(url);
731: }
732: function changeTitle(val) {
733: if (opener.inf.document.forms.resinfo.elements.t) {
734: opener.inf.document.forms.resinfo.elements.t.value=val;
735: }
736: }
737: function changeURL(val) {
738: if (opener.inf.document.forms.resinfo.elements.u) {
739: opener.inf.document.forms.resinfo.elements.u.value=val;
740: }
741: }
742: </script>
743: SCRIPT
1.18 harris41 744: $r->print(<<RESULTS);
1.6 harris41 745: </head>
746: <body bgcolor="#ffffff">
747: <img align=right src=/adm/lonIcons/lonlogos.gif>
748: <h1>Search Catalog</h1>
749: <form method="post" action="/adm/searchcat">
1.9 harris41 750: <input type='button' value='Revise search request'
751: onClick='this.form.submit();'>
1.46 harris41 752: $closebutton
1.9 harris41 753: $persistent
1.6 harris41 754: <hr>
755: <h3>Search Query</h3>
1.43 harris41 756: RESULTS
757: if ($mode eq 'Basic') {
758: $r->print(<<RESULTS);
1.6 harris41 759: <p>
1.19 harris41 760: <b>Basic search:</b> $ENV{'form.basicexp'}
1.6 harris41 761: </p>
1.43 harris41 762: RESULTS
1.44 harris41 763: }
1.43 harris41 764: elsif ($mode eq 'Advanced') {
765: $r->print(<<RESULTS);
766: <p>
767: <b>Advanced search</b>
1.44 harris41 768: $query
1.43 harris41 769: </p>
770: RESULTS
771: }
1.44 harris41 772: $r->print(<<RESULTS);
1.6 harris41 773: <h3>Search Results</h3>
774: $compiledresult
775: </body>
776: </html>
777: RESULTS
1.18 harris41 778: }
1.41 harris41 779: }
780:
781: # ------------------------------------------------------------- build_SQL_query
782: sub build_SQL_query {
1.43 harris41 783: my ($field_name,$logic_statement)=@_;
784: my $q=new Text::Query('abc',
785: -parse => 'Text::Query::ParseAdvanced',
786: -build => 'Text::Query::Build');
1.44 harris41 787: $q->prepare($logic_statement);
1.43 harris41 788: my $matchexp=${$q}{'matchexp'}; chomp $matchexp;
789: my $sql_query=&recursive_SQL_query_build($field_name,$matchexp);
1.44 harris41 790: return $sql_query;
1.43 harris41 791: }
1.41 harris41 792:
1.64 harris41 793: # ------------------------------------------------- build custom metadata query
794: sub build_custommetadata_query {
795: my ($field_name,$logic_statement)=@_;
796: my $q=new Text::Query('abc',
797: -parse => 'Text::Query::ParseAdvanced',
798: -build => 'Text::Query::BuildAdvancedString');
799: $q->prepare($logic_statement);
800: my $matchexp=${$q}{'-parse'}{'-build'}{'matchstring'};
1.69 harris41 801: # quick fix to change literal into xml tag-matching
1.75 harris41 802: # will eventually have to write a separate builder module
1.72 harris41 803: my $oldmatchexp=$matchexp;
1.75 harris41 804: $matchexp=~s/(\w+)\\\=(\w+)/\\\<$1\\\>\[\^\\\<\]\*$2\[\^\\\<\]\*\\\<\\\/$1\\\>/g;
1.76 ! harris41 805: return $matchexp;
1.64 harris41 806: }
807:
1.43 harris41 808: # - Recursively parse a reverse notation expression into a SQL query expression
809: sub recursive_SQL_query_build {
810: my ($dkey,$pattern)=@_;
811: my @matches=($pattern=~/(\[[^\]|\[]*\])/g);
812: return $pattern unless @matches;
813: foreach my $match (@matches) {
814: $match=~/\[ (\w+)\s(.*) \]/;
1.44 harris41 815: my ($key,$value)=($1,$2);
1.43 harris41 816: my $replacement='';
817: if ($key eq 'literal') {
818: $replacement="($dkey like \"\%$value\%\")";
819: }
820: elsif ($key eq 'and') {
821: $value=~/(.*[\"|\)]) ([|\(|\^].*)/;
822: $replacement="($1 AND $2)";
823: }
824: elsif ($key eq 'or') {
825: $value=~/(.*[\"|\)]) ([|\(|\^].*)/;
826: $replacement="($1 OR $2)";
827: }
828: substr($pattern,
829: index($pattern,$match),
830: length($match),
831: $replacement
832: );
833: }
834: &recursive_SQL_query_build($dkey,$pattern);
1.50 harris41 835: }
836:
837: # ------------------------------------------------------ Detailed Citation View
838: sub detailed_citation_view {
839: my ($title,$author,$subject,$url,$keywords,$version,
1.51 harris41 840: $notes,$shortabstract,$mime,$lang,
841: $creationdate,$lastrevisiondate,$owner,$copyright,
842: $hostname,$httphost)=@_;
1.50 harris41 843: my $result=<<END;
1.56 harris41 844: <i>$owner</i>, last revised $lastrevisiondate
845: <h3><A HREF="http://$httphost$url" TARGET='search_preview'>$title</A></h3>
846: <h3>$author</h3>
847: </p>
848: <p>
1.50 harris41 849: <b>Subject:</b> $subject<br>
850: <b>Keyword(s):</b> $keywords<br>
851: <b>Notes:</b> $notes<br>
852: <b>MIME Type:</b> $mimetag{$mime}<br>
853: <b>Language:</b> $language{$lang}<br>
1.56 harris41 854: <b>Copyright/Distribution:</b> $cprtag{$copyright}<br>
855: $shortabstract
1.50 harris41 856: </p>
857: END
858: return $result;
859: }
860:
861: # ---------------------------------------------------------------- Summary View
862: sub summary_view {
863: my ($title,$author,$subject,$url,$keywords,$version,
1.51 harris41 864: $notes,$shortabstract,$mime,$lang,
865: $creationdate,$lastrevisiondate,$owner,$copyright,
866: $hostname,$httphost)=@_;
1.50 harris41 867: my $result=<<END;
1.56 harris41 868: <a href="http://$httphost$url" TARGET='search_preview'>$author</a><br />
869: $title<br />
870: $owner -- $lastrevisiondate<br />
871: $cprtag{$copyright}<br />
1.50 harris41 872: </p>
873: END
874: return $result;
875: }
876:
877: # -------------------------------------------------------------- Fielded Format
878: sub fielded_format_view {
879: my ($title,$author,$subject,$url,$keywords,$version,
1.51 harris41 880: $notes,$shortabstract,$mime,$lang,
881: $creationdate,$lastrevisiondate,$owner,$copyright,
882: $hostname,$httphost)=@_;
1.50 harris41 883: my $result=<<END;
1.51 harris41 884: <b>URL: </b> <A HREF="http://$httphost$url" TARGET='search_preview'>$url</A>
1.56 harris41 885: <br />
886: <b>Title:</b> $title<br />
887: <b>Author(s):</b> $author<br />
888: <b>Subject:</b> $subject<br />
889: <b>Keyword(s):</b> $keywords<br />
890: <b>Notes:</b> $notes<br />
891: <b>MIME Type:</b> $mimetag{$mime}<br />
892: <b>Language:</b> $language{$lang}<br />
893: <b>Creation Date:</b> $creationdate<br />
894: <b>Last Revision Date:</b> $lastrevisiondate<br />
895: <b>Publisher/Owner:</b> $owner<br />
896: <b>Copyright/Distribution:</b> $cprtag{$copyright}<br />
897: <b>Repository Location:</b> $hostname<br />
898: <b>Abstract:</b> $shortabstract<br />
1.50 harris41 899: </p>
900: END
901: return $result;
902: }
903:
904: # -------------------------------------------------------------------- XML/SGML
905: sub xml_sgml_view {
906: my ($title,$author,$subject,$url,$keywords,$version,
1.51 harris41 907: $notes,$shortabstract,$mime,$lang,
908: $creationdate,$lastrevisiondate,$owner,$copyright,
909: $hostname,$httphost)=@_;
1.50 harris41 910: my $result=<<END;
1.56 harris41 911: <pre>
912: <LonCapaResource>
1.57 harris41 913: <url>$url</url>
1.56 harris41 914: <title>$title</title>
915: <author>$author</author>
916: <subject>$subject</subject>
917: <keywords>$keywords</keywords>
918: <notes>$notes</notes>
919: <mimeInfo>
920: <mime>$mime</mime>
921: <mimetag>$mimetag{$mime}</mimetag>
922: </mimeInfo>
923: <languageInfo>
924: <language>$lang</language>
925: <languagetag>$language{$lang}</languagetag>
926: </languageInfo>
927: <creationdate>$creationdate</creationdate>
928: <lastrevisiondate>$lastrevisiondate</lastrevisiondate>
929: <owner>$owner</owner>
930: <copyrightInfo>
931: <copyright>$copyright</copyright>
932: <copyrighttag>$cprtag{$copyright}</copyrighttag>
933: </copyrightInfo>
934: <repositoryLocation>$hostname</repositoryLocation>
935: <shortabstract>$shortabstract</shortabstract>
1.57 harris41 936: </LonCapaResource>
1.56 harris41 937: </pre>
1.50 harris41 938: END
939: return $result;
1.60 harris41 940: }
941:
942: sub build_date_queries {
943: my ($cmonth1,$cday1,$cyear1,$cmonth2,$cday2,$cyear2,
944: $lmonth1,$lday1,$lyear1,$lmonth2,$lday2,$lyear2)=@_;
1.61 harris41 945: my @queries;
946: if ($cmonth1 or $cday1 or $cyear1 or $cmonth2 or $cday2 or $cyear2) {
947: unless ($cmonth1 and $cday1 and $cyear1 and
948: $cmonth2 and $cday2 and $cyear2) {
949: return "Incorrect entry for the creation date. You must specify ".
950: "a starting month, day, and year and an ending month, ".
951: "day, and year.";
952: }
1.63 harris41 953: my $cnumeric1=sprintf("%d%2d%2d",$cyear1,$cmonth1,$cday1);
1.61 harris41 954: $cnumeric1+=0;
1.63 harris41 955: my $cnumeric2=sprintf("%d%2d%2d",$cyear2,$cmonth2,$cday2);
1.61 harris41 956: $cnumeric2+=0;
957: if ($cnumeric1>$cnumeric2) {
958: return "Incorrect entry for the creation date. The starting ".
959: "date must occur before the ending date.";
960: }
961: my $cquery="(creationdate BETWEEN '$cyear1-$cmonth1-$cday1' AND '".
962: "$cyear2-$cmonth2-$cday2 23:59:59')";
963: push @queries,$cquery;
964: }
965: if ($lmonth1 or $lday1 or $lyear1 or $lmonth2 or $lday2 or $lyear2) {
966: unless ($lmonth1 and $lday1 and $lyear1 and
967: $lmonth2 and $lday2 and $lyear2) {
968: return "Incorrect entry for the last revision date. You must ".
969: "specify a starting month, day, and year and an ending ".
970: "month, day, and year.";
1.62 harris41 971: }
1.63 harris41 972: my $lnumeric1=sprintf("%d%2d%2d",$lyear1,$lmonth1,$lday1);
1.61 harris41 973: $lnumeric1+=0;
1.63 harris41 974: my $lnumeric2=sprintf("%d%2d%2d",$lyear2,$lmonth2,$lday2);
1.61 harris41 975: $lnumeric2+=0;
976: if ($lnumeric1>$lnumeric2) {
977: return "Incorrect entry for the last revision date. The ".
978: "starting date must occur before the ending date.";
979: }
980: my $lquery="(lastrevisiondate BETWEEN '$lyear1-$lmonth1-$lday1' AND '".
981: "$lyear2-$lmonth2-$lday2 23:59:59')";
982: push @queries,$lquery;
983: }
984: if (@queries) {
985: return join(" AND ",@queries);
986: }
987: return '';
1.60 harris41 988: }
989:
990: sub output_date_error {
991: my ($r,$message)=@_;
992: # make query information persistent to allow for subsequent revision
1.65 harris41 993: my $persistent=&make_persistent();
1.60 harris41 994:
995: $r->print(<<BEGINNING);
996: <html>
997: <head>
998: <title>The LearningOnline Network with CAPA</title>
999: BEGINNING
1000: $r->print(<<RESULTS);
1001: </head>
1002: <body bgcolor="#ffffff">
1003: <img align=right src=/adm/lonIcons/lonlogos.gif>
1004: <h1>Search Catalog</h1>
1005: <form method="post" action="/adm/searchcat">
1006: $persistent
1007: <input type='button' value='Revise search request'
1008: onClick='this.form.submit();'>
1009: $closebutton
1010: <hr>
1011: <h3>Helpful Message</h3>
1012: <p>
1013: $message
1014: </p>
1015: </body>
1016: </html>
1017: RESULTS
1.3 harris41 1018: }
1.1 www 1019:
1.64 harris41 1020: sub make_persistent {
1.65 harris41 1021: my $persistent='';
1.66 harris41 1022:
1.65 harris41 1023: map {
1024: if (/^form\./ && !/submit/) {
1025: my $name=$_;
1026: my $key=$name;
1.69 harris41 1027: $ENV{$key}=~s/\'//g; # do not mess with html field syntax
1.65 harris41 1028: $name=~s/^form\.//;
1029: $persistent.=<<END;
1.68 harris41 1030: <input type='hidden' name='$name' value='$ENV{$key}' />
1.65 harris41 1031: END
1032: }
1033: } (keys %ENV);
1034: return $persistent;
1.64 harris41 1035: }
1.1 www 1036: 1;
1037: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>