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