Annotation of loncom/publisher/lonretrieve.pm, revision 1.34

1.1       www         1: # The LearningOnline Network with CAPA
                      2: # Handler to retrieve an old version of a file
                      3: #
1.34    ! albertel    4: # $Id: lonretrieve.pm,v 1.33 2006/11/10 18:01:08 albertel Exp $
1.15      albertel    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: #
1.16      harris41   29: ###
1.1       www        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);
1.10      www        37: use Apache::loncacc;
1.16      harris41   38: use Apache::loncommon();
1.23      www        39: use Apache::lonlocal;
1.27      albertel   40: use Apache::lonnet;
1.34    ! albertel   41: use LONCAPA();
1.1       www        42: 
1.16      harris41   43: # ------------------------------------ Interface for selecting previous version
1.2       www        44: sub phaseone {
                     45:     my ($r,$fn,$uname,$udom)=@_;
                     46:     my $docroot=$r->dir_config('lonDocRoot');
                     47: 
1.3       www        48:     my $urldir='/res/'.$udom.'/'.$uname.$fn;
                     49:     $urldir=~s/\/[^\/]+$/\//;
                     50: 
                     51:     my $resfn=$docroot.'/res/'.$udom.'/'.$uname.$fn;
                     52:     my $resdir=$resfn;
1.2       www        53:     $resdir=~s/\/[^\/]+$/\//;
                     54: 
1.31      albertel   55:     my ($main,$suffix,$is_meta) = &get_file_info($fn);
                     56:     
1.6       www        57:     if (-e $resfn) {  
1.32      albertel   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().
1.33      albertel  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>');
1.32      albertel  137:     } else {
                    138: 	$r->print('<h3>'.&mt('No previous versions published.').'</h3>');
1.31      albertel  139:     }
1.25      albertel  140:     $r->print('<p><a href="/priv/'.$uname.$fn.'">'.&mt('Back to').' '.$fn.
                    141: 	      '</a></p>'); 
1.2       www       142: }
1.1       www       143: 
1.16      harris41  144: # ---------------------------------- Interface for presenting specified version
1.4       www       145: sub phasetwo {
                    146:     my ($r,$fn,$uname,$udom)=@_;
1.27      albertel  147:     if ($env{'form.version'}) {
                    148:         my $version=$env{'form.version'};
1.4       www       149: 	if ($version eq 'new') {
1.23      www       150: 	    $r->print('<h3>'.&mt('Retrieving current (most recent) version').'</h3>');
1.4       www       151:         } else {
1.23      www       152:             $r->print('<h3>'.&mt('Retrieving old version').' '.$version.'</h3>');
1.4       www       153:         }
1.31      albertel  154: 	my ($main,$suffix,$is_meta) = &get_file_info($fn);
                    155: 
1.4       www       156:         my $logfile;
                    157:         my $ctarget='/home/'.$uname.'/public_html'.$fn;
1.5       www       158:         my $vfn=$fn;
                    159:         if ($version ne 'new') {
1.31      albertel  160: 	    $vfn=~s/\.(\Q$suffix\E)$/\.$version\.$1/;
1.5       www       161:         }
1.31      albertel  162: 
1.5       www       163:         my $csource=$r->dir_config('lonDocRoot').'/res/'.$udom.'/'.$uname.$vfn;
1.31      albertel  164: 
                    165: 	my $logname = $ctarget;
                    166: 	if ($is_meta) { $logname =~ s/\.meta$//; }
                    167: 	$logname = $ctarget.'.log';
                    168:         unless ($logfile=Apache::File->new('>>'.$logname)) {
1.4       www       169: 	  $r->print(
1.23      www       170:          '<font color=red>'.&mt('No write permission to user directory, FAIL').'</font>');
1.4       www       171:         }
                    172:         print $logfile 
                    173: "\n\n================= Retrieve ".localtime()." ================\n".
1.5       www       174: "Version: $version\nSource: $csource\nTarget: $ctarget\n";
1.23      www       175:         $r->print('<p>'.&mt('Copying file').': ');
1.31      albertel  176: 	if (copy($csource,$ctarget)) {
1.5       www       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.
1.23      www       185:                   '">'.&mt('Back to').' '.$fn.'</a></font>'); 
1.4       www       186:     } else {
1.32      albertel  187:        $r->print('<span class="LC_warning">'.&mt('Please pick a version to retrieve').'</span><p>');
1.4       www       188:        &phaseone($r,$fn,$uname,$udom);
                    189:     }
                    190: }
                    191: 
1.31      albertel  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: 
1.16      harris41  204: # ---------------------------------------------------------------- Main Handler
1.1       www       205: sub handler {
                    206: 
                    207:   my $r=shift;
                    208: 
                    209:   my $fn;
1.14      www       210: 
                    211: 
                    212: # Get query string for limited number of parameters
                    213: 
1.17      stredwic  214:   &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.18      www       215: 					  ['filename']);
1.1       www       216: 
1.27      albertel  217:   if ($env{'form.filename'}) {
                    218:       $fn=$env{'form.filename'};
1.10      www       219:       $fn=~s/^http\:\/\/[^\/]+//;
1.1       www       220:   } else {
1.27      albertel  221:      $r->log_reason($env{'user.name'}.' at '.$env{'user.domain'}.
1.2       www       222:          ' unspecified filename for retrieval', $r->filename); 
                    223:      return HTTP_NOT_FOUND;
1.1       www       224:   }
                    225: 
                    226:   unless ($fn) { 
1.27      albertel  227:      $r->log_reason($env{'user.name'}.' at '.$env{'user.domain'}.
1.2       www       228:          ' trying to retrieve non-existing file', $r->filename); 
1.1       www       229:      return HTTP_NOT_FOUND;
                    230:   } 
                    231: 
                    232: # ----------------------------------------------------------- Start page output
1.10      www       233:   my $uname;
                    234:   my $udom;
1.1       www       235: 
1.13      www       236:   ($uname,$udom)=
                    237:     &Apache::loncacc::constructaccess($fn,$r->dir_config('lonDefDomain'));
                    238:   unless (($uname) && ($udom)) {
1.10      www       239:      $r->log_reason($uname.' at '.$udom.
1.27      albertel  240:          ' trying to publish file '.$env{'form.filename'}.
1.10      www       241:          ' ('.$fn.') - not authorized', 
                    242:          $r->filename); 
                    243:      return HTTP_NOT_ACCEPTABLE;
                    244:   }
                    245: 
1.34    ! albertel  246:   $fn=~s{/~($LONCAPA::username_re)}{};
1.1       www       247: 
1.23      www       248:   &Apache::loncommon::content_type($r,'text/html');
1.1       www       249:   $r->send_http_header;
                    250: 
1.30      albertel  251:   $r->print(&Apache::loncommon::start_page('Retrieve Published Resources'));
1.1       www       252: 
                    253:   
1.23      www       254:   $r->print('<h1>'.&mt('Retrieve previous versions of').' <tt>'.$fn.'</tt></h1>');
1.10      www       255:   
1.27      albertel  256:   if (($uname ne $env{'user.name'}) || ($udom ne $env{'user.domain'})) {
1.32      albertel  257:           $r->print('<h3><span class="LC_diff_coauthor">'.&mt('Co-Author').': '.$uname.
1.23      www       258: 		    &mt(' at ').$udom.
1.32      albertel  259:                '</span></h3>');
1.10      www       260:   }
                    261: 
1.1       www       262: 
1.27      albertel  263:   if ($env{'form.phase'} eq 'two') {
1.4       www       264:       &phasetwo($r,$fn,$uname,$udom);
1.2       www       265:   } else {
                    266:       &phaseone($r,$fn,$uname,$udom);
                    267:   }
1.1       www       268: 
1.30      albertel  269:   $r->print(&Apache::loncommon::end_page());
1.1       www       270:   return OK;  
                    271: }
1.7       www       272: 
                    273: 1;
                    274: __END__
1.16      harris41  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>