File:  [LON-CAPA] / loncom / interface / lonindexer.pm
Revision 1.67: download - view: text, annotated - select for diffs
Mon Jun 16 17:27:33 2003 UTC (21 years ago) by matthew
Branches: MAIN
CVS tags: HEAD
Bug: 1615.  No longer keep hash tied throught network calls.  Copy all
of the db file into memory, do the lonindexer stuff, copy it back to the
filesystem.  No longer 'untie(%hash)' on errors since the hash is not tied.

    1: # The LearningOnline Network with CAPA
    2: # Directory Indexer
    3: #
    4: # $Id: lonindexer.pm,v 1.67 2003/06/16 17:27:33 matthew Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: # YEAR=1999
   29: # 5/21/99, 5/22,5/25,5/26,5/31,6/2,6/10,7/12,7/14 Gerd Kortemeyer)
   30: # 11/23 Gerd Kortemeyer
   31: # YEAR=2000
   32: # 07/20-08/04 H.K. Ng
   33: # YEAR=2001
   34: # 05/9-05/19/2001 H. K. Ng
   35: # 05/21/2001 H. K. Ng
   36: # 05/23/2001 H. K. Ng
   37: # 6/26,7/8 H. K. Ng
   38: # 8/14 H. K. Ng
   39: # 11/30 Matthew Hall
   40: # YEAR=2002
   41: # 6/29/2002 H. K. Ng
   42: #
   43: ###
   44: 
   45: ###############################################################################
   46: ##                                                                           ##
   47: ## ORGANIZATION OF THIS PERL MODULE                                          ##
   48: ##                                                                           ##
   49: ## 1. Description of functions                                               ##
   50: ## 2. Modules used by this module                                            ##
   51: ## 3. Choices for different output views (detailed, summary, xml, etc)       ##
   52: ## 4. BEGIN block (to be run once after compilation)                         ##
   53: ## 5. Handling routine called via Apache and mod_perl                        ##
   54: ## 6. Other subroutines                                                      ##
   55: ##                                                                           ##
   56: ###############################################################################
   57: 
   58: package Apache::lonindexer;
   59: 
   60: # ------------------------------------------------- modules used by this module
   61: use strict;
   62: use Apache::lonnet();
   63: use Apache::loncommon();
   64: use Apache::Constants qw(:common);
   65: use Apache::File;
   66: use GDBM_File;
   67: 
   68: # ---------------------------------------- variables used throughout the module
   69: my %hash; # tied to a user-specific gdbm file
   70: my %dirs; # keys are directories, values are the open/close status
   71: my %language; # has the reference information present in language.tab
   72: 
   73: # ----- Values which are set by the handler subroutine and are accessible to
   74: # -----     other methods.
   75: my $extrafield; # default extra table cell
   76: my $fnum; # file counter
   77: my $dnum; # directory counter
   78: 
   79: # ----- Used to include or exclude files with certain extensions.
   80: my @Only = ();
   81: my @Omit = ();
   82: 
   83: 
   84: # ----------------------------- Handling routine called via Apache and mod_perl
   85: sub handler {
   86:     my $r = shift;
   87:     my $c = $r->connection();
   88:     $r->content_type('text/html');
   89:     &Apache::loncommon::no_cache($r);
   90:     $r->send_http_header;
   91:     return OK if $r->header_only;
   92:     $fnum=0;
   93:     $dnum=0;
   94: 
   95:     # Deal with stupid global variables (is there a way around making
   96:     # these global to this package?  It is just so wrong....)
   97:     undef (@Only);
   98:     undef (@Omit);
   99: 
  100: # ------------------------------------- read in machine configuration variables
  101:     my $iconpath= $r->dir_config('lonIconsURL') . "/";
  102:     my $domain  = $r->dir_config('lonDefDomain');
  103:     my $role    = $r->dir_config('lonRole');
  104:     my $loadlim = $r->dir_config('lonLoadLim');
  105:     my $servadm = $r->dir_config('lonAdmEMail');
  106:     my $sysadm  = $r->dir_config('lonSysEMail');
  107:     my $lonhost = $r->dir_config('lonHostID');
  108:     my $tabdir  = $r->dir_config('lonTabDir');
  109: 
  110:     my $fileclr='#ffffe6';
  111:     my $line;
  112:     my (@attrchk,@openpath);
  113:     my $uri=$r->uri;
  114: 
  115: # -------------------------------------- see if called from an interactive mode
  116:     # Get the parameters from the query string
  117:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  118: 	     ['catalogmode','launch','acts','mode','form','element',
  119:               'only','omit']);
  120:     #-------------------------------------------------------------------
  121:     my $closebutton='';
  122:     my $groupimportbutton='';
  123:     my $colspan=''; 
  124: 
  125:     $extrafield='';
  126:     my $diropendb = 
  127: 	"/home/httpd/perl/tmp/$ENV{'user.domain'}_$ENV{'user.name'}_indexer.db";
  128:     %hash = ();
  129:     my %dbfile;
  130:     if (tie(%dbfile,'GDBM_File',$diropendb,&GDBM_WRCREAT(),0640)) {
  131:         while(my($key,$value)=each(%dbfile)) {
  132:             $hash{$key}=$value;
  133:         }
  134:         untie(%dbfile);
  135: 
  136: 	if ($ENV{'form.launch'} eq '1') {
  137: 	    &start_fresh_session();
  138:         }
  139: # -------------------- refresh environment with user database values (in %hash)
  140: 	&setvalues(\%hash,'form.catalogmode',\%ENV,'form.catalogmode'   );
  141: 
  142: # --------------------- define extra fields and buttons in case of special mode
  143: 	if ($ENV{'form.catalogmode'} eq 'interactive') {
  144: 	    $extrafield='<td bgcolor="'.$fileclr.'" valign="bottom">'.
  145: 		'<a name="$anchor"><img src="'.$iconpath.'whitespace1.gif"'.
  146: 		' border="0" /></td>';
  147: 	    $colspan=" colspan='2' ";
  148:             $closebutton=<<END;
  149: <input type="button" name="close" value='CLOSE' onClick="self.close()">
  150: END
  151:         }
  152: 	elsif ($ENV{'form.catalogmode'} eq 'groupimport') {
  153: 	    $extrafield='<td bgcolor="'.$fileclr.'" valign="bottom">'.
  154: 		'<a name="$anchor"><img src="'.$iconpath.'whitespace1.gif"'.
  155: 		' border="0" /></td>';
  156: 	    $colspan=" colspan='2' ";
  157:             $closebutton=<<END;
  158: <input type="button" name="close" value='CLOSE' onClick="self.close()">
  159: END
  160:             $groupimportbutton=<<END;
  161: <input type="button" name="groupimport" value='GROUP IMPORT'
  162: onClick="javascript:select_group()">
  163: END
  164:         }
  165: 	# Additions made by Matthew to make the browser a little easier to deal
  166: 	# with in the future.
  167: 	#
  168: 	# $mode (at this time) indicates if we are in edit mode.
  169: 	# $form is the name of the form that the URL is placed when the
  170: 	#       selection is made.
  171: 	# $element is the name of the element in $formname which receives
  172: 	#       the URL.
  173: 	# &Apache::lonxml::debug('Checking mode, form, element');
  174: 	&setvalues(\%hash,'form.mode'   ,\%ENV,'form.mode'   );
  175: 	&setvalues(\%hash,'form.form'   ,\%ENV,'form.form'   );
  176: 	&setvalues(\%hash,'form.element',\%ENV,'form.element');
  177: 	&setvalues(\%hash,'form.only'   ,\%ENV,'form.only'   );
  178: 	&setvalues(\%hash,'form.omit'   ,\%ENV,'form.omit'   );
  179: 
  180:         # Deal with 'omit' and 'only' 
  181:         if (exists $ENV{'form.omit'}) {
  182:             @Omit = split(',',$ENV{'form.omit'});
  183:         }
  184:         if (exists $ENV{'form.only'}) {
  185:             @Only = split(',',$ENV{'form.only'});
  186:         }
  187:         
  188: 	my $mode = $ENV{'form.mode'};
  189: 	my ($form,$element);
  190: 	if ($mode eq 'edit' || $mode eq 'parmset') {
  191: 	    $form    = $ENV{'form.form'};
  192: 	    $element = $ENV{'form.element'};
  193: 	}
  194: 	&Apache::lonxml::debug("mode=$mode form=$form element=$element");
  195: # ------ set catalogmodefunctions to have extra needed javascript functionality
  196: 	my $catalogmodefunctions='';
  197: 	if ($ENV{'form.catalogmode'} eq 'interactive' or
  198: 	    $ENV{'form.catalogmode'} eq 'groupimport') {
  199: 	    # The if statement below sets us up to use the old version
  200: 	    # by default (ie. if $mode is undefined).  This is the easy
  201: 	    # way out.  Hopefully in the future I'll find a way to get 
  202: 	    # the calls dealt with in a more comprehensive manner.
  203: 
  204: #
  205: # There is now also mode "simple", which is for the simple version of the rat
  206: #
  207: #
  208: 	    if (!defined($mode) || ($mode ne 'edit' && $mode ne 'parmset')) {
  209:                 my $location = "/adm/groupsort?catalogmode=groupimport&";
  210:                 $location .= "mode=".$mode."&";
  211:                 $location .= "acts=";
  212: 		$catalogmodefunctions=<<"END";
  213: function select_data(title,url) {
  214:     changeTitle(title);
  215:     changeURL(url);
  216:     self.close();
  217: }
  218: function select_group() {
  219:     window.location="$location"+document.forms.fileattr.acts.value;
  220: }
  221: function changeTitle(val) {
  222:     if (opener.inf) {
  223:         if (opener.inf.document.forms.resinfo.elements.t) {
  224:             opener.inf.document.forms.resinfo.elements.t.value=val;
  225:         }
  226:     }
  227: }
  228: function changeURL(val) {
  229:     if (opener.inf) {
  230:         if (opener.inf.document.forms.resinfo.elements.u) {
  231: 	    opener.inf.document.forms.resinfo.elements.u.value=val;
  232:         }
  233:     }
  234: }
  235: END
  236:             } elsif ($mode eq 'edit') { # we are in 'edit' mode
  237:                 my $location = "/adm/groupsort?catalogmode=interactive&";
  238:                 $location .= "form=$form&element=$element&mode=edit&acts=";
  239: 		$catalogmodefunctions=<<END;
  240: // mode = $mode
  241: function select_data(title,url) {
  242:     changeURL(url);
  243:     self.close();
  244: }
  245: 
  246: function select_group() {
  247:     window.location="$location"+document.forms.fileattr.acts.value;
  248: }
  249: 
  250: function changeURL(val) {
  251:     if (window.opener.document) {
  252: 	window.opener.document.forms["$form"].elements["$element"].value=val;
  253:     } else {
  254: 	    alert("The file you selected is: "+val);
  255:     }
  256: }
  257: 
  258: END
  259:             } elsif ($mode eq 'parmset') {
  260:                 my $location = "/adm/groupsort?catalogmode=interactive&";
  261:                 $location .= "form=$form&element=$element&mode=parmset&acts=";
  262: 		$catalogmodefunctions=<<END;
  263: // mode = $mode
  264: function select_data(title,url) {
  265:     changeURL(url);
  266:     self.close();
  267: }
  268: 
  269: function select_group() {
  270:     window.location="$location"+document.forms.fileattr.acts.value;
  271: }
  272: 
  273: function changeURL(val) {
  274:     if (window.opener.document) {
  275:         var elementname  = "$element"+"_value";
  276:         var checkboxname = "$element"+"_setparmval";
  277: 	window.opener.document.forms["$form"].elements[elementname].value=val;
  278:         window.opener.document.forms["$form"].elements[checkboxname].checked=true;
  279:     } else {
  280: 	    alert("The file you selected is: "+val);
  281:     }
  282: }
  283: 
  284: END
  285:             }
  286:         }
  287:         $catalogmodefunctions.=<<END;
  288: var acts='';
  289: function rep_dirpath(suffix,val) {
  290:     eval("document.forms.dirpath"+suffix+".acts.value=val");
  291: }
  292: END
  293: 	if ($ENV{'form.catalogmode'} eq 'groupimport') {
  294:             $catalogmodefunctions.=<<END;
  295: function queue(val) {
  296:     if (eval("document.forms."+val+".filelink.checked")) {
  297: 	var l=val.length;
  298: 	var v=val.substring(4,l);
  299: 	document.forms.fileattr.acts.value+='1a'+v+'b';
  300:     }
  301:     else {
  302: 	var l=val.length;
  303: 	var v=val.substring(4,l);
  304: 	document.forms.fileattr.acts.value+='0a'+v+'b';
  305:     }
  306: }
  307: END
  308: 	}
  309: 
  310: # ---------------------------------------------------------------- Print Header
  311: 	$r->print(<<ENDHEADER);
  312: <html>
  313: <head>
  314: <title>The LearningOnline Network With CAPA Directory Browser</title>
  315: 
  316: <script type="text/javascript">
  317: $catalogmodefunctions
  318: function openWindow(url, wdwName, w, h, toolbar,scrollbar) {
  319:     var options = "width=" + w + ",height=" + h + ",";
  320:     options += "resizable=yes,scrollbars="+scrollbar+",status=no,";
  321:     options += "menubar=no,toolbar="+toolbar+",location=no,directories=no";
  322:     var newWin = window.open(url, wdwName, options);
  323:     newWin.focus();
  324: }
  325: function gothere(val) {
  326:     window.location=val+'?acts='+document.forms.fileattr.acts.value;
  327: }
  328: </script>
  329: 
  330: </head>
  331: ENDHEADER
  332: $r->print(&Apache::loncommon::bodytag('Browse Resources'));
  333: # - Evaluate actions from previous page (both cumulatively and chronologically)
  334:         if ($ENV{'form.catalogmode'} eq 'groupimport') {
  335: 	    my $acts=$ENV{'form.acts'};
  336: 	    my @Acts=split(/b/,$acts);
  337: 	    my %ahash;
  338: 	    my %achash;
  339: 	    my $ac=0;
  340: 	    # some initial hashes for working with data
  341: 	    foreach (@Acts) {
  342: 		my ($state,$ref)=split(/a/);
  343: 		$ahash{$ref}=$state;
  344: 		$achash{$ref}=$ac;
  345: 		$ac++;
  346: 	    }
  347: 	    # sorting through the actions and changing the tied database hash
  348: 	    foreach (sort {$achash{$a}<=>$achash{$b}} (keys %ahash)) {
  349: 		my $key=$_;
  350: 		if ($ahash{$key} eq '1') {
  351: 		    $hash{'store_'.$hash{'pre_'.$key.'_link'}}=
  352: 			$hash{'pre_'.$key.'_title'};
  353: 		    $hash{'storectr_'.$hash{'pre_'.$key.'_link'}}=
  354: 			$hash{'storectr'}+0;
  355: 		    $hash{'storectr'}++;
  356: 		}
  357: 		if ($ahash{$key} eq '0') {
  358: 		    if ($hash{'store_'.$hash{'pre_'.$key.'_link'}}) {
  359: 			delete $hash{'store_'.$hash{'pre_'.$key.'_link'}};
  360: 		    }
  361: 		}
  362: 	    }
  363: 	    # deleting the previously cached listing
  364: 	    foreach (keys %hash) {
  365: 		if ($_ =~ /^pre_/ && $_ =~/link$/) {
  366: 		    my $key = $_;
  367: 		    $key =~ s/^pre_//;
  368: 		    $key =~ s/_[^_]*$//;
  369: 		    delete $hash{'pre_'.$key.'_title'};
  370: 		    delete $hash{'pre_'.$key.'_link'};
  371: 		}
  372: 	    }
  373: 	}
  374: 	
  375: # ---------------------------------- get state of file attributes to be showing
  376: 	if ($ENV{'form.attrs'} ne '') {
  377: 	    for (my $i=0; $i<=8; $i++) {
  378: 		delete $hash{'display_attrs_'.$i};
  379: 		if ($ENV{'form.attr'.$i} == 1) {
  380: 		    $attrchk[$i] = 'checked';
  381: 		    $hash{'display_attrs_'.$i} = 1;
  382: 		}
  383: 	    }
  384: 	} else {
  385: 	    for (my $i=0; $i<=8; $i++) {
  386: 		$attrchk[$i] = 'checked' if $hash{'display_attrs_'.$i} == 1;
  387: 	    }
  388: 	}
  389: # ------------------------------- output state of file attributes to be showing
  390: 	$r->print(<<END);
  391: <form method="post" name="fileattr" action="$uri"
  392:  enctype="application/x-www-form-urlencoded">
  393: <b><font color="#666666">Display file attributes</font></b><br />
  394: <table border=0><tr>
  395: <td><input type="checkbox" name="attr0" value="1" $attrchk[0] /> Title</td>
  396: <td><input type="checkbox" name="attr1" value="1" $attrchk[1] /> Size</td>
  397: <td><input type="checkbox" name="attr2" value="1" $attrchk[2] /> Last access</td>
  398: <td><input type="checkbox" name="attr3" value="1" $attrchk[3] /> Last modified</td>
  399: <td><input type="checkbox" name="attr8" value="1" $attrchk[8] /> All versions</td></tr><tr>
  400: <td><input type="checkbox" name="attr4" value="1" $attrchk[4] /> Author</td>
  401: <td><input type="checkbox" name="attr5" value="1" $attrchk[5] /> Keywords</td>
  402: <td><input type="checkbox" name="attr6" value="1" $attrchk[6] /> Language</td>
  403: <td><input type="checkbox" name="attr7" value="1" $attrchk[7] /> Show Resource</td>
  404: <td>&nbsp;</td>
  405: </tr></table>
  406: <input type="hidden" name="dirPointer" value="on" />
  407: <input type="hidden" name="acts" value="" />
  408: <input type="submit" name="attrs" value="Review" />&nbsp;
  409: <input type="submit" name="attrs" value="Refresh" />
  410: $closebutton
  411: $groupimportbutton
  412: </form>
  413: END
  414: 
  415: # ----------------- output starting row to the indexed file/directory hierarchy
  416:         my $titleclr="#ddffff";
  417: #        $r->print(&initdebug());
  418: #        $r->print(&writedebug("Omit:@Omit")) if (@Omit);
  419: #        $r->print(&writedebug("Only:@Only")) if (@Only);
  420:         $r->print("<table width='100\%' border=0><tr><td bgcolor=#777777>\n");
  421: 	$r->print("<table width='100\%' border=0><tr bgcolor=$titleclr>\n");
  422: 	$r->print("<td $colspan><b>Name</b></td>\n");
  423: 	$r->print("<td><b>Title</b></td>\n") 
  424: 	    if ($hash{'display_attrs_0'} == 1);
  425: 	$r->print("<td align=right><b>Size (bytes) ".
  426: 		  "</b></td>\n") if ($hash{'display_attrs_1'} == 1);
  427: 	$r->print("<td><b>Last accessed</b></td>\n") 
  428: 	    if ($hash{'display_attrs_2'} == 1);
  429: 	$r->print("<td><b>Last modified</b></td>\n")
  430: 	    if ($hash{'display_attrs_3'} == 1);
  431: 	$r->print("<td><b>Author(s)</b></td>\n")
  432: 	    if ($hash{'display_attrs_4'} == 1);
  433: 	$r->print("<td><b>Keywords</b></td>\n")
  434: 	    if ($hash{'display_attrs_5'} == 1);
  435: 	$r->print("<td><b>Language</b></td>\n")
  436: 	    if ($hash{'display_attrs_6'} == 1);
  437: 	$r->print("<td><b>Resource</b></td>\n")
  438: 	    if ($hash{'display_attrs_7'} == 1);
  439: 	$r->print('</tr>');
  440: 
  441: # ----------------- read in what directories have previously been set to "open"
  442: 	foreach (keys %hash) {
  443: 	    if ($_ =~ /^diropen_status_/) {
  444: 		my $key = $_;
  445: 		$key =~ s/^diropen_status_//;
  446: 		$dirs{$key} = $hash{$_};
  447: 	    }
  448: 	}
  449: 
  450: 	if ($ENV{'form.openuri'}) {  # take care of review and refresh options
  451: 	    my $uri=$ENV{'form.openuri'};
  452: 	    if (exists($hash{'diropen_status_'.$uri})) {
  453: 		my $cursta = $hash{'diropen_status_'.$uri};
  454: 		$dirs{$uri} = 'open';
  455: 		$hash{'diropen_status_'.$uri} = 'open';
  456: 		if ($cursta eq 'open') {
  457: 		    $dirs{$uri} = 'closed';
  458: 		    $hash{'diropen_status_'.$uri} = 'closed';
  459: 		}
  460: 	    } else {
  461: 		$hash{'diropen_status_'.$uri} = 'open';
  462: 		$dirs{$uri} = 'open';
  463: 	    }
  464: 	}
  465: 	
  466: 	my $bredir = $ENV{'form.dirPointer'};
  467: 	my $toplevel;
  468: 	my $indent = 0;
  469: 	$uri = $uri.'/' if $uri !~ /.*\/$/;
  470: 
  471: 	if ($bredir ne 'on') {
  472: 	    $hash{'top.level'} = $uri;
  473: 	    $toplevel = $uri;
  474: 
  475: 	} else {
  476: 	    $toplevel = $hash{'top.level'};
  477: 	}
  478: 
  479: # -------------------------------- if not at top level, provide an uplink arrow
  480: 	if ($toplevel ne '/res/'){
  481: 	    my (@uri_com) = split(/\//,$uri);
  482: 	    pop @uri_com;
  483: 	    my $upone = join('/',@uri_com);
  484: 	    my @list = qw (0);
  485: 	    &display_line ($r,'opened',$upone.'&viewOneUp',0,$upone,@list);
  486: 	    $indent = 1;
  487: 	}
  488: 
  489: # -------- recursively go through all the directories and output as appropriate
  490: 	&scanDir ($r,$toplevel,$indent,\%hash);
  491: 	
  492: # ---------------------------- embed hidden information useful for group import
  493: 	$r->print("<form name='fnum'>");
  494: 	$r->print("<input type='hidden' name='fnum' value='$fnum'></form>");
  495: 
  496: # -------------------------------------------------------------- end the tables
  497: 	$r->print('</table>');
  498: 	$r->print('</td></tr></table>');
  499: 
  500: # --------------------------------------------------- end the output and return
  501: 	$r->print('</body></html>'."\n");
  502:     } else {
  503: 	$r->print('<html><head></head><body>Unable to tie hash to db '.
  504: 		  'file</body></html>');
  505: 	return OK;
  506:     }
  507:     if(! $c->aborted()) {
  508:         if (tie(%dbfile,'GDBM_File',$diropendb,&GDBM_NEWDB(),0640)) {
  509:             while (my($key,$value) = each(%hash)) {
  510:                 $dbfile{$key}=$value;
  511:             }
  512:             untie(%dbfile);
  513:         }
  514:     }
  515: 
  516:     return OK;
  517: }
  518: 
  519: # ----------------------------------------------- recursive scan of a directory
  520: sub scanDir {
  521:     my ($r,$startdir,$indent,$hashref)=@_;
  522:     my $c = $r->connection();
  523:     my ($compuri,$curdir);
  524:     my $dirptr=16384;
  525:     $indent++;
  526: 
  527:     my %dupdirs = %dirs;
  528:     my @list=&get_list($r,$startdir);
  529:     foreach my $line (@list) {
  530:         return if ($c->aborted());
  531: 	my ($strip,$dom,undef,$testdir,undef)=split(/\&/,$line,5); 
  532: 	next if $strip =~ /.*\.meta$/;
  533: 	my (@fileparts) = split(/\./,$strip);
  534: 	if ($hash{'display_attrs_8'} != 1) {
  535: 	    if (scalar(@fileparts) >= 3) {
  536: 		my $fext = pop @fileparts;
  537: 		my $ov = pop @fileparts;
  538: 		my $fname = join ('.',@fileparts,$fext);
  539: 		next if (grep /\Q$fname\E/,@list and $ov =~ /\d+/);
  540: 	    }
  541: 	}
  542: 
  543: 	if ($dom eq 'domain') {
  544: 	    $compuri = join('',$strip,'/');  # dom list has /res/<domain name>
  545: 	    $curdir = $compuri;
  546: 	} else {
  547: 	    # user, dir & file have name only, i.e., w/o path
  548: 	    $compuri = join('',$startdir,$strip,'/');
  549: 	    $curdir = $startdir;
  550: 	}
  551: 	my $diropen = 'closed';
  552: 	if (($dirptr&$testdir) or ($dom =~ /^(domain|user)$/)) {
  553: 	    while (my ($key,$val)= each %dupdirs) {
  554: 		if ($key eq $compuri and $val eq "open") {
  555: 		    $diropen = "opened";
  556: 		    delete($dupdirs{$key});
  557: 		    delete($dirs{$key});
  558: 		}
  559: 	    }
  560: 	}
  561: 	&display_line($r,$diropen,$line,$indent,$curdir,$hashref,@list);
  562: 	&scanDir ($r,$compuri,$indent) if $diropen eq 'opened';
  563:     }
  564:     $indent--;
  565: }
  566: 
  567: # --------------- get complete matched list based on the uri (returns an array)
  568: sub get_list {
  569:     my ($r,$uri)=@_;
  570:     my @list;
  571:     (my $luri = $uri) =~ s/\//_/g;
  572: 
  573:     if ($ENV{'form.attrs'} eq 'Refresh') {
  574: 	foreach (keys %hash) {
  575: 	    delete $hash{$_} if ($_ =~ /^dirlist_files_/);
  576: 	    }
  577:     }
  578: 
  579:     if ($hash{'dirlist_files'.$luri}) {
  580: 	@list = split(/\n/,$hash{'dirlist_files_'.$luri});
  581:     } else {
  582: 	@list = &Apache::lonnet::dirlist($uri);
  583: 	$hash{'dirlist_files_'.$luri} = join('\n',@list);
  584:     }
  585:     return @list=&match_ext($r,@list);
  586: }
  587: 
  588: sub initdebug {
  589:     return <<ENDJS;
  590: <script>
  591: var debugging = true;
  592: if (debugging) {
  593:     var debuggingWindow = window.open('','Debug','width=400,height=300',true);
  594: } 
  595: 
  596: function output(text) {
  597:     if (debugging) {
  598:         debuggingWindow.document.writeln(text);
  599:     }
  600: }
  601: output("<html><head><title>Debugging Window</title></head><body><pre>");   
  602: </script>
  603: ENDJS
  604: }
  605: 
  606: sub writedebug {
  607:     my $text = shift;
  608:     return "<script>output('$text');</script>";
  609: }
  610: 
  611: # -------------------- filters out files based on extensions (returns an array)
  612: sub match_ext {
  613:     my ($r,@packlist)=@_;
  614:     my @trimlist;
  615:     my $nextline;
  616:     my @fileext;
  617:     my $dirptr=16384;
  618: 
  619:     foreach my $line (@packlist) {
  620: 	chomp $line;
  621: 	$line =~ s/^\/home\/httpd\/html//;
  622: 	my @unpackline = split (/\&/,$line);
  623: 	next if ($unpackline[0] eq '.');
  624: 	next if ($unpackline[0] eq '..');
  625: 	my @filecom = split (/\./,$unpackline[0]);
  626: 	my $fext = pop(@filecom);
  627: 	my $fnptr = $unpackline[3]&$dirptr;
  628:  	if ($fnptr == 0 and $unpackline[3] ne "") {
  629: 	    my $embstyle = &Apache::loncommon::fileembstyle($fext);
  630:             push @trimlist,$line if (defined($embstyle) && 
  631: 				     ($embstyle ne 'hdn' or $fext eq 'meta'));
  632: 	} else {
  633: 	    push @trimlist,$line;
  634: 	}
  635:     }
  636:     @trimlist = sort (@trimlist);
  637:     return @trimlist;
  638: }
  639: 
  640: # ------------------------------- displays one line in appropriate table format
  641: sub display_line {
  642:     my ($r,$diropen,$line,$indent,$startdir,$hashref,@list)=@_;
  643:     my (@pathfn, $fndir);
  644:     my $dirptr=16384;
  645:     my $fileclr="#ffffe6";
  646:     my $iconpath= $r->dir_config('lonIconsURL') . '/';
  647: 
  648:     my @filecom = split (/\&/,$line);
  649:     my @pathcom = split (/\//,$filecom[0]);
  650:     my $listname = $pathcom[scalar(@pathcom)-1];
  651:     my $fnptr = $filecom[3]&$dirptr;
  652:     my $msg = 'View '.$filecom[0].' resources';
  653:     $msg = 'Close '.$filecom[0].' directory' if $diropen eq 'opened';
  654: 
  655:     my $tabtag='</td>';
  656:     my $i=0;
  657: 
  658:     while ($i<=7) {
  659: 	$tabtag=join('',$tabtag,"<td>&nbsp;</td>")
  660: 	    if $hash{'display_attrs_'.$i} == 1;
  661: 	$i++;
  662:     }
  663: 	
  664:     my $valign = ($hash{'display_attrs_7'} == 1 ? 'top' : 'bottom');
  665: 
  666: # display uplink arrow
  667:     if ($filecom[1] eq 'viewOneUp') {
  668: 	$r->print("<tr bgcolor=$fileclr>$extrafield");
  669: 	$r->print("<td valign=$valign>\n");
  670: 	$r->print ('<form method="post" name="dirpathUP" action="'.$startdir.
  671: 		   '/" '.
  672: 		   'onSubmit="return rep_dirpath(\'UP\','.
  673: 		   'document.forms.fileattr.acts.value)" '.
  674: 		   'enctype="application/x-www-form-urlencoded"'.
  675:                    '>'."\n");
  676: 	$r->print ('<input type=hidden name=openuri value="'.
  677: 		   $startdir.'">'."\n");
  678: 	$r->print ('<input type="hidden" name="acts" value="">'."\n");
  679: 	$r->print ('<input src="'.$iconpath.'arrow_up.gif"');
  680: 	$r->print (' name="'.$msg.'" height="22" type="image" border="0">'.
  681: 		   "\n");
  682: 	$r->print("Up $tabtag</tr></form>\n");
  683: 	return OK;
  684:     }
  685: # Do we have permission to look at this?
  686: 
  687:     return OK if (!&Apache::lonnet::allowed('bre',$startdir.$filecom[0]));
  688: 
  689: # display domain
  690:     if ($filecom[1] eq 'domain') {
  691: 	$r->print ('<input type="hidden" name="dirPointer" value="on">'."\n")
  692: 	    if ($ENV{'form.dirPointer'} eq "on");
  693: 	$r->print("<tr bgcolor=$fileclr>$extrafield");
  694: 	$r->print("<td valign=$valign>");
  695: 	&begin_form ($r,$filecom[0].'/');
  696: 	my $anchor = $filecom[0].'/';
  697: 	$anchor =~ s/\///g;
  698: 	$r->print ('<a name="'.$anchor.'">');
  699: 	$r->print ('<input type="hidden" name="acts" value="">');
  700: 	$r->print ('<input src="'.$iconpath.'folder_pointer_'.
  701: 		   $diropen.'.gif"'); 
  702: 	$r->print (' name="'.$msg.'" height="22" type="image" border="0">'.
  703: 		   "\n");
  704: 	$r->print ('<a href="javascript:gothere(\''.$filecom[0].
  705: 		   '/\')"><img src="'.$iconpath.'server.gif"');
  706: 	$r->print (' border="0" /></a>'."\n");
  707: 	$r->print ("Domain - $listname ");
  708: 	if ($Apache::lonnet::domaindescription{$listname}) {
  709: 	    $r->print("(".$Apache::lonnet::domaindescription{$listname}.
  710: 		      ")");
  711: 	}
  712: 	$r->print (" $tabtag</tr></form>\n");
  713: 	return OK;
  714: 
  715: # display user directory
  716:     }
  717:     if ($filecom[1] eq 'user') {
  718: 	$r->print("<tr bgcolor=$fileclr>$extrafield");
  719: 	$r->print("<td valign=$valign nowrap>\n");
  720: 	my $curdir = $startdir.$filecom[0].'/';
  721: 	my $anchor = $curdir;
  722: 	$anchor =~ s/\///g;
  723: 	&begin_form ($r,$curdir);
  724: 	$r->print ('<a name="'.$anchor.'"><img src="'.$iconpath.
  725: 		   'whitespace1.gif" border="0" />'."\n");
  726: 	$r->print ('<input type="hidden" name="acts" value="">');
  727: 	$r->print ('<input src="'.$iconpath.'folder_pointer_'.$diropen.
  728: 		   '.gif"'); 
  729: 	$r->print (' name="'.$msg.'" height="22" type="image" border="0">'.
  730: 		   "\n");
  731: 	$r->print ('<a href="javascript:gothere(\''.$curdir.'\')"><img src='.
  732: 		   $iconpath.'quill.gif border="0" name="'.$msg.
  733: 		   '" height="22" /></a>');
  734: 	my $domain=(split(m|/|,$startdir))[2];
  735: 	my $plainname=&Apache::loncommon::plainname($listname,$domain);
  736: 	$r->print ($listname);
  737: 	if (defined($plainname) && $plainname) { $r->print(" ($plainname) "); }
  738: 	$r->print ($tabtag.'</tr></form>'."\n");
  739: 	return OK;
  740:     }
  741: 
  742: # display file
  743:     if ($fnptr == 0 and $filecom[3] ne '') {
  744: 	my $filelink = $startdir.$filecom[0];
  745: 	my @file_ext = split (/\./,$listname);
  746: 	my $curfext = $file_ext[-1];
  747:         if (@Omit) {
  748:             foreach (@Omit) { return OK if ($curfext eq $_); }
  749:         }
  750:         if (@Only) {
  751:             my $skip = 1;
  752:             foreach (@Only) { $skip = 0 if ($curfext eq $_); }
  753:             return OK if ($skip > 0);
  754:         }
  755: 	# Set the icon for the file
  756: 	my $iconname = "unknown.gif";
  757: 	my $embstyle = &Apache::loncommon::fileembstyle($curfext);
  758: 	# The unless conditional that follows is a bit of overkill
  759: 	$iconname = $curfext.".gif" unless
  760: 	    (!defined($embstyle) || $embstyle eq 'unk' || $embstyle eq 'hdn');
  761: 	#
  762: 	$r->print("<tr bgcolor=$fileclr><td nowrap valign='$valign'>");
  763: 	my $metafile = grep /^\Q$filecom[0]\E\.meta\&/, @list;
  764: 	my $title;
  765:         if ($ENV{'form.catalogmode'} eq 'interactive') {
  766: 	    $title=$listname;
  767: 	    $title = &Apache::lonnet::metadata($filelink,'title')
  768: 		if ($metafile == 1);
  769: 	    $title=$listname unless $title;
  770: 	    my $titleesc=HTML::Entities::encode($title);
  771: 	    $titleesc=~s/\'/\\'/; #' (clean up this spare quote)
  772:             $r->print("<a href=\"javascript:select_data(\'",
  773:                       $titleesc,"','",$filelink,"')\">");
  774: 	    $r->print("<img src='",$iconpath,"select.gif' border='0' /></a>".
  775: 		      "\n");
  776: 	    $r->print("</td><td valign='$valign' nowrap>");
  777: 	}
  778:         elsif ($ENV{'form.catalogmode'} eq 'groupimport') {
  779: 	    $title=$listname;
  780: 	    $title = &Apache::lonnet::metadata($filelink,'title')
  781: 		if ($metafile == 1);
  782: 	    $title=$listname unless $title;
  783: 	    my $titleesc=&HTML::Entities::encode($title);
  784: 	    $r->print("<form name='form$fnum'>\n");
  785: 	    $r->print("<input type='checkbox' name='filelink"."' ".
  786: 		      "value='$filelink' onClick='".
  787: 		      "javascript:queue(\"form$fnum\")' ");
  788: 	    if ($hash{'store_'.$filelink}) {
  789: 		$r->print("checked");
  790: 	    }
  791: 	    $r->print(">\n");
  792: 	    $r->print("<input type='hidden' name='title"."' ".
  793: 		      "value='$titleesc'>\n");
  794: 	    $r->print("</form>\n");
  795: 	    $r->print("</td><td valign='$valign' nowrap>");
  796: 	    $hash{"pre_${fnum}_link"}=$filelink;
  797: 	    $hash{"pre_${fnum}_title"}=$titleesc;
  798:   	    $fnum++;
  799: 	}
  800: 
  801: 	if ($indent > 0 and $indent < 11) {
  802: 	    $r->print("<img src=",$iconpath,"whitespace",$indent,
  803: 		      ".gif border='0' />\n");
  804: 	} elsif ($indent >0) {
  805: 	    my $ten = int($indent/10.);
  806: 	    my $rem = $indent%10.0;
  807: 	    my $count = 0;
  808: 	    while ($count < $ten) {
  809: 		$r->print("<img src=",$iconpath,
  810: 			  "whitespace10.gif border='0' />\n");
  811: 	    $count++;
  812: 	    }
  813: 	    $r->print("<img src=",$iconpath,"whitespace",$rem,
  814: 		      ".gif border='0' />\n") if $rem > 0;
  815: 	}
  816: 
  817: 	$r->print("<img src=$iconpath$iconname border='0' />\n");
  818: 	$r->print (" <a href=\"javascript:openWindow('".$filelink.
  819: 		   "', 'metadatafile', '450', '500', 'no', 'yes')\";".
  820: 		   " TARGET=_self>$listname</a> ");
  821: 
  822: 	$r->print (" (<a href=\"javascript:openWindow('".$filelink.
  823: 		   ".meta', 'metadatafile', '400', '450', 'no', 'yes')\"; ".
  824: 		   "TARGET=_self>metadata</a>) ") if ($metafile == 1);
  825: 
  826: 	$r->print("</td>\n");
  827: 	if ($hash{'display_attrs_0'} == 1) {
  828: 	    my $title = &Apache::lonnet::metadata($filelink,'title')
  829: 		if ($metafile == 1);
  830: 	    $r->print('<td valign=$valign> '.($title eq '' ? '&nbsp;' : $title).
  831: 		      ' </td>'."\n");
  832: 	}
  833: 	$r->print('<td align=right valign=$valign> ',
  834: 		  $filecom[8]," </td>\n") 
  835: 	    if $hash{'display_attrs_1'} == 1;
  836: 	$r->print('<td valign=$valign> '.
  837: 		  (localtime($filecom[9]))." </td>\n") 
  838: 	    if $hash{'display_attrs_2'} == 1;
  839: 	$r->print('<td valign=$valign> '.
  840: 		  (localtime($filecom[10]))." </td>\n") 
  841: 	    if $hash{'display_attrs_3'} == 1;
  842: 
  843: 	if ($hash{'display_attrs_4'} == 1) {
  844: 	    my $author = &Apache::lonnet::metadata($filelink,'author')
  845: 		if ($metafile == 1);
  846: 	    $r->print('<td valign=$valign> '.($author eq '' ? '&nbsp;' : $author).
  847: 		      " </td>\n");
  848: 	}
  849: 	if ($hash{'display_attrs_5'} == 1) {
  850: 	    my $keywords = &Apache::lonnet::metadata($filelink,'keywords')
  851: 		if ($metafile == 1);
  852: 	    # $keywords = '&nbsp;' if (!$keywords);
  853: 	    $r->print('<td valign=$valign> '.($keywords eq '' ? '&nbsp;' : $keywords).
  854: 		      " </td>\n");
  855: 	}
  856: 	if ($hash{'display_attrs_6'} == 1) {
  857: 	    my $lang = &Apache::lonnet::metadata($filelink,'language')
  858: 		if ($metafile == 1);
  859: 	    $lang = &Apache::loncommon::languagedescription($lang);
  860: 	    $r->print('<td valign=$valign> '.($lang eq '' ? '&nbsp;' : $lang).
  861: 		      " </td>\n");
  862: 	}
  863:         if ($hash{'display_attrs_7'} == 1) {
  864:             my $output='';
  865:             my $embstyle=&Apache::loncommon::fileembstyle($curfext);
  866: 	    if ($embstyle eq 'ssi') {
  867: 	       $output=&Apache::lonnet::ssi_body($filelink);
  868:                $output='<font size="-2">'.$output.'</font>';
  869: 	   } elsif ($embstyle eq 'img') {
  870:                $output='<img src="'.$filelink.'" />';
  871:            } elsif ($filelink=~/^\/res\/(\w+)\/(\w+)\//) {
  872:                $output='<img src="http://'.
  873: 		 $Apache::lonnet::hostname{&Apache::lonnet::homeserver($2,$1)}.
  874:                  '/cgi-bin/thumbnail.gif?url='.$filelink.'" />';
  875:            }
  876: 	   $r->print('<td valign=$valign> '.($output eq '' ? '&nbsp;':$output).
  877: 		      " </td>\n");
  878:         }
  879: 	$r->print("</tr>\n");
  880:     }
  881: 
  882: # -- display directory
  883:     if ($fnptr == $dirptr) {
  884: 	my @file_ext = split (/\./,$listname);
  885: 	my $curfext = $file_ext[scalar(@file_ext)-1];
  886: 	my $curdir = $startdir.$filecom[0].'/';
  887: 	my $anchor = $curdir;
  888: 	$anchor =~ s/\///g;
  889: 	$r->print("<tr bgcolor=$fileclr>$extrafield<td valign=$valign>");
  890: 	&begin_form ($r,$curdir);
  891: 	my $indentm1 = $indent-1;
  892: 	if ($indentm1 < 11 and $indentm1 > 0) {
  893: 	    $r->print("<img src=",$iconpath,"whitespace",$indentm1,
  894: 		      ".gif border='0' />\n");
  895: 	} else {
  896: 	    my $ten = int($indentm1/10.);
  897: 	    my $rem = $indentm1%10.0;
  898: 	    my $count = 0;
  899: 	    while ($count < $ten) {
  900: 		$r->print ("<img src=",$iconpath
  901: 			   ,"whitespace10.gif border='0' />\n");
  902: 		$count++;
  903: 	    }
  904: 	    $r->print ("<img src=",$iconpath,"whitespace",$rem,
  905: 		       ".gif border='0' />\n") if $rem > 0;
  906: 	}
  907: 	$r->print ('<input type="hidden" name="acts" value="">');
  908: 	$r->print ('<a name="'.$anchor.'"><input src="'.$iconpath.
  909: 		   'folder_pointer_'.$diropen.'.gif"');
  910: 	$r->print (' name="'.$msg.'" height="22" type="image" border="0">'.
  911: 		   "\n");
  912: 	$r->print ('<a href="javascript:gothere(\''.$curdir.'\')"><img src="'.
  913: 		   $iconpath.'folder_'.$diropen.'.gif" border="0" /></a>'.
  914: 		   "\n");
  915: 	$r->print ("$listname$tabtag</tr></form>\n");
  916:     }
  917: 
  918: }
  919: 
  920: # ------------------- prints the beginning of a form for directory or file link
  921: sub begin_form {
  922:     my ($r,$uri) = @_;
  923:     my $anchor = $uri;
  924:     $anchor =~ s/\///g;
  925:     $r->print ('<form method="post" name="dirpath'.$dnum.'" action="'.$uri.
  926: 	       '#'.$anchor.
  927: 	       '" onSubmit="return rep_dirpath(\''.$dnum.'\''.
  928: 	       ',document.forms.fileattr.acts.value)" '.
  929: 	       'enctype="application/x-www-form-urlencoded">'."\n");
  930:     $r->print ('<input type="hidden" name="openuri" value="'.$uri.'">'.
  931: 	       "\n");
  932:     $r->print ('<input type="hidden" name="dirPointer" value="on">'."\n");
  933:     $dnum++;
  934: }
  935: 
  936: # --------- settings whenever the user causes the indexer window to be launched
  937: sub start_fresh_session {
  938:     delete $hash{'form.catalogmode'};
  939:     delete $hash{'form.mode'};
  940:     delete $hash{'form.form'};
  941:     delete $hash{'form.element'};
  942:     delete $hash{'form.omit'};
  943:     delete $hash{'form.only'};
  944:     foreach (keys %hash) {
  945:         delete $hash{$_} if (/^(pre_|store)/);
  946:     }
  947: }
  948: 
  949: # ------------------------------------------------------------------- setvalues
  950: sub setvalues {
  951:     # setvalues is used in registerurl to synchronize the database
  952:     # hash and environment hashes
  953:     my ($H1,$h1key,$H2,$h2key) =@_;
  954:     #
  955:     if (exists $H2->{$h2key}) {
  956: 	$H1->{$h1key} = $H2->{$h2key};
  957:     } elsif (exists $H1->{$h1key}) {
  958: 	$H2->{$h2key} = $H1->{$h1key};
  959:     } 
  960: }
  961: 
  962: 1;
  963: 
  964: sub cleanup {
  965:     if (tied(%hash)){
  966: 	&Apache::lonnet::logthis('Cleanup indexer: hash');
  967:     }
  968: }
  969: 
  970: =head1 NAME
  971: 
  972: Apache::lonindexer - mod_perl module for cross server filesystem browsing
  973: 
  974: =head1 SYNOPSIS
  975: 
  976: Invoked by /etc/httpd/conf/srm.conf:
  977: 
  978:  <LocationMatch "^/res.*/$">
  979:  SetHandler perl-script
  980:  PerlHandler Apache::lonindexer
  981:  </LocationMatch>
  982: 
  983: =head1 INTRODUCTION
  984: 
  985: This module enables a scheme of browsing across a cross server.
  986: 
  987: This is part of the LearningOnline Network with CAPA project
  988: described at http://www.lon-capa.org.
  989: 
  990: =head1 BEGIN SUBROUTINE
  991: 
  992: This routine is only run once after compilation.
  993: 
  994: =over 4
  995: 
  996: =item *
  997: 
  998: Initializes %language hash table.
  999: 
 1000: =back
 1001: 
 1002: =head1 HANDLER SUBROUTINE
 1003: 
 1004: This routine is called by Apache and mod_perl.
 1005: 
 1006: =over 4
 1007: 
 1008: =item *
 1009: 
 1010: read in machine configuration variables
 1011: 
 1012: =item *
 1013: 
 1014: see if called from an interactive mode
 1015: 
 1016: =item *
 1017: 
 1018: refresh environment with user database values (in %hash)
 1019: 
 1020: =item *
 1021: 
 1022: define extra fields and buttons in case of special mode
 1023: 
 1024: =item *
 1025: 
 1026: set catalogmodefunctions to have extra needed javascript functionality
 1027: 
 1028: =item *
 1029: 
 1030: print header
 1031: 
 1032: =item *
 1033: 
 1034: evaluate actions from previous page (both cumulatively and chronologically)
 1035: 
 1036: =item *
 1037: 
 1038: output title
 1039: 
 1040: =item *
 1041: 
 1042: get state of file attributes to be showing
 1043: 
 1044: =item *
 1045: 
 1046: output state of file attributes to be showing
 1047: 
 1048: =item *
 1049: 
 1050: output starting row to the indexed file/directory hierarchy
 1051: 
 1052: =item *
 1053: 
 1054: read in what directories have previously been set to "open"
 1055: 
 1056: =item *
 1057: 
 1058: if not at top level, provide an uplink arrow
 1059: 
 1060: =item *
 1061: 
 1062: recursively go through all the directories and output as appropriate
 1063: 
 1064: =item *
 1065: 
 1066: information useful for group import
 1067: 
 1068: =item *
 1069: 
 1070: end the tables
 1071: 
 1072: =item *
 1073: 
 1074: end the output and return
 1075: 
 1076: =back
 1077: 
 1078: =head1 OTHER SUBROUTINES
 1079: 
 1080: =over 4
 1081: 
 1082: =item *
 1083: 
 1084: scanDir - recursive scan of a directory
 1085: 
 1086: =item *
 1087: 
 1088: get_list - get complete matched list based on the uri (returns an array)
 1089: 
 1090: =item *
 1091: 
 1092: match_ext - filters out files based on extensions (returns an array)
 1093: 
 1094: =item *
 1095: 
 1096: display_line - displays one line in appropriate table format
 1097: 
 1098: =item *
 1099: 
 1100: begin_form - prints the beginning of a form for directory or file link
 1101: 
 1102: =item *
 1103: 
 1104: start_fresh_session - settings whenever the user causes the indexer window
 1105: to be launched
 1106: 
 1107: =back
 1108: 
 1109: =cut

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