]> git.uio.no Git - usit-rt.git/blame - sbin/rt-dump-metadata
Merge branch 'master' of git.uio.no:usit-rt
[usit-rt.git] / sbin / rt-dump-metadata
CommitLineData
84fb5b46
MKG
1#!/usr/bin/perl -w
2# BEGIN BPS TAGGED BLOCK {{{
3#
4# COPYRIGHT:
5#
320f0092 6# This software is Copyright (c) 1996-2014 Best Practical Solutions, LLC
84fb5b46
MKG
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 }}}
49use strict;
403d7b0b 50use warnings;
84fb5b46
MKG
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
54binmode( STDOUT, ":utf8" );
55
56# fix lib paths, some may be relative
af59614d 57BEGIN { # BEGIN RT CMD BOILERPLATE
84fb5b46 58 require File::Spec;
af59614d
MKG
59 require Cwd;
60 my @libs = ("lib", "local/lib");
84fb5b46
MKG
61 my $bin_path;
62
63 for my $lib (@libs) {
64 unless ( File::Spec->file_name_is_absolute($lib) ) {
af59614d 65 $bin_path ||= ( File::Spec->splitpath(Cwd::abs_path(__FILE__)) )[1];
84fb5b46
MKG
66 $lib = File::Spec->catfile( $bin_path, File::Spec->updir, $lib );
67 }
68 unshift @INC, $lib;
69 }
70
71}
72
73use Getopt::Long;
74my %opt;
320f0092
MKG
75GetOptions( \%opt, "help|h",
76 "limit-to-privileged|l",
77 "skip-disabled|s",
78 "all|a",
79);
84fb5b46
MKG
80
81if ( $opt{help} ) {
82 require Pod::Usage;
83 Pod::Usage::pod2usage( { verbose => 2 } );
84 exit;
85}
86
87require RT;
88require XML::Simple;
89
90RT::LoadConfig();
91RT::Init();
92
84fb5b46
MKG
93my %RV;
94my %Ignore = (
95 All => [
96 qw(
97 id Created Creator LastUpdated LastUpdatedBy
98 )
99 ],
84fb5b46
MKG
100);
101
102my $SystemUserId = RT->SystemUser->Id;
103my @classes = qw(
104 Users Groups Queues ScripActions ScripConditions
105 Templates Scrips ACL CustomFields
106 );
107foreach my $class (@classes) {
108 require "RT/$class.pm";
109 my $objects = "RT::$class"->new( RT->SystemUser );
320f0092 110 $objects->{find_disabled_rows} = 1 unless $opt{'skip-disabled'};
84fb5b46 111 $objects->UnLimit;
320f0092
MKG
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';
84fb5b46
MKG
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
320f0092 131 unless ($opt{all}) {
84fb5b46
MKG
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';
84fb5b46
MKG
143 }
144
145 my %fields;
320f0092 146OBJECT:
84fb5b46
MKG
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
320f0092
MKG
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 }
84fb5b46
MKG
202 }
203 }
320f0092
MKG
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 }
84fb5b46 227
320f0092
MKG
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 }
84fb5b46
MKG
260 }
261 }
262
c33a4027 263 if ( RT::Attributes->require ) {
84fb5b46
MKG
264 my $attributes = $obj->Attributes;
265 while ( my $attribute = $attributes->Next ) {
266 my $content = $attribute->Content;
320f0092
MKG
267 if ( $class eq 'Users' and $attribute->Name eq 'Bookmarks' ) {
268 next;
269 }
84fb5b46
MKG
270 $rv->{Attributes}{ $attribute->Name } = $content
271 if length($content);
272 }
273 }
274
275 push @{ $RV{$class} }, $rv;
276 }
277}
278
279print(<< ".");
280no 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
286print 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
298rt-dump-metadata - dump configuration metadata from an RT database
299
300=head1 SYNOPSIS
301
320f0092 302 rt-dump-metdata [--all]
84fb5b46
MKG
303
304=head1 DESCRIPTION
305
306C<rt-dump-metadata> is a tool that dumps configuration metadata from the
307Request Tracker database into XML format, suitable for feeding into
308C<rt-setup-database>. To dump and load a full RT database, you should generally
309use the native database tools instead, as well as performing any necessary
310steps from UPGRADING.
311
320f0092
MKG
312This is NOT a tool for backing up an RT database. See also
313L<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
321When run with C<--all>, the dump will include all configuration
322metadata; otherwise, the metadata dump will only include 'local'
84fb5b46
MKG
323configuration changes, i.e. those done manually in the web interface.
324
320f0092
MKG
325=item C<--limit-to-privileged> or C<-l>
326
327Causes the dumper to only dump privileged users.
328
329=item C<--skip-disabled> or C<-s>
330
331Ignores disabled rows in the database.
332
333=back
84fb5b46 334
320f0092 335=cut
84fb5b46 336