| | 424 | package MT::Object; |
|---|
| | 425 | |
|---|
| | 426 | sub _is_element { |
|---|
| | 427 | my $obj = shift; |
|---|
| | 428 | my ($def) = @_; |
|---|
| | 429 | return (('text' eq $def->{type}) || (('string' eq $def->{type}) && (255 < $def->{size}))) ? 1 : 0; |
|---|
| | 430 | } |
|---|
| | 431 | |
|---|
| | 432 | sub to_backup { 1; } |
|---|
| | 433 | |
|---|
| | 434 | sub children_to_xml { |
|---|
| | 435 | my $obj = shift; |
|---|
| | 436 | my ($namespace, $args) = @_; |
|---|
| | 437 | |
|---|
| | 438 | my $t = {}; |
|---|
| | 439 | if (defined($args)) { |
|---|
| | 440 | my $j = $args->{'join'}; |
|---|
| | 441 | $t = $j->[2] if defined($j); |
|---|
| | 442 | } |
|---|
| | 443 | |
|---|
| | 444 | my $children = $obj->children_names; |
|---|
| | 445 | my @children_classes = values %$children; |
|---|
| | 446 | my $xml = ''; |
|---|
| | 447 | |
|---|
| | 448 | for my $child_class (@children_classes) { |
|---|
| | 449 | eval "require $child_class"; |
|---|
| | 450 | my $err = $@; |
|---|
| | 451 | return $err if defined($err) && $err; |
|---|
| | 452 | |
|---|
| | 453 | my $terms = { |
|---|
| | 454 | $obj->datasource . '_id' => $obj->id, |
|---|
| | 455 | %$t, |
|---|
| | 456 | }; |
|---|
| | 457 | |
|---|
| | 458 | my $offset = 0; |
|---|
| | 459 | while (1) { |
|---|
| | 460 | my @objects = $child_class->load( |
|---|
| | 461 | $terms, |
|---|
| | 462 | { offset => $offset, limit => 50, } |
|---|
| | 463 | ); |
|---|
| | 464 | last unless @objects; |
|---|
| | 465 | $offset += scalar @objects; |
|---|
| | 466 | for my $object (@objects) { |
|---|
| | 467 | $xml .= $object->to_xml($namespace) . "\n" if $object->to_backup; |
|---|
| | 468 | } |
|---|
| | 469 | } |
|---|
| | 470 | } |
|---|
| | 471 | $xml; |
|---|
| | 472 | } |
|---|
| | 473 | |
|---|
| | 474 | sub to_xml { |
|---|
| | 475 | my $obj = shift; |
|---|
| | 476 | my ($namespace, $args) = @_; |
|---|
| | 477 | |
|---|
| | 478 | my $coldefs = $obj->column_defs; |
|---|
| | 479 | my $colnames = $obj->column_names; |
|---|
| | 480 | my $xml; |
|---|
| | 481 | |
|---|
| | 482 | $xml = '<' . $obj->datasource; |
|---|
| | 483 | $xml .= " xmlns='$namespace'" if defined($namespace) && $namespace; |
|---|
| | 484 | |
|---|
| | 485 | my @elements; |
|---|
| | 486 | for my $name (@$colnames) { |
|---|
| | 487 | if ($obj->column($name) || (defined($obj->column($name)) && ('0' eq $obj->column($name)))) { |
|---|
| | 488 | if ($obj->_is_element($coldefs->{$name})) { |
|---|
| | 489 | push @elements, $name; |
|---|
| | 490 | next; |
|---|
| | 491 | #} elsif (('datetime' eq $coldefs->{$name}{type}) || ('timestamp' eq $coldefs->{$name}{type})) { |
|---|
| | 492 | # my $ts_iso = MT::Util::ts2iso(undef, $obj->column($name)); |
|---|
| | 493 | # $ts_iso =~ s/ /T/; |
|---|
| | 494 | # $xml .= " $name='" . $ts_iso . "'"; |
|---|
| | 495 | # next; |
|---|
| | 496 | } |
|---|
| | 497 | $xml .= " $name='" . MT::Util::encode_xml($obj->column($name), 1) . "'"; |
|---|
| | 498 | } |
|---|
| | 499 | } |
|---|
| | 500 | $xml .= '>'; |
|---|
| | 501 | $xml .= "<$_>" . MT::Util::encode_xml($obj->column($_), 1) . "</$_>" foreach @elements; |
|---|
| | 502 | $xml .= $obj->children_to_xml($namespace, $args); |
|---|
| | 503 | my $ext_xml = MT->run_callbacks('Backup.' . $obj->datasource, $obj); |
|---|
| | 504 | $xml .= $ext_xml if $ext_xml ne '1'; |
|---|
| | 505 | $xml .= '</' . $obj->datasource . '>'; |
|---|
| | 506 | $xml; |
|---|
| | 507 | } |
|---|
| | 508 | |
|---|
| | 509 | sub children_names { |
|---|
| | 510 | my $obj = shift; |
|---|
| | 511 | {}; |
|---|
| | 512 | } |
|---|
| | 513 | |
|---|
| | 514 | sub parent_names { |
|---|
| | 515 | my $obj = shift; |
|---|
| | 516 | {}; |
|---|
| | 517 | } |
|---|
| | 518 | |
|---|
| | 519 | sub restore_parent_ids { |
|---|
| | 520 | my $obj = shift; |
|---|
| | 521 | my ($data, $objects) = @_; |
|---|
| | 522 | |
|---|
| | 523 | my $parent_names = $obj->parent_names; |
|---|
| | 524 | |
|---|
| | 525 | my $done = 0; |
|---|
| | 526 | for my $parent_element_name (keys %$parent_names) { |
|---|
| | 527 | if (!exists($data->{$parent_element_name . '_id'})) { |
|---|
| | 528 | $done++; |
|---|
| | 529 | next; |
|---|
| | 530 | } |
|---|
| | 531 | my $parent_class_name = $parent_names->{$parent_element_name}; |
|---|
| | 532 | my $old_id = $data->{$parent_element_name . '_id'}; |
|---|
| | 533 | my $new_obj = $objects->{"$parent_class_name#$old_id"}; |
|---|
| | 534 | next if !(defined($new_obj) && $new_obj); |
|---|
| | 535 | $data->{$parent_element_name . '_id'} = $new_obj->id; |
|---|
| | 536 | $done++; |
|---|
| | 537 | } |
|---|
| | 538 | (scalar(keys(%$parent_names)) == $done) ? 1 : 0; |
|---|
| | 539 | } |
|---|
| | 540 | |
|---|
| | 541 | sub from_xml { |
|---|
| | 542 | my $class = shift; |
|---|
| | 543 | my (%param) = @_; |
|---|
| | 544 | my $xp = $param{XPath}; |
|---|
| | 545 | my $element = $param{XmlNode}; |
|---|
| | 546 | my $objects = $param{Objects}; |
|---|
| | 547 | my $deferred = $param{Deferred}; |
|---|
| | 548 | my $error = $param{Error}; |
|---|
| | 549 | my $cb = $param{Callback}; |
|---|
| | 550 | |
|---|
| | 551 | require MT::BackupRestore; |
|---|
| | 552 | my $namespace = $param{Namespace} || MT::BackupRestore::NS_MOVABLETYPE(); |
|---|
| | 553 | |
|---|
| | 554 | if (ref($class)) { |
|---|
| | 555 | $class = ref($class); |
|---|
| | 556 | } |
|---|
| | 557 | |
|---|
| | 558 | my $err = $@; |
|---|
| | 559 | if (defined($err) && $err) { |
|---|
| | 560 | $cb->($err . "\n"); |
|---|
| | 561 | return undef; |
|---|
| | 562 | } |
|---|
| | 563 | |
|---|
| | 564 | my $obj = $class->new; |
|---|
| | 565 | if (!$obj || !($obj->isa('MT::Object'))) { |
|---|
| | 566 | $cb->(MT->translate("Invalid XML element to restore: [_1]\n", $class)); |
|---|
| | 567 | return undef; |
|---|
| | 568 | } |
|---|
| | 569 | |
|---|
| | 570 | my %data; |
|---|
| | 571 | my $coldefs = $obj->column_defs; |
|---|
| | 572 | my $attributes = $element->getAttributeNodes; |
|---|
| | 573 | for my $attribute (@$attributes) { |
|---|
| | 574 | my $colname = $attribute->getLocalName; |
|---|
| | 575 | #if (('datetime' eq $coldefs->{$colname}{type}) || ('timestamp' eq $coldefs->{$colname}{type})) { |
|---|
| | 576 | # $data{$colname} = MT::Util::iso2ts(undef, $attribute->getNodeValue); |
|---|
| | 577 | #} else { |
|---|
| | 578 | $data{$colname} = $attribute->getNodeValue; |
|---|
| | 579 | #} |
|---|
| | 580 | } |
|---|
| | 581 | |
|---|
| | 582 | my $success = 1; |
|---|
| | 583 | my $parent_names = $obj->parent_names; |
|---|
| | 584 | $success = $obj->restore_parent_ids(\%data, $objects) if scalar(keys %$parent_names); |
|---|
| | 585 | if (!$success) { |
|---|
| | 586 | $cb->(MT->translate("Restoring [_1] (ID: [_2]) was deferred because its parents objects have not been restored yet.\n", $class, $data{id})); |
|---|
| | 587 | $deferred->{$class . '#' . $data{id}} = 1; |
|---|
| | 588 | return undef; |
|---|
| | 589 | } |
|---|
| | 590 | |
|---|
| | 591 | $cb->(MT->translate("Restoring [_1]...\n", $class)); |
|---|
| | 592 | |
|---|
| | 593 | my @extension_names; |
|---|
| | 594 | my $child_element_names = $obj->children_names; |
|---|
| | 595 | my $nodeset = $xp->find("*", $element); |
|---|
| | 596 | for my $index (1..$nodeset->size()) { |
|---|
| | 597 | my $node = $nodeset->get_node($index); |
|---|
| | 598 | next if !($node->isa('XML::XPath::Node::Element')); |
|---|
| | 599 | |
|---|
| | 600 | my $ns = $node->getNamespace($node->getPrefix); |
|---|
| | 601 | if ($ns && ($namespace eq $ns->getExpanded)) { |
|---|
| | 602 | if (!exists($child_element_names->{$node->getLocalName})) { |
|---|
| | 603 | $data{$node->getLocalName} = MT::Util::decode_xml($node->string_value); |
|---|
| | 604 | } |
|---|
| | 605 | } elsif ($ns) { |
|---|
| | 606 | push @extension_names, $node->getLocalName; |
|---|
| | 607 | } |
|---|
| | 608 | } |
|---|
| | 609 | |
|---|
| | 610 | my $old_id = $data{id}; |
|---|
| | 611 | delete $data{id}; |
|---|
| | 612 | $obj->set_values(\%data); |
|---|
| | 613 | $obj->save or |
|---|
| | 614 | $cb->($obj->errstr . "\n"), return undef; |
|---|
| | 615 | $cb->(MT->translate("[_1] [_2] (ID: [_3]) has been restored successfully with new ID: [_4]\n", |
|---|
| | 616 | $element->getLocalName =~ m/^[aeiou]/i ? 'An' : 'A', |
|---|
| | 617 | $element->getLocalName, |
|---|
| | 618 | $old_id, |
|---|
| | 619 | $obj->id) |
|---|
| | 620 | ); |
|---|
| | 621 | my $key = "$class#$old_id"; |
|---|
| | 622 | delete $deferred->{$key} if exists $deferred->{$key}; |
|---|
| | 623 | $objects->{$key} = $obj; |
|---|
| | 624 | |
|---|
| | 625 | for my $name (keys %$child_element_names) { |
|---|
| | 626 | my $children_set = $xp->find("*[local-name()='$name']", $element); |
|---|
| | 627 | for my $index2 (1..$children_set->size()) { |
|---|
| | 628 | my $node = $children_set->get_node($index2); |
|---|
| | 629 | my $ns = $node->getNamespace($node->getPrefix); |
|---|
| | 630 | next if !$ns || $namespace ne $ns->getExpanded; |
|---|
| | 631 | |
|---|
| | 632 | $param{XmlNode} = $node; |
|---|
| | 633 | my $class = $child_element_names->{$name}; |
|---|
| | 634 | eval "require $class;"; |
|---|
| | 635 | my $child = $class->from_xml(%param); |
|---|
| | 636 | next if !defined($child); |
|---|
| | 637 | my $child_old_id = $node->getAttribute('id'); |
|---|
| | 638 | my $grand_children_names = $child->children_names; |
|---|
| | 639 | if (scalar(keys %$grand_children_names)) { |
|---|
| | 640 | my $child_key = "$class#$child_old_id"; |
|---|
| | 641 | $objects->{$child_key} = $child; |
|---|
| | 642 | } |
|---|
| | 643 | } |
|---|
| | 644 | } |
|---|
| | 645 | |
|---|
| | 646 | for my $ext_name (@extension_names) { |
|---|
| | 647 | my $extension_set = $xp->find("*[local-name()='$ext_name']", $element); |
|---|
| | 648 | for my $index3 (1..$extension_set->size()) { |
|---|
| | 649 | my $ext_node = $extension_set->get_node($index3); |
|---|
| | 650 | my $ns = $ext_node->getNamespace($ext_node->getPrefix); |
|---|
| | 651 | next if !$ns || $namespace eq $ns->getExpanded; |
|---|
| | 652 | |
|---|
| | 653 | MT->run_callbacks('Restore.' . $obj->datasource . ':' . $ns->getExpanded, |
|---|
| | 654 | $xp, $ext_node, $obj, $objects, $deferred, $cb); |
|---|
| | 655 | } |
|---|
| | 656 | } |
|---|
| | 657 | $obj; |
|---|
| | 658 | } |
|---|
| | 659 | |
|---|
| | 660 | package MT::Asset; |
|---|
| | 661 | |
|---|
| | 662 | sub children_to_xml { |
|---|
| | 663 | my $obj = shift; |
|---|
| | 664 | my ($namespace, $args) = @_; |
|---|
| | 665 | my $xml = ''; |
|---|
| | 666 | |
|---|
| | 667 | require MT::ObjectTag; |
|---|
| | 668 | my $offset = 0; |
|---|
| | 669 | while (1) { |
|---|
| | 670 | my @objecttags = MT::ObjectTag->load( |
|---|
| | 671 | { object_id => $obj->id, object_datasource => $obj->datasource }, |
|---|
| | 672 | { offset => $offset, limit => 50, } |
|---|
| | 673 | ); |
|---|
| | 674 | last unless @objecttags; |
|---|
| | 675 | $offset += scalar @objecttags; |
|---|
| | 676 | for my $objecttag (@objecttags) { |
|---|
| | 677 | $xml .= $objecttag->to_xml($namespace, $args) . "\n" if $objecttag->to_backup; |
|---|
| | 678 | } |
|---|
| | 679 | } |
|---|
| | 680 | |
|---|
| | 681 | $xml; |
|---|
| | 682 | } |
|---|
| | 683 | |
|---|
| | 684 | sub children_names { |
|---|
| | 685 | my $obj = shift; |
|---|
| | 686 | my $children = { |
|---|
| | 687 | objecttag => 'MT::ObjectTag', |
|---|
| | 688 | }; |
|---|
| | 689 | $children; |
|---|
| | 690 | } |
|---|
| | 691 | |
|---|
| | 692 | sub parent_names { |
|---|
| | 693 | my $obj = shift; |
|---|
| | 694 | my $parents = { |
|---|
| | 695 | blog => 'MT::Blog', |
|---|
| | 696 | }; |
|---|
| | 697 | $parents; |
|---|
| | 698 | } |
|---|
| | 699 | |
|---|
| | 700 | package MT::Association; |
|---|
| | 701 | |
|---|
| | 702 | sub parent_names { |
|---|
| | 703 | my $obj = shift; |
|---|
| | 704 | my $parents = { |
|---|
| | 705 | blog => 'MT::Blog', |
|---|
| | 706 | author => 'MT::Author', |
|---|
| | 707 | role => 'MT::Role', |
|---|
| | 708 | }; |
|---|
| | 709 | $parents; |
|---|
| | 710 | } |
|---|
| | 711 | |
|---|
| | 712 | package MT::Author; |
|---|
| | 713 | |
|---|
| | 714 | sub children_names { |
|---|
| | 715 | my $obj = shift; |
|---|
| | 716 | my $children = { |
|---|
| | 717 | permission => 'MT::Permission', |
|---|
| | 718 | association => 'MT::Association', |
|---|
| | 719 | #entry => 'MT::Entry', ## An author is a parent of a category/entry |
|---|
| | 720 | #category => 'MT::Category', ## but a category/entry is not a child of an author |
|---|
| | 721 | ## otherwise they duplicate in restore operation. |
|---|
| | 722 | ## Also, <author> must come before <category> and |
|---|
| | 723 | ## <entry> in the backup file. |
|---|
| | 724 | }; |
|---|
| | 725 | $children; |
|---|
| | 726 | } |
|---|
| | 727 | |
|---|
| | 728 | package MT::Blog; |
|---|
| | 729 | |
|---|
| | 730 | sub children_names { |
|---|
| | 731 | my $obj = shift; |
|---|
| | 732 | my $children = { |
|---|
| | 733 | placement => 'MT::Placement', |
|---|
| | 734 | permission => 'MT::Permission', |
|---|
| | 735 | notification => 'MT::Notification', |
|---|
| | 736 | association => 'MT::Association', |
|---|
| | 737 | fileinfo => 'MT::FileInfo', |
|---|
| | 738 | #template => 'MT::Template', |
|---|
| | 739 | #entry => 'MT::Entry', ## A blog is a parent of an entry/a template but |
|---|
| | 740 | ## they are not a child of a blog |
|---|
| | 741 | ## otherwise they duplicate in restore operation. |
|---|
| | 742 | ## Also, <blog> must come before <entry> and <template>. |
|---|
| | 743 | }; |
|---|
| | 744 | $children; |
|---|
| | 745 | } |
|---|
| | 746 | |
|---|
| | 747 | package MT::Category; |
|---|
| | 748 | |
|---|
| | 749 | sub to_backup { |
|---|
| | 750 | $_[0]->parent ? 0 : 1; |
|---|
| | 751 | } |
|---|
| | 752 | |
|---|
| | 753 | sub children_to_xml { |
|---|
| | 754 | my $obj = shift; |
|---|
| | 755 | my ($namespace, $args) = @_; |
|---|
| | 756 | my $xml = ''; |
|---|
| | 757 | |
|---|
| | 758 | #require MT::Trackback; |
|---|
| | 759 | #my $tb = MT::Trackback->load({ category_id => $obj->id }); |
|---|
| | 760 | #if ($tb) { |
|---|
| | 761 | # require MT::TBPing; |
|---|
| | 762 | # my $offset = 0; |
|---|
| | 763 | # while (1) { |
|---|
| | 764 | # my @pings = MT::TBPing->load( |
|---|
| | 765 | # { tb_id => $tb->id, }, |
|---|
| | 766 | # { offset => $offset, limit => 50, } |
|---|
| | 767 | # ); |
|---|
| | 768 | # last unless @pings; |
|---|
| | 769 | # $offset += scalar @pings; |
|---|
| | 770 | # for my $ping (@pings) { |
|---|
| | 771 | # $xml .= $ping->to_xml($namespace, $args) . "\n" if $ping->to_backup; |
|---|
| | 772 | # } |
|---|
| | 773 | # } |
|---|
| | 774 | #} |
|---|
| | 775 | |
|---|
| | 776 | my $offset = 0; |
|---|
| | 777 | while (1) { |
|---|
| | 778 | my @placements = MT::Placement->load( |
|---|
| | 779 | { 'category_id' => $obj->id, }, |
|---|
| | 780 | { offset => $offset, limit => 50, } |
|---|
| | 781 | ); |
|---|
| | 782 | last unless @placements; |
|---|
| | 783 | $offset += scalar @placements; |
|---|
| | 784 | for my $placement (@placements) { |
|---|
| | 785 | $xml .= $placement->to_xml($namespace, $args) . "\n" if $placement->to_backup; |
|---|
| | 786 | } |
|---|
| | 787 | } |
|---|
| | 788 | |
|---|
| | 789 | my @children = $obj->children_categories; |
|---|
| | 790 | return $xml unless @children; |
|---|
| | 791 | for my $child (@children) { |
|---|
| | 792 | $xml .= $child->to_xml($namespace, $args) . "\n"; |
|---|
| | 793 | } |
|---|
| | 794 | $xml; |
|---|
| | 795 | } |
|---|
| | 796 | |
|---|
| | 797 | sub children_names { |
|---|
| | 798 | my $obj = shift; |
|---|
| | 799 | my $children = { |
|---|
| | 800 | #tbping => 'MT::TBPing', |
|---|
| | 801 | category => 'MT::Category', |
|---|
| | 802 | placement => 'MT::Placement', |
|---|
| | 803 | fileinfo => 'MT::FileInfo', |
|---|
| | 804 | }; |
|---|
| | 805 | $children; |
|---|
| | 806 | } |
|---|
| | 807 | |
|---|
| | 808 | sub parent_names { |
|---|
| | 809 | my $obj = shift; |
|---|
| | 810 | my $parents = { |
|---|
| | 811 | blog => 'MT::Blog', |
|---|
| | 812 | author => 'MT::Author', |
|---|
| | 813 | }; |
|---|
| | 814 | $parents; |
|---|
| | 815 | } |
|---|
| | 816 | |
|---|
| | 817 | sub restore_parent_ids { |
|---|
| | 818 | my $obj = shift; |
|---|
| | 819 | my ($data, $objects) = @_; |
|---|
| | 820 | |
|---|
| | 821 | my $parent_names = $obj->parent_names; |
|---|
| | 822 | |
|---|
| | 823 | my $done = -1; |
|---|
| | 824 | for my $parent_element_name (keys %$parent_names) { |
|---|
| | 825 | my $parent_class_name = $parent_names->{$parent_element_name}; |
|---|
| | 826 | my $old_id = $data->{$parent_element_name . '_id'}; |
|---|
| | 827 | my $new_obj = $objects->{"$parent_class_name#$old_id"}; |
|---|
| | 828 | next if !(defined($new_obj) && $new_obj); |
|---|
| | 829 | $data->{$parent_element_name . '_id'} = $new_obj->id; |
|---|
| | 830 | $done++; |
|---|
| | 831 | } |
|---|
| | 832 | my $old_id = $data->{'parent'}; |
|---|
| | 833 | if (defined($old_id) && ($old_id > 0)) { |
|---|
| | 834 | my $new_obj = $objects->{"MT::Category#$old_id"}; |
|---|
| | 835 | if (defined($new_obj) && $new_obj) { |
|---|
| | 836 | $data->{'parent'} = $new_obj->id; |
|---|
| | 837 | $done++; |
|---|
| | 838 | } |
|---|
| | 839 | } else { |
|---|
| | 840 | $done++; |
|---|
| | 841 | } |
|---|
| | 842 | (scalar(keys(%$parent_names)) == $done) ? 1 : 0; |
|---|
| | 843 | } |
|---|
| | 844 | |
|---|
| | 845 | package MT::Comment; |
|---|
| | 846 | |
|---|
| | 847 | ## To avoid duplicates... |
|---|
| | 848 | #sub children_names { |
|---|
| | 849 | # my $obj = shift; |
|---|
| | 850 | # my $children = { |
|---|
| | 851 | # author -> 'MT::Author', |
|---|
| | 852 | # }; |
|---|
| | 853 | # $children; |
|---|
| | 854 | #} |
|---|
| | 855 | |
|---|
| | 856 | sub parent_names { |
|---|
| | 857 | my $obj = shift; |
|---|
| | 858 | my $parents = { |
|---|
| | 859 | entry => 'MT::Entry', |
|---|
| | 860 | blog => 'MT::Blog', |
|---|
| | 861 | }; |
|---|
| | 862 | $parents; |
|---|
| | 863 | } |
|---|
| | 864 | |
|---|
| | 865 | sub restore_parent_ids { |
|---|
| | 866 | my $obj = shift; |
|---|
| | 867 | my ($data, $objects) = @_; |
|---|
| | 868 | |
|---|
| | 869 | my $parent_names = $obj->parent_names; |
|---|
| | 870 | |
|---|
| | 871 | my $done = -1; |
|---|
| | 872 | for my $parent_element_name (keys %$parent_names) { |
|---|
| | 873 | my $parent_class_name = $parent_names->{$parent_element_name}; |
|---|
| | 874 | my $old_id = $data->{$parent_element_name . '_id'}; |
|---|
| | 875 | my $new_obj = $objects->{"$parent_class_name#$old_id"}; |
|---|
| | 876 | next if !(defined($new_obj) && $new_obj); |
|---|
| | 877 | $data->{$parent_element_name . '_id'} = $new_obj->id; |
|---|
| | 878 | $done++; |
|---|
| | 879 | } |
|---|
| | 880 | my $old_id = $data->{'commenter_id'}; |
|---|
| | 881 | if (defined($old_id) && ($old_id > 0)) { |
|---|
| | 882 | my $new_obj = $objects->{"MT::Author#$old_id"}; |
|---|
| | 883 | if (defined($new_obj) && $new_obj) { |
|---|
| | 884 | $data->{'commenter_id'} = $new_obj->id; |
|---|
| | 885 | $done++; |
|---|
| | 886 | } |
|---|
| | 887 | } else { |
|---|
| | 888 | $done++; |
|---|
| | 889 | } |
|---|
| | 890 | (scalar(keys(%$parent_names)) == $done) ? 1 : 0; |
|---|
| | 891 | } |
|---|
| | 892 | |
|---|
| | 893 | package MT::Entry; |
|---|
| | 894 | |
|---|
| | 895 | sub children_to_xml { |
|---|
| | 896 | my $obj = shift; |
|---|
| | 897 | my ($namespace, $args) = @_; |
|---|
| | 898 | |
|---|
| | 899 | my $xml = ''; |
|---|
| | 900 | |
|---|
| | 901 | $xml .= $obj->_entry_child_to_xml('MT::Placement'); |
|---|
| | 902 | |
|---|
| | 903 | require MT::ObjectTag; |
|---|
| | 904 | my $offset = 0; |
|---|
| | 905 | while (1) { |
|---|
| | 906 | my @objecttags = MT::ObjectTag->load( |
|---|
| | 907 | { object_id => $obj->id, object_datasource => $obj->datasource }, |
|---|
| | 908 | { offset => $offset, limit => 50, } |
|---|
| | 909 | ); |
|---|
| | 910 | last unless @objecttags; |
|---|
| | 911 | $offset += scalar @objecttags; |
|---|
| | 912 | for my $objecttag (@objecttags) { |
|---|
| | 913 | $xml .= $objecttag->to_xml($namespace, $args) . "\n" if $objecttag->to_backup; |
|---|
| | 914 | } |
|---|
| | 915 | } |
|---|
| | 916 | |
|---|
| | 917 | #require MT::Trackback; |
|---|
| | 918 | #my $tb = MT::Trackback->load({ entry_id => $obj->id }); |
|---|
| | 919 | #if ($tb) { |
|---|
| | 920 | # require MT::TBPing; |
|---|
| | 921 | # my $offset = 0; |
|---|
| | 922 | # while (1) { |
|---|
| | 923 | # my @pings = MT::TBPing->load( |
|---|
| | 924 | # { tb_id => $tb->id, }, |
|---|
| | 925 | # { offset => $offset, limit => 50, } |
|---|
| | 926 | # ); |
|---|
| | 927 | # last unless @pings; |
|---|
| | 928 | # $offset += scalar @pings; |
|---|
| | 929 | # for my $ping (@pings) { |
|---|
| | 930 | # $xml .= $ping->to_xml($namespace, $args) . "\n" if $ping->to_backup; |
|---|
| | 931 | # } |
|---|
| | 932 | # } |
|---|
| | 933 | #} |
|---|
| | 934 | |
|---|
| | 935 | $xml .= $obj->_entry_child_to_xml('MT::FileInfo', $namespace, $args); |
|---|
| | 936 | |
|---|
| | 937 | $xml; |
|---|
| | 938 | } |
|---|
| | 939 | |
|---|
| | 940 | sub _entry_child_to_xml { |
|---|
| | 941 | my $obj = shift; |
|---|
| | 942 | my ($child_class, $namespace, $args) = @_; |
|---|
| | 943 | my $xml = ''; |
|---|
| | 944 | |
|---|
| | 945 | eval "require $child_class"; |
|---|
| | 946 | my $err = $@; |
|---|
| | 947 | return $err if defined($err) && $err; |
|---|
| | 948 | |
|---|
| | 949 | my $offset = 0; |
|---|
| | 950 | while (1) { |
|---|
| | 951 | my @objects = $child_class->load( |
|---|
| | 952 | { entry_id => $obj->id, }, |
|---|
| | 953 | { offset => $offset, limit => 50, } |
|---|
| | 954 | ); |
|---|
| | 955 | last unless @objects; |
|---|
| | 956 | $offset += scalar @objects; |
|---|
| | 957 | for my $object (@objects) { |
|---|
| | 958 | $xml .= $object->to_xml($namespace, $args) . "\n" if $object->to_backup; |
|---|
| | 959 | } |
|---|
| | 960 | } |
|---|
| | 961 | $xml; |
|---|
| | 962 | } |
|---|
| | 963 | |
|---|
| | 964 | sub children_names { |
|---|
| | 965 | my $obj = shift; |
|---|
| | 966 | my $children = { |
|---|
| | 967 | objecttag => 'MT::ObjectTag', |
|---|
| | 968 | placement => 'MT::Placement', |
|---|
| | 969 | fileinfo => 'MT::FileInfo', |
|---|
| | 970 | }; |
|---|
| | 971 | $children; |
|---|
| | 972 | } |
|---|
| | 973 | |
|---|
| | 974 | sub parent_names { |
|---|
| | 975 | my $obj = shift; |
|---|
| | 976 | my $parents = { |
|---|
| | 977 | blog => 'MT::Blog', |
|---|
| | 978 | author => 'MT::Author', |
|---|
| | 979 | }; |
|---|
| | 980 | $parents; |
|---|
| | 981 | } |
|---|
| | 982 | |
|---|
| | 983 | package MT::Notification; |
|---|
| | 984 | |
|---|
| | 985 | sub parent_names { |
|---|
| | 986 | my $obj = shift; |
|---|
| | 987 | my $parents = { |
|---|
| | 988 | blog => 'MT::Blog', |
|---|
| | 989 | }; |
|---|
| | 990 | $parents; |
|---|
| | 991 | } |
|---|
| | 992 | |
|---|
| | 993 | package MT::ObjectTag; |
|---|
| | 994 | |
|---|
| | 995 | sub parent_names { |
|---|
| | 996 | my $obj = shift; |
|---|
| | 997 | my $parents = { |
|---|
| | 998 | blog => 'MT::Blog', |
|---|
| | 999 | tag => 'MT::Tag', |
|---|
| | 1000 | entry => 'MT::Entry', |
|---|
| | 1001 | asset => 'MT::Asset', |
|---|
| | 1002 | }; |
|---|
| | 1003 | $parents; |
|---|
| | 1004 | } |
|---|
| | 1005 | |
|---|
| | 1006 | sub restore_parent_ids { |
|---|
| | 1007 | my $obj = shift; |
|---|
| | 1008 | my ($data, $objects) = @_; |
|---|
| | 1009 | |
|---|
| | 1010 | my $parent_names = $obj->parent_names; |
|---|
| | 1011 | |
|---|
| | 1012 | my $done = 1; |
|---|
| | 1013 | for my $parent_element_name (keys %$parent_names) { |
|---|
| | 1014 | my $parent_class_name = $parent_names->{$parent_element_name}; |
|---|
| | 1015 | my $old_id = $data->{$parent_element_name . '_id'}; |
|---|
| | 1016 | my $new_obj = $objects->{"$parent_class_name#$old_id"}; |
|---|
| | 1017 | next if !(defined($new_obj) && $new_obj); |
|---|
| | 1018 | $data->{$parent_element_name . '_id'} = $new_obj->id; |
|---|
| | 1019 | $done++; |
|---|
| | 1020 | } |
|---|
| | 1021 | my $old_id = $data->{'object_id'}; |
|---|
| | 1022 | if (defined($old_id) && ($old_id > 0)) { |
|---|
| | 1023 | my $class = $parent_names->{$data->{'object_datasource'}}; |
|---|
| | 1024 | my $new_obj = $objects->{"$class#$old_id"}; |
|---|
| | 1025 | if (defined($new_obj) && $new_obj) { |
|---|
| | 1026 | $data->{'object_id'} = $new_obj->id; |
|---|
| | 1027 | $done++; |
|---|
| | 1028 | } |
|---|
| | 1029 | } else { |
|---|
| | 1030 | $done++; |
|---|
| | 1031 | } |
|---|
| | 1032 | (scalar(keys(%$parent_names)) == $done) ? 1 : 0; |
|---|
| | 1033 | } |
|---|
| | 1034 | |
|---|
| | 1035 | package MT::Permission; |
|---|
| | 1036 | |
|---|
| | 1037 | sub parent_names { |
|---|
| | 1038 | my $obj = shift; |
|---|
| | 1039 | { author => 'MT::Author', blog => 'MT::Blog' }; |
|---|
| | 1040 | } |
|---|
| | 1041 | |
|---|
| | 1042 | package MT::Placement; |
|---|
| | 1043 | |
|---|
| | 1044 | sub parent_names { |
|---|
| | 1045 | my $obj = shift; |
|---|
| | 1046 | my $parents = { |
|---|
| | 1047 | category => 'MT::Category', |
|---|
| | 1048 | blog => 'MT::Blog', |
|---|
| | 1049 | entry => 'MT::Entry', |
|---|
| | 1050 | }; |
|---|
| | 1051 | $parents; |
|---|
| | 1052 | } |
|---|
| | 1053 | |
|---|
| | 1054 | package MT::Role; |
|---|
| | 1055 | |
|---|
| | 1056 | sub children_names { |
|---|
| | 1057 | my $obj = shift; |
|---|
| | 1058 | my $children = { |
|---|
| | 1059 | association => 'MT::Association', |
|---|
| | 1060 | }; |
|---|
| | 1061 | $children; |
|---|
| | 1062 | } |
|---|
| | 1063 | |
|---|
| | 1064 | package MT::Tag; |
|---|
| | 1065 | |
|---|
| | 1066 | sub children_names { |
|---|
| | 1067 | my $obj = shift; |
|---|
| | 1068 | my $children = { |
|---|
| | 1069 | objecttag => 'MT::ObjectTag', |
|---|
| | 1070 | }; |
|---|
| | 1071 | $children; |
|---|
| | 1072 | } |
|---|
| | 1073 | |
|---|
| | 1074 | package MT::TBPing; |
|---|
| | 1075 | |
|---|
| | 1076 | sub parent_names { |
|---|
| | 1077 | my $obj = shift; |
|---|
| | 1078 | my $parents = { |
|---|
| | 1079 | blog => 'MT::Blog', |
|---|
| | 1080 | }; |
|---|
| | 1081 | $parents; |
|---|
| | 1082 | } |
|---|
| | 1083 | |
|---|
| | 1084 | sub restore_parent_ids { |
|---|
| | 1085 | my $obj = shift; |
|---|
| | 1086 | my ($data, $objects) = @_; |
|---|
| | 1087 | |
|---|
| | 1088 | my $parent_names = $obj->parent_names; |
|---|
| | 1089 | |
|---|
| | 1090 | my $done = -1; |
|---|
| | 1091 | for my $parent_element_name (keys %$parent_names) { |
|---|
| | 1092 | my $parent_class_name = $parent_names->{$parent_element_name}; |
|---|
| | 1093 | my $old_id = $data->{$parent_element_name . '_id'}; |
|---|
| | 1094 | my $new_obj = $objects->{"$parent_class_name#$old_id"}; |
|---|
| | 1095 | next if !(defined($new_obj) && $new_obj); |
|---|
| | 1096 | $data->{$parent_element_name . '_id'} = $new_obj->id; |
|---|
| | 1097 | $done++; |
|---|
| | 1098 | } |
|---|
| | 1099 | my $old_tb_id = $data->{'tb_id'}; |
|---|
| | 1100 | my $new_tb; |
|---|
| | 1101 | require MT::Trackback; |
|---|
| | 1102 | my $tb = MT::Trackback->load($old_tb_id); |
|---|
| | 1103 | if (my $cid = $tb->category_id) { |
|---|
| | 1104 | my $new_obj = $objects->{"MT::Category#" . $cid}; |
|---|
| | 1105 | if (defined($new_obj) && $new_obj) { |
|---|
| | 1106 | $new_tb = MT::Trackback->load({ category_id => $new_obj->id }); |
|---|
| | 1107 | return 0 if (!defined($new_tb) || !$new_tb); |
|---|
| | 1108 | } |
|---|
| | 1109 | } elsif (my $eid = $tb->entry_id) { |
|---|
| | 1110 | my $new_obj = $objects->{"MT::Entry#" . $eid}; |
|---|
| | 1111 | if (defined($new_obj) && $new_obj) { |
|---|
| | 1112 | $new_tb = MT::Trackback->load({ entry_id => $new_obj->id }); |
|---|
| | 1113 | return 0 if (!defined($new_tb) || !$new_tb); |
|---|
| | 1114 | } |
|---|
| | 1115 | } |
|---|
| | 1116 | if (defined($new_tb) && $new_tb) { |
|---|
| | 1117 | $data->{'tb_id'} = $new_tb->id; |
|---|
| | 1118 | $done++; |
|---|
| | 1119 | } |
|---|
| | 1120 | (scalar(keys(%$parent_names)) == $done) ? 1 : 0; |
|---|
| | 1121 | } |
|---|
| | 1122 | |
|---|
| | 1123 | package MT::Template; |
|---|
| | 1124 | |
|---|
| | 1125 | sub children_names { |
|---|
| | 1126 | my $obj = shift; |
|---|
| | 1127 | my $children = { |
|---|
| | 1128 | fileinfo => 'MT::FileInfo', |
|---|
| | 1129 | templatemap => 'MT::TemplateMap', |
|---|
| | 1130 | }; |
|---|
| | 1131 | $children; |
|---|
| | 1132 | } |
|---|
| | 1133 | |
|---|
| | 1134 | sub parent_names { |
|---|
| | 1135 | my $obj = shift; |
|---|
| | 1136 | my $parents = { |
|---|
| | 1137 | blog => 'MT::Blog', |
|---|
| | 1138 | }; |
|---|
| | 1139 | $parents; |
|---|
| | 1140 | } |
|---|
| | 1141 | |
|---|
| | 1142 | package MT::TemplateMap; |
|---|
| | 1143 | |
|---|
| | 1144 | sub parent_names { |
|---|
| | 1145 | my $obj = shift; |
|---|
| | 1146 | my $parents = { |
|---|
| | 1147 | blog => 'MT::Blog', |
|---|
| | 1148 | template => 'MT::Template', |
|---|
| | 1149 | }; |
|---|
| | 1150 | $parents; |
|---|
| | 1151 | } |
|---|
| | 1152 | |
|---|
| | 1153 | package MT::Trackback; |
|---|
| | 1154 | |
|---|
| | 1155 | sub children_names { |
|---|
| | 1156 | my $obj = shift; |
|---|
| | 1157 | my $children = { |
|---|
| | 1158 | tbping => 'MT::TBPing', |
|---|
| | 1159 | }; |
|---|
| | 1160 | $children; |
|---|
| | 1161 | } |
|---|
| | 1162 | |
|---|
| | 1163 | sub children_to_xml { |
|---|
| | 1164 | my $obj = shift; |
|---|
| | 1165 | my ($namespace, $args) = @_; |
|---|
| | 1166 | |
|---|
| | 1167 | my $t = {}; |
|---|
| | 1168 | if (defined($args)) { |
|---|
| | 1169 | my $j = $args->{'join'}; |
|---|
| | 1170 | $t = $j->[2] if defined($j); |
|---|
| | 1171 | } |
|---|
| | 1172 | |
|---|
| | 1173 | my $xml = ''; |
|---|
| | 1174 | |
|---|
| | 1175 | my $terms = { |
|---|
| | 1176 | 'tb_id' => $obj->id, |
|---|
| | 1177 | %$t, |
|---|
| | 1178 | }; |
|---|
| | 1179 | |
|---|
| | 1180 | my $offset = 0; |
|---|
| | 1181 | while (1) { |
|---|
| | 1182 | my @objects = MT::TBPing->load( |
|---|
| | 1183 | $terms, |
|---|
| | 1184 | { offset => $offset, limit => 50, } |
|---|
| | 1185 | ); |
|---|
| | 1186 | last unless @objects; |
|---|
| | 1187 | $offset += scalar @objects; |
|---|
| | 1188 | for my $object (@objects) { |
|---|
| | 1189 | $xml .= $object->to_xml($namespace) . "\n" if $object->to_backup; |
|---|
| | 1190 | } |
|---|
| | 1191 | } |
|---|
| | 1192 | $xml; |
|---|
| | 1193 | } |
|---|
| | 1194 | |
|---|
| | 1195 | sub parent_names { |
|---|
| | 1196 | my $obj = shift; |
|---|
| | 1197 | my $parents = { |
|---|
| | 1198 | entry => 'MT::Entry', |
|---|
| | 1199 | category => 'MT::Category', |
|---|
| | 1200 | }; |
|---|
| | 1201 | $parents; |
|---|
| | 1202 | } |
|---|
| | 1203 | |
|---|
| | 1204 | sub restore_parent_ids { |
|---|
| | 1205 | my $obj = shift; |
|---|
| | 1206 | my ($data, $objects) = @_; |
|---|
| | 1207 | |
|---|
| | 1208 | my $result = 0; |
|---|
| | 1209 | my $new_blog = $objects->{'MT::Blog#' . $data->{blog_id}}; |
|---|
| | 1210 | if ($new_blog) { |
|---|
| | 1211 | $data->{blog_id} = $new_blog->id; |
|---|
| | 1212 | } else { |
|---|
| | 1213 | return 0; |
|---|
| | 1214 | } |
|---|
| | 1215 | if ($data->{category_id}) { |
|---|
| | 1216 | my $new_obj = $objects->{'MT::Category#' . $data->{category_id}}; |
|---|
| | 1217 | if ($new_obj) { |
|---|
| | 1218 | $data->{category_id} = $new_obj->id; |
|---|
| | 1219 | $result = 1; |
|---|
| | 1220 | } |
|---|
| | 1221 | } elsif ($data->{entry_id}) { |
|---|
| | 1222 | my $new_obj = $objects->{'MT::Entry#' . $data->{entry_id}}; |
|---|
| | 1223 | if ($new_obj) { |
|---|
| | 1224 | $data->{entry_id} = $new_obj->id; |
|---|
| | 1225 | $result = 1; |
|---|
| | 1226 | } |
|---|
| | 1227 | } |
|---|
| | 1228 | $result; |
|---|
| | 1229 | } |
|---|
| | 1230 | |
|---|