File:  [LON-CAPA] / loncom / publisher / lonpubdir.pm
Revision 1.128: download - view: text, annotated - select for diffs
Fri Oct 21 18:04:51 2011 UTC (12 years, 7 months ago) by www
Branches: MAIN
CVS tags: HEAD
Bug #1320: Oops, debug code

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

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