fileViewer.cgi #4

  • //
  • guest/
  • fredric_fredricson/
  • P4DB/
  • main/
  • fileViewer.cgi
  • View
  • Commits
  • Open Download .zip Download (5 KB)
#!/usr/bin/perl -w
# -*- perl -*-
use P4CGI ;
use strict ;
#
#################################################################
#  CONFIGURATION INFORMATION 
#  All config info should be in P4CGI.pm
#
#################################################################
#
#  P4 file  viewer
#  View a file
#
#################################################################

use viewConfig ;
use colorView ;

# A hash containing file extensions that can be viewed with special viewers
# Data is:
#             <extension> => <semicolon separated list of:
#                             url      - Url to be used
#                             typecode - Will be sent as parameter TYPE to url
#                             text     - A text for the href to url>
# Other than the TYPE parameter mentioned above the file name (depot format) will
# be sent as FILE parameter and file revision as REV parameter.
#

# Get file spec argument

my $file = P4CGI::cgi()->param("FSPC") ;
&P4CGI::bail("No file specified") unless defined $file ;

print STDERR "File:$file\n" ; ## DEBUG

my $ext = $file ;
$ext =~ s/^.*\.// ;

my $revision = P4CGI::cgi()->param("REV") ;
#    &P4CGI::bail("No revision specified") unless defined $revision ;
$revision = "#$revision" if defined $revision ;
$revision="" unless defined $revision ;

my $force = P4CGI::cgi()->param("FORCE") ;

print STDERR "Rev:$revision\n" ; ## DEBUG

				# find out if p4br.perl is available, if true set smart
