summaryrefslogtreecommitdiffstats
path: root/extensions/TagNewUsers/Extension.pm
blob: 382a3c3d1bbf1ef6dca464a84c3f991831f8b524 (plain)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# -*- 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 TagNewUsers Extension.
#
# The Initial Developer of the Original Code is the Mozilla Foundation
# Portions created by the Initial Developers are Copyright (C) 2011 the
# Initial Developer. All Rights Reserved.
#
# Contributor(s):
#   Byron Jones <bjones@mozilla.com>

package Bugzilla::Extension::TagNewUsers;
use strict;
use base qw(Bugzilla::Extension);
use Bugzilla::Field;
use Bugzilla::User;
use Bugzilla::Install::Util qw(indicate_progress);
use Date::Parse;

# users younger than PROFILE_AGE days will be tagged as new
use constant PROFILE_AGE => 60;

# users with fewer comments than COMMENT_COUNT will be tagged as new
use constant COMMENT_COUNT => 25;

our $VERSION = '1';

#
# install
#

sub install_update_db {
    my ($self) = @_;
    my $dbh = Bugzilla->dbh;

    if (!$dbh->bz_column_info('profiles', 'comment_count')) {
        $dbh->bz_add_column('profiles', 'comment_count',
            {TYPE => 'INT3', NOTNULL => 1, DEFAULT => 0});
        my $sth = $dbh->prepare('UPDATE profiles SET comment_count=? WHERE userid=?');
        my $ra = $dbh->selectall_arrayref('SELECT who,COUNT(*) FROM longdescs GROUP BY who');
        my $count = 1;
        my $total = scalar @$ra;
        foreach my $ra_row (@$ra) {
            indicate_progress({ current => $count++, total => $total, every => 25 });
            my ($user_id, $count) = @$ra_row;
            $sth->execute($count, $user_id);
        }
    }

    if (!$dbh->bz_column_info('profiles', 'creation_ts')) {
        $dbh->bz_add_column('profiles', 'creation_ts',
            {TYPE => 'DATETIME'});
        my $creation_date_fieldid = get_field_id('creation_ts');
        my $sth = $dbh->prepare('UPDATE profiles SET creation_ts=? WHERE userid=?');
        my $ra = $dbh->selectall_arrayref("
            SELECT p.userid, a.profiles_when
              FROM profiles p
                   LEFT JOIN profiles_activity a ON a.userid=p.userid 
                        AND a.fieldid=$creation_date_fieldid
        ");
        my ($now) = Bugzilla->dbh->selectrow_array("SELECT NOW()");
        my $count = 1;
        my $total = scalar @$ra;
        foreach my $ra_row (@$ra) {
            indicate_progress({ current => $count++, total => $total, every => 25 });
            my ($user_id, $when) = @$ra_row;
            if (!$when) {
                ($when) = $dbh->selectrow_array(
                    "SELECT bug_when FROM bugs_activity WHERE who=? ORDER BY bug_when " . 
                        $dbh->sql_limit(1),
                    undef, $user_id
                );
            }
            if (!$when) {
                ($when) = $dbh->selectrow_array(
                    "SELECT bug_when FROM longdescs WHERE who=? ORDER BY bug_when " . 
                        $dbh->sql_limit(1),
                    undef, $user_id
                );
            }
            if (!$when) {
                ($when) = $dbh->selectrow_array(
                    "SELECT creation_ts FROM bugs WHERE reporter=? ORDER BY creation_ts " . 
                        $dbh->sql_limit(1),
                    undef, $user_id
                );
            }
            if (!$when) {
                $when = $now;
            }

            $sth->execute($when, $user_id);
        }
    }

    if (!$dbh->bz_column_info('profiles', 'first_patch_bug_id')) {
        $dbh->bz_add_column('profiles', 'first_patch_bug_id', {TYPE => 'INT3'});
        my $sth_update = $dbh->prepare('UPDATE profiles SET first_patch_bug_id=? WHERE userid=?');
        my $sth_select = $dbh->prepare(
            'SELECT bug_id FROM attachments WHERE submitter_id=? AND ispatch=1 ORDER BY creation_ts ' . $dbh->sql_limit(1)
        );
        my $ra = $dbh->selectcol_arrayref('SELECT DISTINCT submitter_id FROM attachments WHERE ispatch=1');
        my $count = 1;
        my $total = scalar @$ra;
        foreach my $user_id (@$ra) {
            indicate_progress({ current => $count++, total => $total, every => 25 });
            $sth_select->execute($user_id);
            my ($bug_id) = $sth_select->fetchrow_array;
            $sth_update->execute($bug_id, $user_id);
        }
    }
}

#
# objects
#

BEGIN {
    *Bugzilla::User::update_comment_count = \&_update_comment_count;
    *Bugzilla::User::first_patch_bug_id = \&_first_patch_bug_id;
}

sub object_columns {
    my ($self, $args) = @_;
    my ($class, $columns) = @$args{qw(class columns)};
    if ($class->isa('Bugzilla::User')) {
        push(@$columns, qw(comment_count creation_ts first_patch_bug_id));
    }
}

sub object_before_create {
    my ($self, $args) = @_;
    my ($class, $params) = @$args{qw(class params)};
    if ($class->isa('Bugzilla::User')) {
        my ($timestamp) = Bugzilla->dbh->selectrow_array("SELECT NOW()");
        $params->{comment_count} = 0;
        $params->{creation_ts} = $timestamp;
    } elsif ($class->isa('Bugzilla::Attachment')) {
        if ($params->{ispatch} && !Bugzilla->user->first_patch_bug_id) {
            Bugzilla->user->first_patch_bug_id($params->{bug}->id);
        }
    }
}

sub bug_end_of_create {
    Bugzilla->user->update_comment_count();
}

sub bug_end_of_update {
    Bugzilla->user->update_comment_count();
}

sub _update_comment_count {
    my $self = shift;
    my $dbh = Bugzilla->dbh;

    my $id = $self->id;
    my ($count) = $dbh->selectrow_array(
        "SELECT COUNT(*) FROM longdescs WHERE who=?",
        undef, $id
    );
    return if $self->{comment_count} == $count;
    $dbh->do(
        'UPDATE profiles SET comment_count=? WHERE userid=?',
        undef, $count, $id
    );
    $self->{comment_count} = $count;
}

sub _first_patch_bug_id {
    my ($self, $bug_id) = @_;
    return $self->{first_patch_bug_id} unless defined $bug_id;

    Bugzilla->dbh->do(
        'UPDATE profiles SET first_patch_bug_id=? WHERE userid=?',
        undef, $bug_id, $self->id
    );
    $self->{first_patch_bug_id} = $bug_id;
}

#
#
#

sub template_before_process {
    my ($self, $args) = @_;
    my ($vars, $file) = @$args{qw(vars file)};
    if ($file eq 'bug/comments.html.tmpl') {

        # only users in canconfirm will see the new-to-bugzilla tag
        return unless Bugzilla->user->in_group('canconfirm');

        # calculate if each user that has commented on the bug is new
        foreach my $comment (@{$vars->{bug}{comments}}) {
            my $user = $comment->author;
            $user->{is_new} = $self->_user_is_new($user);
        }
    }
}

sub _user_is_new {
    my ($self, $user) = (shift, shift);

    # if the user can confirm bugs, they are no longer new
    return 0 if $user->in_group('canconfirm');

    # store the age in days, for the 'new to bugzilla' tooltip
    my $age = sprintf("%.0f", (time() - str2time($user->{creation_ts})) / 86400);
    $user->{creation_age} = $age;

    return 
        ($user->{comment_count} <= COMMENT_COUNT)
        || ($user->{creation_age} <= PROFILE_AGE);
}

__PACKAGE__->NAME;