[Koha-patches] [PATCH 1/2] Bug 9579: use String::Multibyte to correctly truncate facets

Tomas Cohen Arazi tomascohen at gmail.com
Wed Jan 15 20:57:02 CET 2014


As Bernardo pointed on comment #17, the incorrect display of
multibyte strings in facets is related to the way they are
currently truncated.

C4::Search::getRecords relies on CORE::substr for the task,
which only counts bytes, and thus doesn't check it is breaking
multi-byte characters.

This patch replaces both the use of

    CORE::substr
and CORE::length

for their String::Multibyte counterpart.

To test:
- Set FacetLabelTruncationLength = 4 to make sure it will try to truncate
  the facet label.
- Add a record with multiple multibyte characters in the author name
  (copy and paste 'ակ' several times so it is looong).
  Note: make sure there are at least two records so you're presented the search results.
- Reindex zebra to make it possible to search the record.
- Search for the record.
- Notice the facet with cyrillic characters is broken in the place it gets truncated.
- Apply the patch and repeat the search.
- Notice the string is not broken.
- Sign off.

Regards
To+

Sponsored-by: Universidad Nacional de Cordoba
---
 C4/Search.pm | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/C4/Search.pm b/C4/Search.pm
index 18a0f5f..edaa5de 100644
--- a/C4/Search.pm
+++ b/C4/Search.pm
@@ -36,6 +36,7 @@ use URI::Escape;
 use Business::ISBN;
 use MARC::Record;
 use MARC::Field;
+use String::Multibyte;
 use utf8;
 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $DEBUG);
 
@@ -580,13 +581,13 @@ sub getRecords {
                                 # fix the length that will display in the label,
                                 my $facet_label_value = $one_facet;
                                 my $facet_max_length  = C4::Context->preference(
-                                    'FacetLabelTruncationLength')
-                                  || 20;
-                                $facet_label_value =
-                                  substr( $one_facet, 0, $facet_max_length )
-                                  . "..."
-                                  if length($facet_label_value) >
-                                      $facet_max_length;
+                                    'FacetLabelTruncationLength') || 20;
+                                my $mbsp = String::Multibyte->new('UTF8');
+                                if ($mbsp->length($facet_label_value) > $facet_max_length ) {
+                                    $facet_label_value =
+                                        $mbsp->substr( $one_facet, 0, $facet_max_length )
+                                        . "...";
+                                }
 
                             # if it's a branch, label by the name, not the code,
                                 if ( $link_value =~ /branch/ ) {
-- 
1.8.3.2



More information about the Koha-patches mailing list