]> git.uio.no Git - usit-rt.git/blob - sbin/rt-dump-metadata
Merge branch 'master' of git.uio.no:usit-rt
[usit-rt.git] / sbin / rt-dump-metadata
1 #!/usr/bin/perl -w
2 # BEGIN BPS TAGGED BLOCK {{{
3 #
4 # COPYRIGHT:
5 #
6 # This software is Copyright (c) 1996-2014 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 { # BEGIN RT CMD BOILERPLATE
58     require File::Spec;
59     require Cwd;
60     my @libs = ("lib", "local/lib");
61     my $bin_path;
62
63     for my $lib (@libs) {
64         unless ( File::Spec->file_name_is_absolute($lib) ) {
65             $bin_path ||= ( File::Spec->splitpath(Cwd::abs_path(__FILE__)) )[1];
66             $lib = File::Spec->catfile( $bin_path, File::Spec->updir, $lib );
67         }
68         unshift @INC, $lib;
69     }
70
71 }
72
73 use Getopt::Long;
74 my %opt;
75 GetOptions( \%opt, "help|h",
76     "limit-to-privileged|l",
77     "skip-disabled|s",
78     "all|a",
79 );
80
81 if ( $opt{help} ) {
82     require Pod::Usage;
83     Pod::Usage::pod2usage( { verbose => 2 } );
84     exit;
85 }
86
87 require RT;
88 require XML::Simple;
89
90 RT::LoadConfig();
91 RT::Init();
92
93 my %RV;
94 my %Ignore = (
95     All => [
96         qw(
97             id Created Creator LastUpdated LastUpdatedBy
98             )
99            ],
100 );
101
102 my $SystemUserId = RT->SystemUser->Id;
103 my @classes      = qw(
104     Users Groups Queues ScripActions ScripConditions
105     Templates Scrips ACL CustomFields
106     );
107 foreach my $class (@classes) {
108     require "RT/$class.pm";
109     my $objects = "RT::$class"->new( RT->SystemUser );
110     $objects->{find_disabled_rows} = 1 unless $opt{'skip-disabled'};
111     $objects->UnLimit;
112     $objects->LimitToPrivileged if $class eq 'Users'
113         && $opt{'limit-to-privileged'};
114     $objects->Limit(
115         FIELD    => 'Domain',
116         OPERATOR => '=',
117         VALUE    => 'UserDefined',
118         CASESENSITIVE => 0,
119     ) if $class eq 'Groups';
120
121     if ( $class eq 'CustomFields' ) {
122         $objects->OrderByCols(
123             { FIELD => 'LookupType' },
124             { FIELD => 'SortOrder' },
125             { FIELD => 'Id' },
126         );
127     } else {
128         $objects->OrderBy( FIELD => 'Id' );
129     }
130
131     unless ($opt{all}) {
132         next if $class eq 'ACL';    # XXX - would go into infinite loop - XXX
133         $objects->Limit(
134             FIELD    => 'LastUpdatedBy',
135             OPERATOR => '!=',
136             VALUE    => $SystemUserId
137         ) unless $class eq 'Groups';
138         $objects->Limit(
139             FIELD    => 'Id',
140             OPERATOR => '!=',
141             VALUE    => $SystemUserId
142         ) if $class eq 'Users';
143     }
144
145     my %fields;
146 OBJECT:
147     while ( my $obj = $objects->Next ) {
148         next
149             if $obj->can('LastUpdatedBy')
150                 and $obj->LastUpdatedBy == $SystemUserId;
151
152         if ( !%fields ) {
153             %fields = map { $_ => 1 } keys %{ $obj->_ClassAccessible };
154             delete @fields{ @{ $Ignore{$class} ||= [] },
155                 @{ $Ignore{All} ||= [] }, };
156         }
157
158         my $rv;
159
160         if ( $class ne 'ACL' ) {
161             # next if $obj-> # skip default names
162             foreach my $field ( sort keys %fields ) {
163                 my $value = $obj->__Value($field);
164                 $rv->{$field} = $value if ( defined($value) && length($value) );
165             }
166             delete $rv->{Disabled} unless $rv->{Disabled};
167
168             foreach my $record ( map { /ACL/ ? 'ACE' : substr( $_, 0, -1 ) }
169                 @classes )
170             {
171                 foreach my $key ( map "$record$_", ( '', 'Id' ) ) {
172                     next unless exists $rv->{$key};
173                     my $id = $rv->{$key} or next;
174                     my $obj = "RT::$record"->new( RT->SystemUser );
175                     $obj->LoadByCols( Id => $id ) or next;
176                     $rv->{$key} = $obj->__Value('Name') || 0;
177                 }
178             }
179
180             if ( $class eq 'Users' and defined $obj->Privileged ) {
181                 $rv->{Privileged} = int( $obj->Privileged );
182             } elsif ( $class eq 'CustomFields' ) {
183                 my $values = $obj->Values;
184                 while ( my $value = $values->Next ) {
185                     push @{ $rv->{Values} }, {
186                         map { ( $_ => $value->__Value($_) ) }
187                             qw(
188                             Name Description SortOrder
189                             ),
190                     };
191                 }
192                 if ( $obj->LookupType eq 'RT::Queue-RT::Ticket' ) {
193                     # XXX-TODO: unused CF's turn into global CF when importing
194                     # as the sub InsertData in RT::Handle creates a global CF
195                     # when no queue is specified.
196                     $rv->{Queue} = [];
197                     my $applies = $obj->AppliedTo;
198                     while ( my $queue = $applies->Next ) {
199                         push @{ $rv->{Queue} }, $queue->Name;
200                     }
201                 }
202             }
203         }
204         else {
205             # 1) pick the right
206             $rv->{Right} = $obj->RightName;
207
208             # 2) Pick a level: Granted on Queue, CF, CF+Queue, or Globally?
209             for ( $obj->ObjectType ) {
210                 if ( /^RT::Queue$/ ) {
211                     next OBJECT if $opt{'skip-disabled'} && $obj->Object->Disabled;
212                     $rv->{Queue} = $obj->Object->Name;
213                 }
214                 elsif ( /^RT::CustomField$/ ) {
215                     next OBJECT if $opt{'skip-disabled'} && $obj->Object->Disabled;
216                     $rv->{CF} = $obj->Object->Name;
217                 }
218                 elsif ( /^RT::Group$/ ) {
219                     # No support for RT::Group ACLs in RT::Handle yet.
220                     next OBJECT;
221                 }
222                 elsif ( /^RT::System$/ ) {
223                     # skip setting anything on $rv;
224                     # "Specifying none of the above will get you a global right."
225                 }
226             }
227
228             # 3) Pick a Principal; User or Group or Role
229             if ( $obj->PrincipalType eq 'Group' ) {
230                 next OBJECT if $opt{'skip-disabled'} && $obj->PrincipalObj->Disabled;
231                 my $group = $obj->PrincipalObj->Object;
232                 for ( $group->Domain ) {
233                     # An internal user group
234                     if ( /^SystemInternal$/ ) {
235                         $rv->{GroupDomain} = $group->Domain;
236                         $rv->{GroupType} = $group->Type;
237                     }
238                     # An individual user
239                     elsif ( /^ACLEquivalence$/ ) {
240                         my $member = $group->MembersObj->Next->MemberObj;
241                         next OBJECT if $opt{'skip-disabled'} && $member->Disabled;
242                         $rv->{UserId} = $member->Object->Name;
243                     }
244                     # A group you created
245                     elsif ( /^UserDefined$/ ) {
246                         $rv->{GroupDomain} = 'UserDefined';
247                         $rv->{GroupId} = $group->Name;
248                     }
249                 }
250             } else {
251                 $rv->{GroupType} = $obj->PrincipalType;
252                 # A system-level role
253                 if ( $obj->ObjectType eq 'RT::System' ) {
254                     $rv->{GroupDomain} = 'RT::System-Role';
255                 }
256                 # A queue-level role
257                 elsif ( $obj->ObjectType eq 'RT::Queue' ) {
258                     $rv->{GroupDomain} = 'RT::Queue-Role';
259                 }
260             }
261         }
262
263         if ( RT::Attributes->require ) {
264             my $attributes = $obj->Attributes;
265             while ( my $attribute = $attributes->Next ) {
266                 my $content = $attribute->Content;
267                 if ( $class eq 'Users' and $attribute->Name eq 'Bookmarks' ) {
268                     next;
269                 }
270                 $rv->{Attributes}{ $attribute->Name } = $content
271                     if length($content);
272             }
273         }
274
275         push @{ $RV{$class} }, $rv;
276     }
277 }
278
279 print(<< ".");
280 no strict; use XML::Simple; *_ = XMLin(do { local \$/; readline(DATA) }, ForceArray => [qw(
281  @classes Values
282 )], NoAttr => 1, SuppressEmpty => ''); *\$_ = (\$_{\$_} || []) for keys \%_; 1; # vim: ft=xml
283 __DATA__
284 .
285
286 print XML::Simple::XMLout(
287     { map { ( $_ => ( $RV{$_} || [] ) ) } @classes },
288     RootName      => 'InitialData',
289     NoAttr        => 1,
290     SuppressEmpty => '',
291     XMLDecl       => '<?xml version="1.0" encoding="UTF-8"?>',
292 );
293
294 __END__
295
296 =head1 NAME
297
298 rt-dump-metadata - dump configuration metadata from an RT database
299
300 =head1 SYNOPSIS
301
302     rt-dump-metdata [--all]
303
304 =head1 DESCRIPTION
305
306 C<rt-dump-metadata> is a tool that dumps configuration metadata from the
307 Request Tracker database into XML format, suitable for feeding into
308 C<rt-setup-database>. To dump and load a full RT database, you should generally
309 use the native database tools instead, as well as performing any necessary
310 steps from UPGRADING.
311
312 This is NOT a tool for backing up an RT database.  See also
313 L<docs/initialdata> for more straightforward means of importing data.
314
315 =head1 OPTIONS
316
317 =over
318
319 =item C<--all> or C<-a>
320
321 When run with C<--all>, the dump will include all configuration
322 metadata; otherwise, the metadata dump will only include 'local'
323 configuration changes, i.e. those done manually in the web interface.
324
325 =item C<--limit-to-privileged> or C<-l>
326
327 Causes the dumper to only dump privileged users.
328
329 =item C<--skip-disabled> or C<-s>
330
331 Ignores disabled rows in the database.
332
333 =back
334
335 =cut
336