File:  [LON-CAPA] / loncom / publisher / lonpubdir.pm
Revision 1.145: download - view: text, annotated - select for diffs
Tue Jun 4 22:20:16 2013 UTC (10 years, 11 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Replace term: "Construction Space" with "Authoring Space" for
  consistency with type of role used to access it, and action taken there.

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

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