File:  [LON-CAPA] / loncom / publisher / lonretrieve.pm
Revision 1.33: download - view: text, annotated - select for diffs
Fri Nov 10 18:01:08 2006 UTC (17 years, 6 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- bug5014 let them know that the retrieve doesn't als retrieve the metadata

    1: # The LearningOnline Network with CAPA
    2: # Handler to retrieve an old version of a file
    3: #
    4: # $Id: lonretrieve.pm,v 1.33 2006/11/10 18:01:08 albertel 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: 
   31: package Apache::lonretrieve;
   32: 
   33: use strict;
   34: use Apache::File;
   35: use File::Copy;
   36: use Apache::Constants qw(:common :http :methods);
   37: use Apache::loncacc;
   38: use Apache::loncommon();
   39: use Apache::lonlocal;
   40: use Apache::lonnet;
   41: 
   42: # ------------------------------------ Interface for selecting previous version
   43: sub phaseone {
   44:     my ($r,$fn,$uname,$udom)=@_;
   45:     my $docroot=$r->dir_config('lonDocRoot');
   46: 
   47:     my $urldir='/res/'.$udom.'/'.$uname.$fn;
   48:     $urldir=~s/\/[^\/]+$/\//;
   49: 
   50:     my $resfn=$docroot.'/res/'.$udom.'/'.$uname.$fn;
   51:     my $resdir=$resfn;
   52:     $resdir=~s/\/[^\/]+$/\//;
   53: 
   54:     my ($main,$suffix,$is_meta) = &get_file_info($fn);
   55:     
   56:     if (-e $resfn) {  
   57: 	$r->print('<form action="/adm/retrieve" method="POST">'.
   58: 		  '<input type="hidden" name="filename" value="/~'.$uname.$fn.'" />'.
   59: 		  '<input type="hidden" name="phase" value="two" />'.
   60: 		  &Apache::loncommon::start_data_table().
   61: 		  &Apache::loncommon::start_data_table_header_row().
   62: 		  '<th>'.&mt('Select').'</th>'.
   63: 		  '<th>'.&mt('Version').'</th>'.
   64: 		  '<th>'.&mt('Published on ...').'</th>');
   65: 	if (!$is_meta) {
   66: 	    $r->print('<th>'.&mt('Metadata').'</th>');
   67: 	}
   68: 	if ($is_meta
   69: 	    || &Apache::loncommon::fileembstyle($suffix) eq 'ssi') {
   70: 	    $r->print('<th>'.&mt('Diffs').'</th>');
   71: 	}
   72: 	$r->print(&Apache::loncommon::end_data_table_header_row());
   73: 	
   74: 	opendir(DIR,$resdir);
   75: 	my @files = grep(/^\Q$main\E\.(\d+)\.\Q$suffix\E$/,readdir(DIR));
   76: 	@files = sort {
   77: 	    my ($aver) = ($a=~/^\Q$main\E\.(\d+)\.\Q$suffix\E$/);
   78: 	    my ($bver) = ($b=~/^\Q$main\E\.(\d+)\.\Q$suffix\E$/);
   79: 	    return $aver <=> $bver;
   80: 	} (@files);
   81: 	closedir(DIR);
   82: 	
   83: 	foreach my $filename (@files) {
   84: 	    if ($filename=~/^\Q$main\E\.(\d+)\.\Q$suffix\E$/) {
   85: 		my $version=$1;
   86: 		my $rmtime=&Apache::lonnet::metadata($resdir.'/'.$filename,'lastrevisiondate');
   87: 		$r->print(&Apache::loncommon::start_data_table_row().
   88: 			  '<td><input type="radio" name="version" value="'.
   89: 			  $version.'" /></td><td>'.&mt('Previously published version').' '.$version.'</td><td>'.
   90: 			  localtime($rmtime).'</td>');
   91: 		
   92: 		if (!$is_meta) {
   93: 		    $r->print('<td><a href="'.$urldir.$filename.'.meta" target=cat>'.
   94: 			      &mt('Metadata Version').' '.$version.'</a></td>');
   95: 		}
   96: 		if ($is_meta
   97: 		    || &Apache::loncommon::fileembstyle($suffix) eq 'ssi') {
   98: 		    $r->print(
   99: 			      '<td><a target="cat" href="/adm/diff?filename=/~'.
  100: 			      $uname.$fn.
  101: 			      '&amp;versiontwo=priv&amp;versionone='.$version.
  102: 			      '">'.&mt('Diffs with Version').' '.$version.
  103: 			      '</a></td>');
  104: 		}
  105: 		$r->print(&Apache::loncommon::end_data_table_row());
  106: 	    }
  107: 	}
  108: 	closedir(DIR);
  109: 	my $rmtime=&Apache::lonnet::metadata($resfn,'lastrevisiondate');
  110: 	$r->print(&Apache::loncommon::start_data_table_row().
  111: 		  '<td><input type="radio" name="version" value="new" /></td>'.
  112: 		  '<td><b>'.&mt('Currently published version').'</b></td><td>'.localtime($rmtime).
  113: 		  '</td>');
  114: 	if (!$is_meta) {
  115: 	    $r->print('<td><a href="'.$urldir.$main.'.'.$suffix.'.meta" target=cat>'.
  116: 		      &mt('Metadata current version').'</a></td>');           
  117: 	}
  118: 	if ($is_meta 
  119: 	    || &Apache::loncommon::fileembstyle($suffix) eq 'ssi') {
  120: 	    $r->print(
  121: 		      '<td><a target="cat" href="/adm/diff?filename=/~'.
  122: 		      $uname.$fn.
  123: 		      '&amp;versiontwo=priv'.
  124: 		      '">'.&mt('Diffs with current Version').'</a></td>');
  125: 	}
  126: 	$r->print(&Apache::loncommon::end_data_table_row().
  127: 		  &Apache::loncommon::end_data_table().
  128: 		  '<p>'.'<span class="LC_warning">'.
  129: 		  &mt('Retrieval of an old version will overwrite the file currently in construction space').'</span></p>');
  130: 	if (!$is_meta) {
  131: 	    $r->print('<p>'.'<span class="LC_warning">'.
  132: 		      &mt('This will only retrieve the resource, if you want to retrieve the metadata you will need to do that separately.').
  133: 		      '</span></p>');
  134: 	}
  135: 	$r->print('<input type="submit" value="'.&mt('Retrieve version').'" /></form>');
  136:     } else {
  137: 	$r->print('<h3>'.&mt('No previous versions published.').'</h3>');
  138:     }
  139:     $r->print('<p><a href="/priv/'.$uname.$fn.'">'.&mt('Back to').' '.$fn.
  140: 	      '</a></p>'); 
  141: }
  142: 
  143: # ---------------------------------- Interface for presenting specified version
  144: sub phasetwo {
  145:     my ($r,$fn,$uname,$udom)=@_;
  146:     if ($env{'form.version'}) {
  147:         my $version=$env{'form.version'};
  148: 	if ($version eq 'new') {
  149: 	    $r->print('<h3>'.&mt('Retrieving current (most recent) version').'</h3>');
  150:         } else {
  151:             $r->print('<h3>'.&mt('Retrieving old version').' '.$version.'</h3>');
  152:         }
  153: 	my ($main,$suffix,$is_meta) = &get_file_info($fn);
  154: 
  155:         my $logfile;
  156:         my $ctarget='/home/'.$uname.'/public_html'.$fn;
  157:         my $vfn=$fn;
  158:         if ($version ne 'new') {
  159: 	    $vfn=~s/\.(\Q$suffix\E)$/\.$version\.$1/;
  160:         }
  161: 
  162:         my $csource=$r->dir_config('lonDocRoot').'/res/'.$udom.'/'.$uname.$vfn;
  163: 
  164: 	my $logname = $ctarget;
  165: 	if ($is_meta) { $logname =~ s/\.meta$//; }
  166: 	$logname = $ctarget.'.log';
  167:         unless ($logfile=Apache::File->new('>>'.$logname)) {
  168: 	  $r->print(
  169:          '<font color=red>'.&mt('No write permission to user directory, FAIL').'</font>');
  170:         }
  171:         print $logfile 
  172: "\n\n================= Retrieve ".localtime()." ================\n".
  173: "Version: $version\nSource: $csource\nTarget: $ctarget\n";
  174:         $r->print('<p>'.&mt('Copying file').': ');
  175: 	if (copy($csource,$ctarget)) {
  176: 	    $r->print('ok<p>');
  177:             print $logfile "Copied sucessfully.\n\n";
  178:         } else {
  179:             my $error=$!;
  180: 	    $r->print('fail, '.$error.'<p>');
  181:             print $logfile "Copy failed: $error\n\n";
  182:         }
  183:         $r->print('<font size=+2><a href="/priv/'.$uname.$fn.
  184:                   '">'.&mt('Back to').' '.$fn.'</a></font>'); 
  185:     } else {
  186:        $r->print('<span class="LC_warning">'.&mt('Please pick a version to retrieve').'</span><p>');
  187:        &phaseone($r,$fn,$uname,$udom);
  188:     }
  189: }
  190: 
  191: sub get_file_info {
  192:     my ($fn) = @_;
  193:     my ($main,$suffix) = ($fn=~/\/([^\/]+)\.(\w+)$/);
  194:     my $is_meta=0;
  195:     if ($suffix eq 'meta') {
  196: 	$is_meta = 1;
  197: 	($main,$suffix) = ($main=~/(.+)\.(\w+)$/);	    
  198: 	$suffix .= '.meta';
  199:     }
  200:     return ($main,$suffix,$is_meta);
  201: }
  202: 
  203: # ---------------------------------------------------------------- Main Handler
  204: sub handler {
  205: 
  206:   my $r=shift;
  207: 
  208:   my $fn;
  209: 
  210: 
  211: # Get query string for limited number of parameters
  212: 
  213:   &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  214: 					  ['filename']);
  215: 
  216:   if ($env{'form.filename'}) {
  217:       $fn=$env{'form.filename'};
  218:       $fn=~s/^http\:\/\/[^\/]+//;
  219:   } else {
  220:      $r->log_reason($env{'user.name'}.' at '.$env{'user.domain'}.
  221:          ' unspecified filename for retrieval', $r->filename); 
  222:      return HTTP_NOT_FOUND;
  223:   }
  224: 
  225:   unless ($fn) { 
  226:      $r->log_reason($env{'user.name'}.' at '.$env{'user.domain'}.
  227:          ' trying to retrieve non-existing file', $r->filename); 
  228:      return HTTP_NOT_FOUND;
  229:   } 
  230: 
  231: # ----------------------------------------------------------- Start page output
  232:   my $uname;
  233:   my $udom;
  234: 
  235:   ($uname,$udom)=
  236:     &Apache::loncacc::constructaccess($fn,$r->dir_config('lonDefDomain'));
  237:   unless (($uname) && ($udom)) {
  238:      $r->log_reason($uname.' at '.$udom.
  239:          ' trying to publish file '.$env{'form.filename'}.
  240:          ' ('.$fn.') - not authorized', 
  241:          $r->filename); 
  242:      return HTTP_NOT_ACCEPTABLE;
  243:   }
  244: 
  245:   $fn=~s/\/\~(\w+)//;
  246: 
  247:   &Apache::loncommon::content_type($r,'text/html');
  248:   $r->send_http_header;
  249: 
  250:   $r->print(&Apache::loncommon::start_page('Retrieve Published Resources'));
  251: 
  252:   
  253:   $r->print('<h1>'.&mt('Retrieve previous versions of').' <tt>'.$fn.'</tt></h1>');
  254:   
  255:   if (($uname ne $env{'user.name'}) || ($udom ne $env{'user.domain'})) {
  256:           $r->print('<h3><span class="LC_diff_coauthor">'.&mt('Co-Author').': '.$uname.
  257: 		    &mt(' at ').$udom.
  258:                '</span></h3>');
  259:   }
  260: 
  261: 
  262:   if ($env{'form.phase'} eq 'two') {
  263:       &phasetwo($r,$fn,$uname,$udom);
  264:   } else {
  265:       &phaseone($r,$fn,$uname,$udom);
  266:   }
  267: 
  268:   $r->print(&Apache::loncommon::end_page());
  269:   return OK;  
  270: }
  271: 
  272: 1;
  273: __END__
  274: 
  275: =head1 NAME
  276: 
  277: Apache::lonretrieve - retrieves an old version of a file
  278: 
  279: =head1 SYNOPSIS
  280: 
  281: Invoked by /etc/httpd/conf/srm.conf:
  282: 
  283:  <Location /adm/retrieve>
  284:  PerlAccessHandler       Apache::lonacc
  285:  SetHandler perl-script
  286:  PerlHandler Apache::lonretrieve
  287:  ErrorDocument     403 /adm/login
  288:  ErrorDocument     404 /adm/notfound.html
  289:  ErrorDocument     406 /adm/unauthorized.html
  290:  ErrorDocument	  500 /adm/errorhandler
  291:  </Location>
  292: 
  293: =head1 INTRODUCTION
  294: 
  295: This module retrieves an old published version of a file.
  296: 
  297: This is part of the LearningOnline Network with CAPA project
  298: described at http://www.lon-capa.org.
  299: 
  300: =head1 HANDLER SUBROUTINE
  301: 
  302: This routine is called by Apache and mod_perl.
  303: 
  304: =over 4
  305: 
  306: =item *
  307: 
  308: Get query string for limited number of parameters
  309: 
  310: =item *
  311: 
  312: Start page output
  313: 
  314: =item *
  315: 
  316: print phase relevant output
  317: 
  318: =item *
  319: 
  320: (phase one is to select version; phase two retrieves version)
  321: 
  322: =back
  323: 
  324: =head1 OTHER SUBROUTINES
  325: 
  326: =over 4
  327: 
  328: =item *
  329: 
  330: phaseone() : Interface for selecting previous version.
  331: 
  332: =item *
  333: 
  334: phasetwo() : Interface for presenting specified version.
  335: 
  336: =back
  337: 
  338: =cut

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