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
|
# 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::BMO::Reports::UserActivity;
use strict;
use warnings;
use Bugzilla::Error;
use Bugzilla::Extension::BMO::Util;
use Bugzilla::User;
use Bugzilla::Util qw(trim);
use DateTime;
sub report {
my ($vars) = @_;
my $dbh = Bugzilla->dbh;
my $input = Bugzilla->input_params;
my @who = ();
my $from = trim($input->{'from'} || '');
my $to = trim($input->{'to'} || '');
my $action = $input->{'action'} || '';
# fix non-breaking hyphens
$from =~ s/\N{U+2011}/-/g;
$to =~ s/\N{U+2011}/-/g;
if ($from eq '') {
my $dt = DateTime->now()->subtract('weeks' => 8);
$from = $dt->ymd('-');
}
if ($to eq '') {
my $dt = DateTime->now();
$to = $dt->ymd('-');
}
if ($action eq 'run') {
if (!exists $input->{'who'} || $input->{'who'} eq '') {
ThrowUserError('user_activity_missing_username');
}
Bugzilla::User::match_field({ 'who' => {'type' => 'multi'} });
my $from_dt = string_to_datetime($from);
$from = $from_dt->ymd();
my $to_dt = string_to_datetime($to);
$to = $to_dt->ymd();
# add one day to include all activity that happened on the 'to' date
$to_dt->add(days => 1);
my ($activity_joins, $activity_where) = ('', '');
my ($attachments_joins, $attachments_where) = ('', '');
my ($tags_activity_joins, $tags_activity_where) = ('', '');
if (Bugzilla->params->{"insidergroup"}
&& !Bugzilla->user->in_group(Bugzilla->params->{'insidergroup'}))
{
$activity_joins = "LEFT JOIN attachments
ON attachments.attach_id = bugs_activity.attach_id";
$activity_where = "AND COALESCE(attachments.isprivate, 0) = 0";
$attachments_where = $activity_where;
$tags_activity_joins = 'LEFT JOIN longdescs
ON longdescs_tags_activity.comment_id = longdescs.comment_id';
$tags_activity_where = 'AND COALESCE(longdescs.isprivate, 0) = 0';
}
my @who_bits;
foreach my $who (
ref $input->{'who'}
? @{$input->{'who'}}
: $input->{'who'}
) {
push @who, $who;
push @who_bits, '?';
}
my $who_bits = join(',', @who_bits);
if (!@who) {
my $template = Bugzilla->template;
my $cgi = Bugzilla->cgi;
my $vars = {};
$vars->{'script'} = $cgi->url(-relative => 1);
$vars->{'fields'} = {};
$vars->{'matches'} = [];
$vars->{'matchsuccess'} = 0;
$vars->{'matchmultiple'} = 1;
print $cgi->header();
$template->process("global/confirm-user-match.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
exit;
}
$from_dt = $from_dt->ymd() . ' 00:00:00';
$to_dt = $to_dt->ymd() . ' 23:59:59';
my @params;
for (1..5) {
push @params, @who;
push @params, ($from_dt, $to_dt);
}
my $order = ($input->{'sort'} && $input->{'sort'} eq 'bug')
? 'bug_id, bug_when' : 'bug_when';
my $comment_filter = '';
if (!Bugzilla->user->is_insider) {
$comment_filter = 'AND longdescs.isprivate = 0';
}
my $query = "
SELECT
fielddefs.name,
bugs_activity.bug_id,
bugs_activity.attach_id,
".$dbh->sql_date_format('bugs_activity.bug_when', '%Y.%m.%d %H:%i:%s')." AS ts,
bugs_activity.removed,
bugs_activity.added,
profiles.login_name,
bugs_activity.comment_id,
bugs_activity.bug_when
FROM bugs_activity
$activity_joins
LEFT JOIN fielddefs
ON bugs_activity.fieldid = fielddefs.id
INNER JOIN profiles
ON profiles.userid = bugs_activity.who
WHERE profiles.login_name IN ($who_bits)
AND bugs_activity.bug_when >= ? AND bugs_activity.bug_when <= ?
$activity_where
UNION ALL
SELECT
'comment_tag' AS name,
longdescs_tags_activity.bug_id,
NULL as attach_id,
".$dbh->sql_date_format('longdescs_tags_activity.bug_when',
'%Y.%m.%d %H:%i:%s') . " AS bug_when,
longdescs_tags_activity.removed,
longdescs_tags_activity.added,
profiles.login_name,
longdescs_tags_activity.comment_id,
longdescs_tags_activity.bug_when
FROM longdescs_tags_activity
$tags_activity_joins
INNER JOIN profiles
ON profiles.userid = longdescs_tags_activity.who
WHERE profiles.login_name IN ($who_bits)
AND longdescs_tags_activity.bug_when >= ?
AND longdescs_tags_activity.bug_when <= ?
$tags_activity_where
UNION ALL
SELECT
'bug_id' AS name,
bugs.bug_id,
NULL AS attach_id,
".$dbh->sql_date_format('bugs.creation_ts', '%Y.%m.%d %H:%i:%s')." AS ts,
'(new bug)' AS removed,
bugs.short_desc AS added,
profiles.login_name,
NULL AS comment_id,
bugs.creation_ts AS bug_when
FROM bugs
INNER JOIN profiles
ON profiles.userid = bugs.reporter
WHERE profiles.login_name IN ($who_bits)
AND bugs.creation_ts >= ? AND bugs.creation_ts <= ?
UNION ALL
SELECT
'longdesc' AS name,
longdescs.bug_id,
NULL AS attach_id,
DATE_FORMAT(longdescs.bug_when, '%Y.%m.%d %H:%i:%s') AS ts,
'' AS removed,
'' AS added,
profiles.login_name,
longdescs.comment_id AS comment_id,
longdescs.bug_when
FROM longdescs
INNER JOIN profiles
ON profiles.userid = longdescs.who
WHERE profiles.login_name IN ($who_bits)
AND longdescs.bug_when >= ? AND longdescs.bug_when <= ?
$comment_filter
UNION ALL
SELECT
'attachments.description' AS name,
attachments.bug_id,
attachments.attach_id,
".$dbh->sql_date_format('attachments.creation_ts', '%Y.%m.%d %H:%i:%s')." AS ts,
'(new attachment)' AS removed,
attachments.description AS added,
profiles.login_name,
NULL AS comment_id,
attachments.creation_ts AS bug_when
FROM attachments
INNER JOIN profiles
ON profiles.userid = attachments.submitter_id
WHERE profiles.login_name IN ($who_bits)
AND attachments.creation_ts >= ? AND attachments.creation_ts <= ?
$attachments_where
ORDER BY $order ";
my $list = $dbh->selectall_arrayref($query, undef, @params);
if ($input->{debug}) {
while (my $param = shift @params) {
$query =~ s/\?/$dbh->quote($param)/e;
}
$vars->{debug_sql} = $query;
}
my @operations;
my $operation = {};
my $changes = [];
my $incomplete_data = 0;
my %bug_ids;
foreach my $entry (@$list) {
my ($fieldname, $bugid, $attachid, $when, $removed, $added, $who,
$comment_id) = @$entry;
my %change;
my $activity_visible = 1;
next unless Bugzilla->user->can_see_bug($bugid);
# check if the user should see this field's activity
if ($fieldname eq 'remaining_time'
|| $fieldname eq 'estimated_time'
|| $fieldname eq 'work_time'
|| $fieldname eq 'deadline')
{
$activity_visible = Bugzilla->user->is_timetracker;
}
elsif ($fieldname eq 'longdescs.isprivate'
&& !Bugzilla->user->is_insider
&& $added)
{
$activity_visible = 0;
}
else {
$activity_visible = 1;
}
if ($activity_visible) {
# Check for the results of an old Bugzilla data corruption bug
if (($added eq '?' && $removed eq '?')
|| ($added =~ /^\? / || $removed =~ /^\? /)) {
$incomplete_data = 1;
}
# Start a new changeset if required (depends on the sort order)
my $is_new_changeset;
if ($order eq 'bug_when') {
$is_new_changeset =
$operation->{'who'} &&
(
$who ne $operation->{'who'}
|| $when ne $operation->{'when'}
|| $bugid != $operation->{'bug'}
);
} else {
$is_new_changeset =
$operation->{'bug'} &&
$bugid != $operation->{'bug'};
}
if ($is_new_changeset) {
$operation->{'changes'} = $changes;
push (@operations, $operation);
$operation = {};
$changes = [];
}
$bug_ids{$bugid} = 1;
$operation->{'bug'} = $bugid;
$operation->{'who'} = $who;
$operation->{'when'} = $when;
$change{'fieldname'} = $fieldname;
$change{'attachid'} = $attachid;
$change{'removed'} = $removed;
$change{'added'} = $added;
$change{'when'} = $when;
if ($comment_id) {
$change{'comment'} = Bugzilla::Comment->new($comment_id);
next if $change{'comment'}->count == 0;
}
if ($attachid) {
$change{'attach'} = Bugzilla::Attachment->new($attachid);
}
push (@$changes, \%change);
}
}
if ($operation->{'who'}) {
$operation->{'changes'} = $changes;
push (@operations, $operation);
}
$vars->{'incomplete_data'} = $incomplete_data;
$vars->{'operations'} = \@operations;
my @bug_ids = sort { $a <=> $b } keys %bug_ids;
$vars->{'bug_ids'} = \@bug_ids;
}
$vars->{'action'} = $action;
$vars->{'who'} = join(',', @who);
$vars->{'who_count'} = scalar @who;
$vars->{'from'} = $from;
$vars->{'to'} = $to;
$vars->{'sort'} = $input->{'sort'};
}
1;
|