summaryrefslogtreecommitdiffstats
path: root/contrib/reorg-tools/fix_all_open_status_queries.pl
blob: 7c8d8be68ef127a7371015bdce41114602ab96db (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
#!/usr/bin/perl -w
# 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.

use strict;

use lib qw(. lib);

use Bugzilla;
use Bugzilla::Constants;
use Bugzilla::Status;
use Bugzilla::Util;

sub usage() {
  print <<USAGE;
Usage: fix_all_open_status_queries.pl <new_open_status>

E.g.: fix_all_open_status_queries.pl READY
This will add a new open state to user queries which currently look for
all open bugs by listing every open status in their query criteria.
For users who only look for bug_status=__open__, they will get the new
open status automatically.
USAGE
}

sub do_namedqueries {
    my ($new_status) = @_;
    my $dbh = Bugzilla->dbh;
    my $replace_count = 0;

    my $query = $dbh->selectall_arrayref("SELECT id, query FROM namedqueries");

    if ($query) {
        $dbh->bz_start_transaction();

        my $sth = $dbh->prepare("UPDATE namedqueries SET query = ? WHERE id = ?");

        foreach my $row (@$query) {
            my ($id, $old_query) = @$row;
            my $new_query = all_open_states($new_status, $old_query);
            if ($new_query) {
                trick_taint($new_query);
                $sth->execute($new_query, $id);
                $replace_count++;
            }
        }

        $dbh->bz_commit_transaction();
    }

    print "namedqueries: $replace_count replacements made.\n";
}

# series
sub do_series {
    my ($new_status) = @_;
    my $dbh = Bugzilla->dbh;
    my $replace_count = 0;

    my $query = $dbh->selectall_arrayref("SELECT series_id, query FROM series");

    if ($query) {
        $dbh->bz_start_transaction();

        my $sth = $dbh->prepare("UPDATE series SET query = ? WHERE series_id = ?");

        foreach my $row (@$query) {
            my ($series_id, $old_query) = @$row;
            my $new_query = all_open_states($new_status, $old_query);
            if ($new_query) {
                trick_taint($new_query);
                $sth->execute($new_query, $series_id);
                $replace_count++;
            }
        }

        $dbh->bz_commit_transaction();
    }

    print "series: $replace_count replacements made.\n";
}

sub all_open_states {
    my ($new_status, $query) = @_;

    my @open_states = Bugzilla::Status::BUG_STATE_OPEN();
    my $cgi = Bugzilla::CGI->new($query);
    my @query_states = $cgi->param('bug_status');

    my ($removed, $added) = diff_arrays(\@query_states, \@open_states);

    if (scalar @$added == 1 && $added->[0] eq $new_status) {
        push(@query_states, $new_status);
        $cgi->param('bug_status', @query_states);
        return $cgi->canonicalise_query();
    }

    return '';
}

sub validate_status {
    my ($status) = @_;
    my $dbh = Bugzilla->dbh;
    my $exists = $dbh->selectrow_array("SELECT 1 FROM bug_status 
                                        WHERE value = ?",
                                       undef, $status);
    return $exists ? 1 : 0;
}

#############################################################################
# MAIN CODE
#############################################################################
# This is a pure command line script.
Bugzilla->usage_mode(USAGE_MODE_CMDLINE);

if (scalar @ARGV < 1) {
    usage();
    exit(1);
}

my ($new_status) = @ARGV;

$new_status = uc($new_status);

if (!validate_status($new_status)) {
    print "Invalid status: $new_status\n\n";
    usage();
    exit(1);
}

print "Adding new status '$new_status'.\n\n";

do_namedqueries($new_status);
do_series($new_status);

# It's complex to determine which items now need to be flushed from memcached.
# As this is expected to be a rare event, we just flush the entire cache.
Bugzilla->memcached->clear_all();

exit(0);