File:  [LON-CAPA] / loncom / publisher / lonretrieve.pm
Revision 1.11: download - view: text, annotated - select for diffs
Wed May 9 17:38:13 2001 UTC (23 years ago) by www
Branches: MAIN
CVS tags: HEAD
Make londiff work for co-author

    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,05/09 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: use Apache::loncacc;
   23: 
   24: sub phaseone {
   25:     my ($r,$fn,$uname,$udom)=@_;
   26:     my $docroot=$r->dir_config('lonDocRoot');
   27: 
   28:     my $urldir='/res/'.$udom.'/'.$uname.$fn;
   29:     $urldir=~s/\/[^\/]+$/\//;
   30: 
   31:     my $resfn=$docroot.'/res/'.$udom.'/'.$uname.$fn;
   32:     my $resdir=$resfn;
   33:     $resdir=~s/\/[^\/]+$/\//;
   34: 
   35:     $fn=~/\/([^\/]+)\.(\w+)$/;
   36:     my $main=$1;
   37:     my $suffix=$2;
   38: 
   39:     if (-e $resfn) {  
   40:     $r->print('<form action=/adm/retrieve method=post>'.
   41: 	      '<input type=hidden name=filename value="'.$fn.'">'.
   42:               '<input type=hidden name=phase value=two>'.
   43:               '<table border=2><tr><th>Select</th><th>Version</th>'.
   44:               '<th>Became this version on ...</th>'.
   45:               '<th>Metadata</th></tr>');
   46:     my $filename;
   47:     opendir(DIR,$resdir);
   48:     while ($filename=readdir(DIR)) {
   49:         if ($filename=~/^$main\.(\d+)\.$suffix$/) {
   50: 	   my $version=$1;
   51:            my ($rdev,$rino,$rmode,$rnlink,
   52:                 $ruid,$rgid,$rrdev,$rsize,
   53:                 $ratime,$rmtime,$rctime,
   54:                 $rblksize,$rblocks)=stat($resdir.'/'.$filename);
   55:            $r->print('<tr><td><input type=radio name=version value="'.
   56:                      $version.'"></td><th>'.$version.'</th><td>'.
   57:                      localtime($rmtime).'</td><td>'.
   58:                      '<a href="'.$urldir.$filename.'.meta" target=cat>'.
   59:                      'Metadata Version '.$version.'</a>');
   60:            if (&Apache::lonnet::fileembstyle($suffix) eq 'ssi') {
   61:                $r->print(
   62:                     '&nbsp;&nbsp;<a target=cat href="/adm/diff?filename=/~'.
   63:                         $uname.$fn.
   64:                         '&versionone=priv&versiontwo='.$version.
   65:                         '">Diffs with Version '.$version.'</a>');
   66:            }
   67:            $r->print('</a></td></tr>');
   68:         }
   69:     }
   70:     closedir(DIR);
   71:     my ($rdev,$rino,$rmode,$rnlink,
   72:         $ruid,$rgid,$rrdev,$rsize,
   73:         $ratime,$rmtime,$rctime,
   74:         $rblksize,$rblocks)=stat($resfn);
   75:     $r->print('<tr><td><input type=radio name=version value="new"></td>'.
   76:               '<th>Current</th><td>'.localtime($rmtime).
   77:            '</td><td><a href="'.$urldir.$main.'.'.$suffix.'.meta" target=cat>'.
   78:               'Metadata current version</a>');           
   79:            if (&Apache::lonnet::fileembstyle($suffix) eq 'ssi') {
   80:                $r->print(
   81:                     '&nbsp;&nbsp;<a target=cat href="/adm/diff?filename=/~'.
   82:                         $uname.$fn.
   83:                         '&versionone=priv'.
   84:                         '">Diffs with current Version</a>');
   85:            }
   86:            $r->print('</td></tr></table><p>'.
   87:            '<font size=+1 color=red>Retrieval of an old version will '.
   88:            'overwrite the file currently in construction space</font><p>'.
   89:            '<input type=submit value="Retrieve version"></form>');
   90: } else {
   91:     $r->print('<h3>No previous versions published.</h3>');
   92: }
   93: }
   94: 
   95: sub phasetwo {
   96:     my ($r,$fn,$uname,$udom)=@_;
   97:     if ($ENV{'form.version'}) {
   98:         my $version=$ENV{'form.version'};
   99: 	if ($version eq 'new') {
  100: 	    $r->print('<h3>Retrieving current (most recent) version</h3>');
  101:         } else {
  102:             $r->print('<h3>Retrieving old version '.$version.'</h3>');
  103:         }
  104:         my $logfile;
  105:         my $ctarget='/home/'.$uname.'/public_html'.$fn;
  106:         my $vfn=$fn;
  107:         if ($version ne 'new') {
  108: 	    $vfn=~s/\.(\w+)$/\.$version\.$1/;
  109:         }
  110:         my $csource=$r->dir_config('lonDocRoot').'/res/'.$udom.'/'.$uname.$vfn;
  111:         unless ($logfile=Apache::File->new('>>'.$ctarget.'.log')) {
  112: 	  $r->print(
  113:          '<font color=red>No write permission to user directory, FAIL</font>');
  114:         }
  115:         print $logfile 
  116: "\n\n================= Retrieve ".localtime()." ================\n".
  117: "Version: $version\nSource: $csource\nTarget: $ctarget\n";
  118:         $r->print('<p>Copying file: ');
  119:         if (copy($csource,$ctarget)) {
  120: 	    $r->print('ok<p>');
  121:             print $logfile "Copied sucessfully.\n\n";
  122:         } else {
  123:             my $error=$!;
  124: 	    $r->print('fail, '.$error.'<p>');
  125:             print $logfile "Copy failed: $error\n\n";
  126:         }
  127:         $r->print('<font size=+2><a href="/priv/'.$uname.$fn.
  128:                   '">Back to '.$fn.'</a></font>'); 
  129:     } else {
  130:        $r->print(
  131:    '<font size=+1 color=red>Please pick a version to retrieve</font><p>');
  132:        &phaseone($r,$fn,$uname,$udom);
  133:     }
  134: }
  135: 
  136: sub handler {
  137: 
  138:   my $r=shift;
  139: 
  140:   my $fn;
  141: 
  142:   if ($ENV{'form.filename'}) {
  143:       $fn=$ENV{'form.filename'};
  144:       $fn=~s/^http\:\/\/[^\/]+//;
  145:   } else {
  146:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
  147:          ' unspecified filename for retrieval', $r->filename); 
  148:      return HTTP_NOT_FOUND;
  149:   }
  150: 
  151:   unless ($fn) { 
  152:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
  153:          ' trying to retrieve non-existing file', $r->filename); 
  154:      return HTTP_NOT_FOUND;
  155:   } 
  156: 
  157: # ----------------------------------------------------------- Start page output
  158:   my $uname;
  159:   my $udom;
  160: 
  161:   unless (($uname,$udom)=
  162:     &Apache::loncacc::constructaccess(
  163:              $fn,$r->dir_config('lonDefDomain'))) {
  164:      $r->log_reason($uname.' at '.$udom.
  165:          ' trying to publish file '.$ENV{'form.filename'}.
  166:          ' ('.$fn.') - not authorized', 
  167:          $r->filename); 
  168:      return HTTP_NOT_ACCEPTABLE;
  169:   }
  170: 
  171:   $fn=~s/\/\~(\w+)//;
  172: 
  173:   $r->content_type('text/html');
  174:   $r->send_http_header;
  175: 
  176:   $r->print('<html><head><title>LON-CAPA Construction Space</title></head>');
  177: 
  178:   $r->print(
  179:    '<body bgcolor="#FFFFFF"><img align=right src=/adm/lonIcons/lonlogos.gif>');
  180: 
  181:   
  182:   $r->print('<h1>Retrieve previous versions of <tt>'.$fn.'</tt></h1>');
  183:   
  184:   if (($uname ne $ENV{'user.name'}) || ($udom ne $ENV{'user.domain'})) {
  185:           $r->print('<h3><font color=red>Co-Author: '.$uname.' at '.$udom.
  186:                '</font></h3>');
  187:   }
  188: 
  189: 
  190:   if ($ENV{'form.phase'} eq 'two') {
  191:       &phasetwo($r,$fn,$uname,$udom);
  192:   } else {
  193:       &phaseone($r,$fn,$uname,$udom);
  194:   }
  195: 
  196:   $r->print('</body></html>');
  197:   return OK;  
  198: }
  199: 
  200: 1;
  201: __END__

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