File:  [LON-CAPA] / loncom / interface / lonindexer.pm
Revision 1.40: download - view: text, annotated - select for diffs
Mon Mar 25 19:27:17 2002 UTC (22 years, 2 months ago) by matthew
Branches: MAIN
CVS tags: stable_2002_april, HEAD
Added 'omit=' and 'only=' tags which cause the indexer to omit (or only
include) files with the given extension.

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

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