File:  [LON-CAPA] / loncom / publisher / londiff.pm
Revision 1.38: download - view: text, annotated - select for diffs
Sun May 7 13:26:40 2017 UTC (7 years ago) by raeburn
Branches: MAIN
CVS tags: version_2_12_X, version_2_11_X, version_2_11_4_uiuc, version_2_11_4_msu, version_2_11_4, version_2_11_3_uiuc, version_2_11_3_msu, version_2_11_3, version_2_11_2_uiuc, version_2_11_2_msu, version_2_11_2_educog, version_2_11_2, HEAD
- Diffs of resources in a course visible to users with view-only access
  to Course Editor.

    1: # The LearningOnline Network with CAPA
    2: # Handler to show differences between file versions
    3: #
    4: # $Id: londiff.pm,v 1.38 2017/05/07 13:26:40 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:                     (&Apache::lonnet::allowed('cev',$env{'request.course.id'}))) {
   90:                     $allowed = 1;
   91:                 }
   92:             } else {
   93:                 $allowed = 1;
   94:             }
   95:         } elsif (&Apache::lonnet::allowed('bro',$env{'form.filename'})) {
   96:             $allowed = 1;
   97:         }
   98:         if ($allowed) {
   99:             ($cudom,$cuname,$env{'form.filename'})=
  100:                 ($env{'form.filename'}=~m{^/res/($LONCAPA::domain_re)/($LONCAPA::username_re)(/.*)$});
  101: 
  102:             if (($env{'form.versionone'} eq 'priv') || ($env{'form.versiontwo'} eq 'priv')) {
  103:                 my ($cstrname,$cstrdom) =
  104:                     &Apache::lonnet::constructaccess("/priv/$cudom/$cuname".$env{'form.filename'});
  105:                 unless (($cstrname eq $cuname) && ($cstrdom eq $cudom)) {
  106:                     $allowed = 0;
  107:                 }
  108:             }
  109:         }
  110:     } else {
  111: 	($cuname,$cudom)=
  112: 	    &Apache::lonnet::constructaccess($env{'form.filename'});
  113:         if ($cuname ne '' && $cudom ne '') {
  114:             $allowed = 1;
  115:         } else {
  116: 	    $r->log_reason($env{'user.name'}.':'.$env{'user.domain'}.
  117: 			   ' trying to get diffs file '.$env{'form.filename'}.
  118: 			   '  - not authorized',
  119: 			   $r->filename);
  120: 	}
  121:     }
  122:     unless ($allowed) {
  123:         return HTTP_NOT_ACCEPTABLE;
  124:     }
  125: 
  126: # Get the files
  127: 
  128:     my $efn=$env{'form.filename'};
  129:     $efn=~s{^/priv/$LONCAPA::domain_re/$LONCAPA::username_re}{};
  130: 
  131:     my @f1=();
  132:     my @f2=();
  133: 
  134:     &Apache::loncommon::content_type($r,'text/html');
  135:     $r->send_http_header;
  136: 
  137:     $r->print(&Apache::loncommon::start_page('Resource Differences',undef,
  138:                                              {'no_nav_bar'  => 1, }));
  139:   
  140:     $r->print(($env{'form.filetwo'}?'':&mt('Compare versions of')).
  141: 	      ' <span class="LC_filename">'.$efn.'</span>');
  142:    
  143:     if (($cuname ne $env{'user.name'}) || ($cudom ne $env{'user.domain'})) {
  144:         my $nameshown = &Apache::loncommon::plainname($cuname,$cudom).
  145:                         ' ('.$cuname.':'.$cudom.')';
  146:         $r->print('<p><span class="LC_info">');
  147:         if ($env{'request.role'} =~ /^ca\./) {
  148:             $r->print(&mt('Co-Author in Authoring Space for: [_1]',$nameshown));
  149:         } elsif ($env{'request.role'} =~ /^aa\./) {
  150:             $r->print(&mt('Assistant Author in Authoring Space for: [_1]',$nameshown));
  151:         } else {
  152:             $r->print(&mt('Resource Author is: [_1]',$nameshown));
  153:         }
  154:         $r->print('</span></p>');
  155:     }
  156: 
  157:     if (&Apache::loncommon::fileembstyle(($efn=~/\.(\w+)$/)) eq 'ssi'
  158: 	|| $efn =~ /\.meta$/) {
  159: 	$r->print('<p><span class="LC_diff_removed">');
  160: 	if ($env{'form.versionone'} eq 'priv') {
  161: 	    my $fn=$r->dir_config('lonDocRoot')."/priv/$cudom/$cuname".$efn;
  162: 	    @f1=&get_split_file($fn,'local');
  163: 	    $r->print('<b>'.&mt('Authoring Space Version').'</b>');
  164: 	} else {
  165: 	    my $fn=$r->dir_config('lonDocRoot')."/res/$cudom/$cuname";
  166: 	    if ($env{'form.versionone'}) {
  167: 		my ($main,$suffix,$is_meta)=
  168: 		    &Apache::lonretrieve::get_file_info($efn);
  169: 		
  170: 		$fn.=($efn =~m|(.*/)[^/]+|)[0];
  171: 		# add on to $fn the path information in $efn
  172: 		$fn.=$main.'.'.$env{'form.versionone'}.'.'.$suffix;
  173: 		$r->print('<b>'.&mt('Version').' '.$env{'form.versionone'}.'</b>');
  174: 	    } else {
  175: 		$fn.=$efn;
  176: 		$r->print('<b>'.&mt('Current Version').'</b>');
  177: 	    }
  178: 	    @f1=&get_split_file($fn,'remote');
  179: 	}
  180: 	
  181: 	$r->print('</span><br />'.&mt('versus').'<br /><span class="LC_diff_added">');
  182: 
  183: 	if ($env{'form.filetwo'}) {
  184: 	    my $efn2=$env{'form.filetwo'};
  185: 	    $efn2=~s{^/priv/$LONCAPA::domain_re/$LONCAPA::username_re}{};
  186: 	    my $fn=$r->dir_config('lonDocRoot')."/priv/$cudom/$cuname".$efn2;
  187: 	    @f2=&get_split_file($fn,'local');
  188: 	    $r->print('<tt>'.$efn2.'</tt>');
  189: 	} elsif ($env{'form.versiontwo'} eq 'priv') {
  190: 	    my $fn=$r->dir_config('lonDocRoot')."/priv/$cudom/$cuname".$efn;
  191: 	    @f2=&get_split_file($fn,'local');
  192: 	    $r->print('<b>'.&mt('Authoring Space Version').'</b>');
  193: 	} else {
  194: 	    my $fn=$r->dir_config('lonDocRoot')."/res/$cudom/$cuname/";
  195: 	    if ($env{'form.versiontwo'}) {
  196: 		my ($main,$suffix,$is_meta)=
  197: 		    &Apache::lonretrieve::get_file_info($efn);
  198: 		# add on to $fn the path information in $efn
  199: 		$fn.=($efn =~m|(.*/)[^/]+|)[0];
  200: 		$fn.=$main.'.'.$env{'form.versiontwo'}.'.'.$suffix;
  201: 		$r->print('<b>'.&mt('Version').' '.$env{'form.versiontwo'}.'</b>');
  202: 	    } else {
  203: 		$fn.=$efn;
  204: 		$r->print('<b>'.&mt('Current Version').'</b>');
  205: 	    }
  206: 	    @f2=&get_split_file($fn,'remote');
  207: 	}
  208: 	$r->print('</span></p>');
  209: # Run diff
  210: 
  211: 	my $diffs = diff(\@f1, \@f2);
  212: 
  213:         if (@$diffs) {
  214:             # Start page output
  215:             my $chunk;
  216:             my $line;
  217:             $r->print('<pre>');
  218:             foreach $chunk (@$diffs) {
  219:                 foreach $line (@$chunk) {
  220:                     my ($sign, $lineno, $text) = @$line;
  221:                     $text=&HTML::Entities::encode($text,'<>&"');
  222:                     $lineno=substr($lineno.'        ',0,7);
  223:                     $r->print('<span class="'.(($sign eq '+')?'LC_diff_added'
  224:                                                              :'LC_diff_removed').'">'.
  225:                               $sign.' '.$lineno.' '.$text."</span>\n");
  226:                 }
  227:                 $r->print("</pre><hr /><pre>\n");
  228:             }
  229:             $r->print('</pre>');
  230:         } else {
  231:             $r->print('<p class="LC_info">'.&mt('No differences found').'</p>');
  232:         }
  233:     } else {
  234: 	$r->print('<h1><span class="LC_warning">'.&mt('Binary File').'</span></h1>');
  235:     }
  236:     $r->print(&Apache::loncommon::end_page()); 
  237:     return OK;  
  238: }
  239: 
  240: 
  241: 1;
  242: __END__
  243: 
  244: 
  245: =pod
  246: 
  247: =head1 NAME
  248: 
  249: Apache::londiff
  250: 
  251: =head1 SYNOPSIS
  252: 
  253: Handler to show difference between two files.
  254: 
  255: This is part of the LearningOnline Network with CAPA project
  256: described at http://www.lon-capa.org.
  257: 
  258: =head1 Subroutines
  259: 
  260: =over
  261: 
  262: =item get_split_file()
  263: 
  264: =item are_different_files()
  265: 
  266: =item handler()
  267: 
  268: =cut

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