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

1.1       www         1: # The LearningOnline Network with CAPA
                      2: # Handler to retrieve an old version of a file
                      3: #
1.35    ! jms         4: # $Id: lonretrieve.pm,v 1.34 2006/12/06 22:22:39 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: 
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.32      albertel  123: 	$r->print('<form action="/adm/retrieve" method="POST">'.
                    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="'.
                    155: 			  $version.'" /></td><td>'.&mt('Previously published version').' '.$version.'</td><td>'.
                    156: 			  localtime($rmtime).'</td>');
                    157: 		
                    158: 		if (!$is_meta) {
                    159: 		    $r->print('<td><a href="'.$urldir.$filename.'.meta" target=cat>'.
                    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>'.
                    178: 		  '<td><b>'.&mt('Currently published version').'</b></td><td>'.localtime($rmtime).
                    179: 		  '</td>');
                    180: 	if (!$is_meta) {
                    181: 	    $r->print('<td><a href="'.$urldir.$main.'.'.$suffix.'.meta" target=cat>'.
                    182: 		      &mt('Metadata current version').'</a></td>');           
                    183: 	}
                    184: 	if ($is_meta 
                    185: 	    || &Apache::loncommon::fileembstyle($suffix) eq 'ssi') {
                    186: 	    $r->print(
                    187: 		      '<td><a target="cat" href="/adm/diff?filename=/~'.
                    188: 		      $uname.$fn.
                    189: 		      '&amp;versiontwo=priv'.
                    190: 		      '">'.&mt('Diffs with current Version').'</a></td>');
                    191: 	}
                    192: 	$r->print(&Apache::loncommon::end_data_table_row().
                    193: 		  &Apache::loncommon::end_data_table().
1.33      albertel  194: 		  '<p>'.'<span class="LC_warning">'.
                    195: 		  &mt('Retrieval of an old version will overwrite the file currently in construction space').'</span></p>');
                    196: 	if (!$is_meta) {
                    197: 	    $r->print('<p>'.'<span class="LC_warning">'.
                    198: 		      &mt('This will only retrieve the resource, if you want to retrieve the metadata you will need to do that separately.').
                    199: 		      '</span></p>');
                    200: 	}
                    201: 	$r->print('<input type="submit" value="'.&mt('Retrieve version').'" /></form>');
1.32      albertel  202:     } else {
                    203: 	$r->print('<h3>'.&mt('No previous versions published.').'</h3>');
1.31      albertel  204:     }
1.25      albertel  205:     $r->print('<p><a href="/priv/'.$uname.$fn.'">'.&mt('Back to').' '.$fn.
                    206: 	      '</a></p>'); 
