Bug 31535: Fix warning - uninitialized value $mode in string ne (addbiblio.pl)
[srvgit] / Koha / BackgroundJob.pm
index 76bed24..e4aede3 100644 (file)
@@ -16,8 +16,7 @@ package Koha::BackgroundJob;
 # along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 use Modern::Perl;
-use JSON qw( decode_json encode_json );
-use Encode qw( encode_utf8 );
+use JSON;
 use Carp qw( croak );
 use Net::Stomp;
 use Try::Tiny qw( catch try );
@@ -101,11 +100,12 @@ sub enqueue {
     my $job_args    = $params->{job_args};
     my $job_context = $params->{job_context} // C4::Context->userenv;
     my $job_queue   = $params->{job_queue}  // 'default';
+    my $json = $self->json;
 
     my $borrowernumber = (C4::Context->userenv) ? C4::Context->userenv->{number} : undef;
     $job_context->{interface} = C4::Context->interface;
-    my $json_context = encode_json $job_context;
-    my $json_args = encode_json $job_args;
+    my $json_context = $json->encode($job_context);
+    my $json_args = $json->encode($job_args);
 
     $self->set(
         {
@@ -130,7 +130,7 @@ sub enqueue {
     };
     return unless $conn;
 
-    $json_args = encode_json $job_args;
+    $json_args = $json->encode($job_args);
     try {
         # This namespace is wrong, it must be a vhost instead.
         # But to do so it needs to be created on the server => much more work when a new Koha instance is created.
@@ -167,7 +167,7 @@ sub process {
     $args ||= {};
 
     if ( $self->context ) {
-        my $context = decode_json($self->context);
+        my $context = $self->json->decode($self->context);
         C4::Context->_new_userenv(-1);
         C4::Context->interface( $context->{interface} );
         C4::Context->set_userenv(
@@ -251,11 +251,26 @@ sub finish {
     return $self->set(
         {
             ended_on => \'NOW()',
-            data     => encode_json($data),
+            data     => $self->json->encode($data),
         }
     )->store;
 }
 
+=head3 json
+
+   my $JSON_object = $self->json;
+
+Returns a JSON object with utf8 disabled. Encoding to UTF-8 should be
+done later.
+
+=cut
+
+sub json {
+    my ( $self ) = @_;
+    $self->{_json} //= JSON->new->utf8(0); # TODO Should we allow_nonref ?
+    return $self->{_json};
+}
+
 =head3 decoded_data
 
     my $job_data = $self->decoded_data;
@@ -267,7 +282,7 @@ Returns the decoded JSON contents from $self->data.
 sub decoded_data {
     my ($self) = @_;
 
-    return $self->data ? decode_json( $self->data ) : undef;
+    return $self->data ? $self->json->decode( $self->data ) : undef;
 }
 
 =head3 set_encoded_data
@@ -281,7 +296,7 @@ Serializes I<$data> as a JSON string and sets the I<data> attribute with it.
 sub set_encoded_data {
     my ( $self, $data ) = @_;
 
-    return $self->data( $data ? encode_json($data) : undef );
+    return $self->data( $data ? $self->json->encode($data) : undef );
 }
 
 =head3 job_type
@@ -302,7 +317,7 @@ sub messages {
     my ( $self ) = @_;
 
     my @messages;
-    my $data_dump = decode_json encode_utf8 $self->data;
+    my $data_dump = $self->json->decode($self->data);
     if ( exists $data_dump->{messages} ) {
         @messages = @{ $data_dump->{messages} };
     }
@@ -319,7 +334,7 @@ Report of the job.
 sub report {
     my ( $self ) = @_;
 
-    my $data_dump = decode_json encode_utf8 $self->data;
+    my $data_dump = $self->json->decode($self->data);
     return $data_dump->{report} || {};
 }
 
@@ -380,7 +395,7 @@ Returns the available types to class mappings.
 sub type_to_class_mapping {
     my ($self) = @_;
 
-    my $plugins_mapping = $self->plugin_types_to_classes;
+    my $plugins_mapping = ( C4::Context->config("enable_plugins") ) ? $self->plugin_types_to_classes : {};
 
     return ($plugins_mapping)
       ? { %{ $self->core_types_to_classes }, %$plugins_mapping }