File:  [LON-CAPA] / loncom / publisher / lonretrieve.pm
Revision 1.8: download - view: text, annotated - select for diffs
Wed May 2 21:35:15 2001 UTC (23 years, 2 months ago) by www
Branches: MAIN
CVS tags: HEAD
Diffs built into retrieve

    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,05/02 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);
   59:            if (&Apache::lonnet::fileembstyle($suffix) eq 'ssi') {
   60:                $r->print(
   61:                     '&nbsp;&nbsp;<a target=cat href="/adm/diff?filename='.$fn.
   62:                         '&versionone=priv&versiontwo='.$version.
   63:                         '">Diffs with Version '.$version.'</a>');
   64:            }
   65:            $r->print('</a></td></tr>');
   66:         }
   67:     }
   68:     closedir(DIR);
   69:     my ($rdev,$rino,$rmode,$rnlink,
   70:         $ruid,$rgid,$rrdev,$rsize,
   71:         $ratime,$rmtime,$rctime,
   72:         $rblksize,$rblocks)=stat($resfn);
   73:     $r->print('<tr><td><input type=radio name=version value="new"></td>'.
   74:               '<th>Current</th><td>'.localtime($rmtime).
   75:            '</td><td><a href="'.$urldir.$main.'.'.$suffix.'.meta" target=cat>'.
   76:               'Metadata current version</a></td></tr></table><p>'.
   77:            '<font size=+1 color=red>Retrieval of an old version will '.
   78:            'overwrite the file currently in construction space</font><p>'.
   79:            '<input type=submit value="Retrieve version"></form>');
   80: } else {
   81:     $r->print('<h3>No previous versions published.</h3>');
   82: }
   83: }
   84: 
   85: sub phasetwo {
   86:     my ($r,$fn,$uname,$udom)=@_;
   87:     if ($ENV{'form.version'}) {
   88:         my $version=$ENV{'form.version'};
   89: 	if ($version eq 'new') {
   90: 	    $r->print('<h3>Retrieving current (most recent) version</h3>');
   91:         } else {
   92:             $r->print('<h3>Retrieving old version '.$version.'</h3>');
   93:         }
   94:         my $logfile;
   95:         my $ctarget='/home/'.$uname.'/public_html'.$fn;
   96:         my $vfn=$fn;
   97:         if ($version ne 'new') {
   98: 	    $vfn=~s/\.(\w+)$/\.$version\.$1/;
   99:         }
  100:         my $csource=$r->dir_config('lonDocRoot').'/res/'.$udom.'/'.$uname.$vfn;
  101:         unless ($logfile=Apache::File->new('>>'.$ctarget.'.log')) {
  102: 	  $r->print(
  103:          '<font color=red>No write permission to user directory, FAIL</font>');
  104:         }
  105:         print $logfile 
  106: "\n\n================= Retrieve ".localtime()." ================\n".
  107: "Version: $version\nSource: $csource\nTarget: $ctarget\n";
  108:         $r->print('<p>Copying file: ');
  109:         if (copy($csource,$ctarget)) {
  110: 	    $r->print('ok<p>');
  111:             print $logfile "Copied sucessfully.\n\n";
  112:         } else {
  113:             my $error=$!;
  114: 	    $r->print('fail, '.$error.'<p>');
  115:             print $logfile "Copy failed: $error\n\n";
  116:         }
  117:         $r->print('<font size=+2><a href="/priv/'.$uname.$fn.
  118:                   '">Back to '.$fn.'</a></font>'); 
  119:     } else {
  120:        $r->print(
  121:    '<font size=+1 color=red>Please pick a version to retrieve</font><p>');
  122:        &phaseone($r,$fn,$uname,$udom);
  123:     }
  124: }
  125: 
  126: sub handler {
  127: 
  128:   my $r=shift;
  129: 
  130:   my $fn;
  131: 
  132:   if ($ENV{'form.filename'}) {
  133:       $fn=$ENV{'form.filename'};
  134:       $fn=~s/^http\:\/\/[^\/]+\/\~(\w+)//;
  135:   } else {
  136:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
  137:          ' unspecified filename for retrieval', $r->filename); 
  138:      return HTTP_NOT_FOUND;
  139:   }
  140: 
  141:   unless ($fn) { 
  142:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
  143:          ' trying to retrieve non-existing file', $r->filename); 
  144:      return HTTP_NOT_FOUND;
  145:   } 
  146: 
  147: # ----------------------------------------------------------- Start page output
  148: 
  149:   my $uname=$ENV{'user.name'};
  150:   my $udom=$ENV{'user.domain'};
  151: 
  152:   $r->content_type('text/html');
  153:   $r->send_http_header;
  154: 
  155:   $r->print('<html><head><title>LON-CAPA Construction Space</title></head>');
  156: 
  157:   $r->print(
  158:    '<body bgcolor="#FFFFFF"><img align=right src=/adm/lonIcons/lonlogos.gif>');
  159: 
  160:   
  161:   $r->print('<h1>Retrieve previous versions of <tt>'.$fn.'</tt></h1>');
  162: 
  163:   if ($ENV{'form.phase'} eq 'two') {
  164:       &phasetwo($r,$fn,$uname,$udom);
  165:   } else {
  166:       &phaseone($r,$fn,$uname,$udom);
  167:   }
  168: 
  169:   $r->print('</body></html>');
  170:   return OK;  
  171: }
  172: 
  173: 1;
  174: __END__

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