File:  [LON-CAPA] / loncom / interface / lonindexer.pm
Revision 1.157: download - view: text, annotated - select for diffs
Fri Mar 2 23:17:59 2007 UTC (17 years, 2 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- starting work on moving to distributed DNS, eliminate usage of the lonnet hostanme global

    1: # The LearningOnline Network with CAPA
    2: # Directory Indexer
    3: #
    4: # $Id: lonindexer.pm,v 1.157 2007/03/02 23:17:59 albertel 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: ###
   29: 
   30: ###############################################################################
   31: ##                                                                           ##
   32: ## ORGANIZATION OF THIS PERL MODULE                                          ##
   33: ##                                                                           ##
   34: ## 1. Description of functions                                               ##
   35: ## 2. Modules used by this module                                            ##
   36: ## 3. Choices for different output views (detailed, summary, xml, etc)       ##
   37: ## 4. BEGIN block (to be run once after compilation)                         ##
   38: ## 5. Handling routine called via Apache and mod_perl                        ##
   39: ## 6. Other subroutines                                                      ##
   40: ##                                                                           ##
   41: ###############################################################################
   42: 
   43: package Apache::lonindexer;
   44: 
   45: # ------------------------------------------------- modules used by this module
   46: use strict;
   47: use Apache::lonnet;
   48: use Apache::loncommon();
   49: use Apache::lonhtmlcommon();
   50: use Apache::lonsequence();
   51: use Apache::Constants qw(:common);
   52: use Apache::lonmeta;
   53: use Apache::File;
   54: use Apache::lonlocal;
   55: use Apache::lonsource();
   56: use GDBM_File;
   57: use LONCAPA qw(:match);
   58: 
   59: # ---------------------------------------- variables used throughout the module
   60: my %hash; # global user-specific gdbm file
   61: my %dirs; # keys are directories, values are the open/close status
   62: my %language; # has the reference information present in language.tab
   63: my %dynhash; # hash of hashes for dynamic metadata
   64: my %dynread; # hash of directories already read for dynamic metadata
   65: my %fieldnames; # Metadata fieldnames
   66: # ----- Values which are set by the handler subroutine and are accessible to
   67: # -----     other methods.
   68: my $extrafield; # default extra table cell
   69: my $fnum; # file counter
   70: my $dnum; # directory counter
   71: 
   72: # ----- Used to include or exclude files with certain extensions.
   73: my @Only = ();
   74: my @Omit = ();
   75: 
   76: 
   77: 
   78: 
   79: # ----------------------------- Handling routine called via Apache and mod_perl
   80: sub handler {
   81:     my $r = shift;
   82:     my $c = $r->connection();
   83:     &Apache::loncommon::content_type($r,'text/html');
   84:     &Apache::loncommon::no_cache($r);
   85:     $r->send_http_header;
   86:     return OK if $r->header_only;
   87:     $fnum=0;
   88:     $dnum=0;
   89: 
   90:     # Deal with stupid global variables (is there a way around making
   91:     # these global to this package?  It is just so wrong....)
   92:     undef (@Only);
   93:     undef (@Omit);
   94:     %fieldnames=&Apache::lonmeta::fieldnames();
   95: 
   96: # ------------------------------------- read in machine configuration variables
   97:     my $iconpath= $r->dir_config('lonIconsURL') . "/";
   98:     my $domain  = $r->dir_config('lonDefDomain');
   99:     my $role    = $r->dir_config('lonRole');
  100:     my $loadlim = $r->dir_config('lonLoadLim');
  101:     my $servadm = $r->dir_config('lonAdmEMail');
  102:     my $sysadm  = $r->dir_config('lonSysEMail');
  103:     my $lonhost = $r->dir_config('lonHostID');
  104:     my $tabdir  = $r->dir_config('lonTabDir');
  105: 
  106:     my $fileclr='#ffffe6';
  107:     my $line;
  108:     my (@attrchk,@openpath);
  109:     my $uri=$r->uri;
  110: 
  111: # -------------------------------------- see if called from an interactive mode
  112:     # Get the parameters from the query string
  113:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  114: 	     ['catalogmode','launch','acts','mode','form','element',
  115:               'only','omit','titleelement']);
  116:     #-------------------------------------------------------------------
  117:     my $closebutton='';
  118:     my $groupimportbutton='';
  119:     my $colspan=''; 
  120:     
  121:     $extrafield='';
  122:     my $diropendb = 
  123: 	"/home/httpd/perl/tmp/$env{'user.domain'}_$env{'user.name'}_sel_res.db";
  124:     %hash = ();
  125:     {
  126: 	my %dbfile;
  127: 	if (tie(%dbfile,'GDBM_File',$diropendb,&GDBM_WRITER(),0640)) {
  128: 	    if ($env{'form.launch'} eq '1') {
  129: 		&start_fresh_session(\%dbfile);
  130: 	    }
  131: 
  132: 	    while(my($key,$value)=each(%dbfile)) {
  133: 		$hash{$key}=$value;
  134: 	    }
  135: 	    untie(%dbfile);
  136: 	}
  137:     }
  138:     {
  139:   #Hijack lonindexer to verify a title and be close down.
  140:    if ($env{'form.launch'} eq '2') {
  141:        &Apache::loncommon::content_type($r,'text/html');
  142:        my $extra='';
  143:        if (defined($env{'form.titleelement'}) && 
  144: 	   $env{'form.titleelement'} ne '') {
  145: 	   my $verify_title = &Apache::lonnet::gettitle($env{'form.acts'});
  146: #	   &Apache::lonnet::logthis("Hrrm $env{'form.acts'} -- $verify_title");
  147: 	   $verify_title=~s/'/\\'/g;
  148: 	   $extra='window.opener.document.forms["'.$env{'form.form'}.'"].elements["'.$env{'form.titleelement'}.'"].value=\''.$verify_title.'\';';
  149:        }
  150:        my $js = <<ENDSUBM;
  151: 	       <script type="text/javascript">
  152: 		function load() {
  153: 			window.opener.document.forms["$env{'form.form'}"]
  154: 			    .elements["$env{'form.element'}"]
  155: 			    .value='$env{'form.acts'}';
  156: 			$extra
  157: 			window.close();
  158: 		}
  159:    	       </script>
  160: ENDSUBM
  161:        $r->print(&Apache::loncommon::start_page(undef,$js,
  162: 						{'only_body'   =>1,
  163: 						 'add_entries' =>
  164: 						     {'onload' => "load();"},}
  165: 						).
  166: 		 &Apache::loncommon::end_page());
  167:        return OK;
  168:    }
  169:     
  170: # -------------------- refresh environment with user database values (in %hash)
  171: 	&setvalues(\%hash,'form.catalogmode',\%env,'form.catalogmode'   );
  172: 
  173: # --------------------- define extra fields and buttons in case of special mode
  174: 	if ($env{'form.catalogmode'} eq 'interactive') {
  175: 	    $extrafield='<td bgcolor="'.$fileclr.'" valign="bottom">'.
  176: 		'<img alt="" src="'.$iconpath.'whitespace1.gif"'.
  177: 		' border="0" /></td>';
  178: 	    $colspan=" colspan='2' ";
  179:             my $cl=&mt('Close');
  180:             $closebutton=<<END;
  181: <input type="button" name="close" value='$cl' onClick="self.close()" />
  182: END
  183:         }
  184: 	elsif ($env{'form.catalogmode'} eq 'import') {
  185: 	    $extrafield='<td bgcolor="'.$fileclr.'" valign="bottom">'.
  186: 		'<img alt="" src="'.$iconpath.'whitespace1.gif"'.
  187: 		' border="0" /></td>';
  188: 	    $colspan=" colspan='2' ";
  189: 	    my $cl=&mt('Close');
  190:             my $gi=&mt('Import');
  191:             $closebutton=<<END;
  192: <input type="button" name="close" value='$cl' onClick="self.close()" />
  193: END
  194:             $groupimportbutton=<<END;
  195: <input type="button" name="groupimport" value='$gi'
  196: onClick="javascript:select_group()" />
  197: END
  198:         }
  199: 	# Additions made by Matthew to make the browser a little easier to deal
  200: 	# with in the future.
  201: 	#
  202: 	# $mode (at this time) indicates if we are in edit mode.
  203: 	# $form is the name of the form that the URL is placed when the
  204: 	#       selection is made.
  205: 	# $element is the name of the element in $formname which receives
  206: 	#       the URL.
  207: 	#&Apache::lonxml::debug('Checking mode, form, element');
  208: 	&setvalues(\%hash,'form.mode'        ,\%env,'form.mode'   );
  209: 	&setvalues(\%hash,'form.form'        ,\%env,'form.form'   );
  210: 	&setvalues(\%hash,'form.element'     ,\%env,'form.element');
  211: 	&setvalues(\%hash,'form.titleelement',\%env,'form.titleelement');
  212: 	&setvalues(\%hash,'form.only'        ,\%env,'form.only'   );
  213: 	&setvalues(\%hash,'form.omit'        ,\%env,'form.omit'   );
  214: 
  215:         # Deal with 'omit' and 'only' 
  216:         if (exists $env{'form.omit'}) {
  217:             @Omit = split(',',$env{'form.omit'});
  218:         }
  219:         if (exists $env{'form.only'}) {
  220:             @Only = split(',',$env{'form.only'});
  221:         }
  222:         
  223: 	my $mode = $env{'form.mode'};
  224: 	my ($form,$element,$titleelement);
  225: 	if ($mode eq 'edit' || $mode eq 'parmset') {
  226: 	    $form         = $env{'form.form'};
  227: 	    $element      = $env{'form.element'};
  228: 	    $titleelement = $env{'form.titleelement'};
  229: 	}
  230: 	#&Apache::lonxml::debug("mode=$mode form=$form element=$element titleelement=$titleelement");
  231: # ------ set catalogmodefunctions to have extra needed javascript functionality
  232: 	my $catalogmodefunctions='';
  233: 	if ($env{'form.catalogmode'} eq 'interactive' or
  234: 	    $env{'form.catalogmode'} eq 'import') {
  235: 	    # The if statement below sets us up to use the old version
  236: 	    # by default (ie. if $mode is undefined).  This is the easy
  237: 	    # way out.  Hopefully in the future I'll find a way to get 
  238: 	    # the calls dealt with in a more comprehensive manner.
  239: 
  240: #
  241: # There is now also mode "simple", which is for the simple version of the rat
  242: #
  243: #
  244: 	    if (!defined($mode) || ($mode ne 'edit' && $mode ne 'parmset')) {
  245:                 my $location = "/adm/groupsort?catalogmode=import&";
  246:                 $location .= "mode=".$mode."&";
  247:                 $location .= "acts=";
  248: 		$catalogmodefunctions=<<"END";
  249: function select_data(url) {
  250:     changeURL(url);
  251:     self.close();
  252: }
  253: function select_group() {
  254:     window.location="$location"+document.forms.fileattr.acts.value;
  255: }
  256: function changeURL(val) {
  257:     if (opener.inf) {
  258:         if (opener.inf.document.forms.resinfo.elements.u) {
  259: 	    opener.inf.document.forms.resinfo.elements.u.value=val;
  260:         }
  261:     }
  262: }
  263: END
  264:             } elsif ($mode eq 'edit') { # we are in 'edit' mode
  265:                 my $location = "/adm/groupsort?catalogmode=interactive&";
  266:                 $location .= "form=$form&element=$element&mode=edit&acts=";
  267: 		$catalogmodefunctions=<<END;
  268: // mode = $mode
  269: function select_data(url) {
  270:    var location = "/res/?launch=2&form=$form&element=$element&titleelement=$titleelement&acts=" + url;
  271:    window.location=location;
  272: }
  273: function select_group() {
  274:     window.location="$location"+document.forms.fileattr.acts.value;
  275: }
  276: 
  277: function changeURL(val) {
  278:     if (window.opener.document) {
  279: 	window.opener.document.forms["$form"].elements["$element"].value=val;
  280:     } else {
  281: 	    alert("The file you selected is: "+val);
  282:     }
  283: }
  284: END
  285:                 if (!$titleelement) {
  286: 		    $catalogmodefunctions.='function changeTitle(val) {}';
  287: 		} else {
  288: 		    $catalogmodefunctions.=<<END;
  289: function changeTitle(val) {
  290:     if (window.opener.document) {
  291: 	    window.opener.document.forms["$form"].elements["$titleelement"].value=val;
  292:     } else {
  293: 	    alert("The title of the file you selected is: "+val);
  294:     }
  295: }
  296: END
  297:                 }
  298:             } elsif ($mode eq 'parmset') {
  299:                 my $location = "/adm/groupsort?catalogmode=interactive&";
  300:                 $location .= "form=$form&element=$element&mode=parmset&acts=";
  301: 		$catalogmodefunctions=<<END;
  302: // mode = $mode
  303: function select_data(url) {
  304:     changeURL(url);
  305:     self.close();
  306: }
  307: 
  308: function select_group() {
  309:     window.location="$location"+document.forms.fileattr.acts.value;
  310: }
  311: 
  312: function changeURL(val) {
  313:     if (window.opener.document) {
  314:         var elementname  = "$element"+"_value";
  315:         var checkboxname = "$element"+"_setparmval";
  316: 	window.opener.document.forms["$form"].elements[elementname].value=val;
  317:         window.opener.document.forms["$form"].elements[checkboxname].checked=true;
  318:     } else {
  319: 	    alert("The file you selected is: "+val);
  320:     }
  321: }
  322: 
  323: END
  324:             }
  325:         }
  326:         $catalogmodefunctions.=<<END;
  327: var acts='';
  328: function rep_dirpath(suffix,val) {
  329:     eval("document.forms.dirpath"+suffix+".acts.value=val");
  330: }
  331: END
  332: 	if ($env{'form.catalogmode'} eq 'import') {
  333:             $catalogmodefunctions.=<<END;
  334: function queue(val) {
  335:     if (eval("document.forms."+val+".filelink.checked")) {
  336: 	var l=val.length;
  337: 	var v=val.substring(4,l);
  338: 	document.forms.fileattr.acts.value+='1a'+v+'b';
  339:     }
  340:     else {
  341: 	var l=val.length;
  342: 	var v=val.substring(4,l);
  343: 	document.forms.fileattr.acts.value+='0a'+v+'b';
  344:     }
  345: }
  346: END
  347: 	}
  348: 
  349: # ---------------------------------------------------------------- Print Header
  350: 	my $js = <<"ENDHEADER";
  351: <script type="text/javascript">
  352: $catalogmodefunctions
  353: function openWindow(url, wdwName, w, h, toolbar,scrollbar,locationbar) {
  354:     var xpos = (screen.width-w)/2;
  355:     xpos = (xpos < 0) ? '0' : xpos;
  356:     var ypos = (screen.height-h)/2-30;
  357:     ypos = (ypos < 0) ? '0' : ypos;
  358:     var options = "width=" + w + ",height=" + h + ",screenx="+xpos+",screeny="+ypos+",";
  359:     options += "resizable=yes,scrollbars="+scrollbar+",status=no,";
  360:     options += "menubar=no,toolbar="+toolbar+",location="+locationbar+",directories=no";
  361:     var newWin = window.open(url, wdwName, options);
  362:     newWin.focus();
  363: }
  364: function gothere(val) {
  365:     window.location=val+'?acts='+document.forms.fileattr.acts.value;
  366: }
  367: </script>
  368: ENDHEADER
  369: 
  370:         my ($headerdom)=($uri=~m{^/res/($match_domain)/});
  371: 	$r->print(&Apache::loncommon::start_page('Browse Resources',$js,
  372: 						 {'domain' => $headerdom,}));
  373: # - Evaluate actions from previous page (both cumulatively and chronologically)
  374:         if ($env{'form.catalogmode'} eq 'import') {
  375: 	    my $acts=$env{'form.acts'};
  376: 	    my @Acts=split(/b/,$acts);
  377: 	    my %ahash;
  378: 	    my %achash;
  379: 	    my $ac=0;
  380: 	    # some initial hashes for working with data
  381: 	    foreach (@Acts) {
  382: 		my ($state,$ref)=split(/a/);
  383: 		$ahash{$ref}=$state;
  384: 		$achash{$ref}=$ac;
  385: 		$ac++;
  386: 	    }
  387: 	    # sorting through the actions and changing the global database hash
  388: 	    foreach (sort {$achash{$a}<=>$achash{$b}} (keys %ahash)) {
  389: 		my $key=$_;
  390: 		if ($ahash{$key} eq '1') {
  391: 		    $hash{'store_'.$hash{'pre_'.$key.'_link'}}=
  392: 			$hash{'pre_'.$key.'_title'};
  393: 		    $hash{'storectr_'.$hash{'pre_'.$key.'_link'}}=
  394: 			$hash{'storectr'}+0;
  395: 		    $hash{'storectr'}++;
  396: 		}
  397: 		if ($ahash{$key} eq '0') {
  398: 		    if ($hash{'store_'.$hash{'pre_'.$key.'_link'}}) {
  399: 			delete $hash{'store_'.$hash{'pre_'.$key.'_link'}};
  400: 		    }
  401: 		}
  402: 	    }
  403: 	    # deleting the previously cached listing
  404: 	    foreach (keys %hash) {
  405: 		if ($_ =~ /^pre_/ && $_ =~/link$/) {
  406: 		    my $key = $_;
  407: 		    $key =~ s/^pre_//;
  408: 		    $key =~ s/_[^_]*$//;
  409: 		    delete $hash{'pre_'.$key.'_title'};
  410: 		    delete $hash{'pre_'.$key.'_link'};
  411: 		}
  412: 	    }
  413: 	}
  414: 	
  415: # ---------------------------------- get state of file attributes to be showing
  416: 	if ($env{'form.attrs'}) {
  417: 	    for (my $i=0; $i<=11; $i++) {
  418: 		delete $hash{'display_attrs_'.$i};
  419: 		if ($env{'form.attr'.$i} == 1) {
  420: 		    $attrchk[$i] = 'checked';
  421: 		    $hash{'display_attrs_'.$i} = 1;
  422: 		}
  423: 	    }
  424: 	} else {
  425: 	    for (my $i=0; $i<=11; $i++) {
  426: 		$attrchk[$i] = 'checked' if $hash{'display_attrs_'.$i} == 1;
  427: 	    }
  428: 	}
  429: 
  430: # ------------------------------- output state of file attributes to be showing
  431: #                                 All versions has to the last item
  432: #                                 since it does not take an extra col
  433: 	my %lt=&Apache::lonlocal::texthash(
  434: 					   'ti' => 'Title',
  435: 					   'si' => 'Size',
  436: 					   'la' => 'Last access',
  437: 					   'lm' => 'Last modified',
  438: 					   'st' => 'Statistics',
  439: 					   'au' => 'Author',
  440: 					   'kw' => 'Keywords',
  441: 					   'ln' => 'Language',
  442: 					   'sa' => 'Source Available',
  443: 					   'sr' => 'Show resource',
  444: 					   'li' => 'Linked/Related Resources',
  445: 					   'av' => 'All versions',
  446: 					   'ud' => 'Update Display'
  447: 					   );
  448: 	$r->print(<<END);
  449: <form method="post" name="fileattr" action="$uri"
  450:  enctype="application/x-www-form-urlencoded">
  451: <label><input type="checkbox" name="attr9" value="1" $attrchk[9] onClick="this.form.submit();" /> $lt{'av'}</label>
  452: <table border="0">
  453: <tr>
  454: <td><label><input type="checkbox" name="attr0" value="1" $attrchk[0] onClick="this.form.submit();" /> $lt{'ti'}</label></td>
  455: <td><label><input type="checkbox" name="attr4" value="1" $attrchk[4] onClick="this.form.submit();" /> $lt{'au'}</label></td>
  456: <td><label><input type="checkbox" name="attr5" value="1" $attrchk[5] onClick="this.form.submit();" /> $lt{'kw'}</label></td>
  457: <td><label><input type="checkbox" name="attr6" value="1" $attrchk[6] onClick="this.form.submit();" /> $lt{'ln'}</label></td>
  458: </tr>
  459: <tr>
  460: <td><label><input type="checkbox" name="attr1" value="1" $attrchk[1] onClick="this.form.submit();" /> $lt{'si'}</label></td>
  461: <td><label><input type="checkbox" name="attr2" value="1" $attrchk[2] onClick="this.form.submit();" /> $lt{'la'}</label></td>
  462: <td><label><input type="checkbox" name="attr3" value="1" $attrchk[3] onClick="this.form.submit();" /> $lt{'lm'}</label></td>
  463: <td><label><input type="checkbox" name="attr10" value="1" $attrchk[10] onClick="this.form.submit();" /> $lt{'sa'}</label></td>
  464: </tr>
  465: <tr>
  466: <td><label><input type="checkbox" name="attr8" value="1" $attrchk[8] onClick="this.form.submit();" /> $lt{'st'}</label></td>
  467: <td><label><input type="checkbox" name="attr11" value="1" $attrchk[11] onClick="this.form.submit();" /> $lt{'li'}</label></td>
  468: <td><label><input type="checkbox" name="attr7" value="1" $attrchk[7] onClick="this.form.submit();" /> $lt{'sr'}</label></td>
  469: <td>&nbsp;</td>
  470: </tr>
  471: </table>
  472: <input type="hidden" name="attrs" value="1" />
  473: <input type="submit" name="updatedisplay" value="$lt{'ud'}" />
  474: <input type="hidden" name="acts" value="" />
  475: $closebutton $groupimportbutton
  476: END
  477: # -------------- Filter out sequence containment in crumbs and "recent folders"
  478: 	my $storeuri=$uri;
  479: 	$storeuri='/'.(split(/\.(page|sequence)\/\//,$uri))[-1];
  480: 	$storeuri=~s/\/+/\//g;
  481: # ---------------------------------------------------------------- Bread crumbs
  482:         $r->print(&Apache::lonhtmlcommon::crumbs($storeuri,'','',
  483: 				(($env{'form.catalogmode'} eq 'import')?
  484: 				 'document.forms.fileattr':'')).
  485: 		  &Apache::lonhtmlcommon::select_recent('residx','resrecent',
  486: 'window.status=this.form.resrecent.options[this.form.resrecent.selectedIndex].value;this.form.action=this.form.resrecent.options[this.form.resrecent.selectedIndex].value;this.form.submit();'));
  487: # -------------------------------------------------------- Resource Home Button
  488: 	my $reshome=$env{'course.'.$env{'request.course.id'}.'.reshome'};
  489: 	if ($reshome) {
  490: 	    $r->print("<font size='+2'><a href='");
  491: 	    if ($env{'form.catalogmode'} eq 'import') {
  492: 		$r->print('javascript:document.forms.fileattr.action="'.$reshome.'";document.forms.fileattr.submit();');
  493: 	    } else {
  494: 		$r->print($reshome);
  495: 	    }
  496: 	    $r->print("'>".&mt('Home').'</a></font>');
  497: 	}
  498: 	$r->print('</form>');
  499: # ------------------------------------------------------ Remember where we were
  500: 	&Apache::loncommon::storeresurl($storeuri);
  501: 	&Apache::lonhtmlcommon::store_recent('residx',$storeuri,$storeuri);
  502: # ----------------- output starting row to the indexed file/directory hierarchy
  503:         my $titleclr="#ddffff";
  504:         #$r->print(&initdebug());
  505:         #$r->print(&writedebug("Omit:@Omit")) if (@Omit);
  506:         #$r->print(&writedebug("Only:@Only")) if (@Only);
  507:         $r->print('<table width="100%" border="0"><tr><td bgcolor="#777777">'."\n");
  508: 	$r->print('<table width="100%" border="0"><tr bgcolor="'.$titleclr.'">'."\n");
  509: 	$r->print("<td $colspan><b>".&mt('Name')."</b></td>\n");
  510: 	$r->print("<td><b>".&mt('Title')."</b></td>\n") 
  511: 	    if ($hash{'display_attrs_0'} == 1);
  512: 	$r->print("<td align=right><b>".&mt("Size")." (".&mt("bytes").") ".
  513: 		  "</b></td>\n") if ($hash{'display_attrs_1'} == 1);
  514: 	$r->print("<td><b>".&mt("Last accessed")."</b></td>\n") 
  515: 	    if ($hash{'display_attrs_2'} == 1);
  516: 	$r->print("<td><b>".&mt("Last modified")."</b></td>\n")
  517: 	    if ($hash{'display_attrs_3'} == 1);
  518: 	$r->print("<td><b>".&mt("Author(s)")."</b></td>\n")
  519: 	    if ($hash{'display_attrs_4'} == 1);
  520: 	$r->print("<td><b>".&mt("Keywords")."</b></td>\n")
  521: 	    if ($hash{'display_attrs_5'} == 1);
  522: 	$r->print("<td><b>".&mt("Language")."</b></td>\n")
  523: 	    if ($hash{'display_attrs_6'} == 1);
  524: 	$r->print("<td><b>".&mt("Usage Statistics")." <br />(".
  525: 		  &mt("Courses/Network Hits").")</b> ".&mt('updated periodically')."</td>\n")
  526: 	    if ($hash{'display_attrs_8'} == 1);
  527: 	$r->print("<td><b>".&mt("Source Available")."</b></td>\n")
  528: 	    if ($hash{'display_attrs_10'} == 1);
  529: 	$r->print("<td><b>".&mt("Linked/Related Resources")."</b></td>\n")
  530: 	    if ($hash{'display_attrs_11'} == 1);
  531: 	$r->print("<td><b>".&mt("Resource")."</b></td>\n")
  532: 	    if ($hash{'display_attrs_7'} == 1);
  533: 	$r->print('</tr>');
  534: 
  535: # ----------------- read in what directories have previously been set to "open"
  536: 	foreach (keys %hash) {
  537: 	    if ($_ =~ /^diropen_status_/) {
  538: 		my $key = $_;
  539: 		$key =~ s/^diropen_status_//;
  540: 		$dirs{$key} = $hash{$_};
  541: 	    }
  542: 	}
  543: 
  544: 	if ($env{'form.openuri'}) {  # take care of review and refresh options
  545: 	    my $uri=$env{'form.openuri'};
  546: 	    if (exists($hash{'diropen_status_'.$uri})) {
  547: 		my $cursta = $hash{'diropen_status_'.$uri};
  548: 		$dirs{$uri} = 'open';
  549: 		$hash{'diropen_status_'.$uri} = 'open';
  550: 		if ($cursta eq 'open') {
  551: 		    $dirs{$uri} = 'closed';
  552: 		    $hash{'diropen_status_'.$uri} = 'closed';
  553: 		}
  554: 	    } else {
  555: 		$hash{'diropen_status_'.$uri} = 'open';
  556: 		$dirs{$uri} = 'open';
  557: 	    }
  558: 	}
  559: 	
  560: 	my $toplevel;
  561: 	my $indent = 0;
  562: 	$uri = $uri.'/' if $uri !~ /.*\/$/;
  563: 
  564:  	if ($env{'form.dirPointer'} ne 'on') {
  565:  	    $hash{'top.level'} = $uri;
  566:  	    $toplevel = $uri;
  567:  	} else {
  568:  	    $toplevel = $hash{'top.level'};
  569:  	}
  570: 
  571: # -------------------------------- if not at top level, provide an uplink arrow
  572: 	if ($toplevel ne '/res/'){
  573: 	    my (@uri_com) = split(/\//,$uri);
  574: 	    pop @uri_com;
  575: 	    my $upone = join('/',@uri_com);
  576: 	    my @list = qw (0);
  577: 	    &display_line ($r,'opened',$upone.'&viewOneUp',0,$upone,@list);
  578: 	    $indent = 1;
  579: 	}
  580: 
  581: # -------- recursively go through all the directories and output as appropriate
  582: 	&scanDir ($r,$toplevel,$indent,\%hash);
  583: 	
  584: 
  585: # -------------------------------------------------------------- end the tables
  586: 	$r->print('</table>');
  587: # ---------------------------- embed hidden information useful for group import
  588: 	$r->print("<form name='fnum' action=''>");
  589: 	$r->print("<input type='hidden' name='fnum' value='$fnum' /></form>");
  590: # -------------------------------------------------------------- end the tables
  591: 	$r->print('</td></tr></table>');
  592: 
  593: # --------------------------------------------------- end the output and return
  594: 	$r->print(&Apache::loncommon::end_page()."\n");
  595:     }
  596:     if(! $c->aborted()) {
  597: # write back into the temporary file
  598: 	my %dbfile;
  599:         if (tie(%dbfile,'GDBM_File',$diropendb,&GDBM_NEWDB(),0640)) {
  600:             while (my($key,$value) = each(%hash)) {
  601:                 $dbfile{$key}=$value;
  602:             }
  603:             untie(%dbfile);
  604:         }
  605:     }
  606: 
  607:     return OK;
  608: }
  609: 
  610: # ----------------------------------------------- recursive scan of a directory
  611: sub scanDir {
  612:     my ($r,$startdir,$indent,$hashref)=@_;
  613:     my $c = $r->connection();
  614:     my ($compuri,$curdir);
  615:     my $dirptr=16384;
  616:     my $obs;
  617:     $indent++;
  618:     my %dupdirs = %dirs;
  619:     my @list=&get_list($r,$startdir);
  620:     foreach my $line (@list) {
  621:         return if ($c->aborted());
  622: 	#This is a kludge, sorry aboot this
  623: 	my ($strip,$dom,undef,$testdir,undef,undef,undef,undef,undef,undef,undef,undef,undef,undef,$obs,undef)=split(/\&/,$line,16); 
  624: 	next if($strip =~ /.*\.meta$/ | $obs eq '1');
  625: 	my (@fileparts) = split(/\./,$strip);
  626: 	if ($hash{'display_attrs_9'} != 1) {
  627: # if not all versions to be shown
  628: 	    if (scalar(@fileparts) >= 3) {
  629: 		my $fext = pop @fileparts;
  630: 		my $ov = pop @fileparts;
  631: 		my $fname = join ('.',@fileparts,$fext);
  632: 		next if (grep /\Q$fname\E/,@list and $ov =~ /^\d+$/);
  633: 	    }
  634: 	}
  635: 
  636: 	if ($dom eq 'domain') {
  637: 	    # dom list has full path /res/<domain name>/ already
  638: 	    $curdir='';
  639: 	    $compuri = (split(/\&/,$line))[0];
  640: 	} else {
  641: 	    # user, dir & file have name only, i.e., w/o path
  642: 	    $compuri = join('',$startdir,$strip,'/');
  643: 	    $curdir = $startdir;
  644: 	}
  645: 	my $diropen = 'closed';
  646: 	if (($dirptr&$testdir) or ($dom =~ /^(domain|user)$/) or ($compuri=~/\.(sequence|page)\/$/)) {
  647: 	    while (my ($key,$val)= each %dupdirs) {
  648: 		if ($key eq $compuri and $val eq "open") {
  649: 		    $diropen = "opened";
  650: 		    delete($dupdirs{$key});
  651: 		    delete($dirs{$key});
  652: 		}
  653: 	    }
  654: 	}
  655: 	&display_line($r,$diropen,$line,$indent,$curdir,$hashref,@list);
  656: 	&scanDir ($r,$compuri,$indent) if $diropen eq 'opened';
  657:     }
  658:     $indent--;
  659: }
  660: 
  661: # --------------- get complete matched list based on the uri (returns an array)
  662: sub get_list {
  663:     my ($r,$uri)=@_;
  664:     my @list=();
  665:     (my $luri = $uri) =~ s/\//_/g;
  666:     if ($env{'form.updatedisplay'}) {
  667: 	foreach (keys %hash) {
  668: 	    delete $hash{$_} if ($_ =~ /^dirlist_files_/);
  669: 	    delete $hash{$_} if ($_ =~ /^dirlist_timestamp_files_/);
  670: 	}
  671:     }
  672: 
  673:     if (defined($hash{'dirlist_files_'.$luri}) &&
  674: 	$hash{'dirlist_timestamp_files_'.$luri}+600 > (time)) {
  675: 	@list = split(/\n/,$hash{'dirlist_files_'.$luri});
  676:     } elsif ($uri=~/\.(page|sequence)\/$/) {
  677: # is a page or a sequence
  678: 	$uri=~s/\/$//;
  679: 	$uri='/'.(split(/\.(page|sequence)\/\//,$uri))[-1];
  680: 	$uri=~s/\/+/\//g;
  681: 	foreach (&Apache::lonsequence::attemptread(&Apache::lonnet::filelocation('',$uri))) {
  682: 	    my @ratpart=split(/\:/,$_);
  683: 	    push(@list,&LONCAPA::map::qtescape($ratpart[1]));
  684: 	} 
  685: 	$hash{'dirlist_files_'.$luri} = join("\n",@list);
  686:     } else {
  687: # is really a directory
  688: 	@list = &Apache::lonnet::dirlist($uri);
  689: 	$hash{'dirlist_files_'.$luri} = join("\n",@list);
  690: 	$hash{'dirlist_timestamp_files_'.$luri} = time;
  691:     }
  692:     return @list=&match_ext($r,@list);    
  693: }
  694: 
  695: sub dynmetaread {
  696:     my $uri=shift;
  697:     if (($hash{'display_attrs_8'}==1) || ($hash{'display_attrs_11'}==1)) {
  698: # We don't want the filename
  699: 	$uri=~s/\/[^\/]+$//;
  700: # Did we already see this?
  701: 	my $builddir=$uri;
  702: 	while ($builddir) {
  703: 	    if ($dynread{$builddir}) {
  704: 		return 0;
  705: 	    }
  706: 	    $builddir=~s/\/[^\/]+$//;
  707: 	}
  708: # Actually get the data
  709: 	%dynhash=
  710: 	    (%dynhash,&Apache::lonmeta::get_dynamic_metadata_from_sql($uri.'/'));
  711: # Remember that we got it
  712: 	$dynread{$uri}=1;
  713:     } 
  714: }
  715: 
  716: sub initdebug {
  717:     my $start_page=
  718: 	&Apache::loncommon::start_page('Debug',undef,
  719: 				       {'only_body' => 1,});
  720:     $start_page =~ s/\n/ /g;
  721:     return <<ENDJS;
  722: <script type="text/javascript">
  723: var debugging = true;
  724: if (debugging) {
  725:     var debuggingWindow = window.open('','Debug','width=400,height=300',true);
  726: } 
  727: 
  728: function output(text) {
  729:     if (debugging) {
  730:         debuggingWindow.document.writeln(text);
  731:     }
  732: }
  733: output('$start_page<pre>');   
  734: </script>
  735: ENDJS
  736: }
  737: 
  738: sub writedebug {
  739:     my ($text) = @_;
  740:     return "<script type=\"text/javascript\">output('$text');</script>";
  741: }
  742: 
  743: # -------------------- filters out files based on extensions (returns an array)
  744: sub match_ext {
  745:     my ($r,@packlist)=@_;
  746:     my @trimlist;
  747:     my $nextline;
  748:     my @fileext;
  749:     my $dirptr=16384;
  750: 
  751:     foreach my $line (@packlist) {
  752: 	chomp $line;
  753: 	$line =~ s/^\/home\/httpd\/html//;
  754: 	my @unpackline = split (/\&/,$line);
  755: 	next if ($unpackline[0] eq '.');
  756: 	next if ($unpackline[0] eq '..');
  757: 	my @filecom = split (/\./,$unpackline[0]);
  758: 	my $fext = pop(@filecom);
  759: 	my $fnptr = ($unpackline[3]&$dirptr) || ($fext=~/\.(page|sequence)$/);
  760:  	if ($fnptr == 0 and $unpackline[3] ne "") {
  761: 	    my $embstyle = &Apache::loncommon::fileembstyle($fext);
  762:             push @trimlist,$line if (defined($embstyle) && 
  763: 				     ($embstyle ne 'hdn' or $fext eq 'meta'));
  764: 	} else {
  765: 	    push @trimlist,$line;
  766: 	}
  767:     }
  768:     @trimlist = sort {uc($a) cmp uc($b)} (@trimlist);
  769:     return @trimlist;
  770: }
  771: 
  772: # ------------------------------- displays one line in appropriate table format
  773: sub display_line {
  774:     my ($r,$diropen,$line,$indent,$startdir,$hashref,@list)=@_;
  775:     my (@pathfn, $fndir);
  776: # there could be relative paths (files actually belonging into this directory)
  777: # or absolute paths (for example, from sequences)
  778:     my $absolute;
  779:     my $pathprefix;
  780:     if ($line=~m|^/res/| && $startdir ne '') {
  781: 	$absolute=1;
  782: 	$pathprefix='';
  783:     } else {
  784: 	$absolute=0;
  785: 	$pathprefix=$startdir;
  786:     }
  787:     my $dirptr=16384;
  788:     my $fileclr="#ffffe6";
  789:     my $iconpath= $r->dir_config('lonIconsURL') . '/';
  790: 
  791:     my @filecom = split (/\&/,$line);
  792:     my @pathcom = split (/\//,$filecom[0]);
  793:     my $listname = $pathcom[scalar(@pathcom)-1];
  794:     my $fnptr = $filecom[3]&$dirptr;
  795:     my $msg = &mt('View').' '.$filecom[0].' '.&mt('resources');
  796:     $msg = &mt('Close').' '.$filecom[0].' '.&mt('directory') if $diropen eq 'opened';
  797: 
  798:     my $tabtag='</td>';
  799:     my $i=0;
  800:     while ($i<=11) {
  801: 	$tabtag=join('',$tabtag,"<td>&nbsp;</td>")
  802: 	    if ($i != 9 &&
  803: 		$hash{'display_attrs_'.$i} == 1);
  804: 	$i++;
  805:     }
  806:     my $valign = ($hash{'display_attrs_7'} == 1 ? 'top' : 'bottom');
  807: 
  808: # display uplink arrow
  809:     if ($filecom[1] eq 'viewOneUp') {
  810: 	my $updir=$startdir;
  811: # -------------- Filter out sequence containment in crumbs and "recent folders"
  812: 	$updir='/'.(split(/\.(page|sequence)\/\//,$startdir))[-1];
  813: 	$updir=~s/\/+/\//g;
  814: 
  815: 	$r->print("<tr valign='$valign' bgcolor=$fileclr>$extrafield");
  816: 	$r->print("<td>\n");
  817: 	$r->print ('<form method="post" name="dirpathUP" action="'.$updir.
  818: 		   '/" '.
  819: 		   'onSubmit="return rep_dirpath(\'UP\','.
  820: 		   'document.forms.fileattr.acts.value)" '.
  821: 		   'enctype="application/x-www-form-urlencoded"'.
  822:                    '>'."\n");
  823: 	$r->print ('<input type=hidden name=openuri value="'.
  824: 		   $startdir.'" />'."\n");
  825: 	$r->print ('<input type="hidden" name="acts" value="" />'."\n");
  826: 	$r->print ('<input src="'.$iconpath.'arrow_up.gif"');
  827: 	$r->print (' name="'.$msg.'" height="22" type="image" border="0" />'.
  828: 		   "\n");
  829: 	$r->print(&mt("Up")."</form>$tabtag</tr>\n");
  830: 	return OK;
  831:     }
  832: # Do we have permission to look at this?
  833: 
  834:     if($filecom[15] ne '1') { return OK if (!&Apache::lonnet::allowed('bre',$pathprefix.$filecom[0])); }
  835: 
  836: # make absolute links appear on different background
  837:     if ($absolute) { $fileclr='#ccdd99'; }
  838: 
  839: # display domain
  840:     if ($filecom[1] eq 'domain') {
  841:  	$r->print ('<input type="hidden" name="dirPointer" value="on" />'."\n")
  842:  	    if ($env{'form.dirPointer'} eq "on");
  843: 	$r->print("<tr valign='$valign' bgcolor=$fileclr>$extrafield");
  844: 	$r->print("<td>");
  845: 	&begin_form ($r,$filecom[0]);
  846: 	my $anchor = $filecom[0];
  847: 	$anchor =~ s/\///g;
  848: 	$r->print ('<a name="'.$anchor.'"></a>');
  849: 	$r->print ('<input type="hidden" name="acts" value="" />');
  850: 	$r->print ('<input src="'.$iconpath.'folder_pointer_'.
  851: 		   $diropen.'.gif"'); 
  852: 	$r->print (' name="'.$msg.'" height="22" type="image" border="0" />'.
  853: 		   "\n");
  854: 	my $quotable_filecom = &Apache::loncommon::escape_single($filecom[0]);
  855: 	$r->print ('<a href="javascript:gothere(\''.$quotable_filecom.
  856: 		   '\')"><img alt="" src="'.$iconpath.'server.gif"');
  857: 	$r->print (' border="0" /></a>'."\n");
  858: 	$r->print (&mt("Domain")." - $listname ");
  859: 	if ($Apache::lonnet::domaindescription{$listname}) {
  860: 	    $r->print("(".$Apache::lonnet::domaindescription{$listname}.
  861: 		      ")");
  862: 	}
  863: 	$r->print ("</form>$tabtag</tr>\n");
  864: 	return OK;
  865: 
  866: # display user directory
  867:     }
  868:     if ($filecom[1] eq 'user') {
  869: 	$r->print("<tr valign=$valign bgcolor=$fileclr>$extrafield");
  870: 	$r->print("<td nowrap>\n");
  871: 	my $curdir = $startdir.$filecom[0].'/';
  872: 	my $anchor = $curdir;
  873: 	$anchor =~ s/\///g;
  874: 	&begin_form ($r,$curdir);
  875: 	$r->print ('<a name="'.$anchor.'"></a><img alt="" src="'.$iconpath.
  876: 		   'whitespace1.gif" border="0" />'."\n");
  877: 	$r->print ('<input type="hidden" name="acts" value="" />');
  878: 	$r->print ('<input src="'.$iconpath.'folder_pointer_'.$diropen.
  879: 		   '.gif"'); 
  880: 	$r->print (' name="'.$msg.'" height="22" type="image" border="0" />'.
  881: 		   "\n");
  882: 	my $quotable_curdir = &Apache::loncommon::escape_single($curdir);
  883: 	$r->print ('<a href="javascript:gothere(\''.$quotable_curdir
  884: 		   .'\')"><img alt="" src='.
  885: 		   $iconpath.'quill.gif border="0" name="'.$msg.
  886: 		   '" height="22" /></a>');
  887: 	my $domain=(split(m|/|,$startdir))[2];
  888: 	my $plainname=&Apache::loncommon::plainname($listname,$domain);
  889: 	$r->print ($listname);
  890: 	if (defined($plainname) && $plainname) { $r->print(" ($plainname) "); }
  891: 	$r->print ('</form>'.$tabtag.'</tr>'."\n");
  892: 	return OK;
  893:     }
  894: 
  895: # display file
  896:     if (($fnptr == 0 and $filecom[3] ne '') or $absolute) {
  897: 	my $filelink = $pathprefix.$filecom[0];
  898: 	my @file_ext = split (/\./,$listname);
  899: 	my $curfext = $file_ext[-1];
  900:         if (@Omit) {
  901:             foreach (@Omit) { return OK if ($curfext eq $_); }
  902:         }
  903:         if (@Only) {
  904:             my $skip = 1;
  905:             foreach (@Only) { $skip = 0 if ($curfext eq $_); }
  906:             return OK if ($skip > 0);
  907:         }
  908: 	# Set the icon for the file
  909: 	my $iconname = &Apache::loncommon::icon($listname);
  910: 	$r->print("<tr valign='$valign' bgcolor=$fileclr><td nowrap='nowrap' valign='top'>");
  911: 	
  912:         if ($env{'form.catalogmode'} eq 'interactive') {
  913: 	    my $quotable_filelink = &Apache::loncommon::escape_single($filelink);
  914:             $r->print("<a href=\"javascript:select_data(\'",
  915:                       $quotable_filelink,"')\">");
  916: 	    $r->print("<img alt=\"\" src='",$iconpath,"select.gif' border='0' /></a>".
  917: 		      "\n");
  918: 	    $r->print("</td><td nowrap='nowrap' valign='top'>");
  919: 	} elsif ($env{'form.catalogmode'} eq 'import') {
  920: 	    $r->print("<form name='form$fnum' action=''>\n");
  921: 	    $r->print("<input type='checkbox' name='filelink"."' ".
  922: 		      "value='$filelink' onClick='".
  923: 		      "javascript:queue(\"form$fnum\")' ");
  924: 	    if ($hash{'store_'.$filelink}) {
  925: 		$r->print("checked");
  926: 	    }
  927: 	    $r->print(" />\n");
  928: 	    $r->print("</form></td><td nowrap='nowrap' valign='top'>");
  929: 	    $hash{"pre_${fnum}_link"}=$filelink;
  930:   	    $fnum++;
  931: 	}
  932: # Form to open or close sequences
  933: 	if ($filelink=~/\.(page|sequence)$/) {
  934: 	    my $curdir = $startdir.$filecom[0].'/';
  935: 	    my $anchor = $curdir;
  936: 	    $anchor =~ s/\///g;
  937: 	    &begin_form($r,$curdir);
  938: 	    $indent--;
  939: 	}
  940: # General indentation
  941: 	if ($indent > 0 and $indent < 11) {
  942: 	    $r->print("<img alt=\"\" src=",$iconpath,"whitespace",$indent,
  943: 		      ".gif border='0' />\n");
  944: 	} elsif ($indent >0) {
  945: 	    my $ten = int($indent/10.);
  946: 	    my $rem = $indent%10.0;
  947: 	    my $count = 0;
  948: 	    while ($count < $ten) {
  949: 		$r->print("<img alt=\"\" src=",$iconpath,
  950: 			  "whitespace10.gif border='0' />\n");
  951: 	    $count++;
  952: 	    }
  953: 	    $r->print("<img alt=\"\" src=",$iconpath,"whitespace",$rem,
  954: 		      ".gif border='0' />\n") if $rem > 0;
  955: 	}
  956: # Sequence open/close icon
  957: 	if ($filelink=~/\.(page|sequence)$/) {
  958: 	    my $curdir = $startdir.$filecom[0].'/';
  959: 	    my $anchor = $curdir;
  960: 	    $anchor =~ s/\///g;
  961: 	    $r->print ('<input type="hidden" name="acts" value="" />');
  962: 	    $r->print ('<a name="'.$anchor.'"></a><input src="'.$iconpath.
  963: 		       'folder_pointer_'.$diropen.'.gif"');
  964: 	    $r->print (' name="'.$msg.'" height="22" type="image" border="0" />'.
  965: 		       "\n");
  966: 	}
  967: # Filetype icons
  968: 	$r->print("<img alt=\"\" src='$iconname' border='0' />\n");
  969: 	my $quotable_filelink = &Apache::loncommon::escape_single($filelink);
  970: 
  971: 
  972: 	$r->print (" <a href=\"javascript:openWindow('".$quotable_filelink.
  973: 		   "', 'previewfile', '450', '500', 'no', 'yes','yes');\"".
  974: 		   " target=\"_self\">$listname</a> ");
  975: 
  976: 	$r->print (" (<a href=\"javascript:openWindow('".$quotable_filelink.
  977: 		   ".meta', 'metadatafile', '500', '550', 'no', 'yes','no');\" ".
  978: 		   " target=\"_self\">metadata</a>) ");
  979: # Close form to open/close sequence
  980: 	if ($filelink=~/\.(page|sequence)$/) {
  981: 	    $r->print('</form>');
  982: 	}
  983: 	$r->print("</td>\n");
  984: 	if ($hash{'display_attrs_0'} == 1) {
  985: 	    my $title = &Apache::lonnet::gettitle($filelink,'title');
  986: 	    $r->print('<td> '.($title eq '' ? '&nbsp;' : $title).
  987: 		      ' </td>'."\n");
  988: 	}
  989: 	$r->print('<td align=right> ',
  990: 		  $filecom[8]," </td>\n") 
  991: 	    if $hash{'display_attrs_1'} == 1;
  992: 	$r->print('<td> '.
  993: 		  (localtime($filecom[9]))." </td>\n") 
  994: 	    if $hash{'display_attrs_2'} == 1;
  995: 	$r->print('<td> '.
  996: 		  (localtime($filecom[10]))." </td>\n") 
  997: 	    if $hash{'display_attrs_3'} == 1;
  998: 
  999: 	if ($hash{'display_attrs_4'} == 1) {
 1000: 	    my $author = &Apache::lonnet::metadata($filelink,'author');
 1001: 	    $r->print('<td> '.($author eq '' ? '&nbsp;' : $author).
 1002: 		      " </td>\n");
 1003: 	}
 1004: 	if ($hash{'display_attrs_5'} == 1) {
 1005: 	    my $keywords = &Apache::lonnet::metadata($filelink,'keywords');
 1006: 	    # $keywords = '&nbsp;' if (!$keywords);
 1007: 	    $r->print('<td> '.($keywords eq '' ? '&nbsp;' : $keywords).
 1008: 		      " </td>\n");
 1009: 	}
 1010: #'
 1011: 
 1012: 	if ($hash{'display_attrs_6'} == 1) {
 1013: 	    my $lang = &Apache::lonnet::metadata($filelink,'language');
 1014: 	    $lang = &Apache::loncommon::languagedescription($lang);
 1015: 	    $r->print('<td> '.($lang eq '' ? '&nbsp;' : $lang).
 1016: 		      " </td>\n");
 1017: 	}
 1018: 	if ($hash{'display_attrs_8'} == 1) {
 1019: # statistics
 1020: 	    &dynmetaread($filelink);
 1021: 	    $r->print("<td>");
 1022: 	    &dynmetaprint($r,$filelink,'count');
 1023: 	    &dynmetaprint($r,$filelink,'course');
 1024: 	    &dynmetaprint($r,$filelink,'stdno');
 1025: 	    &dynmetaprint($r,$filelink,'avetries');
 1026: 	    &dynmetaprint($r,$filelink,'difficulty');
 1027: 	    &dynmetaprint($r,$filelink,'disc');
 1028: 	    &dynmetaprint($r,$filelink,'clear');
 1029: 	    &dynmetaprint($r,$filelink,'technical');
 1030: 	    &dynmetaprint($r,$filelink,'correct');
 1031: 	    &dynmetaprint($r,$filelink,'helpful');
 1032: 	    &dynmetaprint($r,$filelink,'depth');
 1033: 	    $r->print("&nbsp;</td>\n");
 1034: 
 1035: 	}
 1036: 	if ($hash{'display_attrs_10'} == 1) {
 1037: 	    my $source = &Apache::lonnet::metadata($filelink,'sourceavail');
 1038: 	    if($source eq 'open') {
 1039: 		my $sourcelink = &Apache::lonsource::make_link($filelink,$listname);
 1040: 		my $quotable_sourcelink = &Apache::loncommon::escape_single($sourcelink);
 1041: 		$r->print('<td>'."<a href=\"javascript:openWindow('"
 1042: 			  .$quotable_sourcelink.
 1043: 			  "', 'previewsource', '700', '700', 'no', 'yes','yes');\"".
 1044: 			  " target=\"_self\">Yes</a> "."</td>\n");
 1045: 	    } else { #A cuddled else. :P
 1046: 		$r->print("<td>&nbsp;</td>\n");
 1047: 	    }
 1048: 	}
 1049: 	if ($hash{'display_attrs_11'} == 1) {
 1050: # links
 1051: 	   &dynmetaread($filelink);
 1052: 	   $r->print('<td>');
 1053: 	   &coursecontext($r,$filelink);
 1054: 	   &dynmetaprint($r,$filelink,'goto_list');
 1055: 	   &dynmetaprint($r,$filelink,'comefrom_list');
 1056: 	   &dynmetaprint($r,$filelink,'sequsage_list');
 1057: 	   &dynmetaprint($r,$filelink,'dependencies');
 1058:            &dynmetaprint($r,$filelink,'course_list');
 1059: 	   $r->print('</td>');
 1060:         }
 1061:         if ($hash{'display_attrs_7'} == 1) {
 1062: # Show resource
 1063: 	   my $output=&showpreview($filelink);
 1064:            $r->print('<td> '.($output eq '' ? '&nbsp;':$output).
 1065: 		      " </td>\n");
 1066:         }
 1067: 	$r->print("</tr>\n");
 1068:     }
 1069: 
 1070: # -- display directory
 1071:     if ($fnptr == $dirptr) {
 1072: 	my $curdir = $startdir.$filecom[0].'/';
 1073: 	my $anchor = $curdir;
 1074: 	$anchor =~ s/\///g;
 1075: 	$r->print("<tr bgcolor=$fileclr>$extrafield<td valign=$valign>");
 1076: 	&begin_form ($r,$curdir);
 1077: 	my $indentm1 = $indent-1;
 1078: 	if ($indentm1 < 11 and $indentm1 > 0) {
 1079: 	    $r->print("<img alt=\"\" src=",$iconpath,"whitespace",$indentm1,
 1080: 		      ".gif border='0' />\n");
 1081: 	} else {
 1082: 	    my $ten = int($indentm1/10.);
 1083: 	    my $rem = $indentm1%10.0;
 1084: 	    my $count = 0;
 1085: 	    while ($count < $ten) {
 1086: 		$r->print ("<img alt=\"\" src=",$iconpath
 1087: 			   ,"whitespace10.gif border='0' />\n");
 1088: 		$count++;
 1089: 	    }
 1090: 	    $r->print ("<img alt=\"\" src=",$iconpath,"whitespace",$rem,
 1091: 		       ".gif border='0' />\n") if $rem > 0;
 1092: 	}
 1093: 	$r->print ('<input type="hidden" name="acts" value="" />');
 1094: 	$r->print ('<a name="'.$anchor.'"></a><input src="'.$iconpath.
 1095: 		   'folder_pointer_'.$diropen.'.gif"');
 1096: 	$r->print (' name="'.$msg.'" height="22" type="image" border="0" />'.
 1097: 		   "\n");
 1098: 	my $quotable_curdir = &Apache::loncommon::escape_single($curdir);
 1099: 	$r->print ('<a href="javascript:gothere(\''
 1100: 		   .$quotable_curdir.'\')">'
 1101: 		   .'<img alt="'.&mt('Open Folder').'"src="'.
 1102: 		   $iconpath.'folder_'.$diropen.'.gif" border="0" /></a>'.
 1103: 		   "\n");
 1104: 	$r->print ("$listname</form></td>\n");
 1105: # Attributes
 1106: 	my $filelink = $startdir.$filecom[0].'/default';
 1107: 
 1108: 	if ($hash{'display_attrs_0'} == 1) {
 1109: 	    my $title = &Apache::lonnet::gettitle($filelink,'title');
 1110: 	    $r->print('<td> '.($title eq '' ? '&nbsp;' : $title).
 1111: 		      ' </td>'."\n");
 1112: 	}
 1113: 	$r->print('<td align=right> ',
 1114: 		  $filecom[8]," </td>\n") 
 1115: 	    if $hash{'display_attrs_1'} == 1;
 1116: 	$r->print('<td> '.
 1117: 		  (localtime($filecom[9]))." </td>\n") 
 1118: 	    if $hash{'display_attrs_2'} == 1;
 1119: 	$r->print('<td> '.
 1120: 		  (localtime($filecom[10]))." </td>\n") 
 1121: 	    if $hash{'display_attrs_3'} == 1;
 1122: 
 1123: 	if ($hash{'display_attrs_4'} == 1) {
 1124: 	    my $author = &Apache::lonnet::metadata($filelink,'author');
 1125: 	    $r->print('<td> '.($author eq '' ? '&nbsp;' : $author).
 1126: 		      " </td>\n");
 1127: 	}
 1128: 	if ($hash{'display_attrs_5'} == 1) {
 1129: 	    my $keywords = &Apache::lonnet::metadata($filelink,'keywords');
 1130: 	    # $keywords = '&nbsp;' if (!$keywords);
 1131: 	    $r->print('<td> '.($keywords eq '' ? '&nbsp;' : $keywords).
 1132: 		      " </td>\n");
 1133: 	}
 1134: 	if ($hash{'display_attrs_6'} == 1) {
 1135: 	    my $lang = &Apache::lonnet::metadata($filelink,'language');
 1136: 	    $lang = &Apache::loncommon::languagedescription($lang);
 1137: 	    $r->print('<td> '.($lang eq '' ? '&nbsp;' : $lang).
 1138: 		      " </td>\n");
 1139: 	}
 1140: 	if ($hash{'display_attrs_8'} == 1) {
 1141: 	   $r->print('<td>&nbsp;</td>');
 1142: 	}
 1143:  	if ($hash{'display_attrs_10'} == 1) {
 1144: 	   $r->print('<td>&nbsp;</td>');
 1145: 	}
 1146: 	if ($hash{'display_attrs_11'} == 1) {
 1147: 	   $r->print('<td>&nbsp;</td>');
 1148: 	}
 1149: 	if ($hash{'display_attrs_7'} == 1) {
 1150: 	   $r->print('<td>&nbsp;</td>');
 1151:         }
 1152: 	$r->print('</tr>');
 1153:     }
 1154: 
 1155: }
 1156: 
 1157: sub coursecontext {
 1158:     my ($r,$filelink)=@_;
 1159:     my $filesymb=&Apache::lonnet::symbread($filelink);
 1160:     if ($filesymb) {
 1161: 	my ($map,$index,$resource)=&Apache::lonnet::decode_symb($filesymb);
 1162: 	$r->print(&mt('Already in this course:<br />[_1] in folder/map [_2].<br />',
 1163: 	      &Apache::lonnet::gettitle($resource),
 1164: 	      &Apache::lonnet::gettitle($map)));
 1165:     }
 1166: }
 1167: 
 1168: sub showpreview {
 1169:     my ($filelink)=@_;
 1170:     if ($filelink=~m-^(/ext/|http://)-) {
 1171: 	return &mt('External Resource, preview not enabled');
 1172:     }
 1173:     my ($curfext)=($filelink=~/\.(\w+)$/);
 1174:     my $output='';
 1175:     my $embstyle=&Apache::loncommon::fileembstyle($curfext);
 1176:     if ($embstyle eq 'ssi') {
 1177:        my $cache=$Apache::lonnet::perlvar{'lonDocRoot'}.$filelink.
 1178:                     '.tmp';
 1179:        if ((!$env{'form.updatedisplay'}) &&
 1180:                     (-e $cache)) {
 1181:           open(FH,$cache);
 1182:           $output=join("\n",<FH>);
 1183:           close(FH);
 1184:        } else {
 1185: # In update display mode, remove old cache. This is done to retroactively
 1186: # clean up course context renderings.
 1187: 	  if (-e $cache) {
 1188: 	       unlink($cache);
 1189: 	  }
 1190:           $output=&Apache::lonnet::ssi_body($filelink);
 1191: # Is access denied? Don't render, don't store
 1192:           if ($output=~/LONCAPAACCESSCONTROLERRORSCREEN/s) {
 1193:              $output='';
 1194: # Was this rendered in course content? Don't store
 1195:           } elsif (!&Apache::lonnet::symbread($filelink)) {
 1196:              open(FH,">$cache");
 1197:              print FH $output;
 1198:              close(FH);
 1199:           }
 1200:        }
 1201:        $output='<font size="-2">'.$output.'</font>';
 1202:     } elsif ($embstyle eq 'img') {
 1203:        $output='<img alt="'.&mt('Preview').'" src="'.$filelink.'" />';
 1204:     } elsif ($filelink=~m{^/res/($match_domain)/($match_username)/}) {
 1205:        $output='<img  alt="'.&mt('Preview').'" src="http://'.
 1206:                  &Apache::lonnet::hostname(&Apache::lonnet::homeserver($2,$1)).
 1207:                  '/cgi-bin/thumbnail.gif?url='.$filelink.'" />';
 1208:     }
 1209:     return $output;
 1210: }
 1211: 
 1212: sub dynmetaprint {
 1213:     my ($r,$filelink,$item)=@_;
 1214:     if ($dynhash{$filelink}->{$item}) {
 1215: 	$r->print("\n<br />".$fieldnames{$item}.': '.
 1216: 		  &Apache::lonmeta::prettyprint($item,
 1217: 						$dynhash{$filelink}->{$item},
 1218: 		  (($env{'form.catalogmode'} ne 'import')?'preview':''),
 1219: 		  '',
 1220: 		  (($env{'form.catalogmode'} eq 'import')?'document.forms.fileattr':''),1));
 1221:     }
 1222: }
 1223: 
 1224: # ------------------- prints the beginning of a form for directory or file link
 1225: sub begin_form {
 1226:     my ($r,$uri) = @_;
 1227:     my $anchor = $uri;
 1228:     $anchor =~ s/\///g;
 1229:     $r->print ('<form method="post" name="dirpath'.$dnum.'" action="'.$uri.
 1230: 	       '#'.$anchor.
 1231: 	       '" onSubmit="return rep_dirpath(\''.$dnum.'\''.
 1232: 	       ',document.forms.fileattr.acts.value)" '.
 1233: 	       'enctype="application/x-www-form-urlencoded">'."\n");
 1234:     $r->print ('<input type="hidden" name="openuri" value="'.$uri.'" />'.
 1235: 	       "\n");
 1236:     $r->print ('<input type="hidden" name="dirPointer" value="on" />'."\n");
 1237:     $dnum++;
 1238: }
 1239: 
 1240: # --------- settings whenever the user causes the indexer window to be launched
 1241: sub start_fresh_session {
 1242:     my ($hash) = @_;
 1243:     delete $hash->{'form.catalogmode'};
 1244:     delete $hash->{'form.mode'};
 1245:     delete $hash->{'form.form'};
 1246:     delete $hash->{'form.element'};
 1247:     delete $hash->{'form.omit'};
 1248:     delete $hash->{'form.only'};
 1249:     foreach (keys %{$hash}) {
 1250:         delete $hash->{$_} if (/^(pre_|store)/);
 1251:     }
 1252: }
 1253: 
 1254: # ------------------------------------------------------------------- setvalues
 1255: sub setvalues {
 1256:     # setvalues is used in registerurl to synchronize the database
 1257:     # hash and environment hashes
 1258:     my ($H1,$h1key,$H2,$h2key) =@_;
 1259:     #
 1260:     if (exists $H2->{$h2key}) {
 1261: 	$H1->{$h1key} = $H2->{$h2key};
 1262:     } elsif (exists $H1->{$h1key}) {
 1263: 	$H2->{$h2key} = $H1->{$h1key};
 1264:     } 
 1265: }
 1266: 
 1267: 1;
 1268: 
 1269: sub cleanup {
 1270:     if (tied(%hash)){
 1271: 	&Apache::lonnet::logthis('Cleanup indexer: hash');
 1272:     }
 1273:     return OK;
 1274: }
 1275: 
 1276: 
 1277: 
 1278: 
 1279: 
 1280: =head1 NAME
 1281: 
 1282: Apache::lonindexer - mod_perl module for cross server filesystem browsing
 1283: 
 1284: =head1 SYNOPSIS
 1285: 
 1286: Invoked by /etc/httpd/conf/srm.conf:
 1287: 
 1288:  <LocationMatch "^/res.*/$">
 1289:  SetHandler perl-script
 1290:  PerlHandler Apache::lonindexer
 1291:  </LocationMatch>
 1292: 
 1293: =head1 INTRODUCTION
 1294: 
 1295: This module enables a scheme of browsing across a cross server.
 1296: 
 1297: This is part of the LearningOnline Network with CAPA project
 1298: described at http://www.lon-capa.org.
 1299: 
 1300: =head1 BEGIN SUBROUTINE
 1301: 
 1302: This routine is only run once after compilation.
 1303: 
 1304: =over 4
 1305: 
 1306: =item *
 1307: 
 1308: Initializes %language hash table.
 1309: 
 1310: =back
 1311: 
 1312: =head1 HANDLER SUBROUTINE
 1313: 
 1314: This routine is called by Apache and mod_perl.
 1315: 
 1316: =over 4
 1317: 
 1318: =item *
 1319: 
 1320: read in machine configuration variables
 1321: 
 1322: =item *
 1323: 
 1324: see if called from an interactive mode
 1325: 
 1326: =item *
 1327: 
 1328: refresh environment with user database values (in %hash)
 1329: 
 1330: =item *
 1331: 
 1332: define extra fields and buttons in case of special mode
 1333: 
 1334: =item *
 1335: 
 1336: set catalogmodefunctions to have extra needed javascript functionality
 1337: 
 1338: =item *
 1339: 
 1340: print header
 1341: 
 1342: =item *
 1343: 
 1344: evaluate actions from previous page (both cumulatively and chronologically)
 1345: 
 1346: =item *
 1347: 
 1348: output title
 1349: 
 1350: =item *
 1351: 
 1352: get state of file attributes to be showing
 1353: 
 1354: =item *
 1355: 
 1356: output state of file attributes to be showing
 1357: 
 1358: =item *
 1359: 
 1360: output starting row to the indexed file/directory hierarchy
 1361: 
 1362: =item *
 1363: 
 1364: read in what directories have previously been set to "open"
 1365: 
 1366: =item *
 1367: 
 1368: if not at top level, provide an uplink arrow
 1369: 
 1370: =item *
 1371: 
 1372: recursively go through all the directories and output as appropriate
 1373: 
 1374: =item *
 1375: 
 1376: information useful for group import
 1377: 
 1378: =item *
 1379: 
 1380: end the tables
 1381: 
 1382: =item *
 1383: 
 1384: end the output and return
 1385: 
 1386: =back
 1387: 
 1388: =head1 OTHER SUBROUTINES
 1389: 
 1390: =over 4
 1391: 
 1392: =item *
 1393: 
 1394: scanDir - recursive scan of a directory
 1395: 
 1396: =item *
 1397: 
 1398: get_list - get complete matched list based on the uri (returns an array)
 1399: 
 1400: =item *
 1401: 
 1402: match_ext - filters out files based on extensions (returns an array)
 1403: 
 1404: =item *
 1405: 
 1406: display_line - displays one line in appropriate table format
 1407: 
 1408: =item *
 1409: 
 1410: begin_form - prints the beginning of a form for directory or file link
 1411: 
 1412: =item *
 1413: 
 1414: start_fresh_session - settings whenever the user causes the indexer window
 1415: to be launched
 1416: 
 1417: =back
 1418: 
 1419: =cut

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