File:  [LON-CAPA] / loncom / publisher / lonpubdir.pm
Revision 1.127: download - view: text, annotated - select for diffs
Fri Oct 21 17:51:23 2011 UTC (12 years, 6 months ago) by www
Branches: MAIN
CVS tags: HEAD
Toward Bug #1320

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

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