-
Notifications
You must be signed in to change notification settings - Fork 1
/
Extension.pm
240 lines (207 loc) · 8.17 KB
/
Extension.pm
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
# -*- Mode: perl; indent-tabs-mode: nil -*-
#
# 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/.
#
# Copyright (C) 2012-2017 Jolla Ltd.
# Contact: Pami Ketolainen <[email protected]>
package Bugzilla::Extension::BugViewPlus;
use strict;
use base qw(Bugzilla::Extension);
use Bugzilla::Config qw(SetParam write_params);
use Bugzilla::Error;
use Bugzilla::Field qw(get_legal_field_values);
use Bugzilla::Template;
use Bugzilla::Util qw(detaint_natural trick_taint);
use Bugzilla::User::Setting qw(add_setting);
use Bugzilla::Extension::BugViewPlus::Template;
use Bugzilla::Extension::BugViewPlus::Util qw(prefix_shortdesc);
use JSON;
our $VERSION = '0.01';
sub install_update_db {
my ($self, $args) = @_;
my $old_group = Bugzilla->dbh->selectrow_hashref(
"SELECT name, isactive, isbuggroup FROM groups ".
"WHERE name = ?", undef, 'bvp_edit_description');
if (defined $old_group &&
!$old_group->{isbuggroup} && $old_group->{isactive}) {
# Fix earlier installations group type
Bugzilla->dbh->do(
"UPDATE groups SET isbuggroup = 1, isactive = 0 WHERE id = ?",
undef, $old_group->id);
}
}
sub install_before_final_checks {
add_setting('bvp_hide_advanced', ['remember', 'on', 'off'], 'remember');
}
sub config_add_panels {
my ($self, $args) = @_;
my $modules = $args->{panel_modules};
$modules->{BugViewPlus} = "Bugzilla::Extension::BugViewPlus::Params";
}
sub bb_group_params {
my ($self, $args) = @_;
push(@{$args->{group_params}}, 'bvp_description_edit_group');
}
sub bug_end_of_create {
my ($self, $args) = @_;
my $bug = $args->{bug};
prefix_shortdesc($bug);
}
sub bug_start_of_update {
my ($self, $args) = @_;
my ($bug, $changes) = @$args{qw(bug changes)};
prefix_shortdesc($bug, $changes);
}
sub bug_end_of_update {
my ($self, $args) = @_;
my $cgi = Bugzilla->cgi;
my $dbh = Bugzilla->dbh;
my ($bug, $timestamp) = @$args{qw(bug timestamp)};
# Get the bug id passed to processbug.cgi
my $bug_id = $cgi->param('id');
return unless $bug_id;
return unless detaint_natural($bug_id);
# Make sure we are udating the same bug and not some related bug
if ($bug->bug_id == $bug_id) {
# Edit description related stuff
my $group = Bugzilla->params->{'bvp_description_edit_group'};
my $type_editable = grep {$_ eq $bug->bug_severity}
@{Bugzilla->params->{bvp_description_editable_types}};
if ($type_editable && Bugzilla->user->in_group($group)) {
# Current description text
my $old_desc = ${ @{ $bug->comments }[0] }{'thetext'};
# Current descriptions comment id (in longdescs table)
my $comment_id = ${ @{ $bug->comments }[0] }{'comment_id'};
# The new description
my $new_desc = $cgi->param('bvp_description');
if ($new_desc) {
trick_taint($new_desc);
# Remove trailing whitespace
$new_desc =~ s/\s*$//s;
$new_desc =~ s/\r\n?/\n/g; # Get rid of \r.
if ($old_desc ne $new_desc) {
# Add or update the timestamp in description
my $stamp = "> Edited by ".Bugzilla->user->name." on ".$timestamp;
if (! ($new_desc =~ s/> Edited by .* on .*$/$stamp/)) {
$new_desc .= "\n\n".$stamp;
}
# Push to database
$dbh->do('UPDATE longdescs SET thetext = ? '.
'WHERE bug_id = ? AND comment_id = ?',
undef, $new_desc, $bug->bug_id, $comment_id);
}
}
}
}
}
sub bug_format_comment {
my ($self, $args) = @_;
return unless Bugzilla->params->{bvp_linkify_severity};
my $regexes = $args->{regexes};
# Turn '<severity> #' into bug link in comments
if (!defined $self->{bug_severities}) {
$self->{bug_severities} = get_legal_field_values('bug_severity');
}
foreach my $value (@{$self->{bug_severities}}) {
push (@$regexes, { match => qr/($value)\s*(\d+)/i,
replace => \&_replace_bug_link } );
}
}
sub db_schema_abstract_schema {
my ($self, $args) = @_;
my $schema = $args->{schema};
# Table for storing the bug templates
$schema->{bvp_templates} = {
FIELDS => [
id => {TYPE => 'MEDIUMSERIAL', NOTNULL => 1, PRIMARYKEY => 1},
name => {TYPE => 'TINYTEXT', NOTNULL => 1},
is_active => {TYPE => 'BOOLEAN', NOTNULL => 1, DEFAULT => 0},
description => {TYPE => 'TINYTEXT'},
content => {TYPE => 'MEDIUMTEXT'},
],
INDEXES => [],
};
}
sub object_end_of_update {
my ($self, $args) = @_;
my ($new_obj, $old_obj, $changes) = @$args{qw(object old_object changes)};
# Update user group param if group name changes
if ($new_obj->isa("Bugzilla::Group") && defined $changes->{name}) {
if ($old_obj->name eq Bugzilla->params->{bvp_description_edit_group}) {
SetParam('bvp_description_edit_group', $new_obj->name);
write_params();
}
}
}
sub page_before_template {
my ($self, $args) = @_;
return unless ($args->{page_id} eq 'bvp_template.html');
ThrowUserError('auth_failure', {
group => 'admin', action => 'access',
object => 'administrative_pages'
}) unless Bugzilla->user->in_group('admin');;
my $vars = $args->{vars};
my $cgi = Bugzilla->cgi;
my $tid = $cgi->param('tid');
my $action = $cgi->param('action') || '';
my $current;
if (defined $tid) {
$current = Bugzilla::Extension::BugViewPlus::Template->check({id=>$tid});
}
if ($action) {
my $values = {
name => scalar $cgi->param('name'),
is_active => scalar $cgi->param('is_active'),
description => scalar $cgi->param('description'),
content => scalar $cgi->param('content'),
};
if ($action eq 'create') {
$current = Bugzilla::Extension::BugViewPlus::Template->create($values);
$vars->{message} = 'bvp_template_created';
} elsif ($action eq 'save') {
ThrowCodeError('param_required', {param => 'tid', function=>'save'})
unless defined $current;
$current->set_all($values);
$current->update();
$vars->{message} = 'bvp_template_saved';
} elsif ($action eq 'remove') {
ThrowCodeError('param_required', {param => 'tid', function=>'remove'})
unless defined $current;
$current->remove_from_db();
$vars->{message} = 'bvp_template_removed';
$vars->{name} = $current->name;
$current = undef;
} else {
ThrowCodeError('param_invalid',
{param => $action, function=>'action'});
}
}
$vars->{current} = $current;
$vars->{templates} = [Bugzilla::Extension::BugViewPlus::Template->get_all()];
}
sub template_before_process {
my ($self, $args) = @_;
if ($args->{file} eq 'bug/comments.html.tmpl') {
my $group = Bugzilla->params->{'bvp_description_edit_group'};
my $severity = $args->{vars}->{bug}->bug_severity;
my $type_editable = grep {$_ eq $severity}
@{Bugzilla->params->{bvp_description_editable_types}};
$args->{vars}->{can_edit_description} =
$type_editable && Bugzilla->user->in_group($group) ? 1 : 0;
} elsif ($args->{file} eq 'bug/create/create.html.tmpl' ||
$args->{file} eq 'pages/quickideas/enter.html.tmpl') {
$args->{vars}->{bvp_templates_json} =
JSON->new->utf8->allow_blessed->convert_blessed->encode(
Bugzilla::Extension::BugViewPlus::Template->match(
{is_active => 1}));
}
}
sub _replace_bug_link {
my $args = shift;
my $type = $args->{matches}->[0];
my $id = $args->{matches}->[1];
return Bugzilla::Template::get_bug_link($id, $type." ".$id);
}
__PACKAGE__->NAME;