Annotation of loncom/publisher/lonretrieve.pm, revision 1.7
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.3 www 14: # 03/31,04/03 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>'.
58: 'Metadata Version '.$version.'</a></td></tr>');
1.2 www 59: }
60: }
61: closedir(DIR);
1.3 www 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>'.
1.4 www 70: '<font size=+1 color=red>Retrieval of an old version will '.
71: 'overwrite the file currently in construction space</font><p>'.
1.3 www 72: '<input type=submit value="Retrieve version"></form>');
1.6 www 73: } else {
74: $r->print('<h3>No previous versions published.</h3>');
75: }
1.2 www 76: }
1.1 www 77:
1.4 www 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;
1.5 www 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;
1.4 www 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".
1.5 www 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>');
1.4 www 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:
1.1 www 119: sub handler {
120:
121: my $r=shift;
122:
123: my $fn;
124:
125: if ($ENV{'form.filename'}) {
126: $fn=$ENV{'form.filename'};
1.2 www 127: $fn=~s/^http\:\/\/[^\/]+\/\~(\w+)//;
1.1 www 128: } else {
1.2 www 129: $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
130: ' unspecified filename for retrieval', $r->filename);
131: return HTTP_NOT_FOUND;
1.1 www 132: }
133:
134: unless ($fn) {
135: $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
1.2 www 136: ' trying to retrieve non-existing file', $r->filename);
1.1 www 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:
1.2 www 154: $r->print('<h1>Retrieve previous versions of <tt>'.$fn.'</tt></h1>');
1.1 www 155:
1.2 www 156: if ($ENV{'form.phase'} eq 'two') {
1.4 www 157: &phasetwo($r,$fn,$uname,$udom);
1.2 www 158: } else {
159: &phaseone($r,$fn,$uname,$udom);
160: }
1.1 www 161:
162: $r->print('</body></html>');
163: return OK;
164: }
1.7 ! www 165:
! 166: 1;
! 167: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>