]> git.donarmstrong.com Git - debbugs.git/blobdiff - cgi/common.pl
[project @ 2003-05-03 20:42:15 by doogie]
[debbugs.git] / cgi / common.pl
index eb3a62d1cd3e9cd0e07f6b1344d6628e74aa0348..8a40da82713022722239cfdaedf666e58a0fd42c 100644 (file)
@@ -23,8 +23,24 @@ sub set_option {
     my ($opt, $val) = @_;
     if ($opt eq "archive") { $common_archive = $val; }
     if ($opt eq "repeatmerged") { $common_repeatmerged = $val; }
-    if ($opt eq "exclude") { %common_exclude = %{$val}; }
-    if ($opt eq "include") { %common_include = %{$val}; }
+    if ($opt eq "exclude") {
+       my @vals;
+       @vals = ( $val ) if (ref($val) eq "" && $val );
+       @vals = ( $$val ) if (ref($val) eq "SCALAR" && $$val );
+       @vals = @{$val} if (ref($val) eq "ARRAY" );
+       %common_exclude = map {
+           if (/^(.*):(.*)$/) { ($1, $2) } else { ($_, 1) }
+       } split /[\s,]+/, join ',', @vals;
+    }
+    if ($opt eq "include") {
+       my @vals;
+       @vals = ( $val, ) if (ref($val) eq "" && $val );
+       @vals = ( $$val, ) if (ref($val) eq "SCALAR" && $$val );
+       @vals = @{$val} if (ref($val) eq "ARRAY" );
+       %common_include = map {
+           if (/^(.*):(.*)$/) { ($1, $2) } else { ($_, 1) }
+       } split /[\s,]+/, join ',', @vals;
+    }
     if ($opt eq "raw") { $common_raw_sort = $val; }
     if ($opt eq "bug-rev") { $common_bug_reverse = $val; }
     if ($opt eq "pend-rev") { $common_pending_reverse = $val; }
@@ -70,7 +86,7 @@ sub readparse {
     } else {
         return;
     }
-    foreach (split(/&/,$in)) {
+    foreach (split(/[&;]/,$in)) {
         s/\+/ /g;
         ($key, $val) = split(/=/,$_,2);
         $key=~s/%(..)/pack("c",hex($1))/ge;
@@ -87,7 +103,7 @@ $debug = 1 if (defined $ret{"debug"} && $ret{"debug"} eq "aj");
     return %ret;
 }
 
-sub quit {
+sub quitcgi {
     my $msg = shift;
     print "Content-Type: text/html\n\n";
     print "<HTML><HEAD><TITLE>Error</TITLE></HEAD><BODY>\n";
@@ -106,6 +122,34 @@ sub quit {
 #    exit 0;
 #}
 
+# Split a package string from the status file into a list of package names.
+sub splitpackages {
+    my $pkgs = shift;
+    return unless defined $pkgs;
+    return split /[ \t?,()]+/, $pkgs;
+}
+
+# Generate a comma-separated list of HTML links to each package given in
+# $pkgs. $pkgs may be empty, in which case an empty string is returned, or
+# it may be a comma-separated list of package names.
+sub htmlpackagelinks {
+    my $pkgs = shift;
+    return unless defined $pkgs and $pkgs ne '';
+    my $strong = shift;
+    my @pkglist = splitpackages($pkgs);
+
+    my $openstrong  = $strong ? '<strong>' : '';
+    my $closestrong = $strong ? '</strong>' : '';
+
+    return 'Package' . (@pkglist > 1 ? 's' : '') . ': ' .
+           join(', ',
+                map {
+                    '<a href="' . pkgurl($_) . '">' .
+                    $openstrong . htmlsanit($_) . $closestrong . '</a>'
+                } @pkglist
+           ) . ";\n";
+}
+
 sub htmlindexentry {
     my $ref = shift;
     my %status = %{getbugstatus($ref)};
@@ -127,9 +171,7 @@ sub htmlindexentrystatus {
         $showseverity = "Severity: <em>$status{severity}</em>;\n";
     }
 
-    $result .= "Package: <a href=\"" . pkgurl($status{"package"}) . "\">"
-               . "<strong>" . htmlsanit($status{"package"}) . "</strong></a>;\n"
-               if (length($status{"package"}));
+    $result .= htmlpackagelinks($status{"package"}, 1);
     $result .= $showseverity;
     $result .= "Reported by: <a href=\"" . submitterurl($status{originator})
                . "\">" . htmlsanit($status{originator}) . "</a>";
@@ -147,10 +189,11 @@ sub htmlindexentrystatus {
 
     if (length($status{done})) {
         $result .= ";\n<strong>Done:</strong> " . htmlsanit($status{done});
-    } elsif (length($status{forwarded})) {
-        $result .= ";\n<strong>Forwarded</strong> to "
-                   . htmlsanit($status{forwarded});
     } else {
+        if (length($status{forwarded})) {
+            $result .= ";\n<strong>Forwarded</strong> to "
+                       . maybelink($status{forwarded});
+        }
         my $daysold = int((time - $status{date}) / 86400);   # seconds to days
         if ($daysold >= 7) {
             my $font = "";
@@ -164,11 +207,13 @@ sub htmlindexentrystatus {
             $daysold = $daysold - $yearsold * 364;
 
             $result .= ";\n $font";
-            $result .= "1 year and " if ($yearsold == 1);
-            $result .= "$yearsold years and " if ($yearsold > 1);
-            $result .= "1 day old" if ($daysold == 1);
-            $result .= "$daysold days old" if ($daysold != 1);
-            $result .= "$efont";
+            my @age;
+            push @age, "1 year" if ($yearsold == 1);
+            push @age, "$yearsold years" if ($yearsold > 1);
+            push @age, "1 day" if ($daysold == 1);
+            push @age, "$daysold days" if ($daysold > 1);
+            $result .= join(" and ", @age);
+            $result .= " old$efont";
         }
     }
 
@@ -181,59 +226,58 @@ sub submitterurl {
     my $ref = shift || "";
     my $params = "submitter=" . emailfromrfc822($ref);
     $params .= "&archive=yes" if ($common_archive);
-    $params .= "&repeatmerged=yes" if ($common_repeatmerged);
-    return urlsanit($debbugs::gCGIDomain . "pkgreport.cgi" . "?" . $params);
+    $params .= "&repeatmerged=no" unless ($common_repeatmerged);
+    return urlsanit("pkgreport.cgi" . "?" . $params);
 }
 
 sub mainturl {
     my $ref = shift || "";
     my $params = "maint=" . emailfromrfc822($ref);
     $params .= "&archive=yes" if ($common_archive);
-    $params .= "&repeatmerged=yes" if ($common_repeatmerged);
-    return urlsanit($debbugs::gCGIDomain . "pkgreport.cgi" . "?" . $params);
+    $params .= "&repeatmerged=no" unless ($common_repeatmerged);
+    return urlsanit("pkgreport.cgi" . "?" . $params);
 }
 
 sub pkgurl {
     my $ref = shift;
     my $params = "pkg=$ref";
     $params .= "&archive=yes" if ($common_archive);
-    $params .= "&repeatmerged=yes" if ($common_repeatmerged);
+    $params .= "&repeatmerged=no" unless ($common_repeatmerged);
     
-    return urlsanit($debbugs::gCGIDomain . "pkgreport.cgi" . "?" . "$params");
+    return urlsanit("pkgreport.cgi" . "?" . "$params");
 }
 
 sub srcurl {
     my $ref = shift;
     my $params = "src=$ref";
     $params .= "&archive=yes" if ($common_archive);
-    $params .= "&repeatmerged=yes" if ($common_repeatmerged);
-    return urlsanit($debbugs::gCGIDomain . "pkgreport.cgi" . "?" . "$params");
+    $params .= "&repeatmerged=no" unless ($common_repeatmerged);
+    return urlsanit("pkgreport.cgi" . "?" . "$params");
 }
 
 sub urlsanit {
     my $url = shift;
     $url =~ s/%/%25/g;
     $url =~ s/\+/%2b/g;
-    my %saniarray = ('<','lt', '>','gt', '"','quot');
-    my $out;
-    while ($url =~ m/[<>"]/) {
-        $out .= $`. '&'. $saniarray{$&}. ';';
-        $url = $';
-    }
-    $out .= $url;
-    return $out;
+    my %saniarray = ('<','lt', '>','gt', '&','amp', '"','quot');
+    $url =~ s/([<>&"])/\&$saniarray{$1};/g;
+    return $url;
 }
 
 sub htmlsanit {
     my %saniarray = ('<','lt', '>','gt', '&','amp', '"','quot');
     my $in = shift || "";
-    my $out;
-    while ($in =~ m/[<>&"]/) {
-        $out .= $`. '&'. $saniarray{$&}. ';';
-        $in = $';
+    $in =~ s/([<>&"])/\&$saniarray{$1};/g;
+    return $in;
+}
+
+sub maybelink {
+    my $in = shift;
+    if ($in =~ /^[a-zA-Z0-9+.-]+:/) { # RFC 1738 scheme
+       return qq{<a href="$in">} . htmlsanit($in) . '</a>';
+    } else {
+       return htmlsanit($in);
     }
-    $out .= $in;
-    return $out;
 }
 
 sub bugurl {
@@ -244,9 +288,9 @@ sub bugurl {
        $params .= "\&archive=yes" if (!$common_archive && $val =~ /^archive.*$/);
     }
     $params .= "&archive=yes" if ($common_archive);
-    $params .= "&repeatmerged=yes" if ($common_repeatmerged);
+    $params .= "&repeatmerged=no" unless ($common_repeatmerged);
 
-    return urlsanit($debbugs::gCGIDomain . "bugreport.cgi" . "?" . "$params");
+    return urlsanit("bugreport.cgi" . "?" . "$params");
 }
 
 sub dlurl {
@@ -259,18 +303,18 @@ sub dlurl {
     }
     $params .= "&archive=yes" if ($common_archive);
 
-    return urlsanit($debbugs::gCGIDomain . "bugreport.cgi/$filename?$params");
+    return urlsanit("bugreport.cgi/$filename?$params");
 }
 
 sub mboxurl {
     my $ref = shift;
-    return urlsanit($debbugs::gCGIDomain . "bugreport.cgi" . "?" . "bug=$ref&mbox=yes");
+    return urlsanit("bugreport.cgi" . "?" . "bug=$ref&mbox=yes");
 }
 
 sub allbugs {
     my @bugs = ();
 
-    opendir(D, "$debbugs::gSpoolDir/db") or &quit("opendir db: $!");
+    opendir(D, "$debbugs::gSpoolDir/db") or &quitcgi("opendir db: $!");
     @bugs = sort {$a<=>$b} grep s/\.status$//,
                 (grep m/^[0-9]+\.status$/,
                 (readdir(D)));
@@ -330,7 +374,13 @@ sub htmlizebugs {
             }
            next unless ($okay);
        }
-           
+       next if @common_pending_include and
+            not grep { $_ eq $status{pending} } @common_pending_include;
+       next if @common_severity_include and
+            not grep { $_ eq $status{severity} } @common_severity_include;
+       next if grep { $_ eq $status{pending} } @common_pending_exclude;
+       next if grep { $_ eq $status{severity} } @common_severity_exclude;
+
        my $html = sprintf "<li><a href=\"%s\">#%d: %s</a>\n<br>",
            bugurl($bug), $bug, htmlsanit($status{subject});
        $html .= htmlindexentrystatus(\%status) . "\n";
@@ -344,25 +394,21 @@ sub htmlizebugs {
        $result .= "<UL>\n" . join("", @rawsort ) . "</UL>\n";
     } else {
        my @pendingList = qw(pending forwarded pending-fixed fixed done);
-       @pendingList = @common_pending_include if @common_pending_include;
        @pendingList = reverse @pendingList if $common_pending_reverse;
 #print STDERR join(",",@pendingList)."\n";
 #print STDERR join(",",@common_pending_include).":$#common_pending_include\n";
     foreach my $pending (@pendingList) {
-       next if grep( /^$pending$/, @common_pending_exclude);
        my @severityList = @debbugs::gSeverityList;
-       @severityList = @common_severity_include if @common_severity_include;
        @severityList = reverse @severityList if $common_severity_reverse;
 #print STDERR join(",",@severityList)."\n";
 
 #        foreach my $severity(@debbugs::gSeverityList) {
         foreach my $severity(@severityList) {
-           next if grep( /^$severity$/, @common_severity_exclude);
             $severity = $debbugs::gDefaultSeverity if ($severity eq '');
             next unless defined $section{${pending} . "_" . ${severity}};
             $result .= "<HR><H2>$debbugs::gSeverityDisplay{$severity} - $displayshowpending{$pending}</H2>\n";
             #$result .= "(A list of <a href=\"http://${debbugs::gWebDomain}/db/si/$pending$severity\">all such bugs</a> is available).\n";
-           $result .= "(A list of all such bugs used to be available).\n";
+            #$result .= "(A list of all such bugs used to be available).\n";
             $result .= "<UL>\n";
            $result .= $section{$pending . "_" . $severity}; 
            $result .= "</UL>\n";
@@ -378,9 +424,9 @@ sub htmlizebugs {
 sub countbugs {
     my $bugfunc = shift;
     if ($common_archive) {
-        open I, "<$debbugs::gSpoolDir/index.archive" or &quit("bugindex: $!");
+        open I, "<$debbugs::gSpoolDir/index.archive" or &quitcgi("bugindex: $!");
     } else {
-        open I, "<$debbugs::gSpoolDir/index.db" or &quit("bugindex: $!");
+        open I, "<$debbugs::gSpoolDir/index.db" or &quitcgi("bugindex: $!");
     }
 
     my %count = ();
@@ -420,10 +466,10 @@ print STDERR "done optimized\n" if ($debug);
     } else {
         if ( $common_archive ) {
             open I, "<$debbugs::gSpoolDir/index.archive" 
-                or &quit("bugindex: $!");
+                or &quitcgi("bugindex: $!");
         } else {
             open I, "<$debbugs::gSpoolDir/index.db" 
-                or &quit("bugindex: $!");
+                or &quitcgi("bugindex: $!");
         }
         while(<I>) {
             if (m/^(\S+)\s+(\d+)\s+(\d+)\s+(\S+)\s+\[\s*([^]]*)\s*\]\s+(\w+)\s+(.*)$/) {
@@ -470,7 +516,7 @@ sub getmaintainers {
     return $_maintainer if $_maintainer;
     my %maintainer;
 
-    open(MM,"$gMaintainerFile") or &quit("open $gMaintainerFile: $!");
+    open(MM,"$gMaintainerFile") or &quitcgi("open $gMaintainerFile: $!");
     while(<MM>) {
        next unless m/^(\S+)\s+(\S.*\S)\s*$/;
        ($a,$b)=($1,$2);
@@ -478,7 +524,7 @@ sub getmaintainers {
        $maintainer{$a}= $b;
     }
     close(MM);
-    open(MM,"$gMaintainerFileOverride") or &quit("open $gMaintainerFileOverride: $!");
+    open(MM,"$gMaintainerFileOverride") or &quitcgi("open $gMaintainerFileOverride: $!");
     while(<MM>) {
        next unless m/^(\S+)\s+(\S.*\S)\s*$/;
        ($a,$b)=($1,$2);
@@ -491,47 +537,57 @@ sub getmaintainers {
 }
 
 my $_pkgsrc;
+my $_pkgcomponent;
 sub getpkgsrc {
     return $_pkgsrc if $_pkgsrc;
     my %pkgsrc;
+    my %pkgcomponent;
 
-    open(MM,"$gPackageSource") or &quit("open $gPackageSource: $!");
+    open(MM,"$gPackageSource") or &quitcgi("open $gPackageSource: $!");
     while(<MM>) {
-       next unless m/^(\S+)\s+(\S.*\S)\s*$/;
-       ($a,$b)=($1,$2);
+       next unless m/^(\S+)\s+(\S+)\s+(\S.*\S)\s*$/;
+       ($a,$b,$c)=($1,$2,$3);
        $a =~ y/A-Z/a-z/;
-       $pkgsrc{$a}= $b;
+       $pkgsrc{$a}= $c;
+       $pkgcomponent{$a}= $b;
     }
     close(MM);
     $_pkgsrc = \%pkgsrc;
+    $_pkgcomponent = \%pkgcomponent;
     return $_pkgsrc;
 }
 
-sub getbugdir {
-    my ( $bugnum, $ext ) = @_;
-    my $archdir = sprintf "%02d", $bugnum % 100;
-    foreach ( ( "$gSpoolDir/db-h/$archdir", "$gSpoolDir/db", "$gSpoolDir/archive/$archdir", "/debian/home/joeyh/tmp/infomagic-95/$archdir" ) ) {
-       return $_ if ( -r "$_/$bugnum.$ext" );
+sub getpkgcomponent {
+    return $_pkgcomponent if $_pkgcomponent;
+    getpkgsrc();
+    return $_pkgcomponent;
+}
+
+my $_pseudodesc;
+sub getpseudodesc {
+    return $_pseudodesc if $_pseudodesc;
+    my %pseudodesc;
+
+    open(PSEUDO, "< $gPseudoDescFile") or &quitcgi("open $gPseudoDescFile: $!");
+    while(<PSEUDO>) {
+       next unless m/^(\S+)\s+(\S.*\S)\s*$/;
+       $pseudodesc{lc $1} = $2;
     }
-    return undef;
+    close(PSEUDO);
+    $_pseudodesc = \%pseudodesc;
+    return $_pseudodesc;
 }
-    
+
 sub getbugstatus {
     my $bugnum = shift;
 
     my %status;
 
-    my $dir = getbugdir( $bugnum, "status" );
-    return {} if ( !$dir );
-    open S, "< $dir/$bugnum.status";
-    my @lines = qw(originator date subject msgid package tags done
-                       forwarded mergedwith severity);
-    while(<S>) {
-        chomp;
-       $status{shift @lines} = $_;
-    }
-    close(S);
-    $status{shift @lines} = '' while(@lines);
+    my $location = getbuglocation( $bugnum, "status" );
+    return {} if ( !$location );
+    %status = %{ readbug( $bugnum, $location ) };
+
+    $status{tags} = $status{keywords};
 
     $status{"package"} =~ s/\s*$//;
     $status{"package"} = 'unknown' if ($status{"package"} eq '');
@@ -539,8 +595,8 @@ sub getbugstatus {
 
     $status{"pending"} = 'pending';
     $status{"pending"} = 'forwarded'       if (length($status{"forwarded"}));
-    $status{"pending"} = 'fixed'           if ($status{"tags"} =~ /\bfixed\b/);
     $status{"pending"} = 'pending-fixed'    if ($status{"tags"} =~ /\bpending\b/);
+    $status{"pending"} = 'fixed'           if ($status{"tags"} =~ /\bfixed\b/);
     $status{"pending"} = 'done'                    if (length($status{"done"}));
 
     return \%status;
@@ -560,9 +616,10 @@ sub getsrcpkgs {
 sub buglog {
     my $bugnum = shift;
 
-    my $dir = getbugdir( $bugnum, "log" );
+    my $dir = getlocationpath( getbuglocation( $bugnum, "log" ) );
+    my $hash = get_hashname( $bugnum );
     return "" if ( !$dir );
-    return "$dir/$bugnum.log";
+    return "$dir/$hash/$bugnum.log";
 }
 
 1;