File:  [LON-CAPA] / loncom / publisher / lonpubdir.pm
Revision 1.124: download - view: text, annotated - select for diffs
Fri Nov 6 18:01:44 2009 UTC (14 years, 6 months ago) by bisitz
Branches: MAIN
CVS tags: HEAD
Optimized display of XML source publication status (open/closed):
- Allow proper translation
- Allow authors to understand

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

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