File:  [LON-CAPA] / loncom / publisher / lonretrieve.pm
Revision 1.9: download - view: text, annotated - select for diffs
Wed May 2 23:00:05 2001 UTC (23 years ago) by www
Branches: MAIN
CVS tags: HEAD
Diff for current version, and unified output

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

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