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

1.1       www         1: # The LearningOnline Network with CAPA
                      2: # Handler to retrieve an old version of a file
                      3: #
1.49    ! raeburn     4: # $Id: lonretrieve.pm,v 1.48 2011/12/23 17:21:18 www 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.16      harris41  102: use Apache::loncommon();
1.23      www       103: use Apache::lonlocal;
1.27      albertel  104: use Apache::lonnet;
1.34      albertel  105: use LONCAPA();
1.1       www       106: 
1.16      harris41  107: # ------------------------------------ Interface for selecting previous version
1.2       www       108: sub phaseone {
                    109:     my ($r,$fn,$uname,$udom)=@_;
                    110: 
1.46      raeburn   111:     my $urldir = "/res/$udom/$uname".$fn;
                    112:     my $resfn = $r->dir_config('lonDocRoot').$urldir;
1.3       www       113: 
1.46      raeburn   114:     $urldir =~ s{[^/]+$}{};
                    115:     my $resdir = $r->dir_config('lonDocRoot').$urldir;
1.2       www       116: 
1.31      albertel  117:     my ($main,$suffix,$is_meta) = &get_file_info($fn);
                    118:     
1.6       www       119:     if (-e $resfn) {  
1.48      www       120: 	$r->print('<form action="/adm/retrieve?inhibitmenu=yes" method="post">'.
1.45      raeburn   121: 		  '<input type="hidden" name="filename" value="/priv/'.$udom.'/'.$uname.$fn.'" />'.
1.32      albertel  122: 		  '<input type="hidden" name="phase" value="two" />'.
                    123: 		  &Apache::loncommon::start_data_table().
                    124: 		  &Apache::loncommon::start_data_table_header_row().
                    125: 		  '<th>'.&mt('Select').'</th>'.
                    126: 		  '<th>'.&mt('Version').'</th>'.
                    127: 		  '<th>'.&mt('Published on ...').'</th>');
                    128: 	if (!$is_meta) {
                    129: 	    $r->print('<th>'.&mt('Metadata').'</th>');
                    130: 	}
                    131: 	if ($is_meta
                    132: 	    || &Apache::loncommon::fileembstyle($suffix) eq 'ssi') {
                    133: 	    $r->print('<th>'.&mt('Diffs').'</th>');
                    134: 	}
                    135: 	$r->print(&Apache::loncommon::end_data_table_header_row());
                    136: 	
                    137: 	opendir(DIR,$resdir);
                    138: 	my @files = grep(/^\Q$main\E\.(\d+)\.\Q$suffix\E$/,readdir(DIR));
                    139: 	@files = sort {
                    140: 	    my ($aver) = ($a=~/^\Q$main\E\.(\d+)\.\Q$suffix\E$/);
                    141: 	    my ($bver) = ($b=~/^\Q$main\E\.(\d+)\.\Q$suffix\E$/);
                    142: 	    return $aver <=> $bver;
                    143: 	} (@files);
                    144: 	closedir(DIR);
                    145: 	
                    146: 	foreach my $filename (@files) {
                    147: 	    if ($filename=~/^\Q$main\E\.(\d+)\.\Q$suffix\E$/) {
                    148: 		my $version=$1;
                    149: 		my $rmtime=&Apache::lonnet::metadata($resdir.'/'.$filename,'lastrevisiondate');
                    150: 		$r->print(&Apache::loncommon::start_data_table_row().
                    151: 			  '<td><input type="radio" name="version" value="'.
1.41      bisitz    152: 			  $version.'" /></td><td>'.&mt('Previously published version').' '.$version.'</td>'.
                    153:               '<td>'.&Apache::lonlocal::locallocaltime($rmtime).'</td>');
1.32      albertel  154: 		
                    155: 		if (!$is_meta) {
1.48      www       156: 		    $r->print('<td>'.
                    157:                               &Apache::loncommon::modal_link($urldir.$filename.'.meta',
                    158: 			      &mt('Metadata Version [_1]',$version),550,450).'</td>');
1.32      albertel  159: 		}
                    160: 		if ($is_meta
                    161: 		    || &Apache::loncommon::fileembstyle($suffix) eq 'ssi') {
1.48      www       162: 		    $r->print('<td>'.
                    163:                               &Apache::loncommon::modal_link(
                    164:                                '/adm/diff?filename=/priv/'.$udom,'/'.$uname.$fn.
                    165: 			      '&amp;versiontwo=priv&amp;versionone='.$version,
                    166: 			       &mt('Diffs with Version [_1]',$version),550,450).
                    167: 			      '</td>');
1.32      albertel  168: 		}
                    169: 		$r->print(&Apache::loncommon::end_data_table_row());
                    170: 	    }
                    171: 	}
                    172: 	closedir(DIR);
                    173: 	my $rmtime=&Apache::lonnet::metadata($resfn,'lastrevisiondate');
                    174: 	$r->print(&Apache::loncommon::start_data_table_row().
                    175: 		  '<td><input type="radio" name="version" value="new" /></td>'.
1.41      bisitz    176: 		  '<td><b>'.&mt('Currently published version').'</b></td>'.
                    177:           '<td>'.&Apache::lonlocal::locallocaltime($rmtime).'</td>'
                    178:     );
1.32      albertel  179: 	if (!$is_meta) {
1.48      www       180: 	    $r->print('<td>',
                    181:                       &Apache::loncommon::modal_link($urldir.$main.'.'.$suffix.'.meta',
                    182: 		      &mt('Metadata current version'),550,450).'</td>');           
1.32      albertel  183: 	}
                    184: 	if ($is_meta 
                    185: 	    || &Apache::loncommon::fileembstyle($suffix) eq 'ssi') {
1.48      www       186: 	    $r->print('<td>'.
                    187:                       &Apache::loncommon::modal_link(
                    188:                        '/adm/diff?filename=/priv/'.
1.45      raeburn   189: 		      $udom.'/'.$uname.$fn.
1.48      www       190: 		      '&amp;versiontwo=priv',
                    191: 		      &mt('Diffs with current Version'),550,450).
                    192:                       '</td>');
1.32      albertel  193: 	}
                    194: 	$r->print(&Apache::loncommon::end_data_table_row().
                    195: 		  &Apache::loncommon::end_data_table().
1.33      albertel  196: 		  '<p>'.'<span class="LC_warning">'.
1.36      bisitz    197: 		  &mt('Retrieval of an old version will overwrite the file currently in construction space.').'</span></p>');
1.33      albertel  198: 	if (!$is_meta) {
                    199: 	    $r->print('<p>'.'<span class="LC_warning">'.
1.36      bisitz    200: 		      &mt('This will only retrieve the resource. If you want to retrieve the metadata, you will need to do that separately.').
1.33      albertel  201: 		      '</span></p>');
                    202: 	}
1.41      bisitz    203: 	$r->print('<input type="submit" value="'.&mt('Retrieve selected Version').'" /></form>');
1.32      albertel  204:     } else {
1.36      bisitz    205: 	$r->print('<p class="LC_warning">'.&mt('No previous versions published.').'</p>');
1.31      albertel  206:     }
1.41      bisitz    207: 
1.2       www       208: }
1.1       www       209: 
1.16      harris41  210: # ---------------------------------- Interface for presenting specified version
1.4       www       211: sub phasetwo {
                    212:     my ($r,$fn,$uname,$udom)=@_;
1.27      albertel  213:     if ($env{'form.version'}) {
                    214:         my $version=$env{'form.version'};
1.4       www       215: 	if ($version eq 'new') {
1.23      www       216: 	    $r->print('<h3>'.&mt('Retrieving current (most recent) version').'</h3>');
1.4       www       217:         } else {
1.23      www       218:             $r->print('<h3>'.&mt('Retrieving old version').' '.$version.'</h3>');
1.4       www       219:         }
1.31      albertel  220: 	my ($main,$suffix,$is_meta) = &get_file_info($fn);
                    221: 
1.4       www       222:         my $logfile;
1.46      raeburn   223:         my $ctarget=$r->dir_config('lonDocRoot')."/priv/$udom/$uname".$fn;
1.5       www       224:         my $vfn=$fn;
                    225:         if ($version ne 'new') {
1.31      albertel  226: 	    $vfn=~s/\.(\Q$suffix\E)$/\.$version\.$1/;
1.5       www       227:         }
1.31      albertel  228: 
1.46      raeburn   229:         my $csource=$r->dir_config('lonDocRoot')."/res/$udom/$uname".$vfn;
1.31      albertel  230: 
                    231: 	my $logname = $ctarget;
                    232: 	if ($is_meta) { $logname =~ s/\.meta$//; }
                    233: 	$logname = $ctarget.'.log';
                    234:         unless ($logfile=Apache::File->new('>>'.$logname)) {
1.36      bisitz    235:           $r->print('<span class="LC_error">'
                    236:                    .&mt('No write permission to user directory, FAIL')
                    237:                    .'</span>');
1.4       www       238:         }
                    239:         print $logfile 
                    240: "\n\n================= Retrieve ".localtime()." ================\n".
1.5       www       241: "Version: $version\nSource: $csource\nTarget: $ctarget\n";
1.23      www       242:         $r->print('<p>'.&mt('Copying file').': ');
1.31      albertel  243: 	if (copy($csource,$ctarget)) {
1.36      bisitz    244: 	    $r->print('<span class="LC_success">'
                    245:                      .&mt('ok')
                    246:                      .'</span>');
1.5       www       247:             print $logfile "Copied sucessfully.\n\n";
1.48      www       248:             $r->print(&Apache::lonhtmlcommon::scripttag('parent.location.reload();'));
1.5       www       249:         } else {
                    250:             my $error=$!;
1.36      bisitz    251: 	    $r->print('<span class="LC_error">'
                    252:                      .&mt('Copy failed: [_1]',$error)
                    253:                      .'</span>');
1.5       www       254:             print $logfile "Copy failed: $error\n\n";
                    255:         }
1.4       www       256:     } else {
1.36      bisitz    257:        $r->print('<p class="LC_info">'.&mt('Please pick a version to retrieve:').'</p>');
1.4       www       258:        &phaseone($r,$fn,$uname,$udom);
                    259:     }
                    260: }
                    261: 
1.31      albertel  262: sub get_file_info {
                    263:     my ($fn) = @_;
                    264:     my ($main,$suffix) = ($fn=~/\/([^\/]+)\.(\w+)$/);
                    265:     my $is_meta=0;
                    266:     if ($suffix eq 'meta') {
                    267: 	$is_meta = 1;
                    268: 	($main,$suffix) = ($main=~/(.+)\.(\w+)$/);	    
                    269: 	$suffix .= '.meta';
                    270:     }
                    271:     return ($main,$suffix,$is_meta);
                    272: }
                    273: 
1.16      harris41  274: # ---------------------------------------------------------------- Main Handler
1.1       www       275: sub handler {
                    276: 
                    277:   my $r=shift;
                    278: 
                    279:   my $fn;
1.14      www       280: 
                    281: 
                    282: # Get query string for limited number of parameters
                    283: 
1.17      stredwic  284:   &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.18      www       285: 					  ['filename']);
1.1       www       286: 
1.27      albertel  287:   if ($env{'form.filename'}) {
                    288:       $fn=$env{'form.filename'};
1.46      raeburn   289:       $fn =~ s{^https?\://[^/]+}{};
1.1       www       290:   } else {
1.27      albertel  291:      $r->log_reason($env{'user.name'}.' at '.$env{'user.domain'}.
1.2       www       292:          ' unspecified filename for retrieval', $r->filename); 
                    293:      return HTTP_NOT_FOUND;
1.1       www       294:   }
                    295: 
                    296:   unless ($fn) { 
1.27      albertel  297:      $r->log_reason($env{'user.name'}.' at '.$env{'user.domain'}.
1.2       www       298:          ' trying to retrieve non-existing file', $r->filename); 
1.1       www       299:      return HTTP_NOT_FOUND;
                    300:   } 
                    301: 
                    302: # ----------------------------------------------------------- Start page output
1.10      www       303:   my $uname;
                    304:   my $udom;
1.1       www       305: 
1.49    ! raeburn   306:   ($uname,$udom) = &Apache::lonnet::constructaccess($fn);
1.45      raeburn   307:   unless (($uname ne '') && ($udom ne '')) {
1.10      www       308:      $r->log_reason($uname.' at '.$udom.
1.27      albertel  309:          ' trying to publish file '.$env{'form.filename'}.
1.10      www       310:          ' ('.$fn.') - not authorized', 
                    311:          $r->filename); 
                    312:      return HTTP_NOT_ACCEPTABLE;
                    313:   }
                    314: 
1.23      www       315:   &Apache::loncommon::content_type($r,'text/html');
1.1       www       316:   $r->send_http_header;
                    317: 
1.41      bisitz    318:     # Breadcrumbs
                    319:     &Apache::lonhtmlcommon::clear_breadcrumbs();
                    320:     &Apache::lonhtmlcommon::add_breadcrumb({
                    321:         'text'  => 'Construction Space',
1.47      raeburn   322:         'href'  => &Apache::loncommon::authorspace($fn),
1.41      bisitz    323:     });
                    324:     &Apache::lonhtmlcommon::add_breadcrumb({
                    325:         'text'  => 'Retrieve previous version',
                    326:         'href'  => '',
                    327:     });
                    328: 
1.46      raeburn   329:     my $londocroot = $r->dir_config('lonDocRoot');
                    330:     my $trailfile = $fn;
                    331:     $trailfile =~ s{^/(priv/)}{$londocroot/$1};
                    332: 
1.41      bisitz    333:   $r->print(&Apache::loncommon::start_page('Retrieve Published Resources')
                    334:            .&Apache::lonhtmlcommon::breadcrumbs()
                    335:            .&Apache::loncommon::head_subbox(
1.46      raeburn   336:                 &Apache::loncommon::CSTR_pageheader($trailfile))
1.41      bisitz    337:     );
1.1       www       338: 
1.46      raeburn   339:   $fn=~s{/priv/$LONCAPA::domain_re/$LONCAPA::username_re}{};
                    340: 
1.40      bisitz    341:   $r->print('<p>'
1.36      bisitz    342:            .&mt('Retrieve previous versions of [_1]'
                    343:                    ,'<span class="LC_filename">'.$fn.'</span>')
1.40      bisitz    344:            .'</p>');
1.10      www       345:   
1.27      albertel  346:   if (($uname ne $env{'user.name'}) || ($udom ne $env{'user.domain'})) {
1.42      www       347:           $r->print('<p><span class="LC_info">'
1.40      bisitz    348:                    .&mt('Co-Author [_1]'
                    349:                        ,&Apache::loncommon::plainname($uname,$udom)
                    350:                        .' ('.$uname.':'.$udom.')')
                    351:                    .'</span></p>');
1.10      www       352:   }
                    353: 
1.1       www       354: 
1.27      albertel  355:   if ($env{'form.phase'} eq 'two') {
1.4       www       356:       &phasetwo($r,$fn,$uname,$udom);
1.2       www       357:   } else {
                    358:       &phaseone($r,$fn,$uname,$udom);
                    359:   }
1.1       www       360: 
1.30      albertel  361:   $r->print(&Apache::loncommon::end_page());
1.1       www       362:   return OK;  
                    363: }
1.7       www       364: 
                    365: 1;
                    366: __END__
1.16      harris41  367: 
                    368: 

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