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