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