File:  [LON-CAPA] / loncom / interface / lonsearchcat.pm
Revision 1.104: download - view: text, annotated - select for diffs
Wed Nov 28 14:02:31 2001 UTC (22 years, 6 months ago) by harris41
Branches: MAIN
CVS tags: HEAD
causing to use a distinct GDBM resource (*_searchcat.db) -Scott

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>