File:  [LON-CAPA] / loncom / publisher / londiff.pm
Revision 1.15: download - view: text, annotated - select for diffs
Thu Mar 11 22:44:01 2004 UTC (20 years, 2 months ago) by albertel
Branches: MAIN
CVS tags: version_1_3_X, version_1_3_3, version_1_3_2, version_1_3_1, version_1_3_0, version_1_2_X, version_1_2_99_1, version_1_2_99_0, version_1_2_1, version_1_2_0, version_1_1_99_5, version_1_1_99_4, version_1_1_99_3, version_1_1_99_2, version_1_1_99_1, version_1_1_99_0, HEAD
- Fixes BUG#2602, line ending changes made diff useless.

    1: # The LearningOnline Network with CAPA
    2: # Handler to show differences between file versions
    3: #
    4: # $Id: londiff.pm,v 1.15 2004/03/11 22:44:01 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: # (Handler to retrieve an old version of a file
   30: #
   31: # (Publication Handler
   32: # 
   33: # (TeX Content Handler
   34: #
   35: # 05/29/00,05/30,10/11 Gerd Kortemeyer)
   36: #
   37: # 11/28,11/29,11/30,12/01,12/02,12/04,12/23 Gerd Kortemeyer
   38: # 03/23 Guy Albertelli
   39: # 03/24,03/29 Gerd Kortemeyer)
   40: #
   41: # 03/31,04/03 Gerd Kortemeyer)
   42: #
   43: # 05/02/01,05/09 Gerd Kortemeyer
   44: #
   45: ###
   46: 
   47: package Apache::londiff;
   48: 
   49: use strict;
   50: use Apache::File;
   51: use File::Copy;
   52: use Algorithm::Diff qw(diff);
   53: use Apache::Constants qw(:common :http :methods);
   54: use Apache::loncacc;
   55: use Apache::lonnet();
   56: use Apache::loncommon();
   57: use Apache::lonlocal;
   58: 
   59: 
   60: sub get_split_file {
   61:     my ($fn,$style)=@_;
   62:     my $f1;
   63:     my @f1;
   64:     if ($style='local') {
   65: 	if (-e $fn) {
   66: 	    my $fh=Apache::File->new($fn);
   67: 	    my $line;	
   68: 	    while($line=<$fh>) {
   69: 		$f1.=$line;
   70: 	    }
   71: 	}
   72:     } elsif ($style eq 'remote') {
   73: 	my $f1=&Apache::lonnet::getfile($fn);
   74:     }
   75:     if ($f1=~/\r/) {
   76: 	@f1=split(/\r/,&Apache::lonnet::getfile($fn));      
   77: 	foreach my $line (@f1) {
   78: 	    $line=~s/\n//g;
   79: 	}
   80:     } else {
   81: 	@f1=split(/\n/,&Apache::lonnet::getfile($fn));      
   82:     }
   83:     return @f1;
   84: }
   85: 
   86: sub handler {
   87: 
   88:   my $r=shift;
   89: 
   90: # Get query string for limited number of parameters
   91: 
   92:    &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
   93:                                        ['filename','versiontwo','versionone']);
   94: # Get the files
   95: 
   96:   my $cuname=$ENV{'user.name'};
   97:   my $cudom=$ENV{'user.domain'};
   98: 
   99:   if ($ENV{'form.filename'}=~/^\/res\//) {
  100:       ($cudom,$cuname,$ENV{'form.filename'})=
  101:        ($ENV{'form.filename'}=~/^\/res\/(\w+)\/(\w+)\/(.*)$/);
  102:   } else {
  103:    unless (($cuname,$cudom)=
  104:     &Apache::loncacc::constructaccess($ENV{'form.filename'},
  105:                                       $r->dir_config('lonDefDomain'))) {
  106:      $r->log_reason($cuname.' at '.$cudom.
  107:          ' trying to get diffs file '.$ENV{'form.filename'}.
  108:          '  - not authorized', 
  109:          $r->filename); 
  110:      return HTTP_NOT_ACCEPTABLE;
  111:    }
  112:   }
  113: 
  114:   my $efn=$ENV{'form.filename'};
  115: 
  116:   $efn=~s/\/\~(\w+)//g;
  117: 
  118:   my @f1=();
  119:   my @f2=();
  120: 
  121:   &Apache::loncommon::content_type($r,'text/html');
  122:   $r->send_http_header;
  123: 
  124:   $r->print('<html><head><title>LON-CAPA Construction Diffs</title></head>');
  125: 
  126:   $r->print(&Apache::loncommon::bodytag('Resource Differences'));
  127: 
  128:   
  129:   $r->print('<h1>'.&mt('Compare versions of').' <tt>'.$efn.'</tt></h1>');
  130:    
  131:        if (($cuname ne $ENV{'user.name'}) || ($cudom ne $ENV{'user.domain'})) {
  132:           $r->print('<h3><font color=red>Co-Author: '.$cuname.' at '.$cudom.
  133:                '</font></h3>');
  134:       }
  135: 
  136: 
  137:  if (&Apache::loncommon::fileembstyle(($efn=~/\.(\w+)$/)) eq
  138:       'ssi') {
  139:   $r->print('<p><font color="red">');
  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)=($efn=~/^(.+)\.(\w+)$/);
  149:          $fn.=$main.'.'.$ENV{'form.versionone'}.'.'.$suffix;
  150: 	 $r->print('<b>'.&mt('Version').' '.$ENV{'form.versionone'}.'</b>');
  151:       } else {
  152:          $fn.=$efn;
  153: 	 $r->print('<b>'.&mt('Current Version').'</b>');
  154:       }
  155:       @f1=&get_split_file($fn,'remote');
  156:   }
  157: 
  158:   $r->print('</font><br />'.&mt('versus').'<br /><font color="green">');
  159: 
  160:   if ($ENV{'form.versiontwo'} eq 'priv') {
  161:       my $fn='/home/'.$cuname.'/public_html/'.$efn;
  162:       @f2=&get_split_file($fn,'local');
  163:       $r->print('<b>'.&mt('Construction Space Version').'</b>');
  164:   } else {
  165:       my $fn=
  166:        '/home/httpd/html/res/'.$cudom.'/'.$cuname.'/';
  167:       if ($ENV{'form.versiontwo'}) {
  168:          my ($main,$suffix)=($efn=~/^(.+)\.(\w+)$/);
  169:          $fn.=$main.'.'.$ENV{'form.versiontwo'}.'.'.$suffix;
  170: 	 $r->print('<b>'.&mt('Version').' '.$ENV{'form.versiontwo'}.'</b>');
  171:       } else {
  172:          $fn.=$efn;
  173: 	 $r->print('<b>'.&mt('Current Version').'</b>');
  174:       }
  175:       @f2=&get_split_file($fn,'remote');
  176:   }
  177:   $r->print('</font></p>');
  178: # Run diff
  179: 
  180:   my $diffs = diff(\@f1, \@f2);
  181: 
  182: # Start page output
  183: 
  184:   my $chunk;
  185:   my $line;
  186: 
  187:   $r->print('<pre>');
  188: 
  189:   foreach $chunk (@$diffs) {
  190: 
  191:     foreach $line (@$chunk) {
  192:       my ($sign, $lineno, $text) = @$line;
  193:       $text=~s/\</\&lt\;/g;
  194:       $text=~s/\>/\&gt\;/g;
  195:       $lineno=substr($lineno.'        ',0,7);
  196:       $r->print('<font color='.(($sign eq '+')?'green':'red').'>'.
  197:                 $sign.' '.$lineno.' '.$text."</font>\n");
  198:     }
  199:     $r->print("<hr>\n");
  200:   }
  201:   $r->print('</pre>');
  202: 
  203: } else {
  204:     $r->print('<h1><font color=red>'.&mt('Binary File').'</font></h1>');
  205: }
  206:   $r->print('<center><a href="javascript:window.close();">'.&mt('Close This Window').'</a></center></body></html>'); 
  207:   return OK;  
  208: }
  209: 
  210: 
  211: 1;
  212: __END__
  213: 
  214: 
  215: 
  216: 

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