Annotation of loncom/publisher/londiff.pm, revision 1.18

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

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