From b71595efb6d0f61342e5edda365c85d898ae9eef Mon Sep 17 00:00:00 2001
From: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Date: Thu, 11 Feb 2021 10:19:29 +0100
Subject: [PATCH] Bug 22824: Replace YAML::Syck with YAML::XS

---
 C4/Installer.pm                                     |  8 ++++----
 C4/Message.pm                                       |  5 +++--
 Koha/OAI/Server/Repository.pm                       |  4 ++--
 Koha/SearchEngine/Elasticsearch.pm                  |  8 ++++----
 admin/preferences.pl                                |  8 ++++----
 admin/systempreferences.pl                          |  4 ++--
 misc/admin/koha-preferences                         | 11 +++++------
 misc/cronjobs/cloud-kw.pl                           |  4 ++--
 misc/translator/LangInstaller.pm                    | 13 ++++++-------
 misc/translator/xgettext-installer                  |  6 ++----
 misc/translator/xgettext-pref                       |  6 ++----
 .../Koha/SearchEngine/Elasticsearch/Reset.t         |  2 +-
 12 files changed, 37 insertions(+), 42 deletions(-)

diff --git a/C4/Installer.pm b/C4/Installer.pm
index a004cb8150..05b8025dbe 100644
--- a/C4/Installer.pm
+++ b/C4/Installer.pm
@@ -21,7 +21,7 @@ use Modern::Perl;
 
 use Encode qw( encode is_utf8 );
 use DBIx::RunSQL;
-use YAML::Syck qw( LoadFile );
+use YAML::XS;
 use C4::Context;
 use DBI;
 use Koha;
