File:  [LON-CAPA] / loncom / publisher / londiff.pm
Revision 1.1: download - view: text, annotated - select for diffs
Wed May 2 21:01:47 2001 UTC (23 years, 1 month ago) by www
Branches: MAIN
CVS tags: HEAD
Diffs between two versions of a file

    1: # The LearningOnline Network with CAPA
    2: # Handler to show differences between file versions
    3: #
    4: # (Handler to retrieve an old version of a file
    5: #
    6: # (Publication Handler
    7: # 
    8: # (TeX Content Handler
    9: #
   10: # 05/29/00,05/30,10/11 Gerd Kortemeyer)
   11: #
   12: # 11/28,11/29,11/30,12/01,12/02,12/04,12/23 Gerd Kortemeyer
   13: # 03/23 Guy Albertelli
   14: # 03/24,03/29 Gerd Kortemeyer)
   15: #
   16: # 03/31,04/03 Gerd Kortemeyer)
   17: #
   18: # 05/02/01 Gerd Kortemeyer
   19: 
   20: package Apache::londiff;
   21: 
   22: use strict;
   23: use Apache::File;
   24: use File::Copy;
   25: use Algorithm::Diff qw(diff);
   26: use Apache::Constants qw(:common :http :methods);
   27: 
   28: 
   29: sub handler {
   30: 
   31:   my $r=shift;
   32: 
   33: # Get query string for limited number of parameters
   34: 
   35:     map {
   36:        my ($name, $value) = split(/=/,$_);
   37:        $value =~ tr/+/ /;
   38:        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
   39:        if (($name eq 'filename') || ($name eq 'versiontwo') || 
   40:            ($name eq 'versionone')) {
   41:            unless ($ENV{'form.'.$name}) {
   42:               $ENV{'form.'.$name}=$value;
   43: 	   }
   44:        }
   45:     } (split(/&/,$ENV{'QUERY_STRING'}));
   46: 
   47: # Get the files
   48: 
   49:   my @f1=();
   50:   my @f2=();
   51: 
   52:   if ($ENV{'form.versionone'} eq 'priv') {
   53:       my $fn='/home/'.$ENV{'user.name'}.'/public_html/'.$ENV{'form.filename'};
   54:       if (-e $fn) {
   55: 	  my $fh=Apache::File->new($fn);
   56:           my $line;
   57:           while($line=<$fh>) {
   58:              chomp($line);
   59:              $f1[$#f1+1]=$line;
   60: 	 }
   61:       }
   62:   } else {
   63:       my $fn=
   64:        '/home/httpd/html//res/'.$ENV{'user.domain'}.'/'.$ENV{'user.name'}.'/';
   65:       if ($ENV{'form.versionone'}) {
   66:          my ($main,$suffix)=($ENV{'form.filename'}=~/^(.+)\.(\w+)$/);
   67:          $fn.=$main.'.'.$ENV{'form.versionone'}.'.'.$suffix;
   68:       } else {
   69:          $fn.=$ENV{'form.filename'};
   70:       }
   71:       @f1=split(/\n/,&Apache::lonnet::getfile($fn));      
   72:   }
   73: 
   74: 
   75:   if ($ENV{'form.versiontwo'} eq 'priv') {
   76:       my $fn='/home/'.$ENV{'user.name'}.'/public_html/'.$ENV{'form.filename'};
   77:       if (-e $fn) {
   78: 	  my $fh=Apache::File->new($fn);
   79:           my $line;
   80:           while($line=<$fh>) {
   81:              chomp($line);
   82:              $f2[$#f2+1]=$line;
   83: 	 }
   84:       }
   85:   } else {
   86:       my $fn=
   87:        '/home/httpd/html/res/'.$ENV{'user.domain'}.'/'.$ENV{'user.name'}.'/';
   88:       if ($ENV{'form.versiontwo'}) {
   89:          my ($main,$suffix)=($ENV{'form.filename'}=~/^(.+)\.(\w+)$/);
   90:          $fn.=$main.'.'.$ENV{'form.versiontwo'}.'.'.$suffix;
   91:       } else {
   92:          $fn.=$ENV{'form.filename'};
   93:       }
   94:       @f2=split(/\n/,&Apache::lonnet::getfile($fn));      
   95:   }
   96: 
   97: # Run diff
   98: 
   99:   my $diffs = diff(\@f1, \@f2);
  100: 
  101: # Start page output
  102: 
  103:   $r->content_type('text/html');
  104:   $r->send_http_header;
  105: 
  106:   $r->print('<html><head><title>LON-CAPA Construction Diffs</title></head>');
  107: 
  108:   $r->print('<body bgcolor="#FFFFFF">');
  109: 
  110:   
  111:   $r->print('<h1>Compare versions of <tt>'.$ENV{'form.filename'}.'</tt></h1>');
  112: 
  113:   my $chunk;
  114:   my $line;
  115: 
  116:   $r->print('<table border=2><tr><th>Version '.$ENV{'form.versionone'}.
  117:                     '</th><th>Version '.$ENV{'form.versiontwo'}.'</th>');
  118:   foreach $chunk (@$diffs) {
  119:     $r->print('</pre></tr><tr><td><pre>');
  120:     my $presign='-';  
  121:     foreach $line (@$chunk) {
  122:       my ($sign, $lineno, $text) = @$line;
  123:       if ($sign ne $presign) {
  124: 	  $r->print('</pre></td><td><pre>');
  125:           $presign=$sign;
  126:       }
  127:       $text=~s/\</\&lt\;/g;
  128:       $text=~s/\>/\&gt\;/g;
  129:       $lineno=substr($lineno.'        ',0,8);
  130:       $r->print($lineno.' '.$text."\n");
  131:     }
  132:   }
  133: 
  134:   $r->print('</tr></table>');
  135: 
  136:   $r->print('</body></html>');
  137:   return OK;  
  138: }
  139: 
  140: 
  141: 1;
  142: __END__
  143: 
  144: 
  145: 

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