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

1.1       www         1: # The LearningOnline Network with CAPA
                      2: # Handler to retrieve an old version of a file
                      3: #
1.32    ! albertel    4: # $Id: lonretrieve.pm,v 1.31 2006/09/13 21:43:26 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().
        !           128: 		  '<p>'.
        !           129: 		  '<span class="LC_error">'.
        !           130: 		  &mt('Retrieval of an old version will overwrite the file currently in construction space').'</span><p>'.
        !           131: 		  '<input type="submit" value="'.&mt('Retrieve version').'" /></form>');
        !           132:     } else {
        !           133: 	$r->print('<h3>'.&mt('No previous versions published.').'</h3>');
1.31      albertel  134:     }
1.25      albertel  135:     $r->print('<p><a href="/priv/'.$uname.$fn.'">'.&mt('Back to').' '.$fn.
                    136: 	      '</a></p>'); 
1.2       www       137: }
1.1       www       138: 
1.16      harris41  139: # ---------------------------------- Interface for presenting specified version
1.4       www       140: sub phasetwo {
                    141:     my ($r,$fn,$uname,$udom)=@_;
1.27      albertel  142:     if ($env{'form.version'}) {
                    143:         my $version=$env{'form.version'};
1.4       www       144: 	if ($version eq 'new') {
1.23      www       145: 	    $r->print('<h3>'.&mt('Retrieving current (most recent) version').'</h3>');
1.4       www       146:         } else {
1.23      www       147:             $r->print('<h3>'.&mt('Retrieving old version').' '.$version.'</h3>');
1.4       www       148:         }
1.31      albertel  149: 	my ($main,$suffix,$is_meta) = &get_file_info($fn);
                    150: 
1.4       www       151:         my $logfile;
                    152:         my $ctarget='/home/'.$uname.'/public_html'.$fn;
1.5       www       153:         my $vfn=$fn;
                    154:         if ($version ne 'new') {
1.31      albertel  155: 	    $vfn=~s/\.(\Q$suffix\E)$/\.$version\.$1/;
1.5       www       156:         }
1.31      albertel  157: 
1.5       www       158:         my $csource=$r->dir_config('lonDocRoot').'/res/'.$udom.'/'.$uname.$vfn;
1.31      albertel  159: 
                    160: 	my $logname = $ctarget;
                    161: 	if ($is_meta) { $logname =~ s/\.meta$//; }
                    162: 	$logname = $ctarget.'.log';
                    163:         unless ($logfile=Apache::File->new('>>'.$logname)) {
1.4       www       164: 	  $r->print(
1.23      www       165:          '<font color=red>'.&mt('No write permission to user directory, FAIL').'</font>');
1.4       www       166:         }
                    167:         print $logfile 
                    168: "\n\n================= Retrieve ".localtime()." ================\n".
1.5       www       169: "Version: $version\nSource: $csource\nTarget: $ctarget\n";
1.23      www       170:         $r->print('<p>'.&mt('Copying file').': ');
1.31      albertel  171: 	if (copy($csource,$ctarget)) {
1.5       www       172: 	    $r->print('ok<p>');
                    173:             print $logfile "Copied sucessfully.\n\n";
                    174:         } else {
                    175:             my $error=$!;
                    176: 	    $r->print('fail, '.$error.'<p>');
                    177:             print $logfile "Copy failed: $error\n\n";
                    178:         }
                    179:         $r->print('<font size=+2><a href="/priv/'.$uname.$fn.
1.23      www       180:                   '">'.&mt('Back to').' '.$fn.'</a></font>'); 
1.4       www       181:     } else {
1.32    ! albertel  182:        $r->print('<span class="LC_warning">'.&mt('Please pick a version to retrieve').'</span><p>');
1.4       www       183:        &phaseone($r,$fn,$uname,$udom);
                    184:     }
                    185: }
                    186: 
1.31      albertel  187: sub get_file_info {
                    188:     my ($fn) = @_;
                    189:     my ($main,$suffix) = ($fn=~/\/([^\/]+)\.(\w+)$/);
                    190:     my $is_meta=0;
                    191:     if ($suffix eq 'meta') {
                    192: 	$is_meta = 1;
                    193: 	($main,$suffix) = ($main=~/(.+)\.(\w+)$/);	    
                    194: 	$suffix .= '.meta';
                    195:     }
                    196:     return ($main,$suffix,$is_meta);
                    197: }
                    198: 
1.16      harris41  199: # ---------------------------------------------------------------- Main Handler
1.1       www       200: sub handler {
                    201: 
                    202:   my $r=shift;
                    203: 
                    204:   my $fn;
1.14      www       205: 
                    206: 
                    207: # Get query string for limited number of parameters
                    208: 
1.17      stredwic  209:   &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.18      www       210: 					  ['filename']);
1.1       www       211: 
1.27      albertel  212:   if ($env{'form.filename'}) {
                    213:       $fn=$env{'form.filename'};
1.10      www       214:       $fn=~s/^http\:\/\/[^\/]+//;
1.1       www       215:   } else {
1.27      albertel  216:      $r->log_reason($env{'user.name'}.' at '.$env{'user.domain'}.
1.2       www       217:          ' unspecified filename for retrieval', $r->filename); 
                    218:      return HTTP_NOT_FOUND;
1.1       www       219:   }
                    220: 
                    221:   unless ($fn) { 
1.27      albertel  222:      $r->log_reason($env{'user.name'}.' at '.$env{'user.domain'}.
1.2       www       223:          ' trying to retrieve non-existing file', $r->filename); 
1.1       www       224:      return HTTP_NOT_FOUND;
                    225:   } 
                    226: 
                    227: # ----------------------------------------------------------- Start page output
1.10      www       228:   my $uname;
                    229:   my $udom;
1.1       www       230: 
1.13      www       231:   ($uname,$udom)=
                    232:     &Apache::loncacc::constructaccess($fn,$r->dir_config('lonDefDomain'));
                    233:   unless (($uname) && ($udom)) {
1.10      www       234:      $r->log_reason($uname.' at '.$udom.
1.27      albertel  235:          ' trying to publish file '.$env{'form.filename'}.
1.10      www       236:          ' ('.$fn.') - not authorized', 
                    237:          $r->filename); 
                    238:      return HTTP_NOT_ACCEPTABLE;
                    239:   }
                    240: 
                    241:   $fn=~s/\/\~(\w+)//;
1.1       www       242: 
1.23      www       243:   &Apache::loncommon::content_type($r,'text/html');
1.1       www       244:   $r->send_http_header;
                    245: 
1.30      albertel  246:   $r->print(&Apache::loncommon::start_page('Retrieve Published Resources'));
1.1       www       247: 
                    248:   
1.23      www       249:   $r->print('<h1>'.&mt('Retrieve previous versions of').' <tt>'.$fn.'</tt></h1>');
1.10      www       250:   
1.27      albertel  251:   if (($uname ne $env{'user.name'}) || ($udom ne $env{'user.domain'})) {
1.32    ! albertel  252:           $r->print('<h3><span class="LC_diff_coauthor">'.&mt('Co-Author').': '.$uname.
1.23      www       253: 		    &mt(' at ').$udom.
1.32    ! albertel  254:                '</span></h3>');
1.10      www       255:   }
                    256: 
1.1       www       257: 
1.27      albertel  258:   if ($env{'form.phase'} eq 'two') {
1.4       www       259:       &phasetwo($r,$fn,$uname,$udom);
1.2       www       260:   } else {
                    261:       &phaseone($r,$fn,$uname,$udom);
                    262:   }
1.1       www       263: 
1.30      albertel  264:   $r->print(&Apache::loncommon::end_page());
1.1       www       265:   return OK;  
                    266: }
