File:  [LON-CAPA] / loncom / interface / lonindexer.pm
Revision 1.4: download - view: text, annotated - select for diffs
Mon May 21 15:22:48 2001 UTC (23 years, 1 month ago) by harris41
Branches: MAIN
CVS tags: HEAD
fix a bug in meta file link
consolidate all tmp files into one db file
create whitespace gif files based on indent space

    1: # The LearningOnline Network with CAPA
    2: # Directory Indexer
    3: # (Login Screen
    4: # 5/21/99,5/22,5/25,5/26,5/31,6/2,6/10,7/12,7/14 Gerd Kortemeyer)
    5: # 11/23 Gerd Kortemeyer
    6: # 07/20-08/04 H.K. Ng
    7: #
    8: # 05/9-05/19/2001 H. K. Ng
    9: # 05/21/2001 H. K. Ng
   10: #
   11: package Apache::lonindexer;
   12: 
   13: use strict;
   14: use Apache::lonnet();
   15: use Apache::Constants qw(:common);
   16: use Apache::File;
   17: use GDBM_File;
   18: 
   19: my %hash;
   20: my %dirs;
   21: my %language;
   22: 
   23: sub BEGIN {
   24:     my $fh=Apache::File->new($Apache::lonnet::perlvar{'lonTabDir'}.'/language.tab');
   25:     map {
   26: 	$_=~/(\w+)\s+([\w\s\-]+)/;
   27: 	$language{$1}=$2;
   28:     } <$fh>;
   29: }
   30: 
   31: sub handler {
   32:     my $r = shift;
   33:     $r->content_type('text/html');
   34:     $r->send_http_header;
   35:     return OK if $r->header_only;
   36: 
   37:     my $iconpath= $r->dir_config('lonIconsURL');
   38:     my $domain  = $r->dir_config('lonDefDomain');
   39:     my $role    = $r->dir_config('lonRole');
   40:     my $loadlim = $r->dir_config('lonLoadLim');
   41:     my $servadm = $r->dir_config('lonAdmEMail');
   42:     my $sysadm  = $r->dir_config('lonSysEMail');
   43:     my $lonhost = $r->dir_config('lonHostID');
   44:     my $tabdir  = $r->dir_config('lonTabDir');
   45: 
   46: # ---------------------------------------------------------------- Print Header
   47:     $r->print(<<ENDHEADER);
   48: <html>
   49: <head>
   50: <title>The LearningOnline Network With CAPA Directory Browser</title>
   51: 
   52: <SCRIPT language="javascript">
   53: function openWindow(url, wdwName, w, h, toolbar,scrollbar) {
   54:     var options = "width=" + w + ",height=" + h + ",";
   55:     options += "resizable=yes,scrollbars="+scrollbar+",status=no,";
   56:     options += "menubar=no,toolbar="+toolbar+",location=no,directories=no";
   57:     var newWin = window.open(url, wdwName, options);
   58:     newWin.focus();
   59: }
   60: </SCRIPT>
   61: 
   62: </head>
   63: <body bgcolor="#FFFFFF">
   64: ENDHEADER
   65: 
   66:     my $line;
   67:     my (@attrchk,@openpath);
   68:     my $uri=$r->uri;
   69:     my $iconpath="/res/adm/pages/indexericons/";
   70: 
   71:     $r->print("<h2><font color=\"\#888888\">The LearningOnline With CAPA Network Directory Browser</font></h2>\n");
   72: 
   73:     for (my $i=0; $i<=5; $i++) {
   74: 	$attrchk[$i] = "checked" if $ENV{'form.attr'.$i} == 1;
   75:     }
   76:     $r->print(<<END);
   77: <b><font color="#666666">Display file attributes</font></b><br>
   78: <form method="post" name="fileattr" action="$uri" enctype="application/x-www-form-urlencoded">
   79: <table border=0><tr>
   80: <td><input type=checkbox name=attr0 value="1" $attrchk[0]> Size</td>
   81: <td><input type=checkbox name=attr1 value="1" $attrchk[1]> Last access</td>
   82: <td><input type=checkbox name=attr2 value="1" $attrchk[2]> Last modified</td>
   83: </tr><tr>
   84: <td><input type=checkbox name=attr3 value="1" $attrchk[3]> Author</td>
   85: <td><input type=checkbox name=attr4 value="1" $attrchk[4]> Keywords</td>
   86: <td><input type=checkbox name=attr5 value="1" $attrchk[5]> Language</td>
   87: </tr></table>
   88: <input type="submit" name="dirlistattr" value="Review">&nbsp;
   89: <input type="submit" name="dirlistattr" value="Refresh">
   90: </form>
   91: END
   92: 
   93:     my $diropen = "/home/httpd/perl/tmp/$domain\_$ENV{'user.name'}_diropen.db";
   94: 
   95:     if (tie(%hash,'GDBM_File',$diropen,&GDBM_WRCREAT,0640)) {
   96: 	my $titleclr="#ddffff";
   97: 	$r->print("<table border=0><tr><td bgcolor=#eeeeee>\n");
   98: 	$r->print("<table border=0><tr>\n");
   99: 	$r->print("<td bgcolor=$titleclr><b>Name</b></td>\n");
  100: 	$r->print("<td bgcolor=$titleclr align=right><b>Size (bytes) </b></td>\n") if ($ENV{'form.attr0'} == 1);
  101: 	$r->print("<td bgcolor=$titleclr><b>Last accessed</b></td>\n") if ($ENV{'form.attr1'} == 1);
  102: 	$r->print("<td bgcolor=$titleclr><b>Last modified</b></td>\n") if ($ENV{'form.attr2'} == 1);
  103: 	$r->print("<td bgcolor=$titleclr><b>Author(s)</b></td>\n") if ($ENV{'form.attr3'} == 1);
  104: 	$r->print("<td bgcolor=$titleclr><b>Keywords</b></td>\n") if ($ENV{'form.attr4'} == 1);
  105: 	$r->print("<td bgcolor=$titleclr><b>Language</b></td>\n") if ($ENV{'form.attr5'} == 1);
  106: 	$r->print("</tr>");
  107: 
  108: 	map {
  109: 	    if ($_ =~ /^diropen_status_/) {
  110: 		my $key = $_;
  111: 		$key =~ s/^diropen_status_//;
  112: 		$dirs{$key} = $hash{$_};
  113: 	    }
  114: 	} keys %hash;
  115: 
  116: 	if ($ENV{'form.openuri'}) {  # take care of review and refresh options
  117: 	    my $uri=$ENV{'form.openuri'};
  118: 	    if (exists($hash{'diropen_status_'.$uri})) {
  119: 		my $cursta = $hash{'diropen_status_'.$uri};
  120: 		$dirs{$uri} = 'open';
  121: 		$hash{'diropen_status_'.$uri} = 'open';
  122: 		if ($cursta eq 'open') {
  123: 		    $dirs{$uri} = 'closed';
  124: 		    $hash{'diropen_status_'.$uri} = 'closed';
  125: 		}
  126: 	    } else {
  127: 		$hash{'diropen_status_'.$uri} = 'open';
  128: 		$dirs{$uri} = 'open';
  129: 	    }
  130: 	}
  131: 
  132: 	my $toplevel = "/res/";
  133: 	my $indent = 0;
  134: 	&scanDir ($r,$toplevel,$indent);
  135: 
  136: 	$r->print("</table>");
  137: 	$r->print("</td></tr></table>");
  138: 	$r->print("</body></html>\n");
  139: 	untie(%hash);
  140:     } else {
  141: 	$r->print("Unable to tie hash to db file");
  142:     }
  143:     return OK;
  144: }
  145: 
  146: # --------------------recursive scan of a directory
  147: sub scanDir {
  148:     my ($r,$startdir,$indent)=@_;
  149:     my ($compuri,$curdir);
  150:     my $dirptr=16384;
  151:     $indent++;
  152: 
  153:     my %dupdirs = %dirs;
  154:     my @list=&get_list($r,$startdir);
  155:     foreach my $line (@list) {
  156: 	my ($strip,$domusr,$foo,$testdir,$foo)=split(/\&/,$line,5); 
  157: 	next if $strip =~ /.*\.meta$/;
  158: 	if ($domusr eq "domain") {
  159: 	    $compuri = join('',$strip,"/");  # domain list has /res/<domain name>
  160: 	    $curdir = $compuri;
  161: 	} else {
  162: 	    $compuri = join('',$startdir,$strip,"/"); # user, dir & file having name only, i.e., w/o path
  163: 	    $curdir = $startdir;
  164: 	}
  165: 	my $diropen = 0;
  166: 	if (($dirptr&$testdir) or ($domusr =~ /^(domain|user)$/)) {
  167: 	    while (my ($key,$val)= each %dupdirs) {
  168: 		$diropen = 1 if ($key eq $compuri and $val eq "open");
  169: 	    }
  170: 	}
  171: 	&display_line($r,$diropen,$line,$indent,$curdir,@list);
  172: 	&scanDir ($r,$compuri,$indent) if $diropen == 1;
  173:     }
  174:     $indent--;
  175: }
  176: 
  177: # ----------------- get complete matched list based on the uri ------
  178: sub get_list {
  179:     my ($r,$uri)=@_;
  180:     my @list;
  181:     my $luri = $uri;
  182:     $luri =~ s/\//_/g;
  183: 
  184:     if ($ENV{'form.dirlistattr'} eq "Refresh") {
  185: 	map {
  186: 	    delete $hash{$_} if ($_ =~ /^dirlist_files_/);
  187: 	    } keys %hash;
  188:     }
  189: 
  190:     if ($hash{'dirlist_files'.$luri}) {
  191: 	@list = split(/\n/,$hash{'dirlist_files_'.$luri});
  192:     } else {
  193: 	@list = &Apache::lonnet::dirlist($uri);
  194: 	$hash{'dirlist_files_'.$luri} = join('\n',@list);
  195:     }
  196:     return @list=&match_ext($r,@list);
  197: }
  198: 
  199: #-------------------------- filters out files based on extensions
  200: sub match_ext {
  201:     my ($r,@packlist)=@_;
  202:     my @trimlist;
  203:     my $nextline;
  204:     my @fileext;
  205:     my $dirptr=16384;
  206: 
  207:     my $tabdir  = $r->dir_config('lonTabDir');
  208:     my $fn = $tabdir."/filetypes.tab";
  209:     if (-e $fn) {
  210: 	my $FH=Apache::File->new($fn);
  211: 	my @content=<$FH>;
  212: 	foreach my $line (@content) {
  213: 	    (my $ext,my $foo) = split /\s+/,$line;
  214: 	    push @fileext,$ext;
  215: 	}
  216:     }
  217:     foreach my $line (@packlist) {
  218: 	chomp $line;
  219: 	$line =~ s/^\/home\/httpd\/html//;
  220: 	my @unpackline = split (/\&/,$line);
  221: 	next if ($unpackline[0] eq ".");
  222: 	next if ($unpackline[0] eq "..");
  223: 	my @filecom = split (/\./,$unpackline[0]);
  224: 	my $fext = pop(@filecom);
  225: 	my $fnptr = $unpackline[3]&$dirptr;
  226:  	if ($fnptr == 0 and $unpackline[3] ne "") {
  227: 	    foreach my $nextline (@fileext) {
  228: 		push @trimlist,$line if $nextline eq $fext;
  229: 	    }
  230: 	} else {
  231: 	    push @trimlist,$line;
  232: 	}
  233:     }
  234:     @trimlist = sort (@trimlist);
  235:     return @trimlist;
  236: }
  237: 
  238: #------------------- displays one line in appropriate table format
  239: sub display_line{
  240:     my ($r,$diropen,$line,$indent,$startdir,@list)=@_;
  241:     my (@pathfn, $fndir, $fnptr);
  242:     my $dirptr=16384;
  243:     my $fileclr="#ffffe6";
  244:     my $iconpath="/res/adm/pages/indexericons/";
  245: 
  246:     my @filecom = split (/\&/,$line);
  247:     my @pathcom = split (/\//,$filecom[0]);
  248:     my $listname = $pathcom[scalar(@pathcom)-1];
  249:     my $fnptr = $filecom[3]&$dirptr;
  250:     my $msg = 'View '.$filecom[0].' resources';
  251:     $msg = 'Close '.$filecom[0].' directory' if $diropen == 1;
  252: 
  253:     my $tabtag="</td>";
  254:     my $i=0;
  255: 
  256:     while ($i<=5) {
  257: 	my $key="form.attr".$i;
  258: 	$tabtag=join('',$tabtag,"<td bgcolor=",$fileclr,">&nbsp;</td>") if $ENV{$key} == 1;
  259: 	$i++;
  260:     }
  261:     if ($filecom[1] eq "domain") {
  262: 	$r->print("<tr>");
  263: 	$r->print("<td bgcolor=$fileclr valign=bottom>");
  264: 	&begin_form ($r,$filecom[0].'/');
  265: 	my $anchor = $filecom[0].'/';
  266: 	$anchor =~ s/\///g;
  267: 	$r->print ("<a name=\"".$anchor."\">\n<input src=\"".$iconpath."comp.blue.gif\"");
  268: 	$r->print (" name=\"$msg\" height=\"22\" type=\"image\" border=\"0\">\n");
  269: 	$r->print("Domain - $listname $tabtag</tr></form>\n");
  270: 	return OK;
  271:     }
  272:     if ($filecom[1] eq "user") {
  273: 	$r->print("<tr>");
  274: 	$r->print("<td bgcolor=$fileclr valign=bottom>\n");
  275: 	my $curdir = $startdir.$filecom[0].'/';
  276: 	&begin_form ($r,$curdir);
  277: 	my $anchor = $curdir;
  278: 	$anchor =~ s/\///g;
  279: #	$r->print ("<a name=\"$anchor\">\n<img src=",$iconpath,"white_space_20_22.gif border=0>\n");
  280: 	$r->print ("<a name=\"$anchor\">\n<img src=",$iconpath,"whitespace1.gif border=0>\n");
  281: 	$r->print ("<input src=\"$iconpath");
  282: 	$r->print ("folder_pointer_closed.gif\"") if $diropen == 0;
  283: 	$r->print ("folder_pointer_opened.gif\"") if $diropen == 1;
  284: 	$r->print (" name=\"$msg\" height=\"22\" type=\"image\" border=\"0\">\n");
  285: 	$r->print ("<img src=",$iconpath,"quill.gif border=0>\n");
  286: 	$r->print ("$listname $tabtag</tr></form>\n");
  287: 	return OK;
  288:     }
  289: # display file
  290:     if ($fnptr == 0 and $filecom[3] ne "") {
  291: 	my @file_ext = split (/\./,$listname);
  292: 	my $curfext = $file_ext[scalar(@file_ext)-1];
  293: 	my $filelink = $startdir.$filecom[0];
  294: 	$r->print("<tr><td bgcolor=$fileclr>");
  295: 
  296: 	if ($indent < 11) {
  297: 	    $r->print("<img src=",$iconpath,"whitespace",$indent,".gif border=0>\n");
  298: 	} else {
  299: 	    my $ten = int($indent/10.);
  300: 	    my $rem = $indent%10.0;
  301: 	    my $count = 0;
  302: 	    while ($count < $ten) {
  303: 		$r->print("<img src=",$iconpath,"whitespace10.gif border=0>\n");
  304: 	    $count++;
  305: 	    }
  306: 	    $r->print("<img src=",$iconpath,"whitespace",$rem,".gif border=0>\n") if $rem > 0;
  307: 	}
  308: 
  309: 	$r->print("<img src=$iconpath$curfext.gif border=0>\n");
  310: 	$r->print(" <a href=$filelink>",$listname,"</a>\n");
  311: 	my $metafile = grep /^$filecom[0]\.meta\&/, @list;
  312: 
  313: 	$r->print (" (<a href=\"javascript:openWindow('".$filelink.".meta', 'metadatafile', '400', '450', 'no', 'yes')\"; TARGET=_self>metadata</a>) ") if ($metafile == 1);
  314: 
  315: 	$r=>print("</td>\n");
  316: 	$r->print("<td bgcolor=$fileclr align=right valign=bottom> ",$filecom[8]," </td>\n") if $ENV{'form.attr0'} == 1;
  317: 	$r->print("<td bgcolor=$fileclr valign=bottom> ".(localtime($filecom[9]))." </td>\n") if $ENV{'form.attr1'} == 1;
  318: 	$r->print("<td bgcolor=$fileclr valign=bottom> ".(localtime($filecom[10]))." </td>\n") if $ENV{'form.attr2'} == 1;
  319: 
  320: 	if ($ENV{'form.attr3'} == 1) {
  321: 	    my $author = &Apache::lonnet::metadata($filelink,'author') if ($metafile == 1);
  322: 	    $author = '&nbsp;' if (!$author);
  323: 	    $r->print("<td bgcolor=$fileclr valign=bottom> ".$author." </td>\n");
  324: 	}
  325: 	if ($ENV{'form.attr4'} == 1) {
  326: 	    my $keywords = &Apache::lonnet::metadata($filelink,'keywords') if ($metafile == 1);
  327: 	    $keywords = '&nbsp;' if (!$keywords);
  328: 	    $r->print("<td bgcolor=$fileclr valign=bottom> ".$keywords." </td>\n");
  329: 	}
  330: 	if ($ENV{'form.attr5'} == 1) {
  331: 	    my $lang = &Apache::lonnet::metadata($filelink,'language') if ($metafile == 1);
  332: 	    $lang = $language{$lang};
  333: 	    $lang = '&nbsp;' if (!$lang);
  334: 	    $r->print("<td bgcolor=$fileclr valign=bottom> ".$lang." </td>\n");
  335: 	}
  336: 	$r->print("</tr>\n");
  337:     }
  338: # -- display directory
  339:     if ($fnptr == $dirptr) {
  340: 	my @file_ext = split (/\./,$listname);
  341: 	my $curfext = $file_ext[scalar(@file_ext)-1];
  342: 	my $curdir = $startdir.$filecom[0].'/';
  343: 	my $anchor = $curdir;
  344: 	$anchor =~ s/\///g;
  345: 	$r->print("<tr><td bgcolor=$fileclr valign=bottom>");
  346: 	&begin_form ($r,$curdir);
  347: 	my $indentm1 = $indent-1;
  348: 	if ($indentm1 < 11) {
  349: 	    $r->print("<img src=",$iconpath,"whitespace",$indentm1,".gif border=0>\n");
  350: 	} else {
  351: 	    my $ten = int($indentm1/10.);
  352: 	    my $rem = $indentm1%10.0;
  353: 	    my $count = 0;
  354: 	    while ($count < $ten) {
  355: 		$r->print("<img src=",$iconpath,"whitespace10.gif border=0>\n");
  356: 	    $count++;
  357: 	    }
  358: 	    $r->print("<img src=",$iconpath,"whitespace",$rem,".gif border=0>\n") if $rem > 0;
  359: 	}
  360: 	$r->print ("<a name=\"$anchor\">\n<input src=\"$iconpath");
  361: 	$r->print ("folder_pointer_closed.gif\"") if $diropen == 0;
  362: 	$r->print ("folder_pointer_opened.gif\"") if $diropen == 1;
  363: 	$r->print (" name=\"$msg\" height=\"22\" type=\"image\" border=\"0\">\n");
  364: 	$r->print("<img src=",$iconpath,"folder_closed.gif border=0>\n") if $diropen == 0;
  365: 	$r->print("<img src=",$iconpath,"folder_opened.gif border=0>\n") if $diropen == 1;
  366: 	$r->print("$listname $tabtag</tr></form>\n");
  367:     }
  368: 
  369: }
  370: 
  371: #---------------------prints the beginning of a form for directory or file link
  372: sub begin_form {
  373:     my ($r,$uri) = @_;
  374:     my $anchor = $uri;
  375:     $anchor =~ s/\///g;
  376:     $r->print ("<form method=\"post\" name=\"dirpath\" action=\"/res/\#$anchor\" enctype=\"application/x-www-form-urlencoded\">\n");
  377:     $r->print ("<input type=hidden name=openuri value=\"$uri\">\n");
  378: 
  379:     for (my $i=0; $i<=5; $i++) {
  380: 	$r->print ("<input type=hidden name=attr$i value=\"1\">\n") if $ENV{'form.attr'.$i} == 1;
  381:     }
  382: }
  383: 
  384: 1;
  385: __END__

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