Annotation of loncom/publisher/lonretrieve.pm, revision 1.9

1.1       www         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: #
1.8       www        14: # 03/31,04/03,05/02 Gerd Kortemeyer
1.1       www        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: 
1.2       www        23: sub phaseone {
                     24:     my ($r,$fn,$uname,$udom)=@_;
                     25:     my $docroot=$r->dir_config('lonDocRoot');
                     26: 
1.3       www        27:     my $urldir='/res/'.$udom.'/'.$uname.$fn;
                     28:     $urldir=~s/\/[^\/]+$/\//;
                     29: 
                     30:     my $resfn=$docroot.'/res/'.$udom.'/'.$uname.$fn;
                     31:     my $resdir=$resfn;
1.2       www        32:     $resdir=~s/\/[^\/]+$/\//;
                     33: 
1.6       www        34:     $fn=~/\/([^\/]+)\.(\w+)$/;
1.2       www        35:     my $main=$1;
                     36:     my $suffix=$2;
1.6       www        37: 
                     38:     if (-e $resfn) {  
1.3       www        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>');
1.2       www        45:     my $filename;
                     46:     opendir(DIR,$resdir);
                     47:     while ($filename=readdir(DIR)) {
                     48:         if ($filename=~/^$main\.(\d+)\.$suffix$/) {
1.3       www        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>'.
1.9     ! www        58:                      'Metadata Version '.$version.'</a>');
1.8       www        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>');
1.2       www        66:         }
                     67:     }
                     68:     closedir(DIR);
1.3       www        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>'.
1.9     ! www        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>'.
1.4       www        84:            '<font size=+1 color=red>Retrieval of an old version will '.
                     85:            'overwrite the file currently in construction space</font><p>'.
1.3       www        86:            '<input type=submit value="Retrieve version"></form>');
1.6       www        87: } else {
                     88:     $r->print('<h3>No previous versions published.</h3>');
                     89: }
1.2       www        90: }
1.1       www        91: 
1.4       www        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;
1.5       www       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;
1.4       www       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".
1.5       www       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>'); 
1.4       www       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: 
1.1       www       133: sub handler {
                    134: 
                    135:   my $r=shift;
                    136: 
                    137:   my $fn;
                    138: 
                    139:   if ($ENV{'form.filename'}) {
                    140:       $fn=$ENV{'form.filename'};
1.2       www       141:       $fn=~s/^http\:\/\/[^\/]+\/\~(\w+)//;
1.1       www       142:   } else {
1.2       www       143:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
                    144:          ' unspecified filename for retrieval', $r->filename); 
                    145:      return HTTP_NOT_FOUND;
1.1       www       146:   }
                    147: 
                    148:   unless ($fn) { 
                    149:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
1.2       www       150:          ' trying to retrieve non-existing file', $r->filename); 
1.1       www       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:   
1.2       www       168:   $r->print('<h1>Retrieve previous versions of <tt>'.$fn.'</tt></h1>');
1.1       www       169: 
1.2       www       170:   if ($ENV{'form.phase'} eq 'two') {
1.4       www       171:       &phasetwo($r,$fn,$uname,$udom);
1.2       www       172:   } else {
                    173:       &phaseone($r,$fn,$uname,$udom);
                    174:   }
1.1       www       175: 
                    176:   $r->print('</body></html>');
                    177:   return OK;  
                    178: }
1.7       www       179: 
                    180: 1;
                    181: __END__

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