File:  [LON-CAPA] / loncom / publisher / lonretrieve.pm
Revision 1.6: download - view: text, annotated - select for diffs
Tue Apr 10 19:45:37 2001 UTC (23 years, 1 month ago) by www
Branches: MAIN
CVS tags: HEAD
Subdir bug fix and detection of non-published files.

    1: # The LearningOnline Network with CAPA
    2: # Handler to retrieve an old version of a file
    3: #
    4: # (Publication Handler
    5: # 
    6: # (TeX Content Handler
    7: #
    8: # 05/29/00,05/30,10/11 Gerd Kortemeyer)
    9: #
   10: # 11/28,11/29,11/30,12/01,12/02,12/04,12/23 Gerd Kortemeyer
   11: # 03/23 Guy Albertelli
   12: # 03/24,03/29 Gerd Kortemeyer)
   13: #
   14: # 03/31,04/03 Gerd Kortemeyer
   15: 
   16: package Apache::lonretrieve;
   17: 
   18: use strict;
   19: use Apache::File;
   20: use File::Copy;
   21: use Apache::Constants qw(:common :http :methods);
   22: 
   23: sub phaseone {
   24:     my ($r,$fn,$uname,$udom)=@_;
   25:     my $docroot=$r->dir_config('lonDocRoot');
   26: 
   27:     my $urldir='/res/'.$udom.'/'.$uname.$fn;
   28:     $urldir=~s/\/[^\/]+$/\//;
   29: 
   30:     my $resfn=$docroot.'/res/'.$udom.'/'.$uname.$fn;
   31:     my $resdir=$resfn;
   32:     $resdir=~s/\/[^\/]+$/\//;
   33: 
   34:     $fn=~/\/([^\/]+)\.(\w+)$/;
   35:     my $main=$1;
   36:     my $suffix=$2;
   37: 
   38:     if (-e $resfn) {  
   39:     $r->print('<form action=/adm/retrieve method=post>'.
   40: 	      '<input type=hidden name=filename value="'.$fn.'">'.
   41:               '<input type=hidden name=phase value=two>'.
   42:               '<table border=2><tr><th>Select</th><th>Version</th>'.
   43:               '<th>Became this version on ...</th>'.
   44:               '<th>Metadata</th></tr>');
   45:     my $filename;
   46:     opendir(DIR,$resdir);
   47:     while ($filename=readdir(DIR)) {
   48:         if ($filename=~/^$main\.(\d+)\.$suffix$/) {
   49: 	   my $version=$1;
   50:            my ($rdev,$rino,$rmode,$rnlink,
   51:                 $ruid,$rgid,$rrdev,$rsize,
   52:                 $ratime,$rmtime,$rctime,
   53:                 $rblksize,$rblocks)=stat($resdir.'/'.$filename);
   54:            $r->print('<tr><td><input type=radio name=version value="'.
   55:                      $version.'"></td><th>'.$version.'</th><td>'.
   56:                      localtime($rmtime).'</td><td>'.
   57:                      '<a href="'.$urldir.$filename.'.meta" target=cat>'.
   58:                      'Metadata Version '.$version.'</a></td></tr>');
   59:         }
   60:     }
   61:     closedir(DIR);
   62:     my ($rdev,$rino,$rmode,$rnlink,
   63:         $ruid,$rgid,$rrdev,$rsize,
   64:         $ratime,$rmtime,$rctime,
   65:         $rblksize,$rblocks)=stat($resfn);
   66:     $r->print('<tr><td><input type=radio name=version value="new"></td>'.
   67:               '<th>Current</th><td>'.localtime($rmtime).
   68:            '</td><td><a href="'.$urldir.$main.'.'.$suffix.'.meta" target=cat>'.
   69:               'Metadata current version</a></td></tr></table><p>'.
   70:            '<font size=+1 color=red>Retrieval of an old version will '.
   71:            'overwrite the file currently in construction space</font><p>'.
   72:            '<input type=submit value="Retrieve version"></form>');
   73: } else {
   74:     $r->print('<h3>No previous versions published.</h3>');
   75: }
   76: }
   77: 
   78: sub phasetwo {
   79:     my ($r,$fn,$uname,$udom)=@_;
   80:     if ($ENV{'form.version'}) {
   81:         my $version=$ENV{'form.version'};
   82: 	if ($version eq 'new') {
   83: 	    $r->print('<h3>Retrieving current (most recent) version</h3>');
   84:         } else {
   85:             $r->print('<h3>Retrieving old version '.$version.'</h3>');
   86:         }
   87:         my $logfile;
   88:         my $ctarget='/home/'.$uname.'/public_html'.$fn;
   89:         my $vfn=$fn;
   90:         if ($version ne 'new') {
   91: 	    $vfn=~s/\.(\w+)$/\.$version\.$1/;
   92:         }
   93:         my $csource=$r->dir_config('lonDocRoot').'/res/'.$udom.'/'.$uname.$vfn;
   94:         unless ($logfile=Apache::File->new('>>'.$ctarget.'.log')) {
   95: 	  $r->print(
   96:          '<font color=red>No write permission to user directory, FAIL</font>');
   97:         }
   98:         print $logfile 
   99: "\n\n================= Retrieve ".localtime()." ================\n".
  100: "Version: $version\nSource: $csource\nTarget: $ctarget\n";
  101:         $r->print('<p>Copying file: ');
  102:         if (copy($csource,$ctarget)) {
  103: 	    $r->print('ok<p>');
  104:             print $logfile "Copied sucessfully.\n\n";
  105:         } else {
  106:             my $error=$!;
  107: 	    $r->print('fail, '.$error.'<p>');
  108:             print $logfile "Copy failed: $error\n\n";
  109:         }
  110:         $r->print('<font size=+2><a href="/priv/'.$uname.$fn.
  111:                   '">Back to '.$fn.'</a></font>'); 
  112:     } else {
  113:        $r->print(
  114:    '<font size=+1 color=red>Please pick a version to retrieve</font><p>');
  115:        &phaseone($r,$fn,$uname,$udom);
  116:     }
  117: }
  118: 
  119: sub handler {
  120: 
  121:   my $r=shift;
  122: 
  123:   my $fn;
  124: 
  125:   if ($ENV{'form.filename'}) {
  126:       $fn=$ENV{'form.filename'};
  127:       $fn=~s/^http\:\/\/[^\/]+\/\~(\w+)//;
  128:   } else {
  129:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
  130:          ' unspecified filename for retrieval', $r->filename); 
  131:      return HTTP_NOT_FOUND;
  132:   }
  133: 
  134:   unless ($fn) { 
  135:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
  136:          ' trying to retrieve non-existing file', $r->filename); 
  137:      return HTTP_NOT_FOUND;
  138:   } 
  139: 
  140: # ----------------------------------------------------------- Start page output
  141: 
  142:   my $uname=$ENV{'user.name'};
  143:   my $udom=$ENV{'user.domain'};
  144: 
  145:   $r->content_type('text/html');
  146:   $r->send_http_header;
  147: 
  148:   $r->print('<html><head><title>LON-CAPA Construction Space</title></head>');
  149: 
  150:   $r->print(
  151:    '<body bgcolor="#FFFFFF"><img align=right src=/adm/lonIcons/lonlogos.gif>');
  152: 
  153:   
  154:   $r->print('<h1>Retrieve previous versions of <tt>'.$fn.'</tt></h1>');
  155: 
  156:   if ($ENV{'form.phase'} eq 'two') {
  157:       &phasetwo($r,$fn,$uname,$udom);
  158:   } else {
  159:       &phaseone($r,$fn,$uname,$udom);
  160:   }
  161: 
  162:   $r->print('</body></html>');
  163:   return OK;  
  164: }

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