Upgrade to 4.0.10.
[usit-rt.git] / lib / RT / Shredder / Plugin / Summary.pm
CommitLineData
84fb5b46
MKG
1# BEGIN BPS TAGGED BLOCK {{{
2#
3# COPYRIGHT:
4#
403d7b0b 5# This software is Copyright (c) 1996-2013 Best Practical Solutions, LLC
84fb5b46
MKG
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
49package RT::Shredder::Plugin::Summary;
50
51use strict;
52use warnings FATAL => 'all';
53
54use base qw(RT::Shredder::Plugin::SQLDump);
55
56sub AppliesToStates { return 'before any action' }
57
58sub TestArgs
59{
60 my $self = shift;
61 my %args = (file_name => '', @_);
62 unless( $args{'file_name'} ) {
63 require POSIX;
64 $args{'file_name'} = POSIX::strftime( "summary-%Y%m%dT%H%M%S.XXXX.txt", gmtime );
65 }
66 return $self->SUPER::TestArgs( %args );
67}
68
69sub Run
70{
71 my $self = shift;
72 my %args = ( Object => undef, @_ );
73 my $class = ref $args{'Object'};
74 $class =~ s/^RT:://;
75 $class =~ s/:://g;
76 my $method = 'WriteDown'. $class;
77 $method = 'WriteDownDefault' unless $self->can($method);
78 return $self->$method( %args );
79 return 1;
80}
81
82my %skip_refs_to = ();
83
84sub WriteDownDefault {
85 my $self = shift;
86 my %args = ( Object => undef, @_ );
87 return $self->_WriteDownHash(
88 $args{'Object'},
89 $self->_MakeHash( $args{'Object'} ),
90 );
91}
92
93# TODO: cover other objects
94# ACE.pm
95# Attachment.pm
96# CustomField.pm
97# CustomFieldValue.pm
98# GroupMember.pm
99# Group.pm
100# Link.pm
101# ObjectCustomFieldValue.pm
102# Principal.pm
103# Queue.pm
104# Ticket.pm
105# User.pm
106
107# ScripAction.pm - works fine with defaults
108# ScripCondition.pm - works fine with defaults
109# Template.pm - works fine with defaults
110
111sub WriteDownCachedGroupMember { return 1 }
112sub WriteDownPrincipal { return 1 }
113
114sub WriteDownGroup {
115 my $self = shift;
116 my %args = ( Object => undef, @_ );
117 if ( $args{'Object'}->Domain =~ /-Role$/ ) {
118 return $skip_refs_to{ $args{'Object'}->_AsString } = 1;
119 }
120 return $self->WriteDownDefault( %args );
121}
122
123sub WriteDownTransaction {
124 my $self = shift;
125 my %args = ( Object => undef, @_ );
126
127 my $props = $self->_MakeHash( $args{'Object'} );
128 $props->{'Object'} = delete $props->{'ObjectType'};
129 $props->{'Object'} .= '-'. delete $props->{'ObjectId'}
130 if $props->{'ObjectId'};
131 return 1 if $skip_refs_to{ $props->{'Object'} };
132
133 delete $props->{$_} foreach grep
134 !defined $props->{$_} || $props->{$_} eq '', keys %$props;
135
136 return $self->_WriteDownHash( $args{'Object'}, $props );
137}
138
139sub WriteDownScrip {
140 my $self = shift;
141 my %args = ( Object => undef, @_ );
142 my $props = $self->_MakeHash( $args{'Object'} );
143 $props->{'Action'} = $args{'Object'}->ActionObj->Name;
144 $props->{'Condition'} = $args{'Object'}->ConditionObj->Name;
145 $props->{'Template'} = $args{'Object'}->TemplateObj->Name;
146 $props->{'Queue'} = $args{'Object'}->QueueObj->Name || 'global';
147
148 return $self->_WriteDownHash( $args{'Object'}, $props );
149}
150
151sub _MakeHash {
152 my ($self, $obj) = @_;
153 my $hash = $self->__MakeHash( $obj );
154 foreach (grep exists $hash->{$_}, qw(Creator LastUpdatedBy)) {
155 my $method = $_ .'Obj';
156 my $u = $obj->$method();
157 $hash->{ $_ } = $u->EmailAddress || $u->Name || $u->_AsString;
158 }
159 return $hash;
160}
161
162sub __MakeHash {
163 my ($self, $obj) = @_;
164 my %hash;
165 $hash{ $_ } = $obj->$_()
166 foreach sort keys %{ $obj->_ClassAccessible };
167 return \%hash;
168}
169
170sub _WriteDownHash {
171 my ($self, $obj, $hash) = @_;
172 return (0, 'no handle') unless my $fh = $self->{'opt'}{'file_handle'};
173
174 print $fh "=== ". $obj->_AsString ." ===\n"
175 or return (0, "Couldn't write to filehandle");
176
177 foreach my $key( sort keys %$hash ) {
178 my $val = $hash->{ $key };
179 next unless defined $val;
180 $val =~ s/\n/\n /g;
181 print $fh $key .': '. $val ."\n"
182 or return (0, "Couldn't write to filehandle");
183 }
184 print $fh "\n" or return (0, "Couldn't write to filehandle");
185 return 1;
186}
187
1881;