rt 4.2.14 (#13852)
[freeside.git] / rt / lib / RT / Shredder / Plugin / Tickets.pm
1 # BEGIN BPS TAGGED BLOCK {{{
2 #
3 # COPYRIGHT:
4 #
5 # This software is Copyright (c) 1996-2017 Best Practical Solutions, LLC
6 #                                          <sales@bestpractical.com>
7 #
8 # (Except where explicitly superseded by other copyright notices)
9 #
10 #
11 # LICENSE:
12 #
13 # This work is made available to you under the terms of Version 2 of
14 # the GNU General Public License. A copy of that license should have
15 # been provided with this software, but in any event can be snarfed
16 # from www.gnu.org.
17 #
18 # This work is distributed in the hope that it will be useful, but
19 # WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 # General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 # 02110-1301 or visit their web page on the internet at
27 # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
28 #
29 #
30 # CONTRIBUTION SUBMISSION POLICY:
31 #
32 # (The following paragraph is not intended to limit the rights granted
33 # to you to modify and distribute this software under the terms of
34 # the GNU General Public License and is only of importance to you if
35 # you choose to contribute your changes and enhancements to the
36 # community by submitting them to Best Practical Solutions, LLC.)
37 #
38 # By intentionally submitting any modifications, corrections or
39 # derivatives to this work, or any other work intended for use with
40 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
41 # you are the copyright holder for those contributions and you grant
42 # Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
43 # royalty-free, perpetual, license to use, copy, create derivative
44 # works based on those contributions, and sublicense and distribute
45 # those contributions and any derivatives thereof.
46 #
47 # END BPS TAGGED BLOCK }}}
48
49 package RT::Shredder::Plugin::Tickets;
50
51 use strict;
52 use warnings FATAL => 'all';
53 use base qw(RT::Shredder::Plugin::Base::Search);
54
55 =head1 NAME
56
57 RT::Shredder::Plugin::Tickets - search plugin for wiping tickets.
58
59 =head1 ARGUMENTS
60
61 =head2 query - query string
62
63 Search tickets with query string.
64 Examples:
65   Queue = 'my queue' AND ( Status = 'deleted' OR Status = 'rejected' )
66   LastUpdated < '2003-12-31 23:59:59'
67
68 B<Hint:> You can construct query with the query builder in RT's web
69 interface and then open advanced page and copy query string.
70
71 Arguments C<queue>, C<status> and C<updated_before> have been dropped
72 as you can easy make the same search with the C<query> option.
73 See examples above.
74
75 =head2 with_linked - boolen
76
77 Deletes all tickets that are linked to tickets that match L<query>.
78
79 =head2 apply_query_to_linked - boolean
80
81 Delete linked tickets only if those too match L<query>.
82
83 See also L<with_linked>.
84
85 =cut
86
87 sub SupportArgs { return $_[0]->SUPER::SupportArgs, qw(query with_linked apply_query_to_linked) }
88
89 sub TestArgs
90 {
91     my $self = shift;
92     my %args = @_;
93     my $queue;
94     if( $args{'query'} ) {
95         my $objs = RT::Tickets->new( RT->SystemUser );
96         $objs->{'allow_deleted_search'} = 1;
97         my ($status, $msg) = $objs->FromSQL( $args{'query'} );
98         return( 0, "Bad query argument, error: $msg" ) unless $status;
99         $self->{'opt'}{'objects'} = $objs;
100     }
101     $args{'with_linked'} = 1 if $args{'apply_query_to_linked'};
102
103     return $self->SUPER::TestArgs( %args );
104 }
105
106 sub Run
107 {
108     my $self = shift;
109     my $objs = $self->{'opt'}{'objects'}
110         or return (1, undef);
111
112     $objs->OrderByCols( { FIELD => 'id', ORDER => 'ASC' } );
113     unless ( $self->{'opt'}{'with_linked'} ) {
114         if( $self->{'opt'}{'limit'} ) {
115             $objs->RowsPerPage( $self->{'opt'}{'limit'} );
116         }
117         return (1, $objs);
118     }
119
120     my (@top, @linked, %seen);
121     $self->FetchNext($objs, 1);
122     while ( my $obj = $self->FetchNext( $objs ) ) {
123         next if $seen{ $obj->id }++;
124         push @linked, $self->GetLinked( Object => $obj, Seen => \%seen );
125         push @top, $obj;
126         last if $self->{'opt'}{'limit'}
127                 && @top >= $self->{'opt'}{'limit'};
128     }
129     return (1, @top, @linked);
130 }
131
132 sub GetLinked
133 {
134     my $self = shift;
135     my %arg = @_;
136     my @res = ();
137     my $query = 'Linked = '. $arg{'Object'}->id;
138     if ( $self->{'opt'}{'apply_query_to_linked'} ) {
139         $query .= " AND ( ". $self->{'opt'}{'query'} ." )";
140     }
141     my $objs = RT::Tickets->new( RT->SystemUser );
142     $objs->{'allow_deleted_search'} = 1;
143     $objs->FromSQL( $query );
144     $self->FetchNext( $objs, 1 );
145     while ( my $linked_obj = $self->FetchNext( $objs ) ) {
146         next if $arg{'Seen'}->{ $linked_obj->id }++;
147         push @res, $self->GetLinked( %arg, Object => $linked_obj );
148         push @res, $linked_obj;
149     }
150     return @res;
151 }
152
153 1;