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

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

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