File:  [LON-CAPA] / loncom / publisher / londiff.pm
Revision 1.37: download - view: text, annotated - select for diffs
Tue Jun 4 22:20:16 2013 UTC (10 years, 11 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_11_1, version_2_11_0_RC3, version_2_11_0_RC2, version_2_11_0_RC1, version_2_11_0, HEAD
- Replace term: "Construction Space" with "Authoring Space" for
  consistency with type of role used to access it, and action taken there.

    1: # The LearningOnline Network with CAPA
    2: # Handler to show differences between file versions
    3: #
    4: # $Id: londiff.pm,v 1.37 2013/06/04 22:20:16 raeburn 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: 
   31: 
   32: 
   33: package Apache::londiff;
   34: 
   35: use strict;
   36: use Apache::File;
   37: use File::Copy;
   38: use File::Compare;
   39: use Algorithm::Diff qw(diff);
   40: use Apache::Constants qw(:common :http :methods);
   41: use Apache::lonnet;
   42: use Apache::loncommon();
   43: use Apache::lonretrieve();
   44: use Apache::lonlocal;
   45: use LONCAPA();
   46: 
   47: sub get_split_file {
   48:     my ($fn,$style)=@_;
   49:     my $f1;
   50:     my @f1;
   51:     if ($style eq 'local') {
   52: 	if (-e $fn) {
   53: 	    my $fh=Apache::File->new($fn);
   54: 	    my $line;	
   55: 	    while($line=<$fh>) {
   56: 		$f1.=$line;
   57: 	    }
   58: 	}
   59:     } elsif ($style eq 'remote') {
   60: 	$f1=&Apache::lonnet::getfile($fn);
   61:     }
   62:     @f1=split(/\r\n|\r|\n/,$f1);
   63:     return @f1;
   64: }
   65: 
   66: sub are_different_files {
   67:     my ($fileone,$filetwo)=@_;
   68:     return &compare($fileone,$filetwo);
   69: }
   70: 
   71: sub handler {
   72: 
   73:     my $r=shift;
   74: # Get query string for limited number of parameters
   75: 
   76:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
   77: 					    ['filename','versiontwo',
   78: 					     'versionone','filetwo']);
   79: 
   80: # Check permissions
   81:     my $allowed=0;
   82:     my $cuname=$env{'user.name'};
   83:     my $cudom=$env{'user.domain'};
   84: 
   85:     if ($env{'form.filename'}=~ m{^/res/}) {
   86:         if (&Apache::lonnet::allowed('bre',$env{'form.filename'})) {
   87:             if ($env{'request.course.id'}) {
   88:                 if (&Apache::lonnet::allowed('mdc',$env{'request.course.id'})) {
   89:                     $allowed = 1;
   90:                 }
   91:             } else {
   92:                 $allowed = 1;
   93:             }
   94:         } elsif (&Apache::lonnet::allowed('bro',$env{'form.filename'})) {
   95:             $allowed = 1;
   96:         }
   97:         if ($allowed) {
   98:             ($cudom,$cuname,$env{'form.filename'})=
   99:                 ($env{'form.filename'}=~m{^/res/($LONCAPA::domain_re)/($LONCAPA::username_re)(/.*)$});
  100: 
  101:             if (($env{'form.versionone'} eq 'priv') || ($env{'form.versiontwo'} eq 'priv')) {
  102:                 my ($cstrname,$cstrdom) =
  103:                     &Apache::lonnet::constructaccess("/priv/$cudom/$cuname".$env{'form.filename'});
  104:                 unless (($cstrname eq $cuname) && ($cstrdom eq $cudom)) {
  105:                     $allowed = 0;
  106:                 }
  107:             }
  108:         }
  109:     } else {
  110: 	($cuname,$cudom)=
  111: 	    &Apache::lonnet::constructaccess($env{'form.filename'});
  112:         if ($cuname ne '' && $cudom ne '') {
  113:             $allowed = 1;
  114:         } else {
  115: 	    $r->log_reason($env{'user.name'}.':'.$env{'user.domain'}.
  116: 			   ' trying to get diffs file '.$env{'form.filename'}.
  117: 			   '  - not authorized',
  118: 			   $r->filename);
  119: 	}
  120:     }
  121:     unless ($allowed) {
  122:         return HTTP_NOT_ACCEPTABLE;
  123:     }
  124: 
  125: # Get the files
  126: 
  127:     my $efn=$env{'form.filename'};
  128:     $efn=~s{^/priv/$LONCAPA::domain_re/$LONCAPA::username_re}{};
  129: 
  130:     my @f1=();
  131:     my @f2=();
  132: 
  133:     &Apache::loncommon::content_type($r,'text/html');
  134:     $r->send_http_header;
  135: 
  136:     $r->print(&Apache::loncommon::start_page('Resource Differences',undef,
  137:                                              {'no_nav_bar'  => 1, }));
  138:   
  139:     $r->print(($env{'form.filetwo'}?'':&mt('Compare versions of')).
  140: 	      ' <span class="LC_filename">'.$efn.'</span>');
  141:    
  142:     if (($cuname ne $env{'user.name'}) || ($cudom ne $env{'user.domain'})) {
  143:         my $nameshown = &Apache::loncommon::plainname($cuname,$cudom).
  144:                         ' ('.$cuname.':'.$cudom.')';
  145:         $r->print('<p><span class="LC_info">');
  146:         if ($env{'request.role'} =~ /^ca\./) {
  147:             $r->print(&mt('Co-Author in Authoring Space for: [_1]',$nameshown));
  148:         } elsif ($env{'request.role'} =~ /^aa\./) {
  149:             $r->print(&mt('Assistant Author in Authoring Space for: [_1]',$nameshown));
  150:         } else {
  151:             $r->print(&mt('Resource Author is: [_1]',$nameshown));
  152:         }
  153:         $r->print('</span></p>');
  154:     }
  155: 
  156:     if (&Apache::loncommon::fileembstyle(($efn=~/\.(\w+)$/)) eq 'ssi'
  157: 	|| $efn =~ /\.meta$/) {
  158: 	$r->print('<p><span class="LC_diff_removed">');
  159: 	if ($env{'form.versionone'} eq 'priv') {
  160: 	    my $fn=$r->dir_config('lonDocRoot')."/priv/$cudom/$cuname".$efn;
  161: 	    @f1=&get_split_file($fn,'local');
  162: 	    $r->print('<b>'.&mt('Authoring Space Version').'</b>');
  163: 	} else {
  164: 	    my $fn=$r->dir_config('lonDocRoot')."/res/$cudom/$cuname";
  165: 	    if ($env{'form.versionone'}) {
  166: 		my ($main,$suffix,$is_meta)=
  167: 		    &Apache::lonretrieve::get_file_info($efn);
  168: 		
  169: 		$fn.=($efn =~m|(.*/)[^/]+|)[0];
  170: 		# add on to $fn the path information in $efn
  171: 		$fn.=$main.'.'.$env{'form.versionone'}.'.'.$suffix;
  172: 		$r->print('<b>'.&mt('Version').' '.$env{'form.versionone'}.'</b>');
  173: 	    } else {
  174: 		$fn.=$efn;
  175: 		$r->print('<b>'.&mt('Current Version').'</b>');
  176: 	    }
  177: 	    @f1=&get_split_file($fn,'remote');
  178: 	}
  179: 	
  180: 	$r->print('</span><br />'.&mt('versus').'<br /><span class="LC_diff_added">');
  181: 
  182: 	if ($env{'form.filetwo'}) {
  183: 	    my $efn2=$env{'form.filetwo'};
  184: 	    $efn2=~s{^/priv/$LONCAPA::domain_re/$LONCAPA::username_re}{};
  185: 	    my $fn=$r->dir_config('lonDocRoot')."/priv/$cudom/$cuname".$efn2;
  186: 	    @f2=&get_split_file($fn,'local');
  187: 	    $r->print('<tt>'.$efn2.'</tt>');
  188: 	} elsif ($env{'form.versiontwo'} eq 'priv') {
  189: 	    my $fn=$r->dir_config('lonDocRoot')."/priv/$cudom/$cuname".$efn;
  190: 	    @f2=&get_split_file($fn,'local');
  191: 	    $r->print('<b>'.&mt('Authoring Space Version').'</b>');
  192: 	} else {
  193: 	    my $fn=$r->dir_config('lonDocRoot')."/res/$cudom/$cuname/";
  194: 	    if ($env{'form.versiontwo'}) {
  195: 		my ($main,$suffix,$is_meta)=
  196: 		    &Apache::lonretrieve::get_file_info($efn);
  197: 		# add on to $fn the path information in $efn
  198: 		$fn.=($efn =~m|(.*/)[^/]+|)[0];
  199: 		$fn.=$main.'.'.$env{'form.versiontwo'}.'.'.$suffix;
  200: 		$r->print('<b>'.&mt('Version').' '.$env{'form.versiontwo'}.'</b>');
  201: 	    } else {
  202: 		$fn.=$efn;
  203: 		$r->print('<b>'.&mt('Current Version').'</b>');
  204: 	    }
  205: 	    @f2=&get_split_file($fn,'remote');
  206: 	}
  207: 	$r->print('</span></p>');
  208: # Run diff
  209: 
  210: 	my $diffs = diff(\@f1, \@f2);
  211: 
  212:         if (@$diffs) {
  213:             # Start page output
  214:             my $chunk;
  215:             my $line;
  216:             $r->print('<pre>');
  217:             foreach $chunk (@$diffs) {
  218:                 foreach $line (@$chunk) {
  219:                     my ($sign, $lineno, $text) = @$line;
  220:                     $text=&HTML::Entities::encode($text,'<>&"');
  221:                     $lineno=substr($lineno.'        ',0,7);
  222:                     $r->print('<span class="'.(($sign eq '+')?'LC_diff_added'
  223:                                                              :'LC_diff_removed').'">'.
  224:                               $sign.' '.$lineno.' '.$text."</span>\n");
  225:                 }
  226:                 $r->print("</pre><hr /><pre>\n");
  227:             }
  228:             $r->print('</pre>');
  229:         } else {
  230:             $r->print('<p class="LC_info">'.&mt('No differences found').'</p>');
  231:         }
  232:     } else {
  233: 	$r->print('<h1><span class="LC_warning">'.&mt('Binary File').'</span></h1>');
  234:     }
  235:     $r->print(&Apache::loncommon::end_page()); 
  236:     return OK;  
  237: }
  238: 
  239: 
  240: 1;
  241: __END__
  242: 
  243: 
  244: =pod
  245: 
  246: =head1 NAME
  247: 
  248: Apache::londiff
  249: 
  250: =head1 SYNOPSIS
  251: 
  252: Handler to show difference between two files.
  253: 
  254: This is part of the LearningOnline Network with CAPA project
  255: described at http://www.lon-capa.org.
  256: 
  257: =head1 Subroutines
  258: 
  259: =over
  260: 
  261: =item get_split_file()
  262: 
  263: =item are_different_files()
  264: 
  265: =item handler()
  266: 
  267: =cut

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