Annotation of loncom/publisher/londiff.pm, revision 1.17
1.1 www 1: # The LearningOnline Network with CAPA
2: # Handler to show differences between file versions
3: #
1.17 ! www 4: # $Id: londiff.pm,v 1.16 2005/04/07 06:56:26 albertel Exp $
1.6 albertel 5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
1.7 harris41 28: ###
1.1 www 29:
30: package Apache::londiff;
31:
32: use strict;
33: use Apache::File;
34: use File::Copy;
35: use Algorithm::Diff qw(diff);
36: use Apache::Constants qw(:common :http :methods);
1.5 www 37: use Apache::loncacc;
1.16 albertel 38: use Apache::lonnet;
1.7 harris41 39: use Apache::loncommon();
1.13 sakharuk 40: use Apache::lonlocal;
1.1 www 41:
1.15 albertel 42:
43: sub get_split_file {
44: my ($fn,$style)=@_;
45: my $f1;
46: my @f1;
47: if ($style='local') {
48: if (-e $fn) {
49: my $fh=Apache::File->new($fn);
50: my $line;
51: while($line=<$fh>) {
52: $f1.=$line;
53: }
54: }
55: } elsif ($style eq 'remote') {
56: my $f1=&Apache::lonnet::getfile($fn);
57: }
58: if ($f1=~/\r/) {
59: @f1=split(/\r/,&Apache::lonnet::getfile($fn));
60: foreach my $line (@f1) {
61: $line=~s/\n//g;
62: }
63: } else {
64: @f1=split(/\n/,&Apache::lonnet::getfile($fn));
65: }
66: return @f1;
67: }
68:
1.1 www 69: sub handler {
70:
71: my $r=shift;
72:
73: # Get query string for limited number of parameters
74:
1.8 stredwic 75: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.17 ! www 76: ['filename','versiontwo','versionone','filetwo']);
1.1 www 77: # Get the files
78:
1.16 albertel 79: my $cuname=$env{'user.name'};
80: my $cudom=$env{'user.domain'};
1.5 www 81:
1.16 albertel 82: if ($env{'form.filename'}=~/^\/res\//) {
83: ($cudom,$cuname,$env{'form.filename'})=
1.17 ! www 84: ($env{'form.filename'}=~/^\/res\/(\w+)\/(\w+)\/(.*)$/);
1.10 www 85: } else {
1.17 ! www 86: unless (($cuname,$cudom)=
! 87: &Apache::loncacc::constructaccess($env{'form.filename'},
! 88: $r->dir_config('lonDefDomain'))) {
! 89: $r->log_reason($cuname.' at '.$cudom.
! 90: ' trying to get diffs file '.$env{'form.filename'}.
! 91: ' - not authorized',
! 92: $r->filename);
! 93: return HTTP_NOT_ACCEPTABLE;
! 94: }
1.5 www 95: }
1.17 ! www 96:
1.16 albertel 97: my $efn=$env{'form.filename'};
1.5 www 98:
99: $efn=~s/\/\~(\w+)//g;
100:
1.1 www 101: my @f1=();
102: my @f2=();
103:
1.14 www 104: &Apache::loncommon::content_type($r,'text/html');
1.2 www 105: $r->send_http_header;
106:
107: $r->print('<html><head><title>LON-CAPA Construction Diffs</title></head>');
108:
1.14 www 109: $r->print(&Apache::loncommon::bodytag('Resource Differences'));
1.2 www 110:
111:
1.17 ! www 112: $r->print('<h1>'.($env{'form.filetwo'}?'':&mt('Compare versions of')).
! 113: ' <tt>'.$efn.'</tt></h1>');
1.5 www 114:
1.16 albertel 115: if (($cuname ne $env{'user.name'}) || ($cudom ne $env{'user.domain'})) {
1.5 www 116: $r->print('<h3><font color=red>Co-Author: '.$cuname.' at '.$cudom.
117: '</font></h3>');
118: }
119:
1.2 www 120:
1.17 ! www 121: if (&Apache::loncommon::fileembstyle(($efn=~/\.(\w+)$/)) eq 'ssi') {
! 122: $r->print('<p><font color="red">');
! 123: if ($env{'form.versionone'} eq 'priv') {
! 124: my $fn='/home/'.$cuname.'/public_html/'.$efn;
! 125: @f1=&get_split_file($fn,'local');
! 126: $r->print('<b>'.&mt('Construction Space Version').'</b>');
! 127: } else {
! 128: my $fn=
! 129: '/home/httpd/html//res/'.$cudom.'/'.$cuname.'/';
! 130: if ($env{'form.versionone'}) {
! 131: my ($main,$suffix)=($efn=~/^(.+)\.(\w+)$/);
! 132: $fn.=$main.'.'.$env{'form.versionone'}.'.'.$suffix;
! 133: $r->print('<b>'.&mt('Version').' '.$env{'form.versionone'}.'</b>');
! 134: } else {
! 135: $fn.=$efn;
! 136: $r->print('<b>'.&mt('Current Version').'</b>');
! 137: }
! 138: @f1=&get_split_file($fn,'remote');
! 139: }
! 140:
! 141: $r->print('</font><br />'.&mt('versus').'<br /><font color="green">');
! 142:
! 143: if ($env{'form.filetwo'}) {
! 144: my $efn2=$env{'form.filetwo'};
! 145: $efn2=~s/\/\~(\w+)//g;
! 146: my $fn='/home/'.$cuname.'/public_html/'.$efn2;
! 147: @f2=&get_split_file($fn,'local');
! 148: $r->print('<tt>'.$efn2.'</tt>');
! 149: } elsif ($env{'form.versiontwo'} eq 'priv') {
! 150: my $fn='/home/'.$cuname.'/public_html/'.$efn;
! 151: @f2=&get_split_file($fn,'local');
! 152: $r->print('<b>'.&mt('Construction Space Version').'</b>');
! 153: } else {
! 154: my $fn=
! 155: '/home/httpd/html/res/'.$cudom.'/'.$cuname.'/';
! 156: if ($env{'form.versiontwo'}) {
! 157: my ($main,$suffix)=($efn=~/^(.+)\.(\w+)$/);
! 158: $fn.=$main.'.'.$env{'form.versiontwo'}.'.'.$suffix;
! 159: $r->print('<b>'.&mt('Version').' '.$env{'form.versiontwo'}.'</b>');
! 160: } else {
! 161: $fn.=$efn;
! 162: $r->print('<b>'.&mt('Current Version').'</b>');
! 163: }
! 164: @f2=&get_split_file($fn,'remote');
! 165: }
! 166: $r->print('</font></p>');
1.1 www 167: # Run diff
168:
1.17 ! www 169: my $diffs = diff(\@f1, \@f2);
1.1 www 170:
171: # Start page output
172:
1.17 ! www 173: my $chunk;
! 174: my $line;
1.1 www 175:
1.17 ! www 176: $r->print('<pre>');
1.3 www 177:
1.17 ! www 178: foreach $chunk (@$diffs) {
! 179:
! 180: foreach $line (@$chunk) {
! 181: my ($sign, $lineno, $text) = @$line;
! 182: $text=~s/\</\<\;/g;
! 183: $text=~s/\>/\>\;/g;
! 184: $lineno=substr($lineno.' ',0,7);
! 185: $r->print('<font color='.(($sign eq '+')?'green':'red').'>'.
! 186: $sign.' '.$lineno.' '.$text."</font>\n");
! 187: }
! 188: $r->print("<hr>\n");
! 189: }
! 190: $r->print('</pre>');
! 191:
! 192: } else {
! 193: $r->print('<h1><font color=red>'.&mt('Binary File').'</font></h1>');
! 194: }
1.13 sakharuk 195: $r->print('<center><a href="javascript:window.close();">'.&mt('Close This Window').'</a></center></body></html>');
1.1 www 196: return OK;
197: }
198:
199:
200: 1;
201: __END__
1.2 www 202:
1.1 www 203:
204:
205:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>