File:  [LON-CAPA] / loncom / interface / lonindexer.pm
Revision 1.54: download - view: text, annotated - select for diffs
Thu Jan 9 22:04:28 2003 UTC (21 years, 5 months ago) by www
Branches: MAIN
CVS tags: version_0_6_2, version_0_6_1, version_0_6, HEAD
Together with the cleanup hander on lonsearchcat.pm, we will hopefully never
see "cannot tie hash" again during group search and group import

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

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