File:  [LON-CAPA] / loncom / publisher / lonretrieve.pm
Revision 1.48: download - view: text, annotated - select for diffs
Fri Dec 23 17:21:18 2011 UTC (12 years, 6 months ago) by www
Branches: MAIN
CVS tags: HEAD, BZ4492-merge, BZ4492-feature_horizontal_radioresponse
Modal windows for retrieve and subsequent screens

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

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