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