summaryrefslogtreecommitdiffstats
path: root/extensions/InlineHistory/Extension.pm
blob: df7ae9f62645a370725e11eb30f5523b46719019 (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
# 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::InlineHistory;
use strict;
use base qw(Bugzilla::Extension);

use Bugzilla::User::Setting;
use Bugzilla::Constants;
use Bugzilla::Attachment;

our $VERSION = '1.5';

# don't show inline history for bugs with lots of changes
use constant MAXIMUM_ACTIVITY_COUNT => 500;

# don't show really long values
use constant MAXIMUM_VALUE_LENGTH   => 256;

sub template_before_create {
    my ($self, $args) = @_;
    $args->{config}->{FILTERS}->{ih_short_value} = sub {
        my ($str) = @_;
        return length($str) <= MAXIMUM_VALUE_LENGTH
               ? $str
               : substr($str, 0, MAXIMUM_VALUE_LENGTH - 3) . '...';
    };
}

sub template_before_process {
    my ($self, $args) = @_;
    my $file = $args->{'file'};
    my $vars = $args->{'vars'};

    return if $file ne 'bug/edit.html.tmpl';

    my $user = Bugzilla->user;
    my $dbh = Bugzilla->dbh;
    return unless $user->id && $user->settings->{'inline_history'}->{'value'} eq 'on';

    # note: bug/edit.html.tmpl doesn't support multiple bugs
    my $bug = exists $vars->{'bugs'} ? $vars->{'bugs'}[0] : $vars->{'bug'};
    my $bug_id = $bug->id;

    # build bug activity
    my ($activity) = Bugzilla::Bug::GetBugActivity($bug_id);
    $activity = _add_duplicates($bug_id, $activity);

    if (scalar @$activity > MAXIMUM_ACTIVITY_COUNT) {
        $activity = [];
        $vars->{'ih_activity'} = 0;
        $vars->{'ih_activity_max'} = 1;
        return;
    }

    # prime caches with objects already loaded
    my %user_cache;
    foreach my $comment (@{$bug->comments}) {
        $user_cache{$comment->{author}->login} = $comment->{author};
    }

    my %attachment_cache;
    foreach my $attachment (@{$bug->attachments}) {
        $attachment_cache{$attachment->id} = $attachment;
    }

    # build a list of bugs we need to check visibility of, so we can check with a single query
    my %visible_bug_ids;

    # augment and tweak
    foreach my $operation (@$activity) {
        # make operation.who an object
        $user_cache{$operation->{who}} ||= Bugzilla::User->new({ name => $operation->{who} });
        $operation->{who} = $user_cache{$operation->{who}};

        for (my $i = 0; $i < scalar(@{$operation->{changes}}); $i++) {
            my $change = $operation->{changes}->[$i];

            # make an attachment object
            if ($change->{attachid}) {
                $change->{attach} = $attachment_cache{$change->{attachid}};
            }

            # empty resolutions are displayed as --- by default
            # make it explicit here to enable correct display of the change
            if ($change->{fieldname} eq 'resolution') {
                $change->{removed} = '---' if $change->{removed} eq '';
                $change->{added} = '---' if $change->{added} eq '';
            }

            # make boolean fields true/false instead of 1/0
            my ($table, $field) = ('bugs', $change->{fieldname});
            if ($field =~ /^([^\.]+)\.(.+)$/) {
                ($table, $field) = ($1, $2);
            }
            my $column = $dbh->bz_column_info($table, $field);
            if ($column && $column->{TYPE} eq 'BOOLEAN') {
                $change->{removed} = '';
                $change->{added} = $change->{added} ? 'true' : 'false';
            }

            my $field_obj;
            if ($change->{fieldname} =~ /^cf_/) {
                $field_obj = Bugzilla::Field->new({ name => $change->{fieldname}, cache => 1 });
            }

            # identify buglist changes
            if ($change->{fieldname} eq 'blocked' ||
                $change->{fieldname} eq 'dependson' ||
                $change->{fieldname} eq 'dupe' ||
                ($field_obj && $field_obj->type == FIELD_TYPE_BUG_ID)
            ) {
                $change->{buglist} = 1;
                foreach my $what (qw(removed added)) {
                    my @buglist = split(/[\s,]+/, $change->{$what});
                    foreach my $id (@buglist) {
                        if ($id && $id =~ /^\d+$/) {
                            $visible_bug_ids{$id} = 1;
                        }
                    }
                }
            }

            # split multiple flag changes (must be processed last)
            if ($change->{fieldname} eq 'flagtypes.name') {
                my @added = split(/, /, $change->{added});
                my @removed = split(/, /, $change->{removed});
                next if scalar(@added) <= 1 && scalar(@removed) <= 1;
                # remove current change
                splice(@{$operation->{changes}}, $i, 1);
                # restructure into added/removed for each flag
                my %flags;
                foreach my $added (@added) {
                    my ($value, $name) = $added =~ /^((.+).)$/;
                    $flags{$name}{added} = $value;
                    $flags{$name}{removed} |= '';
                }
                foreach my $removed (@removed) {
                    my ($value, $name) = $removed =~ /^((.+).)$/;
                    $flags{$name}{added} |= '';
                    $flags{$name}{removed} = $value;
                }
                # clone current change, modify and insert
                foreach my $flag (sort keys %flags) {
                    my $flag_change = {};
                    foreach my $key (keys %$change) {
                        $flag_change->{$key} = $change->{$key};
                    }
                    $flag_change->{removed} = $flags{$flag}{removed};
                    $flag_change->{added} = $flags{$flag}{added};
                    splice(@{$operation->{changes}}, $i, 0, $flag_change);
                }
                $i--;
            }
        }
    }

    $user->visible_bugs([keys %visible_bug_ids]);

    $vars->{'ih_activity'} = $activity;
}

sub _add_duplicates {
    # insert 'is a dupe of this bug' comment to allow js to display
    # as activity

    my ($bug_id, $activity) = @_;

    my $dbh = Bugzilla->dbh;
    my $sth = $dbh->prepare("
        SELECT profiles.login_name, " .
               $dbh->sql_date_format('bug_when', '%Y.%m.%d %H:%i:%s') . ",
               extra_data,
               thetext
          FROM longdescs
               INNER JOIN profiles ON profiles.userid = longdescs.who
         WHERE bug_id = ?
               AND (
                 type = ?
                 OR thetext LIKE '%has been marked as a duplicate of this%'
               )
         ORDER BY bug_when
    ");
    $sth->execute($bug_id, CMT_HAS_DUPE);

    while (my($who, $when, $dupe_id, $the_text) = $sth->fetchrow_array) {
        if (!$dupe_id) {
            next unless $the_text =~ / (\d+) has been marked as a duplicate of this/;
            $dupe_id = $1;
        }
        my $entry = {
            'when' => $when,
            'who' => $who,
            'changes' => [
                {
                    'removed' => '',
                    'added' => $dupe_id,
                    'attachid' => undef,
                    'fieldname' => 'dupe',
                    'dupe' => 1,
                }
            ],
        };
        push @$activity, $entry;
    }

    return [ sort { $a->{when} cmp $b->{when} } @$activity ];
}

sub install_before_final_checks {
    my ($self, $args) = @_;
    add_setting('inline_history', ['on', 'off'], 'off');
}

__PACKAGE__->NAME;