Series.pm 9.61 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
# -*- 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>

use strict;
use lib ".";

# This module implements a series - a set of data to be plotted on a chart.
26 27 28 29 30 31
#
# This Series is in the database if and only if self->{'series_id'} is defined. 
# Note that the series being in the database does not mean that the fields of 
# this object are the same as the DB entries, as the object may have been 
# altered.

32 33 34 35 36 37
package Bugzilla::Series;

use Bugzilla;
use Bugzilla::Util;
use Bugzilla::User;

38 39
use constant PUBLIC_USER_ID => 0;

40 41 42 43 44 45 46 47
sub new {
    my $invocant = shift;
    my $class = ref($invocant) || $invocant;
  
    # Create a ref to an empty hash and bless it
    my $self = {};
    bless($self, $class);

48 49
    my $arg_count = scalar(@_);
    
50 51 52
    # There are three ways of creating Series objects. Two (CGI and Parameters)
    # are for use when creating a new series. One (Database) is for retrieving
    # information on existing series.
53
    if ($arg_count == 1) {
54
        if (ref($_[0])) {
55
            # We've been given a CGI object to create a new Series from.
56 57
            # This series may already exist - external code needs to check
            # before it calls writeToDatabase().
58
            $self->initFromCGI($_[0]);
59 60
        }
        else {
61 62
            # We've been given a series_id, which should represent an existing
            # Series.
63 64 65
            $self->initFromDatabase($_[0]);
        }
    }
66
    elsif ($arg_count >= 6 && $arg_count <= 8) {
67
        # We've been given a load of parameters to create a new Series from.
68 69
        # Currently, undef is always passed as the first parameter; this allows
        # you to call writeToDatabase() unconditionally. 
70
        $self->initFromParameters(@_);
71 72
    }
    else {
73
        die("Bad parameters passed in - invalid number of args: $arg_count");
74 75
    }

76
    return $self;
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
}

sub initFromDatabase {
    my $self = shift;
    my $series_id = shift;
    
    &::detaint_natural($series_id) 
      || &::ThrowCodeError("invalid_series_id", { 'series_id' => $series_id });
    
    my $dbh = Bugzilla->dbh;
    my @series = $dbh->selectrow_array("SELECT series.series_id, cc1.name, " .
        "cc2.name, series.name, series.creator, series.frequency, " .
        "series.query " .
        "FROM series " .
        "LEFT JOIN series_categories AS cc1 " .
        "    ON series.category = cc1.category_id " .
        "LEFT JOIN series_categories AS cc2 " .
        "    ON series.subcategory = cc2.category_id " .
        "WHERE series.series_id = $series_id");
    
    if (@series) {
98 99 100
        # Note that we calculate $self->{'public'} ourselves instead of passing
        # it as the last parameter in @series; this is because isSubscribed()
        # requires the rest of the object to be set up correctly.
101
        $self->initFromParameters(@series);
102
        $self->{'public'} = $self->isSubscribed(PUBLIC_USER_ID);
103 104 105 106 107 108 109
    }
    else {
        &::ThrowCodeError("invalid_series_id", { 'series_id' => $series_id });
    }
}

sub initFromParameters {
110
    # Pass undef as the first parameter if you are creating a new series.
111 112 113 114
    my $self = shift;

    ($self->{'series_id'}, $self->{'category'},  $self->{'subcategory'},
     $self->{'name'}, $self->{'creator'}, $self->{'frequency'},
115
     $self->{'query'}, $self->{'public'}) = @_;
116 117
}

118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
sub initFromCGI {
    my $self = shift;
    my $cgi = shift;

    $self->{'series_id'} = $cgi->param('series_id') || undef;
    if (defined($self->{'series_id'})) {
        detaint_natural($self->{'series_id'})
          || &::ThrowCodeError("invalid_series_id", 
                               { 'series_id' => $self->{'series_id'} });
    }
    
    $self->{'category'} = $cgi->param('category')
      || $cgi->param('newcategory')
      || &::ThrowUserError("missing_category");

    $self->{'subcategory'} = $cgi->param('subcategory')
      || $cgi->param('newsubcategory')
      || &::ThrowUserError("missing_subcategory");

    $self->{'name'} = $cgi->param('name')
      || &::ThrowUserError("missing_name");

    $self->{'creator'} = Bugzilla->user->id;

    $self->{'frequency'} = $cgi->param('frequency');
    detaint_natural($self->{'frequency'})
      || &::ThrowUserError("missing_frequency");

    $self->{'query'} = $cgi->canonicalise_query("format", "ctype", "action",
                                        "category", "subcategory", "name",
                                        "frequency", "public", "query_format");
                                        
    $self->{'public'} = $cgi->param('public') ? 1 : 0;    
}

