File:  [LON-CAPA] / loncom / publisher / londiff.pm
Revision 1.20: download - view: text, annotated - select for diffs
Wed Sep 13 21:43:26 2006 UTC (17 years, 8 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- BUG#5014, when modifying .meta files need to advertise that fact in the
     directoy browser
    - additionaly when doing full dir repbulishes, republish if the .meta file has changed
- also add support for retrieving a previoulsy published .meta file
- add support for getting diffs between previous .meta file revisions

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

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