-
Notifications
You must be signed in to change notification settings - Fork 0
/
Extension.pm
241 lines (213 loc) · 7.79 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
241
# -*- 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/.
#
# The Original Code is the Bugzilla Objective Watchdog Bugzilla Extension.
#
# The Initial Developer of the Original Code is "Nokia Corporation"
# Portions created by the Initial Developer are Copyright (C) 2011 the
# Initial Developer. All Rights Reserved.
#
# Contributor(s):
# Eero Heino <[email protected]>
# Pami Ketolainen <[email protected]>
package Bugzilla::Extension::ChangeLog;
use strict;
use base qw(Bugzilla::Extension);
use Bugzilla::Config qw(SetParam write_params);
use Bugzilla::Error qw(ThrowCodeError ThrowUserError);
use Bugzilla::Util qw(datetime_from);
use Bugzilla::Extension::ChangeLog::Query;
use DateTime;
our $VERSION = '0.2';
sub config {
my ($self, $args) = @_;
my $config = $args->{config};
$config->{ChangeLog} = "Bugzilla::Extension::ChangeLog::Config";
}
sub config_add_panels {
my ($self, $args) = @_;
my $modules = $args->{panel_modules};
$modules->{ChangeLog} = "Bugzilla::Extension::ChangeLog::Config";
}
sub bb_common_links {
my ($self, $args) = @_;
return unless _has_access();
$args->{links}->{ChangeLog} = [
{
text => 'ChangeLog',
href => 'page.cgi?id=ChangeLog.html'
}
];
}
sub bb_group_params {
my ($self, $args) = @_;
push(@{$args->{group_params}}, 'changelog_access_groups');
}
sub db_schema_abstract_schema {
my ($self, $args) = @_;
my $schema = $args->{schema};
# Table for storing the changelog queries
$schema->{changelog_queries} = {
FIELDS => [
id => {TYPE => 'MEDIUMSERIAL', NOTNULL => 1, PRIMARYKEY => 1},
name => {TYPE => 'TINYTEXT', NOTNULL => 1},
statement => {TYPE => 'MEDIUMTEXT'},
is_active => {TYPE => 'BOOLEAN', NOTNULL => 1, DEFAULT => 0},
sort_order => {TYPE => 'INT2', NOTNULL => 1, DEFAULT => 0},
],
INDEXES => [],
};
}
sub install_update_db {
my $queries_param = Bugzilla->params->{changelog_queries};
if ($queries_param ne 'OBSOLETE') {
print "Migrating ChangeLog queries to DB...\n";
for my $line (split(/\n/, $queries_param)) {
# skip empty lines
next if ($line =~ /^\s*$/);
if ($line =~ /"(.+)"\s+"(.+)"/) {
Bugzilla::Extension::ChangeLog::Query->create({
name => $1,
statement => $2,
});
} else {
print "Skipping bad query line: $line\n";
}
}
SetParam('changelog_queries', "OBSOLETE");
write_params();
}
}
sub object_end_of_update {
my ($self, $args) = @_;
my ($obj, $old_obj, $changes) = @$args{qw(object old_object changes)};
# Update params if group names change
if ($obj->isa("Bugzilla::Group") && defined $changes->{name}) {
my @new_names;
my $changed = 0;
for my $old_name (@{Bugzilla->params->{"changelog_access_groups"}}) {
if ($old_name && $old_name eq $old_obj->name) {
push(@new_names, $obj->name);
$changed = 1;
} else {
push(@new_names, $old_name);
}
}
if ($changed) {
SetParam('changelog_access_groups', \@new_names);
write_params();
}
}
}
sub page_before_template {
my ($self, $args) = @_;
my ($vars, $page) = @$args{qw(vars page_id)};
return unless ($page =~ /^ChangeLog/);
_has_access(1);
my $cgi = Bugzilla->cgi;
my $from_date = $cgi->param('from_date');
$from_date ||= '';
$vars->{from_date} = $from_date;
my $now = DateTime->now();
if ($from_date =~ /^-([\d]+)([hdwm])/i) {
my $value = $1;
my $unit = lc($2);
if ($unit eq 'h') {
$from_date = $now->subtract(hours => $value);
} elsif ($unit eq 'd') {
$from_date = $now->subtract(days => $value);
} elsif ($unit eq 'w') {
$from_date = $now->subtract(weeks => $value);
} elsif ($unit eq 'm') {
$from_date = $now->subtract(months => $value);
}
} elsif ( $from_date =~ /^\d{4}-\d{2}-\d{2}$/ ) {
$from_date = datetime_from($from_date, Bugzilla->user->timezone)
} else {
$from_date = $now->subtract(days => 1);
$vars->{from_date} = $from_date->ymd;
}
$from_date->set_time_zone(Bugzilla->local_timezone);
my $qid = $cgi->param('qid');
if ($page eq 'ChangeLog.html') {
$vars->{'queries'} = Bugzilla::Extension::ChangeLog::Query->match(
{is_active => 1});
}
if ($page eq 'ChangeLogTable.html' || $page eq 'ChangeLogTable.csv') {
ThrowCodeError('param_required', { param => 'qid' }) unless $qid;
my $query = Bugzilla::Extension::ChangeLog::Query->check({id => $qid});
$vars->{query} = $query;
my $result = $query->execute({from_date => $from_date});
$vars->{headers} = $result->{columns};
$vars->{table} = $result->{data};
if ($page eq 'ChangeLogTable.csv') {
$vars->{human} = $cgi->param('human');
my $filename = "changelog-".$query->name;
$filename .= "-" . $from_date->ymd;
$filename .= ".csv";
$filename = lc($filename);
print $cgi->header(
-content_type => "text/csv",
-content_disposition => "attachment; filename=$filename"
);
}
}
if ($page eq 'ChangeLogQuery.html') {
ThrowUserError('auth_failure', {
group => 'admin', action => 'access',
object => 'administrative_pages'
}) unless Bugzilla->user->in_group('admin');;
my $action = $cgi->param('action') || '';
my $current;
if (defined $qid) {
$current = Bugzilla::Extension::ChangeLog::Query->check({id=>$qid});
}
if ($action) {
my $values = {
name => scalar $cgi->param('name'),
statement => scalar $cgi->param('statement'),
is_active => scalar $cgi->param('is_active') ? 1 : 0,
sort_order => scalar $cgi->param('sort_order'),
};
if ($action eq 'create') {
$current = Bugzilla::Extension::ChangeLog::Query->create($values);
$vars->{message} = 'changelog_query_created';
} elsif ($action eq 'save') {
ThrowCodeError('param_required', {param => 'qid', function=>'save'})
unless defined $current;
$current->set_all($values);
$current->update();
$vars->{message} = 'changelog_query_saved';
} elsif ($action eq 'remove') {
ThrowCodeError('param_required', {param => 'cid', function=>'remove'})
unless defined $current;
$current->remove_from_db();
$vars->{message} = 'changelog_query_removed';
$vars->{name} = $current->name;
$current = undef;
} else {
ThrowCodeError('param_invalid',
{param => $action, function=>'action'});
}
}
$vars->{current} = $current;
$vars->{queries} = Bugzilla::Extension::ChangeLog::Query->match();
}
}
sub _has_access {
my $throwerror = shift;
my $names = Bugzilla->params->{"changelog_access_groups"};
foreach my $name (@$names) {
return 1 if Bugzilla->user->in_group($name);
}
if ($throwerror) {
ThrowUserError('auth_failure', {
action => 'access', reason => 'not_visible'
});
}
return 1;
}
__PACKAGE__->NAME;