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

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

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