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

1.1       www         1: # The LearningOnline Network with CAPA
                      2: # Handler to show differences between file versions
                      3: #
1.15    ! albertel    4: # $Id: londiff.pm,v 1.14 2003/12/10 16:05:43 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: #
                     28: #
1.1       www        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: #
1.5       www        43: # 05/02/01,05/09 Gerd Kortemeyer
1.7       harris41   44: #
                     45: ###
1.1       www        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);
1.5       www        54: use Apache::loncacc;
1.7       harris41   55: use Apache::lonnet();
                     56: use Apache::loncommon();
1.13      sakharuk   57: use Apache::lonlocal;
1.1       www        58: 
1.15    ! albertel   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: 
1.1       www        86: sub handler {
                     87: 
                     88:   my $r=shift;
                     89: 
                     90: # Get query string for limited number of parameters
                     91: 
1.8       stredwic   92:    &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                     93:                                        ['filename','versiontwo','versionone']);
1.1       www        94: # Get the files
                     95: 
1.5       www        96:   my $cuname=$ENV{'user.name'};
                     97:   my $cudom=$ENV{'user.domain'};
                     98: 
1.10      www        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)=
1.5       www       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;
1.10      www       111:    }
1.5       www       112:   }
                    113: 
                    114:   my $efn=$ENV{'form.filename'};
                    115: 
                    116:   $efn=~s/\/\~(\w+)//g;
                    117: 
1.1       www       118:   my @f1=();
                    119:   my @f2=();
                    120: 
1.14      www       121:   &Apache::loncommon::content_type($r,'text/html');
1.2       www       122:   $r->send_http_header;
                    123: 
                    124:   $r->print('<html><head><title>LON-CAPA Construction Diffs</title></head>');
                    125: 
1.14      www       126:   $r->print(&Apache::loncommon::bodytag('Resource Differences'));
1.2       www       127: 
                    128:   
1.13      sakharuk  129:   $r->print('<h1>'.&mt('Compare versions of').' <tt>'.$efn.'</tt></h1>');
1.5       www       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: 
1.2       www       136: 
1.7       harris41  137:  if (&Apache::loncommon::fileembstyle(($efn=~/\.(\w+)$/)) eq
1.2       www       138:       'ssi') {
1.10      www       139:   $r->print('<p><font color="red">');
1.1       www       140:   if ($ENV{'form.versionone'} eq 'priv') {
1.5       www       141:       my $fn='/home/'.$cuname.'/public_html/'.$efn;
1.15    ! albertel  142:       @f1=&get_split_file($fn,'local');
1.13      sakharuk  143:       $r->print('<b>'.&mt('Construction Space Version').'</b>');
1.1       www       144:   } else {
                    145:       my $fn=
1.5       www       146:        '/home/httpd/html//res/'.$cudom.'/'.$cuname.'/';
1.1       www       147:       if ($ENV{'form.versionone'}) {
1.5       www       148:          my ($main,$suffix)=($efn=~/^(.+)\.(\w+)$/);
1.1       www       149:          $fn.=$main.'.'.$ENV{'form.versionone'}.'.'.$suffix;
1.13      sakharuk  150: 	 $r->print('<b>'.&mt('Version').' '.$ENV{'form.versionone'}.'</b>');
1.1       www       151:       } else {
1.5       www       152:          $fn.=$efn;
1.13      sakharuk  153: 	 $r->print('<b>'.&mt('Current Version').'</b>');
1.1       www       154:       }
1.15    ! albertel  155:       @f1=&get_split_file($fn,'remote');
1.1       www       156:   }
                    157: 
1.13      sakharuk  158:   $r->print('</font><br />'.&mt('versus').'<br /><font color="green">');
1.1       www       159: 
                    160:   if ($ENV{'form.versiontwo'} eq 'priv') {
1.5       www       161:       my $fn='/home/'.$cuname.'/public_html/'.$efn;
1.15    ! albertel  162:       @f2=&get_split_file($fn,'local');
1.13      sakharuk  163:       $r->print('<b>'.&mt('Construction Space Version').'</b>');
1.1       www       164:   } else {
                    165:       my $fn=
1.5       www       166:        '/home/httpd/html/res/'.$cudom.'/'.$cuname.'/';
1.1       www       167:       if ($ENV{'form.versiontwo'}) {
1.5       www       168:          my ($main,$suffix)=($efn=~/^(.+)\.(\w+)$/);
1.1       www       169:          $fn.=$main.'.'.$ENV{'form.versiontwo'}.'.'.$suffix;
1.13      sakharuk  170: 	 $r->print('<b>'.&mt('Version').' '.$ENV{'form.versiontwo'}.'</b>');
1.1       www       171:       } else {
1.5       www       172:          $fn.=$efn;
1.13      sakharuk  173: 	 $r->print('<b>'.&mt('Current Version').'</b>');
1.1       www       174:       }
1.15    ! albertel  175:       @f2=&get_split_file($fn,'remote');
1.1       www       176:   }
1.10      www       177:   $r->print('</font></p>');
1.1       www       178: # Run diff
                    179: 
                    180:   my $diffs = diff(\@f1, \@f2);
                    181: 
                    182: # Start page output
                    183: 
                    184:   my $chunk;
                    185:   my $line;
                    186: 
1.3       www       187:   $r->print('<pre>');
                    188: 
1.1       www       189:   foreach $chunk (@$diffs) {
1.3       www       190: 
1.1       www       191:     foreach $line (@$chunk) {
                    192:       my ($sign, $lineno, $text) = @$line;
                    193:       $text=~s/\</\&lt\;/g;
                    194:       $text=~s/\>/\&gt\;/g;
1.3       www       195:       $lineno=substr($lineno.'        ',0,7);
                    196:       $r->print('<font color='.(($sign eq '+')?'green':'red').'>'.
                    197:                 $sign.' '.$lineno.' '.$text."</font>\n");
1.1       www       198:     }
1.3       www       199:     $r->print("<hr>\n");
1.1       www       200:   }
1.3       www       201:   $r->print('</pre>');
1.1       www       202: 
1.2       www       203: } else {
1.13      sakharuk  204:     $r->print('<h1><font color=red>'.&mt('Binary File').'</font></h1>');
1.2       www       205: }
1.13      sakharuk  206:   $r->print('<center><a href="javascript:window.close();">'.&mt('Close This Window').'</a></center></body></html>'); 
1.1       www       207:   return OK;  
                    208: }
                    209: 
                    210: 
                    211: 1;
                    212: __END__
1.2       www       213: 
1.1       www       214: 
                    215: 
                    216: 

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