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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
|
# 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::UserProfile;
use strict;
use warnings;
use base qw(Bugzilla::Extension);
use Bugzilla::Constants;
use Bugzilla::Extension::UserProfile::Util;
use Bugzilla::Install::Filesystem;
use Bugzilla::User;
use Scalar::Util qw(blessed);
our $VERSION = '1';
#
# user methods
#
BEGIN {
*Bugzilla::User::last_activity_ts = \&_user_last_activity_ts;
*Bugzilla::User::set_last_activity_ts = \&_user_set_last_activity_ts;
*Bugzilla::User::last_statistics_ts = \&_user_last_statistics_ts;
*Bugzilla::User::clear_last_statistics_ts = \&_user_clear_last_statistics_ts;
}
sub _user_last_activity_ts { $_[0]->{last_activity_ts} }
sub _user_last_statistics_ts { $_[0]->{last_statistics_ts} }
sub _user_set_last_activity_ts {
my ($self, $value) = @_;
$self->set('last_activity_ts', $_[1]);
# we update the database directly to avoid audit_log entries
Bugzilla->dbh->do(
"UPDATE profiles SET last_activity_ts = ? WHERE userid = ?",
undef,
$value, $self->id);
}
sub _user_clear_last_statistics_ts {
my ($self) = @_;
$self->set('last_statistics_ts', undef);
# we update the database directly to avoid audit_log entries
Bugzilla->dbh->do(
"UPDATE profiles SET last_statistics_ts = NULL WHERE userid = ?",
undef,
$self->id);
}
#
# hooks
#
sub bug_after_create {
my ($self, $args) = @_;
$self->_bug_touched($args);
}
sub bug_after_update {
my ($self, $args) = @_;
$self->_bug_touched($args);
}
sub _bug_touched {
my ($self, $args) = @_;
my $bug = $args->{bug};
my $user = Bugzilla->user;
my ($assigned_to, $qa_contact);
# bug update
if (exists $args->{changes}) {
return unless
scalar(keys %{ $args->{changes} })
|| exists $args->{bug}->{added_comments};
# if the assignee or qa-contact is changed to someone other than the
# current user, update them
if (exists $args->{changes}->{assigned_to}
&& $args->{changes}->{assigned_to}->[1] ne $user->login)
{
$assigned_to = $bug->assigned_to;
}
if (exists $args->{changes}->{qa_contact}
&& ($args->{changes}->{qa_contact}->[1] || '') ne $user->login)
{
$qa_contact = $bug->qa_contact;
}
# if the product is changed, we need to recount everyone involved with
# this bug
if (exists $args->{changes}->{product}) {
tag_for_recount_from_bug($bug->id);
}
}
# new bug
else {
# if the assignee or qa-contact is created set to someone other than
# the current user, update them
if ($bug->assigned_to->id != $user->id) {
$assigned_to = $bug->assigned_to;
}
if ($bug->qa_contact && $bug->qa_contact->id != $user->id) {
$qa_contact = $bug->qa_contact;
}
}
my $dbh = Bugzilla->dbh;
$dbh->bz_start_transaction();
# update user's last_activity_ts
eval {
$user->set_last_activity_ts($args->{timestamp});
$self->_recalc_remove($user);
};
if ($@) {
warn $@;
$self->_recalc_insert($user);
}
# clear the last_statistics_ts for assignee/qa-contact to force a recount
# at the next poll
if ($assigned_to) {
eval {
$assigned_to->clear_last_statistics_ts();
$self->_recalc_remove($assigned_to);
};
if ($@) {
warn $@;
$self->_recalc_insert($assigned_to);
}
}
if ($qa_contact) {
eval {
$qa_contact->clear_last_statistics_ts();
$self->_recalc_remove($qa_contact);
};
if ($@) {
warn $@;
$self->_recalc_insert($qa_contact);
}
}
$dbh->bz_commit_transaction();
}
sub _recalc_insert {
my ($self, $user) = @_;
Bugzilla->dbh->do(
"INSERT IGNORE INTO profiles_statistics_recalc SET user_id=?",
undef, $user->id
);
}
sub _recalc_remove {
my ($self, $user) = @_;
Bugzilla->dbh->do(
"DELETE FROM profiles_statistics_recalc WHERE user_id=?",
undef, $user->id
);
}
sub object_end_of_create {
my ($self, $args) = @_;
$self->_object_touched($args);
}
sub object_end_of_update {
my ($self, $args) = @_;
$self->_object_touched($args);
}
sub _object_touched {
my ($self, $args) = @_;
my $object = $args->{object}
or return;
return if exists $args->{changes} && !scalar(keys %{ $args->{changes} });
if ($object->isa('Bugzilla::Attachment')) {
# if an attachment is created or updated, that counts as user activity
my $user = Bugzilla->user;
my $timestamp = Bugzilla->dbh->selectrow_array('SELECT LOCALTIMESTAMP(0)');
eval {
$user->set_last_activity_ts($timestamp);
$self->_recalc_remove($user);
};
if ($@) {
warn $@;
$self->_recalc_insert($user);
}
}
elsif ($object->isa('Bugzilla::Product') && exists $args->{changes}->{name}) {
# if a product is renamed by an admin, rename in the
# profiles_statistics_products table
Bugzilla->dbh->do(
"UPDATE profiles_statistics_products SET product=? where product=?",
undef,
$args->{changes}->{name}->[1], $args->{changes}->{name}->[0],
);
}
}
sub reorg_move_bugs {
my ($self, $args) = @_;
my $bug_ids = $args->{bug_ids};
printf "Touching user profile data for %s bugs.\n", scalar(@$bug_ids);
my $count = 0;
foreach my $bug_id (@$bug_ids) {
$count += tag_for_recount_from_bug($bug_id);
}
print "Updated $count users.\n";
}
sub merge_users_before {
my ($self, $args) = @_;
my ($old_id, $new_id) = @$args{qw(old_id new_id)};
# when users are merged, we have to delete all the statistics for both users
# we'll recalcuate the stats after the merge
print "deleting user profile statistics for $old_id and $new_id\n";
my $dbh = Bugzilla->dbh;
foreach my $table (qw( profiles_statistics profiles_statistics_status profiles_statistics_products )) {
$dbh->do("DELETE FROM $table WHERE " . $dbh->sql_in('user_id', [ $old_id, $new_id ]));
}
}
sub merge_users_after {
my ($self, $args) = @_;
my $new_id = $args->{new_id};
print "generating user profile statistics $new_id\n";
update_statistics_by_user($new_id);
}
sub webservice_user_get {
my ($self, $args) = @_;
my ($service, $users) = @$args{qw(webservice users)};
my $dbh = Bugzilla->dbh;
my $ids = [
map { blessed($_->{id}) ? $_->{id}->value : $_->{id} }
grep { exists $_->{id} }
@$users
];
return unless @$ids;
my $timestamps = $dbh->selectall_hashref(
"SELECT userid,last_activity_ts FROM profiles WHERE " . $dbh->sql_in('userid', $ids),
'userid',
);
foreach my $user (@$users) {
my $id = blessed($user->{id}) ? $user->{id}->value : $user->{id};
$user->{last_activity} = $service->type('dateTime', $timestamps->{$id}->{last_activity_ts});
}
}
sub page_before_template {
my ($self, $args) = @_;
my ($vars, $page) = @$args{qw(vars page_id)};
return unless $page eq 'user_profile.html';
my $user = Bugzilla->user;
# check login
my $target;
my $input = Bugzilla->input_params;
my $limit = Bugzilla->params->{'maxusermatches'} + 1;
if (!$input->{login}) {
$target = Bugzilla->login(LOGIN_REQUIRED);
} else {
my $users = Bugzilla::User::match($input->{login}, $limit, 1);
if (scalar(@$users) == 1) {
# always allow singular matches without confirmation
$target = $users->[0];
} else {
Bugzilla::User::match_field({ 'login' => {'type' => 'single'} });
$target = Bugzilla::User->check($input->{login});
}
}
# load statistics into $vars
my $dbh = Bugzilla->switch_to_shadow_db;
my $stats = $dbh->selectall_hashref(
"SELECT name, count
FROM profiles_statistics
WHERE user_id = ?",
"name",
undef,
$target->id,
);
map { $stats->{$_} = $stats->{$_}->{count} } keys %$stats;
my $statuses = $dbh->selectall_hashref(
"SELECT status, count
FROM profiles_statistics_status
WHERE user_id = ?",
"status",
undef,
$target->id,
);
map { $statuses->{$_} = $statuses->{$_}->{count} } keys %$statuses;
my $products = $dbh->selectall_arrayref(
"SELECT product, count
FROM profiles_statistics_products
WHERE user_id = ?
ORDER BY product = '', count DESC",
{ Slice => {} },
$target->id,
);
# ensure there's always an "other" product entry
my ($other_product) = grep { $_->{product} eq '' } @$products;
if (!$other_product) {
$other_product = { product => '', count => 0 };
push @$products, $other_product;
}
# load product objects and validate product visibility
foreach my $product (@$products) {
next if $product->{product} eq '';
my $product_obj = Bugzilla::Product->new({ name => $product->{product} });
if (!$product_obj || !$user->can_see_product($product_obj->name)) {
# products not accessible to current user are moved into "other"
$other_product->{count} += $product->{count};
$product->{count} = 0;
} else {
$product->{product} = $product_obj;
}
}
# set other's name, and remove empty products
$other_product->{product} = { name => 'Other' };
$products = [ grep { $_->{count} } @$products ];
$vars->{stats} = $stats;
$vars->{statuses} = $statuses;
$vars->{products} = $products;
$vars->{target} = $target;
}
sub object_columns {
my ($self, $args) = @_;
my ($class, $columns) = @$args{qw(class columns)};
if ($class->isa('Bugzilla::User')) {
push(@$columns, qw(last_activity_ts last_statistics_ts));
}
}
sub object_update_columns {
my ($self, $args) = @_;
my ($object, $columns) = @$args{qw(object columns)};
if ($object->isa('Bugzilla::User')) {
push(@$columns, qw(last_activity_ts last_statistics_ts));
}
}
#
# installation
#
sub db_schema_abstract_schema {
my ($self, $args) = @_;
$args->{'schema'}->{'profiles_statistics'} = {
FIELDS => [
id => {
TYPE => 'MEDIUMSERIAL',
NOTNULL => 1,
PRIMARYKEY => 1,
},
user_id => {
TYPE => 'INT3',
NOTNULL => 1,
REFERENCES => {
TABLE => 'profiles',
COLUMN => 'userid',
DELETE => 'CASCADE',
}
},
name => {
TYPE => 'VARCHAR(30)',
NOTNULL => 1,
},
count => {
TYPE => 'INT',
NOTNULL => 1,
},
],
INDEXES => [
profiles_statistics_name_idx => {
FIELDS => [ 'user_id', 'name' ],
TYPE => 'UNIQUE',
},
],
};
$args->{'schema'}->{'profiles_statistics_status'} = {
FIELDS => [
id => {
TYPE => 'MEDIUMSERIAL',
NOTNULL => 1,
PRIMARYKEY => 1,
},
user_id => {
TYPE => 'INT3',
NOTNULL => 1,
REFERENCES => {
TABLE => 'profiles',
COLUMN => 'userid',
DELETE => 'CASCADE',
}
},
status => {
TYPE => 'VARCHAR(64)',
NOTNULL => 1,
},
count => {
TYPE => 'INT',
NOTNULL => 1,
},
],
INDEXES => [
profiles_statistics_status_idx => {
FIELDS => [ 'user_id', 'status' ],
TYPE => 'UNIQUE',
},
],
};
$args->{'schema'}->{'profiles_statistics_products'} = {
FIELDS => [
id => {
TYPE => 'MEDIUMSERIAL',
NOTNULL => 1,
PRIMARYKEY => 1,
},
user_id => {
TYPE => 'INT3',
NOTNULL => 1,
REFERENCES => {
TABLE => 'profiles',
COLUMN => 'userid',
DELETE => 'CASCADE',
}
},
product => {
TYPE => 'VARCHAR(64)',
NOTNULL => 1,
},
count => {
TYPE => 'INT',
NOTNULL => 1,
},
],
INDEXES => [
profiles_statistics_products_idx => {
FIELDS => [ 'user_id', 'product' ],
TYPE => 'UNIQUE',
},
],
};
$args->{'schema'}->{'profiles_statistics_recalc'} = {
FIELDS => [
user_id => {
TYPE => 'INT3',
NOTNULL => 1,
REFERENCES => {
TABLE => 'profiles',
COLUMN => 'userid',
DELETE => 'CASCADE',
}
},
],
INDEXES => [
profiles_statistics_recalc_idx => {
FIELDS => [ 'user_id' ],
TYPE => 'UNIQUE',
},
],
};
$args->{'schema'}->{'profiles_statistics_recalc'} = {
FIELDS => [
user_id => {
TYPE => 'INT3',
NOTNULL => 1,
REFERENCES => {
TABLE => 'profiles',
COLUMN => 'userid',
DELETE => 'CASCADE',
}
},
],
INDEXES => [
profiles_statistics_recalc_idx => {
FIELDS => [ 'user_id' ],
TYPE => 'UNIQUE',
},
],
};
}
sub install_update_db {
my $dbh = Bugzilla->dbh;
$dbh->bz_add_column('profiles', 'last_activity_ts', { TYPE => 'DATETIME' });
$dbh->bz_add_column('profiles', 'last_statistics_ts', { TYPE => 'DATETIME' });
}
sub install_filesystem {
my ($self, $args) = @_;
my $files = $args->{'files'};
my $extensions_dir = bz_locations()->{'extensionsdir'};
my $script_name = $extensions_dir . "/" . __PACKAGE__->NAME . "/bin/update.pl";
$files->{$script_name} = {
perms => Bugzilla::Install::Filesystem::WS_EXECUTE
};
$script_name = $extensions_dir . "/" . __PACKAGE__->NAME . "/bin/migrate.pl";
$files->{$script_name} = {
perms => Bugzilla::Install::Filesystem::OWNER_EXECUTE
};
}
__PACKAGE__->NAME;
|