Annotation of loncom/publisher/londiff.pm, revision 1.20
1.1 www 1: # The LearningOnline Network with CAPA
2: # Handler to show differences between file versions
3: #
1.20 ! albertel 4: # $Id: londiff.pm,v 1.19 2006/05/17 13:41:45 www 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;
1.19 www 35: use File::Compare;
1.1 www 36: use Algorithm::Diff qw(diff);
37: use Apache::Constants qw(:common :http :methods);
1.20 ! albertel 38: use Apache::loncacc();
1.16 albertel 39: use Apache::lonnet;
1.7 harris41 40: use Apache::loncommon();
1.20 ! albertel 41: use Apache::lonretrieve();
1.13 sakharuk 42: use Apache::lonlocal;
1.1 www 43:
1.15 albertel 44:
45: sub get_split_file {
46: my ($fn,$style)=@_;
47: my $f1;
48: my @f1;
1.19 www 49: if ($style eq 'local') {
1.15 albertel 50: if (-e $fn) {
51: my $fh=Apache::File->new($fn);
52: my $line;
53: while($line=<$fh>) {
54: $f1.=$line;
55: }
56: }
57: } elsif ($style eq 'remote') {
58: my $f1=&Apache::lonnet::getfile($fn);
59: }
60: if ($f1=~/\r/) {
61: @f1=split(/\r/,&Apache::lonnet::getfile($fn));
62: foreach my $line (@f1) {
63: $line=~s/\n//g;
64: }
65: } else {
66: @f1=split(/\n/,&Apache::lonnet::getfile($fn));
67: }
68: return @f1;
69: }
70:
1.19 www 71: sub are_different_files {
72: my ($fileone,$filetwo)=@_;
73: return &compare($fileone,$filetwo);
74: }
75:
1.1 www 76: sub handler {
77:
78: my $r=shift;
79: # Get query string for limited number of parameters
80:
1.8 stredwic 81: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.17 www 82: ['filename','versiontwo','versionone','filetwo']);
1.1 www 83: # Get the files
84:
1.16 albertel 85: my $cuname=$env{'user.name'};
86: my $cudom=$env{'user.domain'};
1.5 www 87:
1.16 albertel 88: if ($env{'form.filename'}=~/^\/res\//) {
89: ($cudom,$cuname,$env{'form.filename'})=
1.17 www 90: ($env{'form.filename'}=~/^\/res\/(\w+)\/(\w+)\/(.*)$/);
1.10 www 91: } else {
1.17 www 92: unless (($cuname,$cudom)=
93: &Apache::loncacc::constructaccess($env{'form.filename'},
94: $r->dir_config('lonDefDomain'))) {
95: $r->log_reason($cuname.' at '.$cudom.
96: ' trying to get diffs file '.$env{'form.filename'}.
97: ' - not authorized',
98: $r->filename);
99: return HTTP_NOT_ACCEPTABLE;
100: }
1.5 www 101: }
1.17 www 102:
1.16 albertel 103: my $efn=$env{'form.filename'};
1.5 www 104:
105: $efn=~s/\/\~(\w+)//g;
106:
1.1 www 107: my @f1=();
108: my @f2=();
109:
1.14 www 110: &Apache::loncommon::content_type($r,'text/html');
1.2 www 111: $r->send_http_header;
112:
1.18 albertel 113: $r->print(&Apache::loncommon::start_page('Resource Differences'));
1.2 www 114:
115:
1.17 www 116: $r->print('<h1>'.($env{'form.filetwo'}?'':&mt('Compare versions of')).
117: ' <tt>'.$efn.'</tt></h1>');
1.5 www 118:
1.16 albertel 119: if (($cuname ne $env{'user.name'}) || ($cudom ne $env{'user.domain'})) {
1.5 www 120: $r->print('<h3><font color=red>Co-Author: '.$cuname.' at '.$cudom.
121: '</font></h3>');
122: }
123:
1.2 www 124:
1.20 ! albertel 125: if (&Apache::loncommon::fileembstyle(($efn=~/\.(\w+)$/)) eq 'ssi'
! 126: || $efn =~ /\.meta$/) {
1.17 www 127: $r->print('<p><font color="red">');
128: if ($env{'form.versionone'} eq 'priv') {
129: my $fn='/home/'.$cuname.'/public_html/'.$efn;
130: @f1=&get_split_file($fn,'local');
131: $r->print('<b>'.&mt('Construction Space Version').'</b>');
132: } else {
133: my $fn=
1.19 www 134: '/home/httpd/html/res/'.$cudom.'/'.$cuname.'/';
1.17 www 135: if ($env{'form.versionone'}) {
1.20 ! albertel 136: my ($main,$suffix,$is_meta)=
! 137: &Apache::lonretrieve::get_file_info($efn);
! 138:
! 139: $fn.=($efn =~m|(.*/)[^/]+|)[0];
! 140: # add on to $fn the path information in $efn
1.17 www 141: $fn.=$main.'.'.$env{'form.versionone'}.'.'.$suffix;
142: $r->print('<b>'.&mt('Version').' '.$env{'form.versionone'}.'</b>');
143: } else {
144: $fn.=$efn;
145: $r->print('<b>'.&mt('Current Version').'</b>');
146: }
147: @f1=&get_split_file($fn,'remote');
148: }
149:
150: $r->print('</font><br />'.&mt('versus').'<br /><font color="green">');
151:
152: if ($env{'form.filetwo'}) {
153: my $efn2=$env{'form.filetwo'};
154: $efn2=~s/\/\~(\w+)//g;
155: my $fn='/home/'.$cuname.'/public_html/'.$efn2;
156: @f2=&get_split_file($fn,'local');
157: $r->print('<tt>'.$efn2.'</tt>');
158: } elsif ($env{'form.versiontwo'} eq 'priv') {
159: my $fn='/home/'.$cuname.'/public_html/'.$efn;
160: @f2=&get_split_file($fn,'local');
161: $r->print('<b>'.&mt('Construction Space Version').'</b>');
162: } else {
163: my $fn=
164: '/home/httpd/html/res/'.$cudom.'/'.$cuname.'/';
165: if ($env{'form.versiontwo'}) {
1.20 ! albertel 166: my ($main,$suffix,$is_meta)=
! 167: &Apache::lonretrieve::get_file_info($efn);
! 168: # add on to $fn the path information in $efn
! 169: $fn.=($efn =~m|(.*/)[^/]+|)[0];
1.17 www 170: $fn.=$main.'.'.$env{'form.versiontwo'}.'.'.$suffix;
171: $r->print('<b>'.&mt('Version').' '.$env{'form.versiontwo'}.'</b>');
172: } else {
173: $fn.=$efn;
174: $r->print('<b>'.&mt('Current Version').'</b>');
175: }
176: @f2=&get_split_file($fn,'remote');
177: }
178: $r->print('</font></p>');
1.1 www 179: # Run diff
180:
1.17 www 181: my $diffs = diff(\@f1, \@f2);
1.1 www 182:
183: # Start page output
184:
1.17 www 185: my $chunk;
186: my $line;
1.1 www 187:
1.17 www 188: $r->print('<pre>');
1.3 www 189:
1.17 www 190: foreach $chunk (@$diffs) {
191:
192: foreach $line (@$chunk) {
193: my ($sign, $lineno, $text) = @$line;
194: $text=~s/\</\<\;/g;
195: $text=~s/\>/\>\;/g;
196: $lineno=substr($lineno.' ',0,7);
197: $r->print('<font color='.(($sign eq '+')?'green':'red').'>'.
198: $sign.' '.$lineno.' '.$text."</font>\n");
199: }
200: $r->print("<hr>\n");
201: }
202: $r->print('</pre>');
203:
204: } else {
205: $r->print('<h1><font color=red>'.&mt('Binary File').'</font></h1>');
206: }
1.18 albertel 207: $r->print('<center><a href="javascript:window.close();">'.&mt('Close This Window').'</a></center>');
208: $r->print(&Apache::loncommon::end_page());
1.1 www 209: return OK;
210: }
211:
212:
213: 1;
214: __END__
1.2 www 215:
1.1 www 216:
217:
218:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>