[Rt-commit] r5760 - in rt/branches/3.7-EXPERIMENTAL: .
lib/t/regression/shredder
ruz at bestpractical.com
ruz at bestpractical.com
Sat Aug 12 01:19:39 EDT 2006
Author: ruz
Date: Sat Aug 12 01:19:38 2006
New Revision: 5760
Added:
rt/branches/3.7-EXPERIMENTAL/lib/RT/Shredder/Plugin/Summary.pm
rt/branches/3.7-EXPERIMENTAL/lib/t/regression/shredder/03plugin_summary.t
Modified:
rt/branches/3.7-EXPERIMENTAL/ (props changed)
rt/branches/3.7-EXPERIMENTAL/lib/t/regression/shredder/00load.t
rt/branches/3.7-EXPERIMENTAL/lib/t/regression/shredder/03plugin.t
Log:
r3644 at cubic-pc: cubic | 2006-08-12 09:28:54 +0400
Shredder
* add Summary plugin, that dumps objects in user readable format,
couldn't be used to restore data, but for plain text archive
Added: rt/branches/3.7-EXPERIMENTAL/lib/RT/Shredder/Plugin/Summary.pm
==============================================================================
--- (empty file)
+++ rt/branches/3.7-EXPERIMENTAL/lib/RT/Shredder/Plugin/Summary.pm Sat Aug 12 01:19:38 2006
@@ -0,0 +1,112 @@
+package RT::Shredder::Plugin::Summary;
+
+use strict;
+use warnings FATAL => 'all';
+
+use base qw(RT::Shredder::Plugin::SQLDump);
+
+sub AppliesToStates { return 'before any action' }
+
+sub TestArgs
+{
+ my $self = shift;
+ my %args = (file_name => '', @_);
+ unless( $args{'file_name'} ) {
+ require POSIX;
+ $args{'file_name'} = POSIX::strftime( "summary-%Y%m%dT%H%M%S.XXXX.txt", gmtime );
+ }
+ return $self->SUPER::TestArgs( %args );
+}
+
+sub Run
+{
+ my $self = shift;
+ my %args = ( Object => undef, @_ );
+ my $class = ref $args{'Object'};
+ $class =~ s/^RT:://;
+ $class =~ s/:://g;
+ my $method = 'WriteDown'. $class;
+ $method = 'WriteDownDefault' unless $self->can($method);
+ return $self->$method( %args );
+ return 1;
+}
+
+my %skip_refs_to = ();
+
+sub WriteDownDefault {
+ my $self = shift;
+ my %args = ( Object => undef, @_ );
+ return $self->_WriteDownHash(
+ $args{'Object'},
+ $self->_MakeHash( $args{'Object'} ),
+ );
+}
+
+# TODO: cover other objects
+
+sub WriteDownCachedGroupMember { return 1 }
+sub WriteDownPrincipal { return 1 }
+
+sub WriteDownGroup {
+ my $self = shift;
+ my %args = ( Object => undef, @_ );
+ if ( $args{'Object'}->Domain =~ /-Role$/ ) {
+ return $skip_refs_to{ $args{'Object'}->_AsString } = 1;
+ }
+ return $self->WriteDownDefault( %args );
+}
+
+sub WriteDownTransaction {
+ my $self = shift;
+ my %args = ( Object => undef, @_ );
+
+ my $props = $self->_MakeHash( $args{'Object'} );
+ $props->{'Object'} = delete $props->{'ObjectType'};
+ $props->{'Object'} .= '-'. delete $props->{'ObjectId'}
+ if $props->{'ObjectId'};
+ return 1 if $skip_refs_to{ $props->{'Object'} };
+
+ delete $props->{$_} foreach grep
+ !defined $props->{$_} || $props->{$_} eq '', keys %$props;
+
+ return $self->_WriteDownHash( $args{'Object'}, $props );
+}
+
+sub _MakeHash {
+ my ($self, $obj) = @_;
+ my $hash = $self->__MakeHash( $obj );
+ foreach (grep exists $hash->{$_}, qw(Creator LastUpdatedBy)) {
+ my $method = $_ .'Obj';
+ my $u = $obj->$method();
+ $hash->{ $_ } = $u->EmailAddress || $u->Name || $u->_AsString;
+ }
+ return $hash;
+}
+
+sub __MakeHash {
+ my ($self, $obj) = @_;
+ my %hash;
+ $hash{ $_ } = $obj->$_()
+ foreach sort keys %{ $obj->_ClassAccessible };
+ return \%hash;
+}
+
+sub _WriteDownHash {
+ my ($self, $obj, $hash) = @_;
+ return (0, 'no handle') unless my $fh = $self->{'opt'}{'file_handle'};
+
+ print $fh "=== ". $obj->_AsString ." ===\n"
+ or return (0, "Couldn't write to filehandle");
+
+ foreach my $key( sort keys %$hash ) {
+ my $val = $hash->{ $key };
+ next unless defined $val;
+ $val =~ s/\n/\n /g;
+ print $fh $key .': '. $val ."\n"
+ or return (0, "Couldn't write to filehandle");
+ }
+ print $fh "\n" or return (0, "Couldn't write to filehandle");
+ return 1;
+}
+
+1;
Modified: rt/branches/3.7-EXPERIMENTAL/lib/t/regression/shredder/00load.t
==============================================================================
--- rt/branches/3.7-EXPERIMENTAL/lib/t/regression/shredder/00load.t (original)
+++ rt/branches/3.7-EXPERIMENTAL/lib/t/regression/shredder/00load.t Sat Aug 12 01:19:38 2006
@@ -1,4 +1,4 @@
-use Test::More tests => 10;
+use Test::More tests => 11;
BEGIN { require "lib/t/regression/shredder/utils.pl" }
@@ -17,4 +17,5 @@
# dump plugins
use_ok("RT::Shredder::Plugin::Base::Dump");
use_ok("RT::Shredder::Plugin::SQLDump");
+use_ok("RT::Shredder::Plugin::Summary");
Modified: rt/branches/3.7-EXPERIMENTAL/lib/t/regression/shredder/03plugin.t
==============================================================================
--- rt/branches/3.7-EXPERIMENTAL/lib/t/regression/shredder/03plugin.t (original)
+++ rt/branches/3.7-EXPERIMENTAL/lib/t/regression/shredder/03plugin.t Sat Aug 12 01:19:38 2006
@@ -7,7 +7,7 @@
use Test::Deep;
BEGIN { require "lib/t/regression/shredder/utils.pl"; }
-my @PLUGINS = qw(Attachments Base Objects Tickets Users);
+my @PLUGINS = sort qw(Attachments Base Objects SQLDump Summary Tickets Users);
plan tests => 7 + 3 * @PLUGINS;
use_ok('RT::Shredder::Plugin');
Added: rt/branches/3.7-EXPERIMENTAL/lib/t/regression/shredder/03plugin_summary.t
==============================================================================
--- (empty file)
+++ rt/branches/3.7-EXPERIMENTAL/lib/t/regression/shredder/03plugin_summary.t Sat Aug 12 01:19:38 2006
@@ -0,0 +1,18 @@
+#!/usr/bin/perl -w
+
+use strict;
+use warnings;
+
+use Test::More;
+use Test::Deep;
+BEGIN { require "lib/t/regression/shredder/utils.pl"; }
+
+plan tests => 4;
+
+use_ok('RT::Shredder::Plugin');
+my $plugin_obj = new RT::Shredder::Plugin;
+isa_ok($plugin_obj, 'RT::Shredder::Plugin');
+my ($status, $msg) = $plugin_obj->LoadByName('Summary');
+ok($status, 'loaded summary plugin') or diag "error: $msg";
+isa_ok($plugin_obj, 'RT::Shredder::Plugin::Summary');
+
More information about the Rt-commit
mailing list