]> git.donarmstrong.com Git - debbugs.git/blobdiff - cgi/common.pl
[project @ 2003-05-21 20:55:13 by cjwatson]
[debbugs.git] / cgi / common.pl
index 340bbdf3ec1ba891037ea388a4ae11d39e356d5b..c7662c4371f1f7ef451c579dd22d2b084f0b75d5 100644 (file)
@@ -2,6 +2,9 @@
 
 use DB_File;
 use Fcntl qw/O_RDONLY/;
+$config_path = '/etc/debbugs';
+$lib_path = '/usr/lib/debbugs';
+require "$lib_path/errorlib";
 
 my $common_archive = 0;
 my $common_repeatmerged = 1;
@@ -23,8 +26,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 +89,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 +106,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 +125,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 +174,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 +192,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 = "";
@@ -160,8 +206,8 @@ sub htmlindexentrystatus {
             $efont = "</$font>" if ($font);
             $font = "<$font>" if ($font);
 
-            my $yearsold = int($daysold / 364);
-            $daysold = $daysold - $yearsold * 364;
+            my $yearsold = int($daysold / 365);
+            $daysold -= $yearsold * 365;
 
             $result .= ";\n $font";
             my @age;
@@ -184,7 +230,7 @@ sub submitterurl {
     my $params = "submitter=" . emailfromrfc822($ref);
     $params .= "&archive=yes" if ($common_archive);
     $params .= "&repeatmerged=no" unless ($common_repeatmerged);
-    return urlsanit($debbugs::gCGIDomain . "pkgreport.cgi" . "?" . $params);
+    return urlsanit("pkgreport.cgi" . "?" . $params);
 }
 
 sub mainturl {
@@ -192,7 +238,7 @@ sub mainturl {
     my $params = "maint=" . emailfromrfc822($ref);
     $params .= "&archive=yes" if ($common_archive);
     $params .= "&repeatmerged=no" unless ($common_repeatmerged);
-    return urlsanit($debbugs::gCGIDomain . "pkgreport.cgi" . "?" . $params);
+    return urlsanit("pkgreport.cgi" . "?" . $params);
 }
 
 sub pkgurl {
@@ -201,7 +247,7 @@ sub pkgurl {
     $params .= "&archive=yes" if ($common_archive);
     $params .= "&repeatmerged=no" unless ($common_repeatmerged);
     
-    return urlsanit($debbugs::gCGIDomain . "pkgreport.cgi" . "?" . "$params");
+    return urlsanit("pkgreport.cgi" . "?" . "$params");
 }
 
 sub srcurl {
@@ -209,7 +255,7 @@ sub srcurl {
     my $params = "src=$ref";
     $params .= "&archive=yes" if ($common_archive);
     $params .= "&repeatmerged=no" unless ($common_repeatmerged);
-    return urlsanit($debbugs::gCGIDomain . "pkgreport.cgi" . "?" . "$params");
+    return urlsanit("pkgreport.cgi" . "?" . "$params");
 }
 
 sub urlsanit {
@@ -228,6 +274,15 @@ sub htmlsanit {
     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);
+    }
+}
+
 sub bugurl {
     my $ref = shift;
     my $params = "bug=$ref";
@@ -238,7 +293,7 @@ sub bugurl {
     $params .= "&archive=yes" if ($common_archive);
     $params .= "&repeatmerged=no" unless ($common_repeatmerged);
 
-    return urlsanit($debbugs::gCGIDomain . "bugreport.cgi" . "?" . "$params");
+    return urlsanit("bugreport.cgi" . "?" . "$params");
 }
 
 sub dlurl {
@@ -251,18 +306,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)));
@@ -293,11 +348,10 @@ sub htmlizebugs {
     } else {
        @bugs = sort {$a<=>$b} @bugs;
     }
+    my %seenmerged;
     foreach my $bug (@bugs) {
        my %status = %{getbugstatus($bug)};
         next unless %status;
-       my @merged = sort {$a<=>$b} ($bug, split(/ /, $status{mergedwith}));
-       next unless ($common_repeatmerged || $bug == $merged[0]);
        if (%common_include) {
            my $okay = 0;
            foreach my $t (split /\s+/, $status{tags}) {
@@ -329,6 +383,10 @@ sub htmlizebugs {
        next if grep { $_ eq $status{pending} } @common_pending_exclude;
        next if grep { $_ eq $status{severity} } @common_severity_exclude;
 
+       my @merged = sort {$a<=>$b} ($bug, split(/ /, $status{mergedwith}));
+       next unless ($common_repeatmerged || !$seenmerged{$merged[0]});
+       $seenmerged{$merged[0]} = 1;
+
        my $html = sprintf "<li><a href=\"%s\">#%d: %s</a>\n<br>",
            bugurl($bug), $bug, htmlsanit($status{subject});
        $html .= htmlindexentrystatus(\%status) . "\n";
@@ -356,7 +414,7 @@ sub htmlizebugs {
             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";
@@ -372,18 +430,19 @@ 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 = ();
     while(<I>) 
     {
         if (m/^(\S+)\s+(\d+)\s+(\d+)\s+(\S+)\s+\[\s*([^]]*)\s*\]\s+(\w+)\s+(.*)$/) {
-            my $x = $bugfunc->(pkg => $1, bug => $2, status => $4, 
+            my @x = $bugfunc->(pkg => $1, bug => $2, status => $4, 
                                submitter => $5, severity => $6, tags => $7);
-           $count{$x}++;
+            local $_;
+            $count{$_}++ foreach @x;
        }
     }
     close I;
@@ -414,10 +473,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+(.*)$/) {
@@ -464,7 +523,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);
@@ -472,7 +531,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,7 +550,7 @@ sub getpkgsrc {
     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.*\S)\s*$/;
        ($a,$b,$c)=($1,$2,$3);
@@ -516,7 +575,7 @@ sub getpseudodesc {
     return $_pseudodesc if $_pseudodesc;
     my %pseudodesc;
 
-    open(PSEUDO, "< $gPseudoDescFile") or &quit("open $gPseudoDescFile: $!");
+    open(PSEUDO, "< $gPseudoDescFile") or &quitcgi("open $gPseudoDescFile: $!");
     while(<PSEUDO>) {
        next unless m/^(\S+)\s+(\S.*\S)\s*$/;
        $pseudodesc{lc $1} = $2;
@@ -526,31 +585,16 @@ sub getpseudodesc {
     return $_pseudodesc;
 }
 
-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" );
-    }
-    return undef;
-}
-    
 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 '');
@@ -579,9 +623,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;