sub writeToDatabase {
154 155 156 157 158 159 160 161 162
    my $self = shift;

    my $dbh = Bugzilla->dbh;
    $dbh->do("LOCK TABLES series_categories WRITE, series WRITE, " .
             "user_series_map WRITE");

    my $category_id = getCategoryID($self->{'category'});
    my $subcategory_id = getCategoryID($self->{'subcategory'});

163 164 165 166 167 168 169
    my $exists;
    if ($self->{'series_id'}) { 
        $exists = 
            $dbh->selectrow_array("SELECT series_id FROM series
                                   WHERE series_id = $self->{'series_id'}");
    }
    
170
    # Is this already in the database?                              
171
    if ($exists) {
172 173 174 175 176 177 178 179
        # Update existing series
        my $dbh = Bugzilla->dbh;
        $dbh->do("UPDATE series SET " .
                 "category = ?, subcategory = ?," .
                 "name = ?, frequency = ? " .
                 "WHERE series_id = ?", undef,
                 $category_id, $subcategory_id, $self->{'name'},
                 $self->{'frequency'}, $self->{'series_id'});
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
    }
    else {
        # Insert the new series into the series table
        $dbh->do("INSERT INTO series (creator, category, subcategory, " .
                 "name, frequency, query) VALUES ($self->{'creator'}, " .
                 "$category_id, $subcategory_id, " .
                 $dbh->quote($self->{'name'}) . ", $self->{'frequency'}," .
                 $dbh->quote($self->{'query'}) . ")");

        # Retrieve series_id
        $self->{'series_id'} = $dbh->selectrow_array("SELECT MAX(series_id) " .
                                                     "FROM series");
        $self->{'series_id'}
          || &::ThrowCodeError("missing_series_id", { 'series' => $self });

195 196 197 198 199 200 201
        # Subscribe creator to the newly-created series.
        $self->subscribe($self->{'creator'});
    }
    
    # Update publicness by changing subscription
    if ($self->{'public'}) {
        $self->subscribe(PUBLIC_USER_ID);
202
    }
203 204 205
    else {
        $self->unsubscribe(PUBLIC_USER_ID);
    }             
206 207 208 209

    $dbh->do("UNLOCK TABLES");
}

210
# Check whether a series with this name, category and subcategory exists in
211
# the DB and, if so, returns its series_id.
212 213 214 215 216 217 218 219
sub existsInDatabase {
    my $self = shift;
    my $dbh = Bugzilla->dbh;

    my $category_id = getCategoryID($self->{'category'});
    my $subcategory_id = getCategoryID($self->{'subcategory'});
    
    trick_taint($self->{'name'});
220
    my $series_id = $dbh->selectrow_array("SELECT series_id " .
221 222 223 224
                              "FROM series WHERE category = $category_id " .
                              "AND subcategory = $subcategory_id AND name = " .
                              $dbh->quote($self->{'name'}));
                              
225
    return($series_id);
226 227
}

228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
# Get a category or subcategory IDs, creating the category if it doesn't exist.
sub getCategoryID {
    my ($category) = @_;
    my $category_id;
    my $dbh = Bugzilla->dbh;

    # This seems for the best idiom for "Do A. Then maybe do B and A again."
    while (1) {
        # We are quoting this to put it in the DB, so we can remove taint
        trick_taint($category);

        $category_id = $dbh->selectrow_array("SELECT category_id " .
                                      "from series_categories " .
                                      "WHERE name =" . $dbh->quote($category));
        last if $category_id;

        $dbh->do("INSERT INTO series_categories (name) " .
                 "VALUES (" . $dbh->quote($category) . ")");
    }

    return $category_id;
}        

sub subscribe {
    my $self = shift;
    my $userid = shift;
    
    if (!$self->isSubscribed($userid)) {
        # Subscribe current user to series_id
        my $dbh = Bugzilla->dbh;
        $dbh->do("INSERT INTO user_series_map " .
                 "VALUES($userid, $self->{'series_id'})");
    }    
}

sub unsubscribe {
    my $self = shift;
    my $userid = shift;
    
    if ($self->isSubscribed($userid)) {
        # Remove current user's subscription to series_id
        my $dbh = Bugzilla->dbh;
        $dbh->do("DELETE FROM user_series_map " .
                "WHERE user_id = $userid AND series_id = $self->{'series_id'}");
    }        
}

sub isSubscribed {
    my $self = shift;
    my $userid = shift;
    
    my $dbh = Bugzilla->dbh;
    my $issubscribed = $dbh->selectrow_array("SELECT 1 FROM user_series_map " .
                                       "WHERE user_id = $userid " .
                                       "AND series_id = $self->{'series_id'}");
    return $issubscribed;
}

1;