File:  [LON-CAPA] / loncom / publisher / lonpubdir.pm
Revision 1.143: download - view: text, annotated - select for diffs
Fri Apr 27 16:13:47 2012 UTC (12 years, 1 month ago) by bisitz
Branches: MAIN
CVS tags: HEAD
- Bug 1320: Corrected links - show bombs again in CSTR
    Work in progress:
        - Resource row: access to bomb needed
        - Confirm backward compatibility
- Improve lonpubdir.pm 1.142: Only one programmened link "Edit Metadata"
- Remove inconsistent manual padding

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

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