1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2015 Best Practical Solutions, LLC
6 # <sales@bestpractical.com>
8 # (Except where explicitly superseded by other copyright notices)
13 # This work is made available to you under the terms of Version 2 of
14 # the GNU General Public License. A copy of that license should have
15 # been provided with this software, but in any event can be snarfed
18 # This work is distributed in the hope that it will be useful, but
19 # WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 # General Public License for more details.
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 # 02110-1301 or visit their web page on the internet at
27 # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
30 # CONTRIBUTION SUBMISSION POLICY:
32 # (The following paragraph is not intended to limit the rights granted
33 # to you to modify and distribute this software under the terms of
34 # the GNU General Public License and is only of importance to you if
35 # you choose to contribute your changes and enhancements to the
36 # community by submitting them to Best Practical Solutions, LLC.)
38 # By intentionally submitting any modifications, corrections or
39 # derivatives to this work, or any other work intended for use with
40 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
41 # you are the copyright holder for those contributions and you grant
42 # Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable,
43 # royalty-free, perpetual, license to use, copy, create derivative
44 # works based on those contributions, and sublicense and distribute
45 # those contributions and any derivatives thereof.
47 # END BPS TAGGED BLOCK }}}
49 package RT::Migrate::Importer;
60 my $self = bless {}, $class;
76 # Should we attempt to preserve record IDs as they are created?
77 $self->{OriginalId} = $args{OriginalId};
79 $self->{Progress} = $args{Progress};
81 $self->{HandleError} = sub { 0 };
82 $self->{HandleError} = $args{HandleError}
83 if $args{HandleError} and ref $args{HandleError} eq 'CODE';
85 if ($args{DumpObjects}) {
87 $self->{DumpObjects} = { map { $_ => 1 } @{$args{DumpObjects}} };
90 # Objects we've created
93 # Columns we need to update when an object is later created
94 $self->{Pending} = {};
96 # Objects missing from the source database before serialization
97 $self->{Invalid} = [];
100 $self->{ObjectCount} = {};
102 # To know what global CFs need to be unglobal'd and applied to what
103 $self->{NewQueues} = [];
104 $self->{NewCFs} = [];
109 return $self->{Metadata};
116 return if $self->{Metadata};
117 $self->{Metadata} = $data;
119 die "Incompatible format version: ".$data->{Format}
120 if $data->{Format} ne "0.8";
122 $self->{Organization} = $data->{Organization};
123 $self->{Clone} = $data->{Clone};
124 $self->{Incremental} = $data->{Incremental};
125 $self->{Files} = $data->{Files} if $data->{Final};
131 die "Stream initialized after objects have been recieved!"
132 if keys %{ $self->{UIDs} };
134 die "Cloning does not support importing the Original Id separately\n"
135 if $self->{OriginalId} and $self->{Clone};
137 die "RT already contains data; overwriting will not work\n"
138 if ($self->{Clone} and not $self->{Incremental})
139 and RT->SystemUser->Id;
141 # Basic facts of life, as a safety net
142 $self->Resolve( RT->System->UID => ref RT->System, RT->System->Id );
143 $self->SkipTransactions( RT->System->UID );
145 if ($self->{OriginalId}) {
146 # Where to shove the original ticket ID
147 my $cf = RT::CustomField->new( RT->SystemUser );
148 $cf->LoadByName( Name => $self->{OriginalId}, LookupType => RT::Ticket->CustomFieldLookupType, ObjectId => 0 );
150 warn "Failed to find global CF named $self->{OriginalId} -- creating one";
153 Name => $self->{OriginalId},
154 Type => 'FreeformSingle',
162 my ($uid, $class, $id) = @_;
163 $self->{UIDs}{$uid} = [ $class, $id ];
164 return unless $self->{Pending}{$uid};
166 for my $ref (@{$self->{Pending}{$uid}}) {
167 my ($pclass, $pid) = @{ $self->Lookup( $ref->{uid} ) };
168 my $obj = $pclass->new( RT->SystemUser );
169 $obj->LoadByCols( Id => $pid );
171 Field => $ref->{column},
173 ) if defined $ref->{column};
175 Field => $ref->{classcolumn},
177 ) if defined $ref->{classcolumn};
179 Field => $ref->{uri},
180 Value => $self->LookupObj($uid)->URI,
181 ) if defined $ref->{uri};
183 delete $self->{Pending}{$uid};
189 unless (defined $uid) {
190 carp "Tried to lookup an undefined UID";
193 return $self->{UIDs}{$uid};
199 my $ref = $self->Lookup( $uid );
201 my ($class, $id) = @{ $ref };
203 my $obj = $class->new( RT->SystemUser );
214 classcolumn => undef,
218 my $uid = delete $args{for};
221 push @{$self->{Pending}{$uid}}, \%args;
223 push @{$self->{Invalid}}, \%args;
227 sub SkipTransactions {
230 return if $self->{Clone};
231 $self->{SkipTransactions}{$uid} = 1;
234 sub ShouldSkipTransaction {
237 return exists $self->{SkipTransactions}{$uid};
242 my ($obj, $data) = @_;
243 for my $col (keys %{$data}) {
244 next if defined $obj->__Value($col) and length $obj->__Value($col);
245 next unless defined $data->{$col} and length $data->{$col};
247 if (ref $data->{$col}) {
248 my $uid = ${ $data->{$col} };
249 my $ref = $self->Lookup( $uid );
251 $data->{$col} = $ref->[1];
261 $obj->__Set( Field => $col, Value => $data->{$col} );
267 my ($column, $class, $uid, $data) = @_;
269 my $obj = $class->new( RT->SystemUser );
270 $obj->Load( $data->{$column} );
271 return unless $obj->Id;
273 $self->SkipTransactions( $uid );
275 $self->Resolve( $uid => $class => $obj->Id );
281 my ($column, $class, $uid, $data) = @_;
283 my $obj = $self->SkipBy(@_);
285 $self->MergeValues( $obj, $data );
292 return $string if $self->{Clone};
293 return $string if not defined $self->{Organization};
294 return $string if $self->{Organization} eq $RT::Organization;
295 return $self->{Organization}.": $string";
300 my ($class, $uid, $data) = @_;
302 # Use a simpler pre-inflation if we're cloning
303 if ($self->{Clone}) {
304 $class->RT::Record::PreInflate( $self, $uid, $data );
306 # Non-cloning always wants to make its own id
308 return unless $class->PreInflate( $self, $uid, $data );
311 my $obj = $class->new( RT->SystemUser );
312 my ($id, $msg) = eval {
313 # catch and rethrow on the outside so we can provide more info
315 $obj->DBIx::SearchBuilder::Record::Create(
320 $msg ||= ''; # avoid undef
321 my $err = "Failed to create $uid: $msg $@\n" . Data::Dumper::Dumper($data) . "\n";
322 if (not $self->{HandleError}->($self, $err)) {
329 $self->{ObjectCount}{$class}++;
330 $self->Resolve( $uid => $class, $id );
332 # Load it back to get real values into the columns
333 $obj = $class->new( RT->SystemUser );
335 $obj->PostInflate( $self );
344 no warnings 'redefine';
345 local *RT::Ticket::Load = sub {
348 $self->LoadById( $id );
352 my $loaded = Storable::fd_retrieve($fh);
354 # Metadata is stored at the start of the stream as a hashref
355 if (ref $loaded eq "HASH") {
356 $self->LoadMetadata( $loaded );
361 my ($class, $uid, $data) = @{$loaded};
363 if ($self->{Incremental}) {
364 my $obj = $class->new( RT->SystemUser );
365 $obj->Load( $data->{id} );
367 # undef $uid means "delete it"
369 $self->{ObjectCount}{$class}++;
370 } elsif ( $obj->Id ) {
371 # If it exists, update it
372 $class->RT::Record::PreInflate( $self, $uid, $data );
373 $obj->__Set( Field => $_, Value => $data->{$_} )
375 $self->{ObjectCount}{$class}++;
378 $obj = $self->Create( $class, $uid, $data );
380 $self->{Progress}->($obj) if $obj and $self->{Progress};
382 } elsif ($self->{Clone}) {
383 my $obj = $self->Create( $class, $uid, $data );
384 $self->{Progress}->($obj) if $obj and $self->{Progress};
388 # If it's a queue, store its ID away, as we'll need to know
389 # it to split global CFs into non-global across those
390 # fields. We do this before inflating, so that queues which
391 # got merged still get the CFs applied
392 push @{$self->{NewQueues}}, $uid
393 if $class eq "RT::Queue";
395 my $origid = $data->{id};
396 my $obj = $self->Create( $class, $uid, $data );
399 # If it's a ticket, we might need to create a
400 # TicketCustomField for the previous ID
401 if ($class eq "RT::Ticket" and $self->{OriginalId}) {
402 my ($id, $msg) = $obj->AddCustomFieldValue(
403 Field => $self->{OriginalId},
404 Value => $self->Organization . ":$origid",
405 RecordTransaction => 0,
407 warn "Failed to add custom field to $uid: $msg"
411 # If it's a CF, we don't know yet if it's global (the OCF
412 # hasn't been created yet) to store away the CF for later
414 push @{$self->{NewCFs}}, $uid
415 if $class eq "RT::CustomField"
416 and $obj->LookupType =~ /^RT::Queue/;
418 $self->{Progress}->($obj) if $self->{Progress};
424 $self->{Progress}->(undef, 'force') if $self->{Progress};
426 return if $self->{Clone};
428 # Take global CFs which we made and make them un-global
429 my @queues = grep {$_} map {$self->LookupObj( $_ )} @{$self->{NewQueues}};
430 for my $obj (map {$self->LookupObj( $_ )} @{$self->{NewCFs}}) {
431 my $ocf = $obj->IsGlobal or next;
433 $obj->AddToObject( $_ ) for @queues;
435 $self->{NewQueues} = [];
436 $self->{NewCFs} = [];
442 return %{ $self->{ObjectCount} };
447 return wantarray ? sort keys %{ $self->{Pending} }
448 : keys %{ $self->{Pending} };
453 return wantarray ? sort { $a->{uid} cmp $b->{uid} } @{ $self->{Invalid} }
459 return $self->{Organization};
464 return defined $self->{Progress} unless @_;
465 return $self->{Progress} = $_[0];