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