Master to 4.2.8
[usit-rt.git] / lib / RT / Shredder / Plugin.pm
CommitLineData
84fb5b46
MKG
1# BEGIN BPS TAGGED BLOCK {{{
2#
3# COPYRIGHT:
4#
320f0092 5# This software is Copyright (c) 1996-2014 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;
50
51use strict;
52use warnings FATAL => 'all';
53use File::Spec ();
54
55=head1 NAME
56
57RT::Shredder::Plugin - interface to access shredder plugins
58
59=head1 SYNOPSIS
60
61 use RT::Shredder::Plugin;
62
63 # get list of the plugins
64 my %plugins = RT::Shredder::Plugin->List;
65
66 # load plugin by name
67 my $plugin = RT::Shredder::Plugin->new;
68 my( $status, $msg ) = $plugin->LoadByName( 'Tickets' );
69 unless( $status ) {
70 print STDERR "Couldn't load plugin 'Tickets': $msg\n";
71 exit(1);
72 }
73
74 # load plugin by preformatted string
75 my $plugin = RT::Shredder::Plugin->new;
76 my( $status, $msg ) = $plugin->LoadByString( 'Tickets=status,deleted' );
77 unless( $status ) {
78 print STDERR "Couldn't load plugin: $msg\n";
79 exit(1);
80 }
81
82=head1 METHODS
83
84=head2 new
85
86Object constructor, returns new object. Takes optional hash
87as arguments, it's not required and this class doesn't use it,
88but plugins could define some arguments and can handle them
89after your've load it.
90
91=cut
92
93sub new
94{
95 my $proto = shift;
96 my $self = bless( {}, ref $proto || $proto );
97 $self->_Init( @_ );
98 return $self;
99}
100
101sub _Init
102{
103 my $self = shift;
104 my %args = ( @_ );
105 $self->{'opt'} = \%args;
c33a4027 106 return;
84fb5b46
MKG
107}
108
109=head2 List
110
111Returns hash with names of the available plugins as keys and path to
112library files as values. Method has no arguments. Can be used as class
113method too.
114
115Takes optional argument C<type> and leaves in the result hash only
116plugins of that type.
117
118=cut
119
120sub List
121{
122 my $self = shift;
123 my $type = shift;
124
125 my @files;
126 foreach my $root( @INC ) {
127 my $mask = File::Spec->catfile( $root, qw(RT Shredder Plugin *.pm) );
128 push @files, glob $mask;
129 }
130
131 my %res;
132 for my $f (reverse @files) {
133 $res{$1} = $f if $f =~ /([^\\\/]+)\.pm$/;
134 }
135
136 return %res unless $type;
137
138 delete $res{'Base'};
139 foreach my $name( keys %res ) {
140 my $class = join '::', qw(RT Shredder Plugin), $name;
c33a4027 141 unless( $class->require ) {
84fb5b46
MKG
142 delete $res{ $name };
143 next;
144 }
145 next if lc $class->Type eq lc $type;
146 delete $res{ $name };
147 }
148
149 return %res;
150}
151
152=head2 LoadByName
153
154Takes name of the plugin as first argument, loads plugin,
155creates new plugin object and reblesses self into plugin
156if all steps were successfuly finished, then you don't need to
157create new object for the plugin.
158
159Other arguments are sent to the constructor of the plugin
160(method new.)
161
162Returns C<$status> and C<$message>. On errors status
163is C<false> value.
164
af59614d
MKG
165In scalar context, returns $status only.
166
84fb5b46
MKG
167=cut
168
169sub LoadByName
170{
171 my $self = shift;
172 my $name = shift or return (0, "Name not specified");
173 $name =~ /^\w+(::\w+)*$/ or return (0, "Invalid plugin name");
174
84fb5b46 175 my $plugin = "RT::Shredder::Plugin::$name";
c33a4027 176 $plugin->require or return( 0, "Failed to load $plugin" );
af59614d 177 return wantarray ? ( 0, "Plugin '$plugin' has no method new") : 0 unless $plugin->can('new');
84fb5b46
MKG
178
179 my $obj = eval { $plugin->new( @_ ) };
af59614d
MKG
180 return wantarray ? ( 0, $@ ) : 0 if $@;
181 return wantarray ? ( 0, 'constructor returned empty object' ) : 0 unless $obj;
84fb5b46
MKG
182
183 $self->Rebless( $obj );
af59614d 184 return wantarray ? ( 1, "successfuly load plugin" ) : 1;
84fb5b46
MKG
185}
186
187=head2 LoadByString
188
189Takes formatted string as first argument and which is used to
190load plugin. The format of the string is
191
192 <plugin name>[=<arg>,<val>[;<arg>,<val>]...]
193
194exactly like in the L<rt-shredder> script. All other
195arguments are sent to the plugins constructor.
196
197Method does the same things as C<LoadByName>, but also
198checks if the plugin supports arguments and values are correct,
199so you can C<Run> specified plugin immediatly.
200
201Returns list with C<$status> and C<$message>. On errors status
202is C<false>.
203
204=cut
205
206sub LoadByString
207{
208 my $self = shift;
209 my ($plugin, $args) = split /=/, ( shift || '' ), 2;
210
211 my ($status, $msg) = $self->LoadByName( $plugin, @_ );
212 return( $status, $msg ) unless $status;
213
214 my %args;
215 foreach( split /\s*;\s*/, ( $args || '' ) ) {
216 my( $k,$v ) = split /\s*,\s*/, ( $_ || '' ), 2;
217 unless( $args{$k} ) {
218 $args{$k} = $v;
219 next;
220 }
221
222 $args{$k} = [ $args{$k} ] unless UNIVERSAL::isa( $args{ $k }, 'ARRAY');
223 push @{ $args{$k} }, $v;
224 }
225
226 ($status, $msg) = $self->HasSupportForArgs( keys %args );
227 return( $status, $msg ) unless $status;
228
229 ($status, $msg) = $self->TestArgs( %args );
230 return( $status, $msg ) unless $status;
231
232 return( 1, "successfuly load plugin" );
233}
234
235=head2 Rebless
236
237Instance method that takes one object as argument and rebless
238the current object into into class of the argument and copy data
239of the former. Returns nothing.
240
241Method is used by C<Load*> methods to automaticaly rebless
242C<RT::Shredder::Plugin> object into class of the loaded
243plugin.
244
245=cut
246
247sub Rebless
248{
249 my( $self, $obj ) = @_;
250 bless( $self, ref $obj );
251 %{$self} = %{$obj};
252 return;
253}
254
2551;