2003-07-01 02:23:17 +04:00
|
|
|
#!/usr/bin/perl -wT
|
2003-06-26 03:23:13 +04:00
|
|
|
# -*- Mode: perl; indent-tabs-mode: nil -*-
|
|
|
|
#
|
|
|
|
# The contents of this file are subject to the Mozilla Public
|
|
|
|
# License Version 1.1 (the "License"); you may not use this file
|
|
|
|
# except in compliance with the License. You may obtain a copy of
|
|
|
|
# the License at http://www.mozilla.org/MPL/
|
|
|
|
#
|
|
|
|
# Software distributed under the License is distributed on an "AS
|
|
|
|
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
|
|
|
# implied. See the License for the specific language governing
|
|
|
|
# rights and limitations under the License.
|
|
|
|
#
|
|
|
|
# The Original Code is the Bugzilla Bug Tracking System.
|
|
|
|
#
|
|
|
|
# The Initial Developer of the Original Code is Netscape Communications
|
|
|
|
# Corporation. Portions created by Netscape are
|
|
|
|
# Copyright (C) 1998 Netscape Communications Corporation. All
|
|
|
|
# Rights Reserved.
|
|
|
|
#
|
|
|
|
# Contributor(s): Gervase Markham <gerv@gerv.net>
|
2005-10-24 23:21:05 +04:00
|
|
|
# Lance Larsh <lance.larsh@oracle.com>
|
2003-06-26 03:23:13 +04:00
|
|
|
|
|
|
|
# Glossary:
|
2004-02-13 01:33:07 +03:00
|
|
|
# series: An individual, defined set of data plotted over time.
|
|
|
|
# data set: What a series is called in the UI.
|
|
|
|
# line: A set of one or more series, to be summed and drawn as a single
|
|
|
|
# line when the series is plotted.
|
|
|
|
# chart: A set of lines
|
|
|
|
#
|
2003-06-26 03:23:13 +04:00
|
|
|
# So when you select rows in the UI, you are selecting one or more lines, not
|
|
|
|
# series.
|
|
|
|
|
|
|
|
# Generic Charting TODO:
|
|
|
|
#
|
|
|
|
# JS-less chart creation - hard.
|
|
|
|
# Broken image on error or no data - need to do much better.
|
|
|
|
# Centralise permission checking, so UserInGroup('editbugs') not scattered
|
|
|
|
# everywhere.
|
|
|
|
# User documentation :-)
|
|
|
|
#
|
|
|
|
# Bonus:
|
|
|
|
# Offer subscription when you get a "series already exists" error?
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use lib qw(.);
|
|
|
|
|
2005-08-10 05:30:41 +04:00
|
|
|
require "globals.pl";
|
|
|
|
use Bugzilla;
|
2004-03-27 06:51:44 +03:00
|
|
|
use Bugzilla::Constants;
|
2003-06-26 03:23:13 +04:00
|
|
|
use Bugzilla::Chart;
|
|
|
|
use Bugzilla::Series;
|
2005-03-16 01:10:14 +03:00
|
|
|
use Bugzilla::User;
|
2003-06-26 03:23:13 +04:00
|
|
|
|
2005-08-10 05:30:41 +04:00
|
|
|
my $cgi = Bugzilla->cgi;
|
|
|
|
my $template = Bugzilla->template;
|
2005-10-25 03:11:56 +04:00
|
|
|
my $vars = {};
|
2003-06-26 03:23:13 +04:00
|
|
|
|
|
|
|
# Go back to query.cgi if we are adding a boolean chart parameter.
|
|
|
|
if (grep(/^cmd-/, $cgi->param())) {
|
|
|
|
my $params = $cgi->canonicalise_query("format", "ctype", "action");
|
|
|
|
print "Location: query.cgi?format=" . $cgi->param('query_format') .
|
|
|
|
($params ? "&$params" : "") . "\n\n";
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $action = $cgi->param('action');
|
|
|
|
my $series_id = $cgi->param('series_id');
|
|
|
|
|
|
|
|
# Because some actions are chosen by buttons, we can't encode them as the value
|
|
|
|
# of the action param, because that value is localisation-dependent. So, we
|
|
|
|
# encode it in the name, as "action-<action>". Some params even contain the
|
|
|
|
# series_id they apply to (e.g. subscribe, unsubscribe.)
|
|
|
|
my @actions = grep(/^action-/, $cgi->param());
|
|
|
|
if ($actions[0] && $actions[0] =~ /^action-([^\d]+)(\d*)$/) {
|
|
|
|
$action = $1;
|
|
|
|
$series_id = $2 if $2;
|
|
|
|
}
|
|
|
|
|
|
|
|
$action ||= "assemble";
|
|
|
|
|
|
|
|
# Go to buglist.cgi if we are doing a search.
|
|
|
|
if ($action eq "search") {
|
|
|
|
my $params = $cgi->canonicalise_query("format", "ctype", "action");
|
|
|
|
print "Location: buglist.cgi" . ($params ? "?$params" : "") . "\n\n";
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2005-10-31 00:31:29 +03:00
|
|
|
my $user = Bugzilla->login(LOGIN_REQUIRED);
|
2003-06-26 03:23:13 +04:00
|
|
|
|
2005-01-16 17:07:31 +03:00
|
|
|
UserInGroup(Param("chartgroup"))
|
|
|
|
|| ThrowUserError("auth_failure", {group => Param("chartgroup"),
|
|
|
|
action => "use",
|
|
|
|
object => "charts"});
|
2004-07-06 05:12:29 +04:00
|
|
|
|
2004-02-13 01:33:07 +03:00
|
|
|
# Only admins may create public queries
|
|
|
|
UserInGroup('admin') || $cgi->delete('public');
|
|
|
|
|
2003-06-26 03:23:13 +04:00
|
|
|
# All these actions relate to chart construction.
|
|
|
|
if ($action =~ /^(assemble|add|remove|sum|subscribe|unsubscribe)$/) {
|
|
|
|
# These two need to be done before the creation of the Chart object, so
|
|
|
|
# that the changes they make will be reflected in it.
|
|
|
|
if ($action =~ /^subscribe|unsubscribe$/) {
|
2003-12-08 01:43:20 +03:00
|
|
|
detaint_natural($series_id) || ThrowCodeError("invalid_series_id");
|
2003-06-26 03:23:13 +04:00
|
|
|
my $series = new Bugzilla::Series($series_id);
|
2005-10-31 00:31:29 +03:00
|
|
|
$series->$action($user->id);
|
2003-06-26 03:23:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
my $chart = new Bugzilla::Chart($cgi);
|
|
|
|
|
|
|
|
if ($action =~ /^remove|sum$/) {
|
|
|
|
$chart->$action(getSelectedLines());
|
|
|
|
}
|
|
|
|
elsif ($action eq "add") {
|
|
|
|
my @series_ids = getAndValidateSeriesIDs();
|
|
|
|
$chart->add(@series_ids);
|
|
|
|
}
|
|
|
|
|
|
|
|
view($chart);
|
|
|
|
}
|
|
|
|
elsif ($action eq "plot") {
|
|
|
|
plot();
|
|
|
|
}
|
|
|
|
elsif ($action eq "wrap") {
|
|
|
|
# For CSV "wrap", we go straight to "plot".
|
|
|
|
if ($cgi->param('ctype') && $cgi->param('ctype') eq "csv") {
|
|
|
|
plot();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
wrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elsif ($action eq "create") {
|
|
|
|
assertCanCreate($cgi);
|
2004-02-13 01:33:07 +03:00
|
|
|
|
2003-06-26 03:23:13 +04:00
|
|
|
my $series = new Bugzilla::Series($cgi);
|
|
|
|
|
2003-12-08 01:43:20 +03:00
|
|
|
if (!$series->existsInDatabase()) {
|
|
|
|
$series->writeToDatabase();
|
2003-06-26 03:23:13 +04:00
|
|
|
$vars->{'message'} = "series_created";
|
|
|
|
}
|
|
|
|
else {
|
2004-02-13 01:33:07 +03:00
|
|
|
ThrowUserError("series_already_exists", {'series' => $series});
|
2003-06-26 03:23:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
$vars->{'series'} = $series;
|
|
|
|
|
2004-09-12 19:46:07 +04:00
|
|
|
print $cgi->header();
|
2003-06-26 03:23:13 +04:00
|
|
|
$template->process("global/message.html.tmpl", $vars)
|
|
|
|
|| ThrowTemplateError($template->error());
|
|
|
|
}
|
|
|
|
elsif ($action eq "edit") {
|
2003-12-08 01:43:20 +03:00
|
|
|
detaint_natural($series_id) || ThrowCodeError("invalid_series_id");
|
2003-06-26 03:23:13 +04:00
|
|
|
assertCanEdit($series_id);
|
|
|
|
|
|
|
|
my $series = new Bugzilla::Series($series_id);
|
2003-12-08 01:43:20 +03:00
|
|
|
|
2003-06-26 03:23:13 +04:00
|
|
|
edit($series);
|
|
|
|
}
|
|
|
|
elsif ($action eq "alter") {
|
2003-12-08 01:43:20 +03:00
|
|
|
# This is the "commit" action for editing a series
|
|
|
|
detaint_natural($series_id) || ThrowCodeError("invalid_series_id");
|
2003-06-26 03:23:13 +04:00
|
|
|
assertCanEdit($series_id);
|
|
|
|
|
2003-12-08 01:43:20 +03:00
|
|
|
my $series = new Bugzilla::Series($cgi);
|
2004-02-13 01:33:07 +03:00
|
|
|
|
|
|
|
# We need to check if there is _another_ series in the database with
|
|
|
|
# our (potentially new) name. So we call existsInDatabase() to see if
|
|
|
|
# the return value is us or some other series we need to avoid stomping
|
|
|
|
# on.
|
|
|
|
my $id_of_series_in_db = $series->existsInDatabase();
|
|
|
|
if (defined($id_of_series_in_db) &&
|
|
|
|
$id_of_series_in_db != $series->{'series_id'})
|
|
|
|
{
|
|
|
|
ThrowUserError("series_already_exists", {'series' => $series});
|
|
|
|
}
|
|
|
|
|
2003-12-08 01:43:20 +03:00
|
|
|
$series->writeToDatabase();
|
2004-02-13 01:33:07 +03:00
|
|
|
$vars->{'changes_saved'} = 1;
|
2003-12-08 01:43:20 +03:00
|
|
|
|
2003-06-26 03:23:13 +04:00
|
|
|
edit($series);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ThrowCodeError("unknown_action");
|
|
|
|
}
|
|
|
|
|
|
|
|
exit;
|
|
|
|
|
|
|
|
# Find any selected series and return either the first or all of them.
|
|
|
|
sub getAndValidateSeriesIDs {
|
|
|
|
my @series_ids = grep(/^\d+$/, $cgi->param("name"));
|
|
|
|
|
|
|
|
return wantarray ? @series_ids : $series_ids[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
# Return a list of IDs of all the lines selected in the UI.
|
|
|
|
sub getSelectedLines {
|
|
|
|
my @ids = map { /^select(\d+)$/ ? $1 : () } $cgi->param();
|
|
|
|
|
|
|
|
return @ids;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Check if the user is the owner of series_id or is an admin.
|
|
|
|
sub assertCanEdit {
|
|
|
|
my ($series_id) = @_;
|
2005-10-31 00:31:29 +03:00
|
|
|
my $user = Bugzilla->user;
|
|
|
|
|
|
|
|
return if $user->in_group('admin');
|
2003-06-26 03:23:13 +04:00
|
|
|
|
|
|
|
my $dbh = Bugzilla->dbh;
|
2005-10-24 23:21:05 +04:00
|
|
|
my $iscreator = $dbh->selectrow_array("SELECT CASE WHEN creator = ? " .
|
|
|
|
"THEN 1 ELSE 0 END FROM series " .
|
2003-06-26 03:23:13 +04:00
|
|
|
"WHERE series_id = ?", undef,
|
2005-10-31 00:31:29 +03:00
|
|
|
$user->id, $series_id);
|
2003-06-26 03:23:13 +04:00
|
|
|
$iscreator || ThrowUserError("illegal_series_edit");
|
|
|
|
}
|
|
|
|
|
|
|
|
# Check if the user is permitted to create this series with these parameters.
|
|
|
|
sub assertCanCreate {
|
|
|
|
my ($cgi) = shift;
|
|
|
|
|
|
|
|
UserInGroup("editbugs") || ThrowUserError("illegal_series_creation");
|
|
|
|
|
|
|
|
# Check permission for frequency
|
|
|
|
my $min_freq = 7;
|
|
|
|
if ($cgi->param('frequency') < $min_freq && !UserInGroup("admin")) {
|
|
|
|
ThrowUserError("illegal_frequency", { 'minimum' => $min_freq });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub validateWidthAndHeight {
|
|
|
|
$vars->{'width'} = $cgi->param('width');
|
|
|
|
$vars->{'height'} = $cgi->param('height');
|
|
|
|
|
|
|
|
if (defined($vars->{'width'})) {
|
|
|
|
(detaint_natural($vars->{'width'}) && $vars->{'width'} > 0)
|
|
|
|
|| ThrowCodeError("invalid_dimensions");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (defined($vars->{'height'})) {
|
|
|
|
(detaint_natural($vars->{'height'}) && $vars->{'height'} > 0)
|
|
|
|
|| ThrowCodeError("invalid_dimensions");
|
|
|
|
}
|
|
|
|
|
|
|
|
# The equivalent of 2000 square seems like a very reasonable maximum size.
|
|
|
|
# This is merely meant to prevent accidental or deliberate DOS, and should
|
|
|
|
# have no effect in practice.
|
|
|
|
if ($vars->{'width'} && $vars->{'height'}) {
|
|
|
|
(($vars->{'width'} * $vars->{'height'}) <= 4000000)
|
|
|
|
|| ThrowUserError("chart_too_large");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub edit {
|
|
|
|
my $series = shift;
|
|
|
|
|
|
|
|
$vars->{'category'} = Bugzilla::Chart::getVisibleSeries();
|
|
|
|
$vars->{'creator'} = new Bugzilla::User($series->{'creator'});
|
2004-02-13 01:33:07 +03:00
|
|
|
$vars->{'default'} = $series;
|
2003-06-26 03:23:13 +04:00
|
|
|
|
2004-09-12 19:46:07 +04:00
|
|
|
print $cgi->header();
|
2003-06-26 03:23:13 +04:00
|
|
|
$template->process("reports/edit-series.html.tmpl", $vars)
|
|
|
|
|| ThrowTemplateError($template->error());
|
|
|
|
}
|
|
|
|
|
|
|
|
sub plot {
|
|
|
|
validateWidthAndHeight();
|
|
|
|
$vars->{'chart'} = new Bugzilla::Chart($cgi);
|
|
|
|
|
2005-08-25 18:02:41 +04:00
|
|
|
my $format = $template->get_format("reports/chart", "", scalar($cgi->param('ctype')));
|
2003-06-26 03:23:13 +04:00
|
|
|
|
|
|
|
# Debugging PNGs is a pain; we need to be able to see the error messages
|
|
|
|
if ($cgi->param('debug')) {
|
2004-09-12 19:46:07 +04:00
|
|
|
print $cgi->header();
|
2003-06-26 03:23:13 +04:00
|
|
|
$vars->{'chart'}->dump();
|
|
|
|
}
|
|
|
|
|
2004-09-12 19:46:07 +04:00
|
|
|
print $cgi->header($format->{'ctype'});
|
2003-06-26 03:23:13 +04:00
|
|
|
$template->process($format->{'template'}, $vars)
|
|
|
|
|| ThrowTemplateError($template->error());
|
|
|
|
}
|
|
|
|
|
|
|
|
sub wrap {
|
|
|
|
validateWidthAndHeight();
|
|
|
|
|
|
|
|
# We create a Chart object so we can validate the parameters
|
|
|
|
my $chart = new Bugzilla::Chart($cgi);
|
|
|
|
|
|
|
|
$vars->{'time'} = time();
|
|
|
|
|
|
|
|
$vars->{'imagebase'} = $cgi->canonicalise_query(
|
2005-05-12 05:52:13 +04:00
|
|
|
"action", "action-wrap", "ctype", "format", "width", "height");
|
2003-06-26 03:23:13 +04:00
|
|
|
|
2004-09-12 19:46:07 +04:00
|
|
|
print $cgi->header();
|
2003-06-26 03:23:13 +04:00
|
|
|
$template->process("reports/chart.html.tmpl", $vars)
|
|
|
|
|| ThrowTemplateError($template->error());
|
|
|
|
}
|
|
|
|
|
|
|
|
sub view {
|
|
|
|
my $chart = shift;
|
|
|
|
|
|
|
|
# Set defaults
|
|
|
|
foreach my $field ('category', 'subcategory', 'name', 'ctype') {
|
|
|
|
$vars->{'default'}{$field} = $cgi->param($field) || 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Pass the state object to the display UI.
|
|
|
|
$vars->{'chart'} = $chart;
|
|
|
|
$vars->{'category'} = Bugzilla::Chart::getVisibleSeries();
|
|
|
|
|
2004-09-12 19:46:07 +04:00
|
|
|
print $cgi->header();
|
2003-06-26 03:23:13 +04:00
|
|
|
|
|
|
|
# If we have having problems with bad data, we can set debug=1 to dump
|
|
|
|
# the data structure.
|
|
|
|
$chart->dump() if $cgi->param('debug');
|
|
|
|
|
|
|
|
$template->process("reports/create-chart.html.tmpl", $vars)
|
|
|
|
|| ThrowTemplateError($template->error());
|
|
|
|
}
|