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