[Rt-commit] rt branch, 4.4.1/json-initialdata, created. rt-4.4.1-1-g7775ef9

Aaron Kondziela aaron at bestpractical.com
Fri Mar 10 17:51:51 EST 2017


The branch, 4.4.1/json-initialdata has been created
        at  7775ef9d761eaf050940471db8c2b7b268f5d37e (commit)

- Log -----------------------------------------------------------------
commit 7775ef9d761eaf050940471db8c2b7b268f5d37e
Author: Aaron Kondziela <aaron at bestpractical.com>
Date:   Fri Mar 10 17:48:22 2017 -0500

    Custom initialdata handler support
    
    Adds functionality to support plugins for initialdata files that are
    written in something other than the standard perl. Built to support
    the RT::Extension::Initialdata::JSON module.
    
    Fixes T#180408

diff --git a/etc/RT_Config.pm.in b/etc/RT_Config.pm.in
index 7722558..0cd2531 100644
--- a/etc/RT_Config.pm.in
+++ b/etc/RT_Config.pm.in
@@ -2918,6 +2918,40 @@ Set($ExternalStorageDirectLink, 0);
 
 =back
 
+
+=head1 Initialdata Formats
+
+RT Supports pluggable data format parsers for F<initialdata> files.
+
+If you add format handlers, note that you can remove the perl entry if you
+don't want it available. B<Removing the default perl entry may cause problems
+installing plugins and RT updates>. If so, re-enable it temporarily.
+
+=over 4
+
+=item C<$InitialdataFormatHandlers>
+
+Set the C<$InitialdataFormatHandlers> to an arrayref containing a list of
+format handler modules. The 'perl' entry is the system default, and handles
+perl-style intialdata files.
+
+    Set( $InitialdataFormatHandlers,
+         [
+            'perl',
+            'RT::Extension::Initialdata::Foo',
+            ...
+         [
+       );
+
+=back
+
+=cut
+
+Set( $InitialdataFormatHandlers, [
+    'perl',
+]);
+
+
 =head1 Lifecycles
 
 =head2 Lifecycle definitions
diff --git a/lib/RT/Handle.pm b/lib/RT/Handle.pm
index 81747ad..b1b7aa4 100644
--- a/lib/RT/Handle.pm
+++ b/lib/RT/Handle.pm
@@ -859,9 +859,66 @@ sub InsertData {
            @Catalogs, @Assets);
 
     local $@;
+
     $RT::Logger->debug("Going to load '$datafile' data file");
-    eval { require $datafile }
-      or return (0, "Couldn't load data from '$datafile' for import:\n\nERROR:". $@);
+    
+    my $datafile_content = do {
+        local $/;
+        open (my $f, '<:encoding(UTF-8)', $datafile)
+            or die "Cannot open initialdata file '$datafile' for read: $@";
+        <$f>;
+    };
+
+    my $format_handler;
+    my $handlers = RT->Config->Get('InitialdataFormatHandlers');
+    
+    foreach my $handler_candidate (@$handlers) {
+        next if $handler_candidate eq 'perl';
+        $handler_candidate->require
+            or die "Config option InitialdataFormatHandlers lists '$handler_candidate', but it failed to load:\n$@\n";
+
+        no strict 'refs';
+        if (&{"${handler_candidate}::CanLoad"}($datafile_content)) {
+            $RT::Logger->debug("Initialdata file '$datafile' can be loaded by $handler_candidate");
+            $format_handler = $handler_candidate;
+            last;
+        } else {
+            $RT::Logger->debug("Initialdata file '$datafile' can not be loaded by $handler_candidate");
+        }
+    }
+
+    if ( $format_handler ) {
+        no strict 'refs';
+        &{"${format_handler}::Load"}(
+            $datafile_content,
+            {
+                Groups          => \@Groups,
+                Users           => \@Users,
+                Members         => \@Members,
+                ACL             => \@ACL,
+                Queues          => \@Queues,
+                Classes         => \@Classes,
+                ScripActions    => \@ScripActions,
+                ScripConditions => \@ScripConditions,
+                Templates       => \@Templates,
+                CustomFields    => \@CustomFields,
+                CustomRoles     => \@CustomRoles,
+                Scrips          => \@Scrips,
+                Attributes      => \@Attributes,
+                Initial         => \@Initial,
+                Final           => \@Final,
+                Catalogs        => \@Catalogs,
+                Assets          => \@Assets,
+            },
+        ) or return (0, "Couldn't load data from '$datafile' for import:\n\nERROR:" . $@);
+    }
+    
+    if ( !$format_handler and grep(/^perl$/, @$handlers) ) {
+        # Use perl-style initialdata
+        # Note: eval of perl initialdata should only be done once
+        eval { require $datafile }
+          or return (0, "Couldn't load data from '$datafile':\nERROR:" . $@ . "\n\nDo you have the correct initialdata handler in RT_Config for this type of file?");
+    }
 
     if ( @Initial ) {
         $RT::Logger->debug("Running initial actions...");

-----------------------------------------------------------------------


More information about the rt-commit mailing list