File:  [LON-CAPA] / loncom / publisher / lonretrieve.pm
Revision 1.34: download - view: text, annotated - select for diffs
Wed Dec 6 22:22:39 2006 UTC (17 years, 4 months ago) by albertel
Branches: MAIN
CVS tags: version_2_8_X, version_2_7_X, version_2_7_1, version_2_7_0, version_2_6_X, version_2_6_99_1, version_2_6_99_0, version_2_6_3, version_2_6_2, version_2_6_1, version_2_6_0, version_2_5_X, version_2_5_99_1, version_2_5_99_0, version_2_5_2, version_2_5_1, version_2_5_0, version_2_4_X, version_2_4_99_0, version_2_4_2, version_2_4_1, version_2_4_0, version_2_3_X, version_2_3_99_0, version_2_3_2, version_2_3_1, version_2_3_0, version_2_2_99_1, version_2_2_99_0, HEAD
- more re fix ups

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

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