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

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

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