1.7       www       267: 
                    268: 1;
                    269: __END__
1.16      harris41  270: 
                    271: =head1 NAME
                    272: 
                    273: Apache::lonretrieve - retrieves an old version of a file
                    274: 
                    275: =head1 SYNOPSIS
                    276: 
                    277: Invoked by /etc/httpd/conf/srm.conf:
                    278: 
                    279:  <Location /adm/retrieve>
                    280:  PerlAccessHandler       Apache::lonacc
                    281:  SetHandler perl-script
                    282:  PerlHandler Apache::lonretrieve
                    283:  ErrorDocument     403 /adm/login
                    284:  ErrorDocument     404 /adm/notfound.html
                    285:  ErrorDocument     406 /adm/unauthorized.html
                    286:  ErrorDocument	  500 /adm/errorhandler
                    287:  </Location>
                    288: 
                    289: =head1 INTRODUCTION
                    290: 
                    291: This module retrieves an old published version of a file.
                    292: 
                    293: This is part of the LearningOnline Network with CAPA project
                    294: described at http://www.lon-capa.org.
                    295: 
                    296: =head1 HANDLER SUBROUTINE
                    297: 
                    298: This routine is called by Apache and mod_perl.
                    299: 
                    300: =over 4
                    301: 
                    302: =item *
                    303: 
                    304: Get query string for limited number of parameters
                    305: 
                    306: =item *
                    307: 
                    308: Start page output
                    309: 
                    310: =item *
                    311: 
                    312: print phase relevant output
                    313: 
                    314: =item *
                    315: 
                    316: (phase one is to select version; phase two retrieves version)
                    317: 
                    318: =back
                    319: 
                    320: =head1 OTHER SUBROUTINES
                    321: 
                    322: =over 4
                    323: 
                    324: =item *
                    325: 
                    326: phaseone() : Interface for selecting previous version.
                    327: 
                    328: =item *
                    329: 
                    330: phasetwo() : Interface for presenting specified version.
                    331: 
                    332: =back
                    333: 
                    334: =cut

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