local *P4 ;
my $smart;
my ( $name, $rev, $type ) ;
if(-x "p4pr.perl") {
    open(P4,"./p4pr.perl \"$file$revision\" |") or 
	&P4CGI::bail("Can't start p4pr!!!!. too bad!") ;
    # Get header line
    #  line   author/branch change  rev //main/jam/Jamfile#39 - edit change 1749 (text)
    $_ = <P4>;	
    if(defined $_ and (/^\s+\S+\s+\S+\s+\S+\s+\S+\s+(\S+)\#(\d+) - \S+ \S+ \S+ \((.+)\)/)) {
	( $name, $rev, $type ) = ($1,$2,$3) ;
	$_ = <P4>;
	$smart="Yes";
    }
    else {
	close P4 ;
    }
}
if(!defined $smart) {
    &P4CGI::p4call( *P4, "print \"$file$revision\"" );
    $smart="No, stupid." ;
    # Get header line
    # //main/jam/Jamfile#39 - edit change 1749 (text)
    $_ = <P4>;
    if(defined $_ and (/^(.+)\#(\d+) - \S+ \S+ \S+ \((\w+)\)/)) {	
	( $name, $rev, $type ) = ($1,$2,$3) ;
    } ;
}

my @legend = "" ;

$ext = uc($ext) ;
if(exists $viewConfig::ExtensionToType{$ext}) {
    my $type = $viewConfig::ExtensionToType{$ext} ;
    my ($url,$desc,$content,$about) = @{$viewConfig::TypeData{$type}} ;    
    push @legend, &P4CGI::buttonCell($url,
				     "View file $file",
				     "TYPE=$type",
				     "FSPC=$file",
				     "REV=$rev",
				     "View $desc") ;
} ;
push @legend, &P4CGI::buttonCell("fileLogView.cgi",
				 "View file log for $file",
				 "FSPC=$file",
				 "File log");

push @legend, &P4CGI::buttonCell("fileDownLoad.cgi",
				 "Download file $file",
				 "FSPC=$file",
				 "REV=$rev",
				 "Download") ;

print STDERR "About to start\n" ; ## DEBUG

print 
    &P4CGI::start_page("View&nbsp;file <tt>$file\#$rev</tt>",@legend),
    "<br>",
    &P4CGI::start_framedTable("File") ;

print STDERR "Started\n" ; ## DEBUG

if(!defined $force and ($type =~ /.*binary/))
{
    print
	"<h2>Type is $type.</h2>\n",
	&P4CGI::buttonHMenuTable(&P4CGI::buttonCell(&P4CGI::cgi()->url,
						    "Force view of file $file",
						    "FSPC=$file",
						    "REV=$rev",
						    "FORCE=Y",
						    "View anyway!")) ;
    
}
else
{
    print "Type: $type<br>\n<pre>\n";

    print STDERR "Type: $type<br>\n<pre>\n"; ##DEBUG
    my @prompts  ;
    my @filetext ;

    if(!defined $force and $smart eq "Yes"){
	my ($lineno,$authorBranch,$change,$rev,$line) ;
	print "<small>Line<tt>   </tt>Ch.  Rev</small>\n";
	my $oldch=-1;
	while( <P4> ) {
	    ($lineno,$authorBranch,$change,$rev,$line) =
		m/^\s+(\d+)\s+(\S+)\s+(\d+)\s+(\d+) (.*)$/ ;		
	    my $linenos = sprintf("<a name=\"L%d\"></A><tt>%3d:</tt>",$lineno,$lineno) ;	   
	    my($chstr,$revstr)=("     ","   ");
	    if($oldch != $change){
		$chstr=
		    substr("     ",0,5-length("$change")) . 
		    &P4CGI::ahref(-url => "changeView.cgi",
				  -title => "\"View change $change\"",
				  "CH=$change",
				  "$change") ;
		$revstr = 
		    substr("     ",0,3-length("$rev")) .
		    &P4CGI::ahref("-url","fileDiffView.cgi",
				  -title => "\"View diff for change\"",
				  "FSPC=$name",
				  "REV=$rev","ACT=edit",
				  "$rev");
	    }		    
	    $oldch= $change ;
	    if(($lineno % 5) != 0) {
		while($linenos =~ s/>( *)\d/>$1 /) {} ;
		$linenos =~ s/:<\/tt>/ <\/tt>/ ;
	    } ;
	    push @filetext,&P4CGI::htmlEncode($line) ;
	    push @prompts,"<small>$linenos $chstr$revstr </small><font color=red>|</font>" ;
	}	    
    }
    else {
	while( <P4> ) {
	    chomp ;
	    push @filetext,&P4CGI::htmlEncode($_) ;
	    push @prompts,"" ;
	}	    
    }
    my $FILE = join("\n",@filetext) ;
    if(&P4CGI::VIEW_WITH_COLORS()) {
	&colorView::color($file,\$FILE) ;
	@filetext = split("\n",$FILE) unless $@ ;
    } ;
	
    while(@filetext) {
	print (shift @prompts,shift @filetext,"\n") ;
    }
    print "</pre>\n";
}
close P4;

print
    &P4CGI::end_framedTable(),
    &P4CGI::end_page() ;

#
# That's all folks
#
# Change User Description Committed
#11 4916 Fredric Fredricson P4DB: Improved file viewer (and simplified the code)
#10 4306 Fredric Fredricson P4DB: Hardened P4DB against malicious parameters (cross site scripting),
performed some cleanup and increased version to 3.1.1.
#9 4237 Fredric Fredricson P4DB: Maybe the final submit for P4DB 3.1.0
#8 4216 Fredric Fredricson P4DB: Another partial submit on my way to P4DB 3.1...
#7 4176 Fredric Fredricson P4DB: Still working my way to P4DB 3.1...
#6 4152 Fredric Fredricson P4DB: Some more work on tha way to version 3.1....
#5 4069 Fredric Fredricson P4DB: More changes on the way to 3.1
#4 4048 Fredric Fredricson P4DB: Updated for Explorer.
* Updated Style Sheet to work for Explorer as well as Netscape
* Improved alternate header
* Some other small fixes....
#3 2875 Fredric Fredricson P4DB 3.0 first beta...
#2 1920 Fredric Fredricson P4DB: Mainly some user interface fixes:
* Added a small arrow that points to selection in list of options
* Added tooltip help
* Added user prefereces to turn the above off (or on)
* Some other user interface fixes
And fixed a bug in jobList.cgi and some minor bugs in label and branch
viewers.
#1 1638 Fredric Fredricson P4DB: Added all (I think) files for P4DB