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

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

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