File:  [LON-CAPA] / loncom / publisher / londiff.pm
Revision 1.23: download - view: text, annotated - select for diffs
Mon Jan 15 18:34:32 2007 UTC (17 years, 3 months ago) by albertel
Branches: MAIN
CVS tags: version_2_8_X, version_2_8_2, version_2_8_1, version_2_8_0, version_2_7_X, version_2_7_99_1, version_2_7_99_0, version_2_7_1, version_2_7_0, version_2_6_X, version_2_6_99_1, version_2_6_99_0, version_2_6_3, version_2_6_2, version_2_6_1, version_2_6_0, version_2_5_X, version_2_5_99_1, version_2_5_99_0, version_2_5_2, version_2_5_1, version_2_5_0, version_2_4_X, version_2_4_99_0, version_2_4_2, version_2_4_1, version_2_4_0, version_2_3_99_0, HEAD, GCI_1
- BUG#5146, handle files with different line endins in a single file

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>