File:  [LON-CAPA] / loncom / publisher / londiff.pm
Revision 1.24: download - view: text, annotated - select for diffs
Mon Nov 10 13:20:04 2008 UTC (15 years, 7 months ago) by jms
Branches: MAIN
CVS tags: HEAD
POD documentation changes

    1: # The LearningOnline Network with CAPA
    2: # Handler to show differences between file versions
    3: #
    4: # $Id: londiff.pm,v 1.24 2008/11/10 13:20:04 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: =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: 
   48: package Apache::londiff;
   49: 
   50: use strict;
   51: use Apache::File;
   52: use File::Copy;
   53: use File::Compare;
   54: use Algorithm::Diff qw(diff);
   55: use Apache::Constants qw(:common :http :methods);
   56: use Apache::loncacc();
   57: use Apache::lonnet;
   58: use Apache::loncommon();
   59: use Apache::lonretrieve();
   60: use Apache::lonlocal;
   61: use LONCAPA();
   62: 
   63: sub get_split_file {
   64:     my ($fn,$style)=@_;
   65:     my $f1;
   66:     my @f1;
   67:     if ($style eq 'local') {
   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') {
   76: 	$f1=&Apache::lonnet::getfile($fn);
   77:     }
   78:     @f1=split(/\r\n|\r|\n/,$f1);
   79:     return @f1;
   80: }
   81: 
   82: sub are_different_files {
   83:     my ($fileone,$filetwo)=@_;
   84:     return &compare($fileone,$filetwo);
   85: }
   86: 
   87: sub handler {
   88: 
   89:     my $r=shift;
   90: # Get query string for limited number of parameters
   91: 
   92:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
   93: 					    ['filename','versiontwo',
   94: 					     'versionone','filetwo']);
   95: # Get the files
   96: 
   97:     my $cuname=$env{'user.name'};
   98:     my $cudom=$env{'user.domain'};
   99: 
  100:     if ($env{'form.filename'}=~/^\/res\//) {
  101: 	($cudom,$cuname,$env{'form.filename'})=
  102: 	    ($env{'form.filename'}=~m{^/res/($LONCAPA::domain_re)/($LONCAPA::username_re)/(.*)$});
  103:     } else {
  104: 	unless (($cuname,$cudom)=
  105: 		&Apache::loncacc::constructaccess($env{'form.filename'},
  106: 						  $r->dir_config('lonDefDomain'))) {
  107: 	    $r->log_reason($cuname.':'.$cudom.
  108: 			   ' trying to get diffs file '.$env{'form.filename'}.
  109: 			   '  - not authorized', 
  110: 			   $r->filename); 
  111: 	    return HTTP_NOT_ACCEPTABLE;
  112: 	}
  113:     }
  114:   
  115:     my $efn=$env{'form.filename'};
  116: 
  117:     $efn=~s{/\~($LONCAPA::username_re)}{}g;
  118: 
  119:     my @f1=();
  120:     my @f2=();
  121: 
  122:     &Apache::loncommon::content_type($r,'text/html');
  123:     $r->send_http_header;
  124: 
  125:     $r->print(&Apache::loncommon::start_page('Resource Differences'));
  126: 
  127:   
  128:     $r->print('<h1>'.($env{'form.filetwo'}?'':&mt('Compare versions of')).
  129: 	      ' <tt>'.$efn.'</tt></h1>');
  130:    
  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'};
  166: 	    $efn2=~s{/\~($LONCAPA::username_re)}{}g;
  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>');
  191: # Run diff
  192: 
  193: 	my $diffs = diff(\@f1, \@f2);
  194: 	
  195: # Start page output
  196: 
  197: 	my $chunk;
  198: 	my $line;
  199: 
  200: 	$r->print('<pre>');
  201: 	
  202: 	foreach $chunk (@$diffs) {
  203: 	 
  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;  
  222: }
  223: 
  224: 
  225: 1;
  226: __END__
  227: 
  228: 
  229: 
  230: 

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