rt 4.0.23
[freeside.git] / rt / sbin / rt-dump-metadata.in
1 #!@PERL@ -w
2 # BEGIN BPS TAGGED BLOCK {{{
3 #
4 # COPYRIGHT:
5 #
6 # This software is Copyright (c) 1996-2015 Best Practical Solutions, LLC
7 #                                          <sales@bestpractical.com>
8 #
9 # (Except where explicitly superseded by other copyright notices)
10 #
11 #
12 # LICENSE:
13 #
14 # This work is made available to you under the terms of Version 2 of
15 # the GNU General Public License. A copy of that license should have
16 # been provided with this software, but in any event can be snarfed
17 # from www.gnu.org.
18 #
19 # This work is distributed in the hope that it will be useful, but
20 # WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22 # General Public License for more details.
23 #
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the Free Software
26 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27 # 02110-1301 or visit their web page on the internet at
28 # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
29 #
30 #
31 # CONTRIBUTION SUBMISSION POLICY:
32 #
33 # (The following paragraph is not intended to limit the rights granted
34 # to you to modify and distribute this software under the terms of
35 # the GNU General Public License and is only of importance to you if
36 # you choose to contribute your changes and enhancements to the
37 # community by submitting them to Best Practical Solutions, LLC.)
38 #
39 # By intentionally submitting any modifications, corrections or
40 # derivatives to this work, or any other work intended for use with
41 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
42 # you are the copyright holder for those contributions and you grant
43 # Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
44 # royalty-free, perpetual, license to use, copy, create derivative
45 # works based on those contributions, and sublicense and distribute
46 # those contributions and any derivatives thereof.
47 #
48 # END BPS TAGGED BLOCK }}}
49 use strict;
50 use warnings;
51
52 # As we specify that XML is UTF-8 and we output it to STDOUT, we must be sure
53 # it is UTF-8 so further XMLin will not break
54 binmode( STDOUT, ":utf8" );
55
56 # fix lib paths, some may be relative
57 BEGIN {
58     require File::Spec;
59     my @libs = ( "@RT_LIB_PATH@", "@LOCAL_LIB_PATH@" );
60     my $bin_path;
61
62     for my $lib (@libs) {
63         unless ( File::Spec->file_name_is_absolute($lib) ) {
64             unless ($bin_path) {
65                 if ( File::Spec->file_name_is_absolute(__FILE__) ) {
66                     $bin_path = ( File::Spec->splitpath(__FILE__) )[1];
67                 } else {
68                     require FindBin;
69                     no warnings "once";
70                     $bin_path = $FindBin::Bin;
71                 }
72             }
73             $lib = File::Spec->catfile( $bin_path, File::Spec->updir, $lib );
74         }
75         unshift @INC, $lib;
76     }
77
78 }
79
80 use Getopt::Long;
81 my %opt;
82 GetOptions( \%opt, "help|h",
83     "limit-to-privileged|l",
84     "skip-disabled|s",
85     "all|a",
86 );
87
88 if ( $opt{help} ) {
89     require Pod::Usage;
90     Pod::Usage::pod2usage( { verbose => 2 } );
91     exit;
92 }
93
94 require RT;
95 require XML::Simple;
96
97 RT::LoadConfig();
98 RT::Init();
99
100 my %RV;
101 my %Ignore = (
102     All => [
103         qw(
104             id Created Creator LastUpdated LastUpdatedBy
105             )
106            ],
107     Templates => [
108         qw(
109             TranslationOf
110             )
111     ],
112 );
113
114 my $SystemUserId = RT->SystemUser->Id;
115 my @classes      = qw(
116     Users Groups Queues ScripActions ScripConditions
117     Templates Scrips ACL CustomFields
118     );
119 foreach my $class (@classes) {
120     require "RT/$class.pm";
121     my $objects = "RT::$class"->new( RT->SystemUser );
122     $objects->{find_disabled_rows} = 1 unless $opt{'skip-disabled'};
123     $objects->UnLimit;
124     $objects->LimitToPrivileged if $class eq 'Users'
125         && $opt{'limit-to-privileged'};
126     $objects->Limit(
127         FIELD    => 'Domain',
128         OPERATOR => '=',
129         VALUE    => 'UserDefined'
130     ) if $class eq 'Groups';
131
132     if ( $class eq 'CustomFields' ) {
133         $objects->OrderByCols(
134             { FIELD => 'LookupType' },
135             { FIELD => 'SortOrder' },
136             { FIELD => 'Id' },
137         );
138     } else {
139         $objects->OrderBy( FIELD => 'Id' );
140     }
141
142     unless ($opt{all}) {
143         next if $class eq 'ACL';    # XXX - would go into infinite loop - XXX
144         $objects->Limit(
145             FIELD    => 'LastUpdatedBy',
146             OPERATOR => '!=',
147             VALUE    => $SystemUserId
148         ) unless $class eq 'Groups';
149         $objects->Limit(
150             FIELD    => 'Id',
151             OPERATOR => '!=',
152             VALUE    => $SystemUserId
153         ) if $class eq 'Users';
154     }
155
156     my %fields;
157 OBJECT:
158     while ( my $obj = $objects->Next ) {
159         next
160             if $obj->can('LastUpdatedBy')
161                 and $obj->LastUpdatedBy == $SystemUserId;
162
163         if ( !%fields ) {
164             %fields = map { $_ => 1 } keys %{ $obj->_ClassAccessible };
165             delete @fields{ @{ $Ignore{$class} ||= [] },
166                 @{ $Ignore{All} ||= [] }, };
167         }
168
169         my $rv;
170
171         if ( $class ne 'ACL' ) {
172             # next if $obj-> # skip default names
173             foreach my $field ( sort keys %fields ) {
174                 my $value = $obj->__Value($field);
175                 $rv->{$field} = $value if ( defined($value) && length($value) );
176             }
177             delete $rv->{Disabled} unless $rv->{Disabled};
178
179             foreach my $record ( map { /ACL/ ? 'ACE' : substr( $_, 0, -1 ) }
180                 @classes )
181             {
182                 foreach my $key ( map "$record$_", ( '', 'Id' ) ) {
183                     next unless exists $rv->{$key};
184                     my $id = $rv->{$key} or next;
185                     my $obj = "RT::$record"->new( RT->SystemUser );
186                     $obj->LoadByCols( Id => $id ) or next;
187                     $rv->{$key} = $obj->__Value('Name') || 0;
188                 }
189             }
190
191             if ( $class eq 'Users' and defined $obj->Privileged ) {
192                 $rv->{Privileged} = int( $obj->Privileged );
193             } elsif ( $class eq 'CustomFields' ) {
194                 my $values = $obj->Values;
195                 while ( my $value = $values->Next ) {
196                     push @{ $rv->{Values} }, {
197                         map { ( $_ => $value->__Value($_) ) }
198                             qw(
199                             Name Description SortOrder
200                             ),
201                     };
202                 }
203                 if ( $obj->LookupType eq 'RT::Queue-RT::Ticket' ) {
204                     # XXX-TODO: unused CF's turn into global CF when importing
205                     # as the sub InsertData in RT::Handle creates a global CF
206                     # when no queue is specified.
207                     $rv->{Queue} = [];
208                     my $applies = $obj->AppliedTo;
209                     while ( my $queue = $applies->Next ) {
210                         push @{ $rv->{Queue} }, $queue->Name;
211                     }
212                 }
213             }
214         }
215         else {
216             # 1) pick the right
217             $rv->{Right} = $obj->RightName;
218
219             # 2) Pick a level: Granted on Queue, CF, CF+Queue, or Globally?
220             for ( $obj->ObjectType ) {
221                 if ( /^RT::Queue$/ ) {
222                     next OBJECT if $opt{'skip-disabled'} && $obj->Object->Disabled;
223                     $rv->{Queue} = $obj->Object->Name;
224                 }
225                 elsif ( /^RT::CustomField$/ ) {
226                     next OBJECT if $opt{'skip-disabled'} && $obj->Object->Disabled;
227                     $rv->{CF} = $obj->Object->Name;
228                 }
229                 elsif ( /^RT::Group$/ ) {
230                     # No support for RT::Group ACLs in RT::Handle yet.
231                     next OBJECT;
232                 }
233                 elsif ( /^RT::System$/ ) {
234                     # skip setting anything on $rv;
235                     # "Specifying none of the above will get you a global right."
236                 }
237             }
238
239             # 3) Pick a Principal; User or Group or Role
240             if ( $obj->PrincipalType eq 'Group' ) {
241                 next OBJECT if $opt{'skip-disabled'} && $obj->PrincipalObj->Disabled;
242                 my $group = $obj->PrincipalObj->Object;
243                 for ( $group->Domain ) {
244                     # An internal user group
245                     if ( /^SystemInternal$/ ) {
246                         $rv->{GroupDomain} = $group->Domain;
247                         $rv->{GroupType} = $group->Type;
248                     }
249                     # An individual user
250                     elsif ( /^ACLEquivalence$/ ) {
251                         my $member = $group->MembersObj->Next->MemberObj;
252                         next OBJECT if $opt{'skip-disabled'} && $member->Disabled;
253                         $rv->{UserId} = $member->Object->Name;
254                     }
255                     # A group you created
256                     elsif ( /^UserDefined$/ ) {
257                         $rv->{GroupDomain} = 'UserDefined';
258                         $rv->{GroupId} = $group->Name;
259                     }
260                 }
261             } else {
262                 $rv->{GroupType} = $obj->PrincipalType;
263                 # A system-level role
264                 if ( $obj->ObjectType eq 'RT::System' ) {
265                     $rv->{GroupDomain} = 'RT::System-Role';
266                 }
267                 # A queue-level role
268                 elsif ( $obj->ObjectType eq 'RT::Queue' ) {
269                     $rv->{GroupDomain} = 'RT::Queue-Role';
270                 }
271             }
272             if ( $obj->LookupType eq 'RT::Queue-RT::Ticket' ) {
273                 # XXX-TODO: unused CF's turn into global CF when importing
274                 # as the sub InsertData in RT::Handle creates a global CF
275                 # when no queue is specified.
276                 $rv->{Queue} = [];
277                 my $applies = $obj->AppliedTo;
278                 while ( my $queue = $applies->Next ) {
279                     push @{ $rv->{Queue} }, $queue->Name;
280                 }
281             }
282         }
283
284         if ( eval { require RT::Attributes; 1 } ) {
285             my $attributes = $obj->Attributes;
286             while ( my $attribute = $attributes->Next ) {
287                 my $content = $attribute->Content;
288                 if ( $class eq 'Users' and $attribute->Name eq 'Bookmarks' ) {
289                     next;
290                 }
291                 $rv->{Attributes}{ $attribute->Name } = $content
292                     if length($content);
293             }
294         }
295
296         push @{ $RV{$class} }, $rv;
297     }
298 }
299
300 print(<< ".");
301 no strict; use XML::Simple; *_ = XMLin(do { local \$/; readline(DATA) }, ForceArray => [qw(
302  @classes Values
303 )], NoAttr => 1, SuppressEmpty => ''); *\$_ = (\$_{\$_} || []) for keys \%_; 1; # vim: ft=xml
304 __DATA__
305 .
306
307 print XML::Simple::XMLout(
308     { map { ( $_ => ( $RV{$_} || [] ) ) } @classes },
309     RootName      => 'InitialData',
310     NoAttr        => 1,
311     SuppressEmpty => '',
312     XMLDecl       => '<?xml version="1.0" encoding="UTF-8"?>',
313 );
314
315 __END__
316
317 =head1 NAME
318
319 rt-dump-metadata - dump configuration metadata from an RT database
320
321 =head1 SYNOPSIS
322
323     rt-dump-metdata [--all]
324
325 =head1 DESCRIPTION
326
327 C<rt-dump-metadata> is a tool that dumps configuration metadata from the
328 Request Tracker database into XML format, suitable for feeding into
329 C<rt-setup-database>. To dump and load a full RT database, you should generally
330 use the native database tools instead, as well as performing any necessary
331 steps from UPGRADING.
332
333 This is NOT a tool for backing up an RT database.  See also
334 L<docs/initialdata> for more straightforward means of importing data.
335
336 =head1 OPTIONS
337
338 =over
339
340 =item C<--all> or C<-a>
341
342 When run with C<--all>, the dump will include all configuration
343 metadata; otherwise, the metadata dump will only include 'local'
344 configuration changes, i.e. those done manually in the web interface.
345
346 =item C<--limit-to-privileged> or C<-l>
347
348 Causes the dumper to only dump privileged users.
349
350 =item C<--skip-disabled> or C<-s>
351
352 Ignores disabled rows in the database.
353
354 =back
355
356 =cut
357