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

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