import rt 3.4.4
[freeside.git] / rt / sbin / rt-dump-database.in
1 #!@PERL@ -w
2 # BEGIN BPS TAGGED BLOCK {{{
3
4 # COPYRIGHT:
5 #  
6 # This software is Copyright (c) 1996-2005 Best Practical Solutions, LLC 
7 #                                          <jesse@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., 675 Mass Ave, Cambridge, MA 02139, USA.
27
28
29 # CONTRIBUTION SUBMISSION POLICY:
30
31 # (The following paragraph is not intended to limit the rights granted
32 # to you to modify and distribute this software under the terms of
33 # the GNU General Public License and is only of importance to you if
34 # you choose to contribute your changes and enhancements to the
35 # community by submitting them to Best Practical Solutions, LLC.)
36
37 # By intentionally submitting any modifications, corrections or
38 # derivatives to this work, or any other work intended for use with
39 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
40 # you are the copyright holder for those contributions and you grant
41 # Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
42 # royalty-free, perpetual, license to use, copy, create derivative
43 # works based on those contributions, and sublicense and distribute
44 # those contributions and any derivatives thereof.
45
46 # END BPS TAGGED BLOCK }}}
47 use strict;
48 use lib "@RT_LIB_PATH@";
49 use RT;
50 use XML::Simple;
51
52 RT::LoadConfig();
53 RT::Init();
54
55 my $LocalOnly = @ARGV ? shift(@ARGV) : 1;
56
57 my %RV;
58 my %Ignore = (
59     All => [qw(
60         id Created Creator LastUpdated LastUpdatedBy
61     )],
62     Templates => [qw(
63         TranslationOf
64     )],
65 );
66
67 my $SystemUserId = $RT::SystemUser->Id;
68 my @classes = qw(
69     Users Groups Queues ScripActions ScripConditions
70     Templates Scrips ACL CustomFields
71 );
72 foreach my $class (@classes) {
73     require "RT/$class.pm";
74     my $objects = "RT::$class"->new($RT::SystemUser);
75     $objects->{find_disabled_rows} = 1;
76     $objects->UnLimit;
77
78     if ($class eq 'CustomFields') {
79         $objects->OrderByCols(
80             { FIELD => 'LookupType' },
81             { FIELD => 'SortOrder' },
82             { FIELD => 'Id' },
83         );
84     }
85     else {
86         $objects->OrderBy( FIELD => 'Id' );
87     }
88
89     if ($LocalOnly) {
90         next if $class eq 'ACL'; # XXX - would go into infinite loop - XXX
91         $objects->Limit( FIELD => 'LastUpdatedBy', OPERATOR => '!=', VALUE => $SystemUserId )
92             unless $class eq 'Groups';
93         $objects->Limit( FIELD => 'Id', OPERATOR => '!=', VALUE => $SystemUserId )
94             if $class eq 'Users';
95         $objects->Limit( FIELD => 'Domain', OPERATOR => '=', VALUE => 'UserDefined' )
96             if $class eq 'Groups';
97     }
98
99     my %fields;
100     while (my $obj = $objects->Next) {
101         next if $obj->can('LastUpdatedBy') and $obj->LastUpdatedBy == $SystemUserId;
102
103         if (!%fields) {
104             %fields = map { $_ => 1 } keys %{$obj->_ClassAccessible};
105             delete @fields{
106                 @{$Ignore{$class}||=[]},
107                 @{$Ignore{All}||=[]},
108             };
109         }
110
111         my $rv;
112         # next if $obj-> # skip default names
113         foreach my $field (sort keys %fields) {
114             my $value = $obj->__Value($field);
115             $rv->{$field} = $value if length($value);
116         }
117         delete $rv->{Disabled} unless $rv->{Disabled};
118
119         foreach my $record (map { /ACL/ ? 'ACE' : substr($_, 0, -1) } @classes) {
120             foreach my $key (map "$record$_", ('', 'Id')) {
121                 next unless exists $rv->{$key};
122                 my $id = $rv->{$key} or next;
123                 my $obj = "RT::$record"->new($RT::SystemUser);
124                 $obj->LoadByCols( Id => $id ) or next;
125                 $rv->{$key} = $obj->__Value('Name') || 0;
126             }
127         }
128
129         if ($class eq 'Users' and defined $obj->Privileged) {
130             $rv->{Privileged} = int($obj->Privileged);
131         }
132         elsif ($class eq 'CustomFields') {
133             my $values = $obj->Values;
134             while (my $value = $values->Next) {
135                 push @{$rv->{Values}}, {
136                     map { ($_ => $value->__Value($_)) } qw(
137                         Name Description SortOrder
138                     ),
139                 };
140             }
141         }
142
143         if (eval { require RT::Attributes; 1 }) {
144             my $attributes = $obj->Attributes;
145             while (my $attribute = $attributes->Next) {
146                 my $content = $attribute->Content;
147                 $rv->{Attributes}{$attribute->Name} = $content if length($content);
148             }
149         }
150
151         push @{$RV{$class}}, $rv;
152     }
153 }
154
155 print(<< ".");
156 no strict; use XML::Simple; *_ = XMLin(do { local \$/; readline(DATA) }, ForceArray => [qw(
157  @classes Values
158 )], NoAttr => 1, SuppressEmpty => ''); *\$_ = (\$_{\$_} || []) for keys \%_; 1; # vim: ft=xml
159 __DATA__
160 .
161
162 print XMLout(
163     { map { ($_ => ($RV{$_} || [])) } @classes },
164     RootName => 'InitialData',
165     NoAttr => 1,
166     SuppressEmpty => '',
167     XMLDecl => '<?xml version="1.0" encoding="UTF-8"?>',
168 );