File:  [LON-CAPA] / loncom / publisher / lonretrieve.pm
Revision 1.32: download - view: text, annotated - select for diffs
Tue Sep 19 21:47:29 2006 UTC (17 years, 8 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- cleanup londiff/lonretrieve (style tabination xhtml css)

    1: # The LearningOnline Network with CAPA
    2: # Handler to retrieve an old version of a file
    3: #
    4: # $Id: lonretrieve.pm,v 1.32 2006/09/19 21:47:29 albertel 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: package Apache::lonretrieve;
   32: 
   33: use strict;
   34: use Apache::File;
   35: use File::Copy;
   36: use Apache::Constants qw(:common :http :methods);
   37: use Apache::loncacc;
   38: use Apache::loncommon();
   39: use Apache::lonlocal;
   40: use Apache::lonnet;
   41: 
   42: # ------------------------------------ Interface for selecting previous version
   43: sub phaseone {
   44:     my ($r,$fn,$uname,$udom)=@_;
   45:     my $docroot=$r->dir_config('lonDocRoot');
   46: 
   47:     my $urldir='/res/'.$udom.'/'.$uname.$fn;
   48:     $urldir=~s/\/[^\/]+$/\//;
   49: 
   50:     my $resfn=$docroot.'/res/'.$udom.'/'.$uname.$fn;
   51:     my $resdir=$resfn;
   52:     $resdir=~s/\/[^\/]+$/\//;
   53: 
   54:     my ($main,$suffix,$is_meta) = &get_file_info($fn);
   55:     
   56:     if (-e $resfn) {  
   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>');
  134:     }
  135:     $r->print('<p><a href="/priv/'.$uname.$fn.'">'.&mt('Back to').' '.$fn.
  136: 	      '</a></p>'); 
  137: }
  138: 
  139: # ---------------------------------- Interface for presenting specified version
  140: sub phasetwo {
  141:     my ($r,$fn,$uname,$udom)=@_;
  142:     if ($env{'form.version'}) {
  143:         my $version=$env{'form.version'};
  144: 	if ($version eq 'new') {
  145: 	    $r->print('<h3>'.&mt('Retrieving current (most recent) version').'</h3>');
  146:         } else {
  147:             $r->print('<h3>'.&mt('Retrieving old version').' '.$version.'</h3>');
  148:         }
  149: 	my ($main,$suffix,$is_meta) = &get_file_info($fn);
  150: 
  151:         my $logfile;
  152:         my $ctarget='/home/'.$uname.'/public_html'.$fn;
  153:         my $vfn=$fn;
  154:         if ($version ne 'new') {
  155: 	    $vfn=~s/\.(\Q$suffix\E)$/\.$version\.$1/;
  156:         }
  157: 
  158:         my $csource=$r->dir_config('lonDocRoot').'/res/'.$udom.'/'.$uname.$vfn;
  159: 
  160: 	my $logname = $ctarget;
  161: 	if ($is_meta) { $logname =~ s/\.meta$//; }
  162: 	$logname = $ctarget.'.log';
  163:         unless ($logfile=Apache::File->new('>>'.$logname)) {
  164: 	  $r->print(
  165:          '<font color=red>'.&mt('No write permission to user directory, FAIL').'</font>');
  166:         }
  167:         print $logfile 
  168: "\n\n================= Retrieve ".localtime()." ================\n".
  169: "Version: $version\nSource: $csource\nTarget: $ctarget\n";
  170:         $r->print('<p>'.&mt('Copying file').': ');
  171: 	if (copy($csource,$ctarget)) {
  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.
  180:                   '">'.&mt('Back to').' '.$fn.'</a></font>'); 
  181:     } else {
  182:        $r->print('<span class="LC_warning">'.&mt('Please pick a version to retrieve').'</span><p>');
  183:        &phaseone($r,$fn,$uname,$udom);
  184:     }
  185: }
  186: 
  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: 
  199: # ---------------------------------------------------------------- Main Handler
  200: sub handler {
  201: 
  202:   my $r=shift;
  203: 
  204:   my $fn;
  205: 
  206: 
  207: # Get query string for limited number of parameters
  208: 
  209:   &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  210: 					  ['filename']);
  211: 
  212:   if ($env{'form.filename'}) {
  213:       $fn=$env{'form.filename'};
  214:       $fn=~s/^http\:\/\/[^\/]+//;
  215:   } else {
  216:      $r->log_reason($env{'user.name'}.' at '.$env{'user.domain'}.
  217:          ' unspecified filename for retrieval', $r->filename); 
  218:      return HTTP_NOT_FOUND;
  219:   }
  220: 
  221:   unless ($fn) { 
  222:      $r->log_reason($env{'user.name'}.' at '.$env{'user.domain'}.
  223:          ' trying to retrieve non-existing file', $r->filename); 
  224:      return HTTP_NOT_FOUND;
  225:   } 
  226: 
  227: # ----------------------------------------------------------- Start page output
  228:   my $uname;
  229:   my $udom;
  230: 
  231:   ($uname,$udom)=
  232:     &Apache::loncacc::constructaccess($fn,$r->dir_config('lonDefDomain'));
  233:   unless (($uname) && ($udom)) {
  234:      $r->log_reason($uname.' at '.$udom.
  235:          ' trying to publish file '.$env{'form.filename'}.
  236:          ' ('.$fn.') - not authorized', 
  237:          $r->filename); 
  238:      return HTTP_NOT_ACCEPTABLE;
  239:   }
  240: 
  241:   $fn=~s/\/\~(\w+)//;
  242: 
  243:   &Apache::loncommon::content_type($r,'text/html');
  244:   $r->send_http_header;
  245: 
  246:   $r->print(&Apache::loncommon::start_page('Retrieve Published Resources'));
  247: 
  248:   
  249:   $r->print('<h1>'.&mt('Retrieve previous versions of').' <tt>'.$fn.'</tt></h1>');
  250:   
  251:   if (($uname ne $env{'user.name'}) || ($udom ne $env{'user.domain'})) {
  252:           $r->print('<h3><span class="LC_diff_coauthor">'.&mt('Co-Author').': '.$uname.
  253: 		    &mt(' at ').$udom.
  254:                '</span></h3>');
  255:   }
  256: 
  257: 
  258:   if ($env{'form.phase'} eq 'two') {
  259:       &phasetwo($r,$fn,$uname,$udom);
  260:   } else {
  261:       &phaseone($r,$fn,$uname,$udom);
  262:   }
  263: 
  264:   $r->print(&Apache::loncommon::end_page());
  265:   return OK;  
  266: }
  267: 
  268: 1;
  269: __END__
  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>