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
|
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.
package Bugzilla::Extension::Review::Util;
use strict;
use warnings;
use base qw(Exporter);
use Bugzilla;
our @EXPORT = qw( rebuild_review_counters );
sub rebuild_review_counters {
my ($callback) = @_;
my $dbh = Bugzilla->dbh;
$dbh->bz_start_transaction;
my $rows = $dbh->selectall_arrayref("
SELECT flags.requestee_id AS user_id,
flagtypes.name AS flagtype,
COUNT(*) as count
FROM flags
INNER JOIN profiles ON profiles.userid = flags.requestee_id
INNER JOIN flagtypes ON flagtypes.id = flags.type_id
WHERE flags.status = '?'
AND flagtypes.name IN ('review', 'feedback', 'needinfo')
GROUP BY flags.requestee_id, flagtypes.name
", { Slice => {} });
my ($count, $total, $current) = (1, scalar(@$rows), { id => 0 });
foreach my $row (@$rows) {
$callback->($count++, $total) if $callback;
if ($row->{user_id} != $current->{id}) {
_update_profile($dbh, $current) if $current->{id};
$current = { id => $row->{user_id} };
}
$current->{$row->{flagtype}} = $row->{count};
}
_update_profile($dbh, $current) if $current->{id};
foreach my $field (qw( review feedback needinfo )) {
_fix_negatives($dbh, $field);
}
$dbh->bz_commit_transaction;
}
sub _fix_negatives {
my ($dbh, $field) = @_;
my $user_ids = $dbh->selectcol_arrayref(
"SELECT userid FROM profiles WHERE ${field}_request_count < 0"
);
return unless @$user_ids;
$dbh->do(
"UPDATE profiles SET ${field}_request_count = 0 WHERE " . $dbh->sql_in('userid', $user_ids)
);
foreach my $user_id (@$user_ids) {
Bugzilla->memcached->clear({ table => 'profiles', id => $user_id });
}
}
sub _update_profile {
my ($dbh, $data) = @_;
$dbh->do("
UPDATE profiles
SET review_request_count = ?,
feedback_request_count = ?,
needinfo_request_count = ?
WHERE userid = ?",
undef,
$data->{review} || 0,
$data->{feedback} || 0,
$data->{needinfo} || 0,
$data->{id}
);
Bugzilla->memcached->clear({ table => 'profiles', id => $data->{id} });
}
1;
|