1.2       www       207: }
1.1       www       208: 
1.16      harris41  209: # ---------------------------------- Interface for presenting specified version
1.4       www       210: sub phasetwo {
                    211:     my ($r,$fn,$uname,$udom)=@_;
1.27      albertel  212:     if ($env{'form.version'}) {
                    213:         my $version=$env{'form.version'};
1.4       www       214: 	if ($version eq 'new') {
1.23      www       215: 	    $r->print('<h3>'.&mt('Retrieving current (most recent) version').'</h3>');
1.4       www       216:         } else {
1.23      www       217:             $r->print('<h3>'.&mt('Retrieving old version').' '.$version.'</h3>');
1.4       www       218:         }
1.31      albertel  219: 	my ($main,$suffix,$is_meta) = &get_file_info($fn);
                    220: 
1.4       www       221:         my $logfile;
                    222:         my $ctarget='/home/'.$uname.'/public_html'.$fn;
1.5       www       223:         my $vfn=$fn;
                    224:         if ($version ne 'new') {
1.31      albertel  225: 	    $vfn=~s/\.(\Q$suffix\E)$/\.$version\.$1/;
1.5       www       226:         }
1.31      albertel  227: 
1.5       www       228:         my $csource=$r->dir_config('lonDocRoot').'/res/'.$udom.'/'.$uname.$vfn;
1.31      albertel  229: 
                    230: 	my $logname = $ctarget;
                    231: 	if ($is_meta) { $logname =~ s/\.meta$//; }
                    232: 	$logname = $ctarget.'.log';
                    233:         unless ($logfile=Apache::File->new('>>'.$logname)) {
1.4       www       234: 	  $r->print(
1.23      www       235:          '<font color=red>'.&mt('No write permission to user directory, FAIL').'</font>');
1.4       www       236:         }
                    237:         print $logfile 
                    238: "\n\n================= Retrieve ".localtime()." ================\n".
1.5       www       239: "Version: $version\nSource: $csource\nTarget: $ctarget\n";
1.23      www       240:         $r->print('<p>'.&mt('Copying file').': ');
1.31      albertel  241: 	if (copy($csource,$ctarget)) {
1.5       www       242: 	    $r->print('ok<p>');
                    243:             print $logfile "Copied sucessfully.\n\n";
                    244:         } else {
                    245:             my $error=$!;
                    246: 	    $r->print('fail, '.$error.'<p>');
                    247:             print $logfile "Copy failed: $error\n\n";
                    248:         }
                    249:         $r->print('<font size=+2><a href="/priv/'.$uname.$fn.
1.23      www       250:                   '">'.&mt('Back to').' '.$fn.'</a></font>'); 
1.4       www       251:     } else {
1.32      albertel  252:        $r->print('<span class="LC_warning">'.&mt('Please pick a version to retrieve').'</span><p>');
1.4       www       253:        &phaseone($r,$fn,$uname,$udom);
                    254:     }
                    255: }
                    256: 
1.31      albertel  257: sub get_file_info {
                    258:     my ($fn) = @_;
                    259:     my ($main,$suffix) = ($fn=~/\/([^\/]+)\.(\w+)$/);
                    260:     my $is_meta=0;
                    261:     if ($suffix eq 'meta') {
                    262: 	$is_meta = 1;
                    263: 	($main,$suffix) = ($main=~/(.+)\.(\w+)$/);	    
                    264: 	$suffix .= '.meta';
                    265:     }
                    266:     return ($main,$suffix,$is_meta);
                    267: }
                    268: 
1.16      harris41  269: # ---------------------------------------------------------------- Main Handler
1.1       www       270: sub handler {
                    271: 
                    272:   my $r=shift;
                    273: 
                    274:   my $fn;
1.14      www       275: 
                    276: 
                    277: # Get query string for limited number of parameters
                    278: 
1.17      stredwic  279:   &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.18      www       280: 					  ['filename']);
1.1       www       281: 
1.27      albertel  282:   if ($env{'form.filename'}) {
                    283:       $fn=$env{'form.filename'};
1.10      www       284:       $fn=~s/^http\:\/\/[^\/]+//;
1.1       www       285:   } else {
1.27      albertel  286:      $r->log_reason($env{'user.name'}.' at '.$env{'user.domain'}.
1.2       www       287:          ' unspecified filename for retrieval', $r->filename); 
                    288:      return HTTP_NOT_FOUND;
1.1       www       289:   }
                    290: 
                    291:   unless ($fn) { 
1.27      albertel  292:      $r->log_reason($env{'user.name'}.' at '.$env{'user.domain'}.
1.2       www       293:          ' trying to retrieve non-existing file', $r->filename); 
1.1       www       294:      return HTTP_NOT_FOUND;
                    295:   } 
                    296: 
                    297: # ----------------------------------------------------------- Start page output
1.10      www       298:   my $uname;
                    299:   my $udom;
1.1       www       300: 
1.13      www       301:   ($uname,$udom)=
                    302:     &Apache::loncacc::constructaccess($fn,$r->dir_config('lonDefDomain'));
                    303:   unless (($uname) && ($udom)) {
1.10      www       304:      $r->log_reason($uname.' at '.$udom.
1.27      albertel  305:          ' trying to publish file '.$env{'form.filename'}.
1.10      www       306:          ' ('.$fn.') - not authorized', 
                    307:          $r->filename); 
                    308:      return HTTP_NOT_ACCEPTABLE;
                    309:   }
                    310: 
1.34      albertel  311:   $fn=~s{/~($LONCAPA::username_re)}{};
1.1       www       312: 
1.23      www       313:   &Apache::loncommon::content_type($r,'text/html');
1.1       www       314:   $r->send_http_header;
                    315: 
1.30      albertel  316:   $r->print(&Apache::loncommon::start_page('Retrieve Published Resources'));
1.1       www       317: 
                    318:   
1.23      www       319:   $r->print('<h1>'.&mt('Retrieve previous versions of').' <tt>'.$fn.'</tt></h1>');
1.10      www       320:   
1.27      albertel  321:   if (($uname ne $env{'user.name'}) || ($udom ne $env{'user.domain'})) {
1.32      albertel  322:           $r->print('<h3><span class="LC_diff_coauthor">'.&mt('Co-Author').': '.$uname.
1.23      www       323: 		    &mt(' at ').$udom.
1.32      albertel  324:                '</span></h3>');
1.10      www       325:   }
                    326: 
1.1       www       327: 
1.27      albertel  328:   if ($env{'form.phase'} eq 'two') {
1.4       www       329:       &phasetwo($r,$fn,$uname,$udom);
1.2       www       330:   } else {
                    331:       &phaseone($r,$fn,$uname,$udom);
                    332:   }
1.1       www       333: 
1.30      albertel  334:   $r->print(&Apache::loncommon::end_page());
1.1       www       335:   return OK;  
                    336: }
1.7       www       337: 
                    338: 1;
                    339: __END__
1.16      harris41  340: 
                    341: 

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