File:  [LON-CAPA] / loncom / publisher / lonpubdir.pm
Revision 1.112: download - view: text, annotated - select for diffs
Mon Nov 10 14:11:08 2008 UTC (15 years, 6 months ago) by jms
Branches: MAIN
CVS tags: HEAD
Added/updated POD documentation

    1: # The LearningOnline Network with CAPA
    2: # Construction Space Directory Lister
    3: #
    4: # $Id: lonpubdir.pm,v 1.112 2008/11/10 14:11:08 jms 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: =head1 NAME
   31: 
   32: Apache::lonpubdir - Construction space directory lister
   33: 
   34: =head1 SYNOPSIS
   35: 
   36: Invoked (for various locations) by /etc/httpd/conf/srm.conf:
   37: 
   38:  <LocationMatch "^/\~.*/$">
   39:  PerlAccessHandler       Apache::loncacc
   40:  SetHandler perl-script
   41:  PerlHandler Apache::lonpubdir
   42:  ErrorDocument     403 /adm/login
   43:  ErrorDocument     404 /adm/notfound.html
   44:  ErrorDocument     406 /adm/unauthorized.html
   45:  ErrorDocument	  500 /adm/errorhandler
   46:  </LocationMatch>
   47: 
   48:  <Location /adm/pubdir>
   49:  PerlAccessHandler       Apache::lonacc
   50:  SetHandler perl-script
   51:  PerlHandler Apache::lonpubdir
   52:  ErrorDocument     403 /adm/login
   53:  ErrorDocument     404 /adm/notfound.html
   54:  ErrorDocument     406 /adm/unauthorized.html
   55:  ErrorDocument	  500 /adm/errorhandler
   56:  </Location>
   57: 
   58: =head1 INTRODUCTION
   59: 
   60: This module publishes a directory of files.
   61: 
   62: This is part of the LearningOnline Network with CAPA project
   63: described at http://www.lon-capa.org.
   64: 
   65: =head1 HANDLER SUBROUTINE
   66: 
   67: This routine is called by Apache and mod_perl.
   68: 
   69: =over 4
   70: 
   71: =item *
   72: 
   73: read in information
   74: 
   75: =item *
   76: 
   77: start page output
   78: 
   79: =item *
   80: 
   81: run through list of files and attempt to publish unhidden files
   82: 
   83: =back
   84: 
   85: =head2 OTHER SUBROUTINES:
   86: 
   87: =head3 startpage($r, $uame, $udom, $thisdisfn)
   88: 
   89: Output the header of the page.  This includes:
   90:  - The HTML header 
   91:  - The H1/H3  stuff which includes the directory.
   92:  
   93:     startpage($r, $uame, $udom, $thisdisfn);
   94:         $r     - The apache request object.
   95:         $uname - User name.
   96:         $udom  - Domain name the user is logged in under.
   97:         $thisdisfn - Displayable version of the filename.
   98: 
   99: =head3 getTitleString($fullname)
  100: 
  101:     Get the title string or "[untitled]" if the file has no title metadata:
  102:     Without the latter substitution, it's impossible to examine metadata for
  103:     untitled resources.  Resources may be legitimately untitled, to prevent
  104:     searches from locating them.
  105:     
  106:     $str = getTitleString($fullname);
  107:         $fullname - Fully qualified filename to check.
  108: 
  109: =head3 putdirectory(r, base, here, dirname, modtime)
  110: 
  111:     Put out a directory table row:
  112:     
  113:     putdirectory($r, $base, $here, $dirname, $modtime)
  114:         $r       - Apache request object.
  115:         $reqfile - File in request.
  116:         $here    - Where we are in directory tree.
  117:         $dirname - Name of directory special file.
  118:         $modtime - Encoded modification time.
  119: 
  120: =head3 CategorizeFiles($location, $files)
  121:     
  122:     Categorize files in the directory.
  123:     For each file in a list of files in a file directory, 
  124:     the  file categorized as one of:
  125:         - directory  
  126:         - sequence
  127:         - problem 
  128:         - Other resource.
  129: 
  130:     For each file the modification date is determined as well.
  131:     Returned is a list of sublists:
  132:         (directories, sequences, problems, other)
  133:     each of the sublists contains entries of the following form (sorted by filename):
  134:     (filename, typecode, lastmodtime)
  135:     
  136:         $list = CategorizeFiles($location, $files)
  137:         $location   - Directory in which the files live (relative to our execution)
  138:         $files      - list of files.
  139: 
  140: =cut
  141: 
  142: package Apache::lonpubdir;
  143: 
  144: use strict;
  145: use Apache::File;
  146: use File::Copy;
  147: use Apache::Constants qw(:common :http :methods);
  148: use Apache::loncacc;
  149: use Apache::loncommon();
  150: use Apache::lonhtmlcommon();
  151: use Apache::londiff();
  152: use Apache::lonlocal;
  153: use Apache::lonmsg;
  154: use Apache::lonmenu;
  155: use Apache::lonnet;
  156: use LONCAPA;
  157: 
  158: sub handler {
  159: 
  160:   my $r=shift;
  161: 
  162:   my $fn;
  163: 
  164: 
  165: 
  166:   $fn = getEffectiveUrl($r);
  167: 
  168:   # Validate access to the construction space and get username@domain.
  169: 
  170:   my $uname;
  171:   my $udom;
  172: 
  173:   ($uname,$udom)=
  174:     &Apache::loncacc::constructaccess(
  175:              $fn,$r->dir_config('lonDefDomain')); 
  176:   unless (($uname) && ($udom)) {
  177:      $r->log_reason($uname.' at '.$udom.
  178:          ' trying to list directory '.$env{'form.filename'}.
  179:          ' ('.$fn.') - not authorized', 
  180:          $r->filename); 
  181:      return HTTP_NOT_ACCEPTABLE;
  182:   }
  183: 
  184:   # Remove trailing / from directory name.
  185: 
  186:   $fn=~s/\/$//;
  187: 
  188:   unless ($fn) { 
  189:      $r->log_reason($env{'user.name'}.' at '.$env{'user.domain'}.
  190:          ' trying to list empty directory', $r->filename); 
  191:      return HTTP_NOT_FOUND;
  192:   } 
  193: 
  194: # ----------------------------------------------------------- Start page output
  195: 
  196:   my $thisdisfn=$fn;
  197:   $thisdisfn=~s/^\/home\/$uname\/public_html//;	# subdirectory part of
  198:                                                 # construction space. 
  199:   my $docroot=$r->dir_config('lonDocRoot');     # Apache  londocument root.
  200: 
  201:   my $resdir=$docroot.'/res/'.$udom.'/'.$uname.$thisdisfn; # Resource directory
  202:   my $targetdir=$udom.'/'.$uname.$thisdisfn; # Publiction target directory.
  203:   my $linkdir='/priv/'.$uname.$thisdisfn;      # Full URL name of constr space.
  204: 
  205:   my %bombs=&Apache::lonmsg::all_url_author_res_msg($uname,$udom);
  206: 
  207:   &startpage($r, $uname, $udom, $thisdisfn);   # Put out the start of page.
  208:   if ($env{'environment.remote'} eq 'off') {
  209:       &dircontrols($r,$uname,$udom,$thisdisfn);    # Put out actions for directory, 
  210:                                                # browse/upload + new file page.
  211:   } else {
  212:       &pubbuttons($r,$uname,$thisdisfn);
  213:   }
  214:   &resourceactions($r,$uname,$udom,$thisdisfn); #Put out form used for printing/deletion etc.
  215: 
  216:   my $numdir = 0;
  217:   my $numres = 0;
  218:   
  219:   # Start off the directory table.
  220:   $r->print('<h3>'.&mt('Directory Contents:').'</h3>');
  221:   $r->print('<table id="LC_browser"><tr>'.
  222:             '<th>'.&mt('Type').'</th>'.
  223:             '<th>'.&mt('Actions').'</th>'.
  224:             '<th>'.&mt('Name').'</th>'.
  225:             '<th>'.&mt('Title').'</th>'.
  226: 	    '<th>'.&mt('Status').'</th>'.
  227:             '<th>'.&mt('Last Modified').
  228: 	    '</th></tr>'."\n");
  229: 
  230:   my $filename;
  231:   my $dirptr=16384;		# Mask indicating a directory in stat.cmode.
  232: 
  233:   opendir(DIR,$fn);
  234:   my @files=sort {uc($a) cmp uc($b)} (readdir(DIR));
  235:   foreach my $filename (@files) {
  236:      my ($cdev,$cino,$cmode,$cnlink,
  237:          $cuid,$cgid,$crdev,$csize,
  238:          $catime,$cmtime,$cctime,
  239:          $cblksize,$cblocks)=stat($fn.'/'.$filename);
  240: 
  241:      my $extension='';
  242:      if ($filename=~/\.(\w+)$/) { $extension=$1; }
  243:      if ($cmode&$dirptr) {
  244: 	 putdirectory($r, $thisdisfn, $linkdir, $filename, $cmtime,$targetdir,\%bombs,\$numdir);
  245:      } elsif (&Apache::loncommon::fileembstyle($extension) ne 'hdn') {
  246: 	 putresource($r, $udom, $uname, $filename, $thisdisfn, $resdir, 
  247: 		     $targetdir, $linkdir, $cmtime,\%bombs,\$numres);
  248:      } else {
  249: 	# "hidden" extension and not a directory, so hide it away.
  250:      }
  251:   }
  252:   closedir(DIR);
  253: 
  254:   $r->print('</table>'.&Apache::loncommon::end_page());
  255:   return OK;  
  256: }
  257: #
  258: #  Gets the effective URL of the request and returns it:
  259: #    $effn = getEffectiveUrl($r);
  260: #       $r  - The Apache Request object.
  261: sub getEffectiveUrl {
  262:     my $r = shift;
  263:     my $fn;
  264:     
  265:     if ($env{'form.filename'}) {	# If a form filename is defined.
  266: 	$fn=$env{'form.filename'};
  267: 	#
  268: 	#   Replace the ~username of the URL with /home/username/public_html
  269: 	#   so that we don't have to worry about ~ expansion internally.
  270: 	#
  271: 	$fn=~s/^http\:\/\/[^\/]+\///;
  272:         $fn=~s/^\///;
  273:         $fn=~s{~($LONCAPA::username_re)}{/home/$1/public_html};
  274: 	
  275: 	#  Remove trailing / strings (?) 
  276: 	
  277: 	$fn=~s/\/[^\/]+$//;
  278:     } else {
  279: 	#   If no form is defined, use request filename.
  280: 	$fn = $r->filename();
  281: 	my $lonDocRoot=$r->dir_config('lonDocRoot');
  282: 	if ( $fn =~ /$lonDocRoot/ ) {
  283: 	    #internal authentication, needs fixup.
  284: 	    $fn = $r->uri(); # non users do not get the full path request
  285:                              # through SCRIPT_FILENAME
  286: 	    $fn=~s{^/~($LONCAPA::username_re)}{/home/$1/public_html};
  287: 	}
  288:     }
  289:     $fn=~s/\/+/\//g;
  290:     return $fn;
  291: }
  292: #
  293: #   Output the header of the page.  This includes:
  294: #   - The HTML header 
  295: #   - The H1/H3  stuff which includes the directory.
  296: #
  297: #     startpage($r, $uame, $udom, $thisdisfn);
  298: #      $r     - The apache request object.
  299: #      $uname - User name.
  300: #      $udom  - Domain name the user is logged in under.
  301: #      $thisdisfn - Displayable version of the filename.
  302: 
  303: sub startpage {
  304:     my ($r, $uname, $udom, $thisdisfn) = @_;
  305:     my $currdir = '/priv/'.$uname.$thisdisfn;
  306:     &Apache::loncommon::content_type($r,'text/html');
  307:     $r->send_http_header;
  308: 
  309:     my $formaction='/priv/'.$uname.$thisdisfn.'/';
  310:     $formaction=~s|/+|/|g;
  311:     my $pagetitle .= &Apache::loncommon::help_open_menu('','',3,'Authoring').
  312:         '<font face="Arial, Helvetica, sans-serif" size="+1"><b>'.&mt('Construction Space').'</b>:</font>&nbsp;'.
  313:         '<form name="dirs" method="post" action="'.$formaction.
  314:         '" target="_parent"><tt><b>'.
  315:         &Apache::lonhtmlcommon::crumbs($uname.$thisdisfn.'/','_top','/priv','','+1',1)."</b></tt><br />".
  316:         &Apache::lonhtmlcommon::select_recent('construct','recent',
  317:                  'this.form.action=this.form.recent.value;this.form.submit()').
  318:               '</form>';
  319:     &Apache::lonhtmlcommon::store_recent('construct',$formaction,$formaction);
  320:     if ($env{'environment.remote'} eq 'off') {
  321: 	$env{'request.noversionuri'}=$currdir.'/';
  322: 	$r->print(&Apache::loncommon::start_page('Construction Space',undef,
  323: 						 {'body_title' =>
  324: 						      $pagetitle,}));
  325:     } else {
  326: 	$r->print(&Apache::loncommon::start_page('Construction Space',undef,
  327: 						 { 'only_body' => 1,}));
  328: 	$r->print($pagetitle);
  329:     }
  330: 
  331:     my $esc_thisdisfn = &Apache::loncommon::escape_single($thisdisfn);
  332:     my $doctitle = 'LON-CAPA '.&mt('Construction Space');
  333:     my $newname = &mt('New Name');
  334:     my $pubdirscript=(<<ENDPUBDIRSCRIPT);
  335: <script type="text/javascript">
  336: top.document.title = '$esc_thisdisfn/ - $doctitle';
  337: // Store directory location for menu bar to find
  338: 
  339: parent.lastknownpriv='/~$uname$esc_thisdisfn/';
  340: 
  341: // Confirmation dialogues
  342: 
  343:     function currdiract(theform) {
  344:         if (theform.dirtask.options[theform.dirtask.selectedIndex].value == 'publish') {
  345:             document.publishdir.filename.value = theform.filename.value;
  346: 	    document.publishdir.submit();
  347:         }
  348:         if (theform.dirtask.options[theform.dirtask.selectedIndex].value == 'editcat') {
  349:             top.location=theform.filename.value+'default.meta'
  350:         }
  351:         if (theform.dirtask.options[theform.dirtask.selectedIndex].value == 'printdir' ) {
  352:             document.printdir.postdata.value=theform.filename.value
  353:             document.printdir.submit();
  354:         }
  355:         if (theform.dirtask.options[theform.dirtask.selectedIndex].value == "delete") {
  356:               var delform = document.delresource
  357:               delform.filename.value = theform.filename.value
  358:               delform.submit()
  359:         }
  360:     }
  361:   
  362:     function checkUpload(theform) {
  363:         if (theform.file == '') {
  364:             alert("Please use 'Browse..' to choose a file first, before uploading")
  365:             return 
  366:         }
  367:         theform.submit()  
  368:     }
  369: 
  370:     function SetPubDir(theform,printForm) {
  371:         if (theform.diraction.options[theform.diraction.selectedIndex].value == "open") {
  372:             top.location = theform.openname.value
  373:             return
  374:         }
  375:         if (theform.diraction.options[theform.diraction.selectedIndex].value == "publish") {
  376:             theform.submit();
  377:         }
  378:         if (theform.diraction.options[theform.diraction.selectedIndex].value == "editcat") {
  379:             top.location=theform.filename.value+'default.meta'
  380:         }
  381:         if (theform.diraction.options[theform.diraction.selectedIndex].value == "printdir") {
  382:             theform.action = '/adm/printout'
  383:             theform.postdata.value = theform.filename.value
  384:             theform.submit()
  385:         }
  386:         if (theform.diraction.options[theform.diraction.selectedIndex].value == "delete") {
  387:               var delform = document.delresource
  388:               delform.filename.value = theform.filename.value
  389:               delform.submit()
  390:         }
  391:         return
  392:     }
  393:     function SetResChoice(theform) {
  394:       var activity = theform.reschoice.options[theform.reschoice.selectedIndex].value
  395:       if ((activity == 'rename') || (activity == 'copy') || (activity == 'move')) {
  396:           changename(theform,activity)
  397:       }
  398:       if (activity == 'publish') {
  399:           var pubform = document.pubresource
  400:           pubform.filename.value = theform.filename.value
  401:           pubform.submit()
  402:       }
  403:       if (activity == 'delete') {
  404:           var delform = document.delresource
  405:           delform.filename.value = theform.filename.value
  406:           delform.submit()
  407:       }
  408:       if (activity == 'obsolete') {
  409:           var pubform = document.pubresource
  410:           pubform.filename.value = theform.filename.value
  411:           pubform.makeobsolete.value=1;
  412:           pubform.submit()
  413:       }
  414:       if (activity == 'print') {
  415:           document.printresource.postdata.value = theform.filename.value
  416:           document.printresource.submit()
  417:       }
  418:       if (activity == 'retrieve') {
  419:           document.retrieveres.filename.value = theform.filename.value
  420:           document.retrieveres.submit()
  421:       }
  422:       if (activity == 'cleanup') {
  423:           document.cleanup.filename.value = theform.filename.value
  424:           document.cleanup.submit()
  425:       }
  426:       return
  427:     }
  428:     function changename(theform,activity) {
  429:         var oldname=theform.dispfilename.value;
  430:         var newname=prompt('$newname',oldname);
  431:         if (newname == "" || !newname || newname == oldname)  {
  432:             return
  433:         }
  434:         document.moveresource.newfilename.value = newname
  435:         document.moveresource.filename.value = theform.filename.value
  436:         document.moveresource.action.value = activity
  437:         document.moveresource.submit();
  438:     }
  439: </script>
  440: ENDPUBDIRSCRIPT
  441:     $r->print($pubdirscript);
  442: 
  443:     if ((($uname ne $env{'user.name'}) || ($udom ne $env{'user.domain'})) &&
  444: 	$env{'environment.remote'} ne 'off') {
  445: 	$r->print('<h3>'.&mt('Co-Author').': '.$uname.' at '.$udom.
  446: 		  '</h3>');
  447:     }
  448: }
  449: 
  450: sub dircontrols {
  451:     my ($r,$uname,$udom,$thisdisfn) = @_;
  452:     my %lt=&Apache::lonlocal::texthash(
  453:                                        cnpd => 'Cannot publish directory',
  454:                                        cnrd => 'Cannot retrieve directory',
  455:                                        mcdi => 'Must create new subdirectory inside a directory',
  456:                                        pubr => 'Publish this Resource',
  457:                                        pubd => 'Publish this Directory',
  458:                                        dedr => 'Delete Directory',
  459:                                        rtrv => 'Retrieve Old Version',
  460:                                        list => 'List Directory',
  461:                                        uplo => 'Upload file',  
  462:                                        dele => 'Delete',
  463:                                        edit => 'Edit Catalog Information', 
  464:                                        sela => 'Select Action',
  465:                                        nfil => 'New file',
  466:                                        nhtm => 'New HTML file',
  467:                                        nprb => 'New problem',
  468:                                        npag => 'New assembled page',
  469:                                        nseq => 'New assembled sequence',
  470:                                        ncrf => 'New custom rights file',
  471:                                        nsty => 'New style file',
  472:                                        nlib => 'New library file',
  473:                                        nbt  => 'New bridgetask file',
  474:                                        nsub => 'New subdirectory',
  475:                                        renm => 'Rename current file to',
  476:                                        move => 'Move current file to',
  477:                                        copy => 'Copy current file to',
  478:                                        type => 'Type Name Here',
  479:                                        go   => 'Go',
  480:                                        prnt => 'Print contents of directory',
  481:                                        crea => 'Create a new directory or LON-CAPA document',
  482: 				       acti => 'Actions for current directory',
  483: 				       updc => 'Upload a new document',
  484: 				       pick => 'Please select an action to perform using the new filename',
  485:                                       );
  486:     my $mytype = $lt{'type'}; # avoid conflict with " and ' in javascript
  487:     $r->print(<<END);
  488:         <table id="LC_cstr_controls">
  489:          <tr>
  490:           <th>$lt{'acti'}</th>
  491:           <th>$lt{'updc'}</th>
  492:           <th>$lt{'crea'}</th>
  493:         </tr>
  494:         <tr>
  495:          <td>
  496:           <form name="curractions" method="post" action="">
  497:            <select name="dirtask" onchange="currdiract(this.form)">
  498:             <option>$lt{'sela'}</option>
  499:             <option value="publish">$lt{'pubd'}</option>
  500:             <option value="editcat">$lt{'edit'}</option>
  501:             <option value="printdir">$lt{'prnt'}</option>
  502:             <option value="delete">$lt{'dedr'}</option>
  503:            </select>
  504:            <input type="hidden" name="filename" value="/~$uname$thisdisfn/" />
  505:           </form>
  506:           <form name="publishdir" method="post" action="/adm/publish" target="_parent">
  507:            <input type="hidden" name="pubrec" value="" />
  508:            <input type="hidden" name="filename" value="" />
  509:           </form>
  510:           <form name="printdir" method="post" action="/adm/printout" target="_parent">
  511:            <input type="hidden" name="postdata" value="" />
  512:           </form>
  513:          </td>
  514:          <td>
  515: 	    <form name="upublisher" enctype="multipart/form-data" method="post" action="/adm/upload" target="_parent">
  516: 	      <input type="hidden" name="filename" value="/~$uname$thisdisfn/" />
  517: 	      <input type="file" name="upfile" size="20" />
  518: 	      <input type="button" value="$lt{'uplo'}"  onclick="checkUpload(this.form)" />
  519: 	    </form>
  520: 	 </td>
  521: 	 <td>
  522: 	    <form name="fileaction" method="post" action="/adm/cfile" target="_parent">
  523: 	      <span style="white-space: nowrap">
  524: 		<input type="hidden" name="filename" value="/~$uname$thisdisfn/" />
  525:                   <script type="text/javascript">
  526:                     function validate_go() {
  527:                         var selected = document.fileaction.action.selectedIndex;
  528:                         if (selected == 0) {
  529:                             alert('$lt{'pick'}');
  530:                         } else {
  531:                             document.fileaction.submit();
  532:                         }
  533:                     }
  534:                   </script>
  535: 		  <select name="action">
  536: 		    <option value="none">$lt{'sela'}</option>
  537: 		    <option value="newfile">$lt{'nfil'}:</option>
  538: 		    <option value="newhtmlfile">$lt{'nhtm'}:</option>
  539: 		    <option value="newproblemfile">$lt{'nprb'}:</option>
  540:                     <option value="newpagefile">$lt{'npag'}:</option>
  541:                     <option value="newsequencefile">$lt{'nseq'}:</option>
  542:                     <option value="newrightsfile">$lt{'ncrf'}:</option>
  543:                     <option value="newstyfile">$lt{'nsty'}:</option>
  544:                     <option value="newtaskfile">$lt{'nbt'}:</option>
  545:                     <option value="newlibraryfile">$lt{'nlib'}:</option>
  546: 	            <option value="newdir">$lt{'nsub'}:</option>
  547: 		  </select>&nbsp;<input type="text" name="newfilename" value="$lt{'type'}" onfocus="if (this.value == '$mytype') this.value=''" />&nbsp;<input type="button" value="Go" onclick="validate_go();" />
  548: 		 </span>
  549: 		</form>
  550: 	  </td>
  551:          </tr>
  552:         </table>
  553: END
  554: }
  555: 
  556: sub pubbuttons {
  557:     my ($r,$uname,$thisdisfn) = @_;
  558:     $r->print('<form method="post" action="/adm/publish" target="_parent">'.
  559:               '<table><tr><td><input type="hidden" name="filename" value="/~'.
  560:                $uname.$thisdisfn.'/" />'.
  561:               '<input type="submit" value="'.&mt('Publish Directory').'" /></td><td>'.
  562: '<input type="button" onclick="window.location='."'/~".
  563:                $uname.$thisdisfn."/default.meta'".'" value="'.
  564: &mt('Edit Directory Catalog Information').'" /></td></tr></table></form>');
  565: }
  566: 
  567: sub resourceactions {
  568:     my ($r,$uname,$udom,$thisdisfn) = @_;
  569:     $r->print(<<END);
  570:        <form name="moveresource" action="/adm/cfile" target="_parent" method="post">
  571:          <input type="hidden" name="filename" value="" />
  572:          <input type="hidden" name="newfilename" value="" />
  573:          <input type="hidden" name="action" value="" />
  574:        </form>
  575:        <form name="delresource" action="/adm/cfile" target="_parent" method="post">
  576:          <input type="hidden" name="filename" value="" />
  577:          <input type="hidden" name="action" value="delete" />
  578:        </form>
  579:        <form name="pubresource" action="/adm/publish" target="_parent" method="post">
  580:          <input type="hidden" name="filename" value="" />
  581:          <input type="hidden" name="makeobsolete" value="0" />
  582:        </form>
  583:        <form name="printresource" action="/adm/printout" target="_parent" method="post">
  584:            <input type="hidden" name="postdata" value="" />
  585:        </form>
  586:        <form name="retrieveres" action="/adm/retrieve" target="_parent" method="post">
  587:            <input type="hidden" name="filename" value="" />
  588:        </form>
  589:        <form name="cleanup" action="/adm/cleanup" target="_parent" method="post">
  590:            <input type="hidden" name="filename" value="" />
  591:        </form>
  592: END
  593: }
  594: 
  595: #
  596: #   Get the title string or "[untitled]" if the file has no title metadata:
  597: #   Without the latter substitution, it's impossible to examine metadata for
  598: #   untitled resources.  Resources may be legitimately untitled, to prevent
  599: #   searches from locating them.
  600: #
  601: #   $str = getTitleString($fullname);
  602: #       $fullname - Fully qualified filename to check.
  603: #
  604: sub getTitleString {
  605:     my $fullname = shift;
  606:     my $title    = &Apache::lonnet::metadata($fullname, 'title');
  607: 
  608:     unless ($title) {
  609: 	$title = "[".&mt('untitled')."]";
  610:     }
  611:     return $title;
  612: }
  613: 
  614: sub getCopyRightString {
  615:     my $fullname = shift;
  616:     return &Apache::lonnet::metadata($fullname, 'copyright');
  617: }
  618: 
  619: sub getSourceRightString {
  620:     my $fullname = shift;
  621:     return &Apache::lonnet::metadata($fullname, 'sourceavail');
  622: }
  623: #
  624: #  Put out a directory table row:
  625: #    putdirectory(r, base, here, dirname, modtime)
  626: #      r       - Apache request object.
  627: #      reqfile - File in request.
  628: #      here    - Where we are in directory tree.
  629: #      dirname - Name of directory special file.
  630: #      modtime - Encoded modification time.
  631: # 
  632: sub putdirectory {
  633:     my ($r, $reqfile, $here, $dirname, $modtime, $resdir, $bombs, $numdir) = @_;
  634:     # construct the display filename: the directory name unless ..:
  635:     
  636:     my $disfilename = $dirname;
  637:     if ($dirname eq '..') {
  638: 	$disfilename = '<i>'.&mt('Parent Directory').'</i>';
  639:     }
  640:     unless ( (($dirname eq '..') && ($reqfile eq '')) || ($dirname eq '.')) {
  641: 	my $kaputt=0;
  642: 	foreach (keys %{$bombs}) {
  643: 	    if ($_=~m:^\Q$resdir\E/\Q$disfilename\E/:) { $kaputt=1; last; }
  644: 	}
  645: 	%Apache::lonpublisher::metadatafields=();
  646: 	%Apache::lonpublisher::metadatakeys=();
  647: 	my $construct=$here;
  648: 	$construct=~s{^/priv/($LONCAPA::username_re)$}{/home/$1/public_html};
  649:         my $dirpath = $here;
  650:         $dirpath=~s{^/priv/}{/~};
  651: 	&Apache::lonpublisher::metaeval(&Apache::lonnet::getfile(
  652:        				 $construct.'/'.$dirname.'/default.meta'
  653: 								 ));
  654:         my $actionitem = '';
  655:         if ($dirname eq '..') {
  656:             $actionitem = &mt('Go to ...');
  657:         } else {
  658:             $actionitem = 
  659:                     '<form name="dirselect_'.$$numdir.
  660:                     '" action="/adm/publish" target="_parent">'.
  661:                     '<select name="diraction" onchange="SetPubDir(this.form,document)">'.
  662:                       '<option selected="selected">'.&mt('Select action').'</option>'.
  663:                       '<option value="open">'.&mt('Open').'</option>'.
  664:                       '<option value="publish">'.&mt('Publish').'</option>'.
  665:                       '<option value="editcat">'.&mt('Edit catalog information').'</option>'.
  666:                       '<option value="printdir">'.&mt('Print directory').'</option>'.
  667:                       '<option value="delete">'.&mt('Delete directory').'</option>'.
  668:                     '</select>'.
  669:                      '<input type="hidden" name="filename" value="'.&HTML::Entities::encode($dirpath.'/'.$dirname,'<>&"').'/" />'.
  670:                      '<input type="hidden" name="openname" value="'.$here.'/'.$dirname.'/" />'.
  671:                      '<input type="hidden" name="postdata" value="" />'.
  672:                    '</form>';
  673:             $$numdir ++;
  674:         }
  675: 	$r->print('<tr class="LC_browser_folder">'.
  676: 		  '<td><img src="'.
  677: 		  $Apache::lonnet::perlvar{'lonIconsURL'}.'/folder_closed.gif" alt="folder" /></td>'.
  678: 		  '<td>'.$actionitem.'</td>'.
  679: 		  '<td><span class="LC_filename"><a href="'.&HTML::Entities::encode($here.'/'.$dirname,'<>&"').'/" target="_parent">'.
  680: 		  $disfilename.'</a></span></td>'.
  681: 		        '<td colspan="2">'.($kaputt?&Apache::lonhtmlcommon::authorbombs($resdir.'/'.$disfilename.'/'):'').$Apache::lonpublisher::metadatafields{'title'});
  682: 	if ($Apache::lonpublisher::metadatafields{'subject'} ne '') {
  683: 	    $r->print(' <i>'.
  684: 		      $Apache::lonpublisher::metadatafields{'subject'}.
  685: 		      '</i> ');
  686: 	}
  687: 	$r->print($Apache::lonpublisher::metadatafields{'keywords'}.'</td>'.
  688: 		  '<td>'.&Apache::lonlocal::locallocaltime($modtime).'</td>'.
  689: 		  "</tr>\n");
  690:     }
  691:     return OK;
  692: }
  693: #
  694: #   Put a table row for a file resource.
  695: #
  696: sub putresource {
  697:     my ($r, $udom, $uname, $filename, $thisdisfn, 
  698: 	$resdir, $targetdir, $linkdir,
  699: 	$cmtime,$bombs,$numres) = @_;
  700:     &Apache::lonnet::devalidate_cache_new('meta',$targetdir.'/'.$filename);
  701:     my $pubstatus = 'unpublished';
  702:     my $status=&mt('Unpublished');
  703:     my $css_class='LC_browser_file';
  704:     my $title='&nbsp;';
  705:     my $publish_button=&mt('Publish');
  706:     my $cstr_dir = '/home/'.$uname.'/public_html/'.$thisdisfn.'/';
  707: #    my $action_buttons=
  708: #        '<br /><a target="_parent" href="/adm/cfile?action=delete&filename=/~'.
  709: #	$uname.'/'.$thisdisfn.'/'.$filename.'">'.
  710: #	&mt('Delete').'</a>';
  711:     if (-e $resdir.'/'.$filename) {
  712:         my $same=0;
  713: 	my ($rdev,$rino,$rmode,$rnlink,
  714: 	    $ruid,$rgid,$rrdev,$rsize,
  715: 	    $ratime,$rmtime,$rctime,
  716: 	    $rblksize,$rblocks)=stat($resdir.'/'.$filename);
  717:         if ($rmtime>=$cmtime) {
  718:            $same=1;
  719:         } else {
  720:            if (&Apache::londiff::are_different_files($resdir.'/'.$filename,
  721: 						     $cstr_dir.'/'.$filename)) {
  722:               $same=0;
  723:            } else {
  724:               $same=1;
  725:            }
  726:         }
  727: 	my $meta_cmtime = (stat($cstr_dir.'/'.$filename.'.meta'))[9];
  728: 	my $meta_rmtime = (stat($resdir.'/'.$filename.'.meta'))[9];
  729: 	my $meta_same = 1;
  730: 	if ($meta_rmtime < $meta_cmtime
  731: 	    && &Apache::londiff::are_different_files($resdir.'/'.$filename.'.meta',
  732: 						     $cstr_dir.'/'.$filename.'.meta')) {
  733: 	    $meta_same = 0;
  734: 	}
  735: 	$publish_button=&mt('Re-publish');
  736: 	my $rights_status =
  737: 	    &mt(&getCopyRightString($targetdir.'/'.$filename)).' '.
  738: 	    &mt(&getSourceRightString($targetdir.'/'.$filename));
  739: 	$title = '<a href="/res/'.$targetdir.'/'.$filename.
  740: 	    '.meta" target="cat">'.
  741: 	    &getTitleString($targetdir.'/'.$filename).'</a>';
  742: 	if ($same) {
  743: 	    if (&Apache::lonnet::metadata($targetdir.'/'.$filename,'obsolete')) {
  744:                 $pubstatus = 'obsolete';
  745: 		$status=&mt('Obsolete');
  746:             } else {
  747: 		if (!$meta_same) {
  748: 		    $pubstatus = 'metamodified';
  749: 		} else {
  750: 		    $pubstatus = 'published';
  751: 		}
  752: 		$status=&mt('Published').
  753: 		    '<br />'. $rights_status;
  754: 	    }
  755: #	    } else {
  756: #		$action_buttons='';
  757: #	    }
  758: 	} else {
  759:             $pubstatus = 'modified';
  760: 	    $status=&mt('Modified').
  761: 		'<br />'. $rights_status;
  762: #	    $action_buttons='';
  763: 	    if (&Apache::loncommon::fileembstyle(($filename=~/\.(\w+)$/)) eq 'ssi') {
  764: 		$status.='<br /><a href="/adm/diff?filename=/~'.$uname.
  765: 		    $thisdisfn.'/'.$filename.
  766: 		    '&amp;versiontwo=priv" target="cat">'.&mt('Diffs').'</a>';
  767: 	    }
  768: 	} 
  769: 
  770: 	$title.="\n".'<br /><a href="/~'.$uname.$thisdisfn.'/'.$filename.'.meta">'. 
  771: 	    ($$bombs{$targetdir.'/'.$filename}?'<img src="/adm/lonMisc/bomb.gif" border="0" alt="'.&mt('bomb').'" />':&mt('Edit Metadata')).'</a>';
  772: 
  773: 	if (!$meta_same) {
  774: 	    $title = &mt('Metadata Modified').'<br />'.$title.
  775: 		'<br /><a href="/adm/diff?filename=/~'.$uname.
  776: 		$thisdisfn.'/'.$filename.'.meta'.
  777: 		'&amp;versiontwo=priv" target="cat">'.&mt('Metadata Diffs').'</a>';
  778: 	    $title.="\n".'<br /><a href="/adm/retrieve?filename=/~'.$uname.
  779: 		$thisdisfn.'/'.$filename.'.meta" target="_parent">'.&mt('Retrieve Metadata').'</a>';
  780: 	}
  781: 	$status.="\n".'<br /><a href="/adm/retrieve?filename=/~'.$uname.
  782: 	    $thisdisfn.'/'.$filename.'" target="_parent">'.&mt('Retrieve').'</a>';
  783:     }
  784:     my $editlink='';
  785:     my $editlink2='';
  786:     if ($filename=~/\.(xml|html|htm|xhtml|xhtm|sty)$/) {
  787: 	$editlink=' <br />(<a href="'.$linkdir.'/'.$filename.'?forceedit=1" target="_parent">'.&mt('Edit').'</a>)';
  788:     }
  789:     if ($filename=~/\.(problem|exam|quiz|assess|survey|form|library)$/) {
  790: 	$editlink=' (<a href="'.$linkdir.'/'.$filename.'?forceedit=1" target="_parent">'.&mt('EditXML').'</a>)';
  791: 	$editlink2=' <br />(<a href="'.$linkdir.'/'.$filename.'?forceColoredit=1" target="_parent">'.&mt('Edit').'</a>)';
  792:     }
  793:     if ($filename=~/\.(problem|exam|quiz|assess|survey|form|library|xml|html|htm|xhtml|xhtm|sty)$/) {
  794: 	$editlink.=' (<a href="/adm/cleanup?filename=/~'.$uname.
  795: 	    $thisdisfn.'/'.$filename.'" target="_parent">'.&mt('Clean Up').')</a>';
  796:     }
  797:     if ($filename=~/\.(zip|tar|bz2|gz|tar.gz|tar.bz2|tgz)$/) {
  798: 	$editlink=' (<a target="_parent" href="/adm/cfile?decompress=/~'.
  799: 	      $uname.$thisdisfn.'/'.$filename.'">'.&mt('Decompress').'</a>)';
  800:     }
  801:     my $pub_select = '';
  802:     &create_pubselect($r,\$pub_select,$udom,$uname,$thisdisfn,$filename,$resdir,$pubstatus,$publish_button,$numres);
  803:     $r->print('<tr class="LC_browser_file_'.$pubstatus.'">'.
  804: 	      '<td>'.($filename=~/[\#\~]$/?'&nbsp;':
  805: 		      '<img src="'.&Apache::loncommon::icon($filename).'" alt="" />').'</td>'.
  806:               '<td>'.$pub_select.'</td>'.
  807: 	      '<td><span class="LC_filename">'.
  808: 	      '<a href="'.$linkdir.'/'.$filename.'" target="_parent">'.
  809:                $filename.'</a></span>'.$editlink2.$editlink.
  810: 	      '</td>'.
  811: 	      '<td>'.$title.'</td>'.
  812: 	      '<td>'.$status.'</td>'.
  813: 	      '<td>'.&Apache::lonlocal::locallocaltime($cmtime).'</td>'.
  814: 	      "</tr>\n");
  815:     return OK;
  816: }
  817: 
  818: sub create_pubselect {
  819:     my ($r,$pub_select,$udom,$uname,$thisdisfn,$filename,$resdir,$pubstatus,$publish_button,$numres) = @_;
  820:     $$pub_select = '
  821: <form name="resselect_'.$$numres.'" action="">
  822: <select name="reschoice"  onchange="SetResChoice(this.form)">
  823: <option>'.&mt('Select action').'</option>'.
  824: '<option value="copy">'.&mt('Copy').'</option>';
  825:     if ($pubstatus eq 'obsolete' || $pubstatus eq 'unpublished') {
  826:         $$pub_select .= 
  827: '<option value="rename">'.&mt('Rename').'</option>'.
  828: '<option value="move">'.&mt('Move').'</option>'.
  829: '<option value="delete">'.&mt('Delete').'</option>';
  830:     } else {
  831:         $$pub_select .= '
  832: <option value="obsolete">'.&mt('Mark obsolete').'</option>';
  833:     }
  834: # check for versions
  835:     my $versions = &check_for_versions($r,'/'.$filename,$udom,$uname);
  836:     if ($versions > 0) {
  837:         $$pub_select .='
  838: <option value="retrieve">'.&mt('Retrieve old version').'</option>';
  839:     }
  840:     $$pub_select .= '
  841: <option value="publish">'.$publish_button.'</option>'.
  842: '<option value="cleanup">'.&mt('Clean up').'</option>'.
  843: '<option value="print">'.&mt('Print').'</option>'.
  844: '</select>
  845: <input type="hidden" name="filename" value="/~'.
  846:  &HTML::Entities::encode($uname.$thisdisfn.'/'.$filename,'<>&"').'" />
  847:  <input type="hidden" name="dispfilename" value="'.
  848:  &HTML::Entities::encode($filename).'" /></form>';
  849:     $$numres ++;
  850: }
  851: 
  852: sub check_for_versions {
  853:     my ($r,$fn,$udom,$uname) = @_;
  854:     my $versions = 0;
  855:     my $docroot=$r->dir_config('lonDocRoot');
  856:     my $resfn=$docroot.'/res/'.$udom.'/'.$uname.$fn;
  857:     my $resdir=$resfn;
  858:     $resdir=~s/\/[^\/]+$/\//;
  859:     $fn=~/\/([^\/]+)\.(\w+)$/;
  860:     my $main=$1;
  861:     my $suffix=$2;
  862:     opendir(DIR,$resdir);
  863:     while (my $filename=readdir(DIR)) {
  864:         if ($filename=~/^\Q$main\E\.(\d+)\.\Q$suffix\E$/) {
  865:             $versions ++;        
  866:         }
  867:     }
  868:     return $versions;
  869: }
  870: 
  871: #
  872: #   Categorize files in the directory.
  873: #   For each file in a list of files in a file directory, 
  874: #   the  file categorized as one of:
  875: #    - directory  
  876: #    - sequence
  877: #    - problem 
  878: #    - Other resource.
  879: #
  880: #   For each file the modification date is determined as well.
  881: #   Returned is a list of sublists:
  882: #    (directories, sequences, problems, other)
  883: #   each of the sublists contains entries of the following form (sorted by
  884: #   filename):
  885: #     (filename, typecode, lastmodtime)
  886: #
  887: #   $list = CategorizeFiles($location, $files)
  888: #       $location   - Directory in which the files live (relative to our
  889: #                     execution.
  890: #       $files      - list of files.
  891: #
  892: sub CategorizeFiles {
  893:     my $location = shift;
  894:     my $files    = shift;
  895: }
  896: 
  897: 1;
  898: __END__
  899: 
  900: 

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