Bug 14544: Get rid of GetSomeShelfNames
[koha-ffzg.git] / C4 / Charset.pm
index e56422f..667c2d2 100644 (file)
@@ -4,27 +4,27 @@ package C4::Charset;
 #
 # This file is part of Koha.
 #
-# Koha is free software; you can redistribute it and/or modify it under the
-# terms of the GNU General Public License as published by the Free Software
-# Foundation; either version 2 of the License, or (at your option) any later
-# version.
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
 #
-# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
-# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
 #
-# You should have received a copy of the GNU General Public License along
-# with Koha; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 use strict;
 use warnings;
 
 use MARC::Charset qw/marc8_to_utf8/;
 use Text::Iconv;
-use C4::Context;
 use C4::Debug;
 use Unicode::Normalize;
+use Encode qw( decode encode is_utf8 );
 
 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
 
@@ -111,8 +111,8 @@ will assume that this situation occur does not very often.
 sub IsStringUTF8ish {
     my $str = shift;
 
-    return 1 if utf8::is_utf8($str);
-    return utf8::decode($str);
+    return 1 if Encode::is_utf8($str);
+    return utf8::decode( $str );
 }
 
 =head2 SetUTF8Flag
@@ -179,7 +179,8 @@ Sample code :
 
 sub NormalizeString{
        my ($string,$nfd,$transform)=@_;
-       utf8::decode($string) unless (utf8::is_utf8($string));
+    return $string unless defined($string); # force scalar context return.
+    $string = Encode::decode('UTF-8', $string) unless (Encode::is_utf8($string));
        if ($nfd){
                $string= NFD($string);
        }
@@ -331,6 +332,7 @@ sub SetMarcUnicodeFlag {
         substr($leader, 9, 1) = 'a';
         $marc_record->leader($leader); 
     } elsif ($marc_flavour =~/UNIMARC/) {
+        require C4::Context;
        my $defaultlanguage = C4::Context->preference("UNIMARCField100Language");
         $defaultlanguage = "fre" if (!$defaultlanguage || length($defaultlanguage) != 3);
         my $string; 
@@ -429,7 +431,7 @@ sub nsb_clean {
 SanitizeRecord($marcrecord);
 
 Sanitize a record
-This routine is called in the maintenance script misc/maintenance/batch_sanitize_records.pl.
+This routine is called in the maintenance script misc/maintenance/sanitize_records.pl.
 It cleans any string with '&amp;amp;...', replacing it by '&'
 
 =cut
@@ -444,7 +446,7 @@ sub SanitizeRecord {
     foreach my $field ( $record->fields() ) {
         if ( $field->is_control_field() ) {
             my $value           = $field->data();
-            my $sanitized_value = _entity_clean($value);
+            my $sanitized_value = _clean_ampersand($value);
             $record_modified = 1 if $sanitized_value ne $value;
             $field->update($sanitized_value);
         }
@@ -456,7 +458,7 @@ sub SanitizeRecord {
                   if $url_field eq $field->tag()
                       and $url_subfield eq $subfield->[0];
                 my $value           = $subfield->[1];
-                my $sanitized_value = _entity_clean($value);
+                my $sanitized_value = _clean_ampersand($value);
                 push @new_subfields, $subfield->[0] => $sanitized_value;
                 $record_modified = 1 if $sanitized_value ne $value;
             }
@@ -481,7 +483,7 @@ sub SanitizeRecord {
     return $record, $record_modified;
 }
 
-sub _entity_clean {
+sub _clean_ampersand {
     my ($string) = @_;
     $string =~ s/(&)(amp;)+/$1/g;
     return $string;
@@ -623,7 +625,7 @@ sub _marc_marc8_to_utf8 {
                     # occurs, upgrade the string in place.  Moral of the story seems to be
                     # that pack("U", ...) is better than chr(...) if you need to guarantee
                     # that the resulting string is UTF-8.
-                    utf8::upgrade($utf8sf);
+                    $utf8sf = Encode::encode('UTF-8', $utf8sf);
                 }
                 push @converted_subfields, $subfield->[0], $utf8sf;
             }