@@ -153,7 +153,7 @@ sub marc_framework_sql_list {
             my ( $name, $ext ) = split /\./, $_;
             my @lines;
             if ( $ext =~ /yml/ ) {
-                my $yaml = LoadFile("$dir/$requirelevel/$name\.$ext");
+                my $yaml = YAML::XS::LoadFile("$dir/$requirelevel/$name\.$ext");
                 @lines = map { Encode::decode('UTF-8', $_) } @{ $yaml->{'description'} };
             } else {
                 open my $fh, "<:encoding(UTF-8)", "$dir/$requirelevel/$name.txt";
@@ -236,7 +236,7 @@ sub sample_data_sql_list {
             my ( $name, $ext ) = split /\./, $_;
             my @lines;
             if ( $ext =~ /yml/ ) {
-                my $yaml = LoadFile("$dir/$requirelevel/$name\.$ext");
+                my $yaml = YAML::XS::LoadFile("$dir/$requirelevel/$name\.$ext");
                 @lines = map { Encode::decode('UTF-8', $_) } @{ $yaml->{'description'} };
             } else {
                 open my $fh, "<:encoding(UTF-8)", "$dir/$requirelevel/$name.txt";
@@ -560,7 +560,7 @@ sub load_sql {
         }
         else {                                                                       # YAML files
             eval {
-                my $yaml         = LoadFile( $filename );                            # Load YAML
+                my $yaml         = YAML::XS::LoadFile( $filename );                            # Load YAML
                 for my $table ( @{ $yaml->{'tables'} } ) {
                     my $query_info   = process_yml_table($table);
                     my $query        = $query_info->{query};
diff --git a/C4/Message.pm b/C4/Message.pm
index e4dafdb215..659ff96aeb 100644
--- a/C4/Message.pm
+++ b/C4/Message.pm
@@ -23,7 +23,8 @@ use strict;
 use warnings;
 use C4::Context;
 use C4::Letters;
-use YAML::Syck;
+use YAML::XS;
+use Encode;
 use Carp;
 
 =head1 NAME
@@ -282,7 +283,7 @@ sub metadata {
         $self->content($self->render_metadata);
         return $data;
     } else {
-        return Load($self->{metadata});
+        return YAML::XS::Load(Encode::encode_utf8($self->{metadata}));
     }
 }
 
diff --git a/Koha/OAI/Server/Repository.pm b/Koha/OAI/Server/Repository.pm
index ca34ee8d00..ad18c2dc13 100644
--- a/Koha/OAI/Server/Repository.pm
+++ b/Koha/OAI/Server/Repository.pm
@@ -32,7 +32,7 @@ use Koha::OAI::Server::GetRecord;
 use Koha::OAI::Server::ListRecords;
 use Koha::OAI::Server::ListIdentifiers;
 use XML::SAX::Writer;
-use YAML::Syck qw( LoadFile );
+use YAML::XS;
 use CGI qw/:standard -oldstyle_urls/;
 use C4::Context;
 use C4::Biblio;
@@ -108,7 +108,7 @@ sub new {
 
     # Load configuration file if defined in OAI-PMH:ConfFile syspref
     if ( my $file = C4::Context->preference("OAI-PMH:ConfFile") ) {
-        $self->{ conf } = LoadFile( $file );
+        $self->{ conf } = YAML::XS::LoadFile( $file );
         my @formats = keys %{ $self->{conf}->{format} };
         $self->{ koha_metadata_format } =  \@formats;
     }
diff --git a/Koha/SearchEngine/Elasticsearch.pm b/Koha/SearchEngine/Elasticsearch.pm
index 4a2d75d03d..ce35958b06 100644
--- a/Koha/SearchEngine/Elasticsearch.pm
+++ b/Koha/SearchEngine/Elasticsearch.pm
@@ -37,7 +37,7 @@ use Modern::Perl;
 use Readonly;
 use Search::Elasticsearch;
 use Try::Tiny;
-use YAML::Syck;
+use YAML::XS;
 
 use List::Util qw( sum0 reduce all );
 use MARC::File::XML;
@@ -164,7 +164,7 @@ sub get_elasticsearch_settings {
     if (!defined $settings) {
         my $config_file = C4::Context->config('elasticsearch_index_config');
         $config_file ||= C4::Context->config('intranetdir') . '/admin/searchengine/elasticsearch/index_config.yaml';
-        $settings = LoadFile( $config_file );
+        $settings = YAML::XS::LoadFile( $config_file );
     }
 
     return $settings;
@@ -312,7 +312,7 @@ sub _get_elasticsearch_field_config {
     if (!defined $settings) {
         my $config_file = C4::Context->config('elasticsearch_field_config');
         $config_file ||= C4::Context->config('intranetdir') . '/admin/searchengine/elasticsearch/field_config.yaml';
-        $settings = LoadFile( $config_file );
+        $settings = YAML::XS::LoadFile( $config_file );
     }
 
     if (!defined $settings->{$purpose}) {
@@ -341,7 +341,7 @@ $indexes = _load_elasticsearch_mappings();
 sub _load_elasticsearch_mappings {
     my $mappings_yaml = C4::Context->config('elasticsearch_index_mappings');
     $mappings_yaml ||= C4::Context->config('intranetdir') . '/admin/searchengine/elasticsearch/mappings.yaml';
-    return LoadFile( $mappings_yaml );
+    return YAML::XS::LoadFile( $mappings_yaml );
 }
 
 sub reset_elasticsearch_mappings {
diff --git a/admin/preferences.pl b/admin/preferences.pl
index 1353397b9b..8805e3dbfc 100755
--- a/admin/preferences.pl
+++ b/admin/preferences.pl
@@ -31,10 +31,9 @@ use C4::Templates;
 use Koha::Acquisition::Currencies;
 use File::Spec;
 use IO::File;
-use YAML::Syck qw();
+use YAML::XS;
+use Encode;
 use List::MoreUtils qw(any);
-$YAML::Syck::ImplicitTyping = 1;
-$YAML::Syck::ImplicitUnicode = 1;
 
 # use Smart::Comments;
 #
@@ -53,7 +52,7 @@ sub GetTab {
         local_currency => $local_currency, # currency code is used, because we do not know how a given currency is formatted.
     );
 
-    return YAML::Syck::Load( $tab_template->output() );
+    return YAML::XS::Load( Encode::encode_utf8($tab_template->output()));
 }
 
 sub _get_chunk {
@@ -237,6 +236,7 @@ sub SearchPrefs {
     our @terms = split( /\s+/, $searchfield );
 
     foreach my $tab_name ( sort keys %tab_files ) {
+        # FIXME Hum?
         # Force list context to remove 'uninitialized value in goto' warn coming from YAML::Syck; note that the other GetTab call is in list context too. The actual cause however is the null value for the pref OpacRenewalBranch in opac.pref
         my ($data) = GetTab( $input, $tab_name );
         my $title = ( keys( %$data ) )[0];
diff --git a/admin/systempreferences.pl b/admin/systempreferences.pl
index e124f25663..c203385584 100755
--- a/admin/systempreferences.pl
+++ b/admin/systempreferences.pl
@@ -50,7 +50,7 @@ use C4::Koha;
 use C4::Languages qw(getTranslatedLanguages);
 use C4::ClassSource;
 use C4::Output;
-use YAML::Syck qw( LoadFile );
+use YAML::XS;
 
 my %tabsysprefs; #we do no longer need to keep track of a tab per pref (yaml)
 
@@ -407,7 +407,7 @@ sub get_prefs_from_files {
         }
     };
     for my $file (@pref_files) {
-        my $pref = LoadFile( "$path_pref_en/$file" );
+        my $pref = YAML::XS::LoadFile( "$path_pref_en/$file" );
         for my $tab ( keys %$pref ) {
             my $content = $pref->{$tab};
             if ( ref($content) eq 'ARRAY' ) {
diff --git a/misc/admin/koha-preferences b/misc/admin/koha-preferences
index 05667b8133..24c0384cac 100755
--- a/misc/admin/koha-preferences
+++ b/misc/admin/koha-preferences
@@ -26,9 +26,8 @@ use C4::Debug;
 use C4::Log;
 use Getopt::Long;
 use Pod::Usage;
-use YAML::Syck qw();
-$YAML::Syck::ImplicitTyping = 1;
-$YAML::Syck::SortKeys = 1;
+use YAML::XS;
+use Encode;
 our %NOT_SET_PREFS = map { $_, 1 } qw( Version );
 
 =head1 NAME
@@ -239,9 +238,9 @@ my %commands = (
         ) || _print_usage( 1 );
 
         if ( $outfile ) {
-            YAML::Syck::DumpFile( $outfile, GetPreferences() );
+            YAML::XS::DumpFile( $outfile, GetPreferences() );
         } else {
-            print YAML::Syck::Dump( GetPreferences() );
+            print YAML::XS::Dump( Encode::encode_utf8(GetPreferences()) );
         }
     },
     load => sub {
@@ -252,7 +251,7 @@ my %commands = (
             'f' => \$force_version,
         );
 
-        my $preferences = YAML::Syck::LoadFile($infile || \*STDIN);
+        my $preferences = YAML::XS::LoadFile($infile || \*STDIN);
 
         die "Expected a YAML mapping" if ( ref($preferences) ne 'HASH' );
 
diff --git a/misc/cronjobs/cloud-kw.pl b/misc/cronjobs/cloud-kw.pl
index 207312e3e1..3dadf998a7 100755
--- a/misc/cronjobs/cloud-kw.pl
+++ b/misc/cronjobs/cloud-kw.pl
@@ -22,7 +22,7 @@ use strict;
 use warnings;
 use diagnostics;
 use Carp;
-use YAML::Syck;
+use YAML::XS;
 use Pod::Usage;
 use Getopt::Long;
 
@@ -51,7 +51,7 @@ cronlogaction();
 my @clouds;
 print "Reading configuration file: $conf\n" if $verbose;
 eval {
-    @clouds = LoadFile( $conf );
+    @clouds = YAML::XS::LoadFile( $conf );
 };
 croak "Unable to read configuration file: $conf\n" if $@;
 
diff --git a/misc/translator/LangInstaller.pm b/misc/translator/LangInstaller.pm
index 5baddeeccc..d15d15a82c 100644
--- a/misc/translator/LangInstaller.pm
+++ b/misc/translator/LangInstaller.pm
@@ -22,15 +22,14 @@ use Modern::Perl;
 use C4::Context;
 # WARNING: Any other tested YAML library fails to work properly in this
 # script content
-use YAML::Syck qw( LoadFile DumpFile );
+# FIXME Really?
+use YAML::XS;
 use Locale::PO;
 use FindBin qw( $Bin );
 use File::Basename;
 use File::Path qw( make_path );
 use File::Copy;
 
-$YAML::Syck::ImplicitTyping = 1;
-
 sub set_lang {
     my ($self, $lang) = @_;
 
@@ -244,7 +243,7 @@ sub install_prefs {
     $self->{po} = Locale::PO->load_file_ashash($self->po_filename("-pref.po"), 'utf8');
 
     for my $file ( @{$self->{pref_files}} ) {
-        my $pref = LoadFile( $self->{path_pref_en} . "/$file" );
+        my $pref = YAML::XS::LoadFile( $self->{path_pref_en} . "/$file" );
 
         my $translated_pref = {
             map {
@@ -258,7 +257,7 @@ sub install_prefs {
 
         my $file_trans = $self->{po_path_lang} . "/$file";
         print "Write $file\n" if $self->{verbose};
-        DumpFile($file_trans, $translated_pref);
+        YAML::XS::DumpFile($file_trans, $translated_pref);
     }
 }
 
@@ -309,7 +308,7 @@ sub translate_yaml {
 
     my $po_ref  = Locale::PO->load_file_ashash( $po_file );
 
-    my $dstyml   = LoadFile( $srcyml );
+    my $dstyml   = YAML::XS::LoadFile( $srcyml );
 
     # translate fields in table rows
     my @tables = @{ $dstyml->{'tables'} };
@@ -402,7 +401,7 @@ sub install_installer {
                 if ( $file =~ /yml$/ ) {
                     my $translated_yaml = translate_yaml( $self, $target, "$intradir/$dir/$file" );
                     open(my $fh, ">:encoding(UTF-8)", "$intradir/$tdir/$file");
-                    DumpFile( $fh, $translated_yaml );
+                    YAML::XS::DumpFile( $fh, $translated_yaml );
                     close($fh);
                 } else {
                     File::Copy::copy( "$intradir/$dir/$file", "$intradir/$tdir/$file" );
diff --git a/misc/translator/xgettext-installer b/misc/translator/xgettext-installer
index 79be95798c..64410c7693 100755
--- a/misc/translator/xgettext-installer
+++ b/misc/translator/xgettext-installer
@@ -48,9 +48,7 @@ use Modern::Perl;
 use Getopt::Long;
 use Locale::PO;
 use Pod::Usage;
-use YAML::Syck qw(LoadFile);
-
-$YAML::Syck::ImplicitTyping = 1;
+use YAML::XS;
 
 my $output = 'messages.pot';
 my $files_from;
@@ -89,7 +87,7 @@ my $pot = {
 };
 
 for my $file (@files) {
-    my $yaml = LoadFile($file);
+    my $yaml = YAML::XS::LoadFile($file);
     my @tables = @{ $yaml->{'tables'} };
 
     my $tablec = 0;
diff --git a/misc/translator/xgettext-pref b/misc/translator/xgettext-pref
index 1113a27f95..f95c619f60 100755
--- a/misc/translator/xgettext-pref
+++ b/misc/translator/xgettext-pref
@@ -49,9 +49,7 @@ use File::Basename;
 use Getopt::Long;
 use Locale::PO;
 use Pod::Usage;
-use YAML::Syck qw(LoadFile);
-
-$YAML::Syck::ImplicitTyping = 1;
+use YAML::XS;
 
 my $output = 'messages.pot';
 my $files_from;
@@ -89,7 +87,7 @@ my $pot = {
 };
 
 for my $file (@files) {
-    my $pref = LoadFile($file);
+    my $pref = YAML::XS::LoadFile($file);
     while ( my ($tab, $tab_content) = each %$pref ) {
         add_po(undef, basename($file));
 
diff --git a/t/db_dependent/Koha/SearchEngine/Elasticsearch/Reset.t b/t/db_dependent/Koha/SearchEngine/Elasticsearch/Reset.t
index 57f4e06ea4..64a3874081 100755
--- a/t/db_dependent/Koha/SearchEngine/Elasticsearch/Reset.t
+++ b/t/db_dependent/Koha/SearchEngine/Elasticsearch/Reset.t
@@ -44,7 +44,7 @@ my $indexes = {
     }
 };
 
-my $yaml = Test::MockModule->new('YAML::Syck');
+my $yaml = Test::MockModule->new('YAML::XS');
 $yaml->mock( 'LoadFile', sub { return $indexes; } );
 
 use_ok('Koha::SearchEngine::Elasticsearch');
-- 
2.20.1