File:  [LON-CAPA] / loncom / publisher / londiff.pm
Revision 1.7: download - view: text, annotated - select for diffs
Thu Dec 13 14:02:54 2001 UTC (22 years, 6 months ago) by harris41
Branches: MAIN
CVS tags: stable_2002_spring, stable_2002_april, HEAD
adding use for loncommon and lonnet; using loncommon's fileembstyle; removing
void context map

    1: # The LearningOnline Network with CAPA
    2: # Handler to show differences between file versions
    3: #
    4: # $Id: londiff.pm,v 1.7 2001/12/13 14:02:54 harris41 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: # 12/13 Scott Harrison
   45: #
   46: ###
   47: 
   48: package Apache::londiff;
   49: 
   50: use strict;
   51: use Apache::File;
   52: use File::Copy;
   53: use Algorithm::Diff qw(diff);
   54: use Apache::Constants qw(:common :http :methods);
   55: use Apache::loncacc;
   56: use Apache::lonnet();
   57: use Apache::loncommon();
   58: 
   59: sub handler {
   60: 
   61:   my $r=shift;
   62: 
   63: # Get query string for limited number of parameters
   64: 
   65:     foreach (split(/&/,$ENV{'QUERY_STRING'})) {
   66:        my ($name, $value) = split(/=/,$_);
   67:        $value =~ tr/+/ /;
   68:        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
   69:        if (($name eq 'filename') || ($name eq 'versiontwo') || 
   70:            ($name eq 'versionone')) {
   71:            unless ($ENV{'form.'.$name}) {
   72:               $ENV{'form.'.$name}=$value;
   73: 	   }
   74:        }
   75:     }
   76: 
   77: # Get the files
   78: 
   79:   my $cuname=$ENV{'user.name'};
   80:   my $cudom=$ENV{'user.domain'};
   81: 
   82:   unless (($cuname,$cudom)=
   83:     &Apache::loncacc::constructaccess($ENV{'form.filename'},
   84:                                       $r->dir_config('lonDefDomain'))) {
   85:      $r->log_reason($cuname.' at '.$cudom.
   86:          ' trying to get diffs file '.$ENV{'form.filename'}.
   87:          '  - not authorized', 
   88:          $r->filename); 
   89:      return HTTP_NOT_ACCEPTABLE;
   90:   }
   91: 
   92:   my $efn=$ENV{'form.filename'};
   93: 
   94:   $efn=~s/\/\~(\w+)//g;
   95: 
   96:   my @f1=();
   97:   my @f2=();
   98: 
   99:   $r->content_type('text/html');
  100:   $r->send_http_header;
  101: 
  102:   $r->print('<html><head><title>LON-CAPA Construction Diffs</title></head>');
  103: 
  104:   $r->print('<body bgcolor="#FFFFFF">');
  105: 
  106:   
  107:   $r->print('<h1>Compare versions of <tt>'.$efn.'</tt></h1>');
  108:    
  109:        if (($cuname ne $ENV{'user.name'}) || ($cudom ne $ENV{'user.domain'})) {
  110:           $r->print('<h3><font color=red>Co-Author: '.$cuname.' at '.$cudom.
  111:                '</font></h3>');
  112:       }
  113: 
  114: 
  115:  if (&Apache::loncommon::fileembstyle(($efn=~/\.(\w+)$/)) eq
  116:       'ssi') {
  117:   if ($ENV{'form.versionone'} eq 'priv') {
  118:       my $fn='/home/'.$cuname.'/public_html/'.$efn;
  119:       if (-e $fn) {
  120: 	  my $fh=Apache::File->new($fn);
  121:           my $line;
  122:           while($line=<$fh>) {
  123:              chomp($line);
  124:              $f1[$#f1+1]=$line;
  125: 	 }
  126:       }
  127:       $r->print('<h3>Construction Space Version</h3>');
  128:   } else {
  129:       my $fn=
  130:        '/home/httpd/html//res/'.$cudom.'/'.$cuname.'/';
  131:       if ($ENV{'form.versionone'}) {
  132:          my ($main,$suffix)=($efn=~/^(.+)\.(\w+)$/);
  133:          $fn.=$main.'.'.$ENV{'form.versionone'}.'.'.$suffix;
  134: 	 $r->print('<h3>Version '.$ENV{'form.versionone'}.'</h3>');
  135:       } else {
  136:          $fn.=$efn;
  137: 	 $r->print('<h3>Current Version</h3>');
  138:       }
  139:       @f1=split(/\n/,&Apache::lonnet::getfile($fn));      
  140:   }
  141: 
  142:   $r->print('versus');
  143: 
  144:   if ($ENV{'form.versiontwo'} eq 'priv') {
  145:       my $fn='/home/'.$cuname.'/public_html/'.$efn;
  146:       if (-e $fn) {
  147: 	  my $fh=Apache::File->new($fn);
  148:           my $line;
  149:           while($line=<$fh>) {
  150:              chomp($line);
  151:              $f2[$#f2+1]=$line;
  152: 	 }
  153:       }
  154:       $r->print('<h3>Construction Space Version</h3>');
  155:   } else {
  156:       my $fn=
  157:        '/home/httpd/html/res/'.$cudom.'/'.$cuname.'/';
  158:       if ($ENV{'form.versiontwo'}) {
  159:          my ($main,$suffix)=($efn=~/^(.+)\.(\w+)$/);
  160:          $fn.=$main.'.'.$ENV{'form.versiontwo'}.'.'.$suffix;
  161: 	 $r->print('<h3>Version '.$ENV{'form.versiontwo'}.'</h3>');
  162:       } else {
  163:          $fn.=$efn;
  164: 	 $r->print('<h3>Current Version</h3>');
  165:       }
  166:       @f2=split(/\n/,&Apache::lonnet::getfile($fn));      
  167:   }
  168: 
  169: # Run diff
  170: 
  171:   my $diffs = diff(\@f1, \@f2);
  172: 
  173: # Start page output
  174: 
  175:   my $chunk;
  176:   my $line;
  177: 
  178:   $r->print('<pre>');
  179: 
  180:   foreach $chunk (@$diffs) {
  181: 
  182:     foreach $line (@$chunk) {
  183:       my ($sign, $lineno, $text) = @$line;
  184:       $text=~s/\</\&lt\;/g;
  185:       $text=~s/\>/\&gt\;/g;
  186:       $lineno=substr($lineno.'        ',0,7);
  187:       $r->print('<font color='.(($sign eq '+')?'green':'red').'>'.
  188:                 $sign.' '.$lineno.' '.$text."</font>\n");
  189:     }
  190:     $r->print("<hr>\n");
  191:   }
  192:   $r->print('</pre>');
  193: 
  194: } else {
  195:     $r->print('<h1><font color=red>Binary File</font></h1>');
  196: }
  197:   $r->print('</body></html>');
  198:   return OK;  
  199: }
  200: 
  201: 
  202: 1;
  203: __END__
  204: 
  205: 
  206: 
  207: 

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