[10398] | 1 | ###########################################################################
|
---|
| 2 | #
|
---|
[18568] | 3 | # List.pm -- A general and flexible list classifier with most of
|
---|
[10398] | 4 | # the abilities of AZCompactList, and better Unicode,
|
---|
| 5 | # metadata and sorting capabilities.
|
---|
| 6 | #
|
---|
| 7 | # A component of the Greenstone digital library software
|
---|
| 8 | # from the New Zealand Digital Library Project at the
|
---|
| 9 | # University of Waikato, New Zealand.
|
---|
| 10 | #
|
---|
| 11 | # Author: Michael Dewsnip, NZDL Project, University of Waikato, NZ
|
---|
| 12 | #
|
---|
| 13 | # Copyright (C) 2005 New Zealand Digital Library Project
|
---|
| 14 | #
|
---|
| 15 | # This program is free software; you can redistribute it and/or modify
|
---|
| 16 | # it under the terms of the GNU General Public License as published by
|
---|
| 17 | # the Free Software Foundation; either version 2 of the License, or
|
---|
| 18 | # (at your option) any later version.
|
---|
| 19 | #
|
---|
| 20 | # This program is distributed in the hope that it will be useful,
|
---|
| 21 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 22 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 23 | # GNU General Public License for more details.
|
---|
| 24 | #
|
---|
| 25 | # You should have received a copy of the GNU General Public License
|
---|
| 26 | # along with this program; if not, write to the Free Software
|
---|
| 27 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 28 | #
|
---|
[13741] | 29 | # TO DO: - Remove punctuation from metadata values before sorting.
|
---|
| 30 | # - Add an AZCompactList-style hlist option?
|
---|
[10398] | 31 | #
|
---|
| 32 | ###########################################################################
|
---|
| 33 |
|
---|
[18568] | 34 | package List;
|
---|
[10398] | 35 |
|
---|
| 36 |
|
---|
[17209] | 37 | use BaseClassifier;
|
---|
[10398] | 38 |
|
---|
| 39 | use strict;
|
---|
| 40 |
|
---|
| 41 |
|
---|
| 42 | sub BEGIN {
|
---|
[18568] | 43 | @List::ISA = ('BaseClassifier');
|
---|
[10398] | 44 | }
|
---|
| 45 |
|
---|
[18572] | 46 | my $partition_type_list =
|
---|
| 47 | [ { 'name' => "per_letter",
|
---|
| 48 | 'desc' => "{List.level_partition.per_letter}" },
|
---|
| 49 | { 'name' => "constant_size",
|
---|
[18619] | 50 | 'desc' => "{List.level_partition.constant_size}" },
|
---|
| 51 | { 'name' => "per_letter_fixed_size",
|
---|
| 52 | 'desc' => "{List.level_partition.per_letter_fixed_size}"},
|
---|
[18572] | 53 | { 'name' => "none",
|
---|
| 54 | 'desc' => "{List.level_partition.none}" } ];
|
---|
[10398] | 55 |
|
---|
[20825] | 56 | # following used to check types later on
|
---|
| 57 | my $valid_partition_types = { 'per_letter' => 1,
|
---|
| 58 | 'constant_size' => 1,
|
---|
| 59 | 'per_letter_fixed_size' => 1,
|
---|
| 60 | 'none' => 1};
|
---|
| 61 |
|
---|
[18619] | 62 | my $bookshelf_type_list =
|
---|
| 63 | [ { 'name' => "always",
|
---|
| 64 | 'desc' => "{List.bookshelf_type.always}" },
|
---|
| 65 | { 'name' => "duplicate_only",
|
---|
| 66 | 'desc' => "{List.bookshelf_type.duplicate_only}" },
|
---|
| 67 | { 'name' => "never",
|
---|
[20008] | 68 | 'desc' => "{List.bookshelf_type.never}" } ];
|
---|
[18619] | 69 |
|
---|
[10398] | 70 | my $arguments =
|
---|
| 71 | [ { 'name' => "metadata",
|
---|
[19234] | 72 | 'desc' => "{List.metadata}",
|
---|
[10398] | 73 | 'type' => "metadata",
|
---|
| 74 | 'reqd' => "yes" },
|
---|
[12889] | 75 |
|
---|
[10398] | 76 | # The interesting options
|
---|
[18619] | 77 | { 'name' => "bookshelf_type",
|
---|
[19234] | 78 | 'desc' => "{List.bookshelf_type}",
|
---|
[18619] | 79 | 'type' => "enum",
|
---|
| 80 | 'list' => $bookshelf_type_list,
|
---|
[19645] | 81 | 'deft' => "never" },
|
---|
[10498] | 82 | { 'name' => "classify_sections",
|
---|
[19234] | 83 | 'desc' => "{List.classify_sections}",
|
---|
[10498] | 84 | 'type' => "flag" },
|
---|
| 85 | { 'name' => "partition_type_within_level",
|
---|
[19234] | 86 | 'desc' => "{List.partition_type_within_level}",
|
---|
[20679] | 87 | 'type' => "enumstring", # Must be enumstring because multiple values can be specified (separated by '/')
|
---|
[20008] | 88 | 'list' => $partition_type_list,
|
---|
| 89 | 'deft' => "per_letter" },
|
---|
[10498] | 90 | { 'name' => "partition_size_within_level",
|
---|
[19234] | 91 | 'desc' => "{List.partition_size_within_level}",
|
---|
[20679] | 92 | 'type' => "string" }, # Must be string because multiple values can be specified (separated by '/')
|
---|
[14084] | 93 | { 'name' => "partition_name_length",
|
---|
[19234] | 94 | 'desc' => "{List.partition_name_length}",
|
---|
[14084] | 95 | 'type' => "string" },
|
---|
[10498] | 96 | { 'name' => "sort_leaf_nodes_using",
|
---|
[19234] | 97 | 'desc' => "{List.sort_leaf_nodes_using}",
|
---|
[10398] | 98 | 'type' => "metadata",
|
---|
[10499] | 99 | 'deft' => "Title" },
|
---|
[13551] | 100 | { 'name' => "sort_using_unicode_collation",
|
---|
[19234] | 101 | 'desc' => "{List.sort_using_unicode_collation}",
|
---|
[13551] | 102 | 'type' => "flag" },
|
---|
[10499] | 103 | { 'name' => "use_hlist_for",
|
---|
[19234] | 104 | 'desc' => "{List.use_hlist_for}",
|
---|
[18619] | 105 | 'type' => "string" },
|
---|
| 106 | { 'name' => "removeprefix",
|
---|
| 107 | 'desc' => "{BasClas.removeprefix}",
|
---|
| 108 | 'type' => "regexp" },
|
---|
| 109 | { 'name' => "removesuffix",
|
---|
| 110 | 'desc' => "{BasClas.removesuffix}",
|
---|
| 111 | 'type' => "regexp" } ];
|
---|
[10398] | 112 |
|
---|
[18568] | 113 | my $options = { 'name' => "List",
|
---|
[19234] | 114 | 'desc' => "{List.desc}",
|
---|
[10502] | 115 | 'abstract' => "no",
|
---|
[18572] | 116 | 'inherits' => "yes",
|
---|
[10398] | 117 | 'args' => $arguments };
|
---|
| 118 |
|
---|
| 119 |
|
---|
| 120 | sub new
|
---|
| 121 | {
|
---|
| 122 | my ($class) = shift(@_);
|
---|
| 123 | my ($classifierslist, $inputargs, $hashArgOptLists) = @_;
|
---|
| 124 | push(@$classifierslist, $class);
|
---|
| 125 |
|
---|
[17209] | 126 | push(@{$hashArgOptLists->{"ArgList"}}, @{$arguments});
|
---|
| 127 | push(@{$hashArgOptLists->{"OptList"}}, $options);
|
---|
[10398] | 128 |
|
---|
[17209] | 129 | my $self = new BaseClassifier($classifierslist, $inputargs, $hashArgOptLists);
|
---|
[10398] | 130 |
|
---|
| 131 | if ($self->{'info_only'}) {
|
---|
| 132 | # don't worry about any options etc
|
---|
| 133 | return bless $self, $class;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | # The metadata elements to use (required)
|
---|
[12894] | 137 | if (!$self->{'metadata'}) {
|
---|
[18568] | 138 | die "Error: No metadata fields specified for List.\n";
|
---|
[10398] | 139 | }
|
---|
[12894] | 140 | my @metadata_groups = split(/\//, $self->{'metadata'});
|
---|
[12889] | 141 | $self->{'metadata_groups'} = \@metadata_groups;
|
---|
[10398] | 142 |
|
---|
[12894] | 143 | # The classifier button name (default: the first metadata element specified)
|
---|
| 144 | if (!$self->{'buttonname'}) {
|
---|
| 145 | my $first_metadata_group = $metadata_groups[0];
|
---|
[20008] | 146 | my $first_metadata_element = (split(/\;|,/, $first_metadata_group))[0];
|
---|
[12894] | 147 | $self->{'buttonname'} = $self->generate_title_from_metadata($first_metadata_element);
|
---|
[10398] | 148 | }
|
---|
| 149 |
|
---|
[18619] | 150 | # Whether to group items into a bookshelf, (must be 'always' for all metadata fields except the last)
|
---|
[12889] | 151 | foreach my $metadata_group (@metadata_groups) {
|
---|
[18619] | 152 | $self->{$metadata_group . ".bookshelf_type"} = "always";
|
---|
| 153 | }
|
---|
| 154 | my $last_metadata_group = $metadata_groups[$#metadata_groups];
|
---|
| 155 | # Default: duplicate_only, ie. leave leaf nodes ungrouped (equivalent to AZCompactList -mingroup 2)
|
---|
| 156 | $self->{$last_metadata_group . ".bookshelf_type"} = $self->{'bookshelf_type'};
|
---|
| 157 |
|
---|
[12892] | 158 | # Whether to use an hlist or a vlist for each level in the hierarchy (default: vlist)
|
---|
[12889] | 159 | foreach my $metadata_group (@metadata_groups) {
|
---|
[12892] | 160 | $self->{$metadata_group . ".list_type"} = "VList";
|
---|
[10499] | 161 | }
|
---|
[12892] | 162 | foreach my $metadata_group (split(/\,/, $self->{'use_hlist_for'})) {
|
---|
| 163 | $self->{$metadata_group . ".list_type"} = "HList";
|
---|
[10499] | 164 | }
|
---|
| 165 |
|
---|
[18619] | 166 | # How the items are grouped into partitions (default: no partition)
|
---|
| 167 | # for each level (metadata group), separated by '/'
|
---|
[12894] | 168 | if (!$self->{"partition_type_within_level"}) {
|
---|
[18619] | 169 | foreach my $metadata_group (@metadata_groups) {
|
---|
| 170 | $self->{$metadata_group . ".partition_type_within_level"} = "none";
|
---|
| 171 | }
|
---|
| 172 | } else {
|
---|
| 173 | my @partition_type_within_levellist = split(/\//, $self->{'partition_type_within_level'});
|
---|
[20825] | 174 |
|
---|
| 175 | my $first = 1;
|
---|
[18619] | 176 | foreach my $metadata_group (@metadata_groups) {
|
---|
| 177 | my $partition_type_within_levelelem = shift(@partition_type_within_levellist);
|
---|
[20825] | 178 | if (defined($partition_type_within_levelelem) && defined $valid_partition_types->{$partition_type_within_levelelem}) {
|
---|
[18619] | 179 | $self->{$metadata_group . ".partition_type_within_level"} = $partition_type_within_levelelem;
|
---|
| 180 | }
|
---|
| 181 | else {
|
---|
[20825] | 182 | if ($first) {
|
---|
| 183 | $self->{$metadata_group . ".partition_type_within_level"} = "none";
|
---|
| 184 | $first = 0;
|
---|
| 185 | } else {
|
---|
| 186 | $self->{$metadata_group . ".partition_type_within_level"} = $self->{$metadata_groups[0] . ".partition_type_within_level"};
|
---|
| 187 | }
|
---|
| 188 | if (defined($partition_type_within_levelelem)) {
|
---|
| 189 | # ie invalid entry
|
---|
| 190 | print STDERR "invalid partition type for level $metadata_group: $partition_type_within_levelelem, defaulting to ". $self->{$metadata_group . ".partition_type_within_level"} ."\n";
|
---|
| 191 | }
|
---|
[18619] | 192 | }
|
---|
| 193 | }
|
---|
[10398] | 194 | }
|
---|
[20825] | 195 |
|
---|
[10499] | 196 | # The number of items in each partition
|
---|
[12894] | 197 | if (!$self->{'partition_size_within_level'}) {
|
---|
[10398] | 198 | # Default: 20
|
---|
[12889] | 199 | foreach my $metadata_group (@metadata_groups) {
|
---|
| 200 | $self->{$metadata_group . ".partition_size_within_level"} = 20;
|
---|
[10398] | 201 | }
|
---|
| 202 | }
|
---|
| 203 | else {
|
---|
[12894] | 204 | my @partition_size_within_levellist = split(/\//, $self->{'partition_size_within_level'});
|
---|
[10398] | 205 |
|
---|
[10498] | 206 | # Assign values based on the partition_size_within_level parameter
|
---|
[12889] | 207 | foreach my $metadata_group (@metadata_groups) {
|
---|
[10498] | 208 | my $partition_size_within_levelelem = shift(@partition_size_within_levellist);
|
---|
| 209 | if (defined($partition_size_within_levelelem)) {
|
---|
[12889] | 210 | $self->{$metadata_group . ".partition_size_within_level"} = $partition_size_within_levelelem;
|
---|
[10398] | 211 | }
|
---|
| 212 | else {
|
---|
[12889] | 213 | $self->{$metadata_group . ".partition_size_within_level"} = $self->{$metadata_groups[0] . ".partition_size_within_level"};
|
---|
[10398] | 214 | }
|
---|
| 215 | }
|
---|
| 216 | }
|
---|
| 217 |
|
---|
[18619] | 218 | # The removeprefix and removesuffix expressions
|
---|
| 219 | if ($self->{'removeprefix'}) {
|
---|
| 220 | # If there are more than one expressions, use '' to quote each experession and '/' to separate
|
---|
| 221 | my @removeprefix_exprs_within_levellist = split(/'\/'/, $self->{'removeprefix'});
|
---|
| 222 |
|
---|
| 223 | foreach my $metadata_group (@metadata_groups) {
|
---|
| 224 | my $removeprefix_expr_within_levelelem = shift(@removeprefix_exprs_within_levellist);
|
---|
| 225 | if (defined($removeprefix_expr_within_levelelem) && $removeprefix_expr_within_levelelem ne "") {
|
---|
| 226 | # Remove the other ' at the beginning and the end if there is any
|
---|
| 227 | $removeprefix_expr_within_levelelem =~ s/^'//;
|
---|
| 228 | $removeprefix_expr_within_levelelem =~ s/'$//;
|
---|
| 229 | # Remove the extra ^ at the beginning
|
---|
| 230 | $removeprefix_expr_within_levelelem =~ s/^\^//;
|
---|
| 231 | $self->{$metadata_group . ".remove_prefix_expr"} = $removeprefix_expr_within_levelelem;
|
---|
| 232 | } else {
|
---|
| 233 | $self->{$metadata_group . ".remove_prefix_expr"} = $self->{$metadata_groups[0] . ".remove_prefix_expr"};
|
---|
| 234 | }
|
---|
| 235 | }
|
---|
| 236 | }
|
---|
| 237 | if ($self->{'removesuffix'}) {
|
---|
| 238 | my @removesuffix_exprs_within_levellist = split(/'\/'/, $self->{'removesuffix'});
|
---|
| 239 |
|
---|
| 240 | foreach my $metadata_group (@metadata_groups) {
|
---|
| 241 | my $removesuffix_expr_within_levelelem = shift(@removesuffix_exprs_within_levellist);
|
---|
| 242 | if (defined($removesuffix_expr_within_levelelem) && $removesuffix_expr_within_levelelem ne "") {
|
---|
| 243 | $removesuffix_expr_within_levelelem =~ s/^'//;
|
---|
| 244 | $removesuffix_expr_within_levelelem =~ s/'$//;
|
---|
| 245 | # Remove the extra $ at the end
|
---|
| 246 | $removesuffix_expr_within_levelelem =~ s/\$$//;
|
---|
| 247 | $self->{$metadata_group . ".remove_suffix_expr"} = $removesuffix_expr_within_levelelem;
|
---|
| 248 | } else {
|
---|
| 249 | $self->{$metadata_group . ".remove_suffix_expr"} = $self->{$metadata_groups[0] . ".remove_suffix_expr"};
|
---|
| 250 | }
|
---|
| 251 | }
|
---|
| 252 | }
|
---|
| 253 |
|
---|
[12894] | 254 | # The metadata elements to use to sort the leaf nodes (default: Title)
|
---|
| 255 | my @sort_leaf_nodes_using_metadata_groups = ( "Title" );
|
---|
| 256 | if ($self->{'sort_leaf_nodes_using'}) {
|
---|
| 257 | @sort_leaf_nodes_using_metadata_groups = split(/\|/, $self->{'sort_leaf_nodes_using'});
|
---|
[10398] | 258 | }
|
---|
[12894] | 259 | $self->{'sort_leaf_nodes_using_metadata_groups'} = \@sort_leaf_nodes_using_metadata_groups;
|
---|
[10398] | 260 |
|
---|
[13551] | 261 | # Create an instance of the Unicode::Collate object if better Unicode sorting is desired
|
---|
| 262 | if ($self->{'sort_using_unicode_collation'}) {
|
---|
[13791] | 263 | # To use this you first need to download the allkeys.txt file from
|
---|
| 264 | # http://www.unicode.org/Public/UCA/latest/allkeys.txt and put it in the Perl
|
---|
| 265 | # Unicode/Collate directory.
|
---|
[13551] | 266 | require Unicode::Collate;
|
---|
| 267 | $self->{'unicode_collator'} = Unicode::Collate->new();
|
---|
| 268 | }
|
---|
| 269 |
|
---|
[12894] | 270 | # An empty array for the document OIDs
|
---|
| 271 | $self->{'OIDs'} = [];
|
---|
| 272 |
|
---|
[10398] | 273 | return bless $self, $class;
|
---|
| 274 | }
|
---|
| 275 |
|
---|
| 276 |
|
---|
| 277 | sub init
|
---|
| 278 | {
|
---|
| 279 | # Nothing to do...
|
---|
| 280 | }
|
---|
| 281 |
|
---|
| 282 |
|
---|
[12896] | 283 | # Called for each document in the collection
|
---|
[10398] | 284 | sub classify
|
---|
| 285 | {
|
---|
| 286 | my $self = shift(@_);
|
---|
[18455] | 287 | my ($doc_obj,$edit_mode) = @_;
|
---|
[10398] | 288 |
|
---|
[12896] | 289 | # If "-classify_sections" is set, classify every section of the document
|
---|
[10398] | 290 | if ($self->{'classify_sections'}) {
|
---|
| 291 | my $section = $doc_obj->get_next_section($doc_obj->get_top_section());
|
---|
| 292 | while (defined $section) {
|
---|
[18455] | 293 | $self->classify_section($doc_obj, $doc_obj->get_OID() . ".$section", $section, $edit_mode);
|
---|
[10398] | 294 | $section = $doc_obj->get_next_section($section);
|
---|
| 295 | }
|
---|
| 296 | }
|
---|
[12896] | 297 | # Otherwise just classify the top document section
|
---|
[10398] | 298 | else {
|
---|
[18455] | 299 | $self->classify_section($doc_obj, $doc_obj->get_OID(), $doc_obj->get_top_section(), $edit_mode);
|
---|
[10398] | 300 | }
|
---|
| 301 | }
|
---|
| 302 |
|
---|
| 303 |
|
---|
| 304 | sub classify_section
|
---|
| 305 | {
|
---|
| 306 | my $self = shift(@_);
|
---|
[18455] | 307 | my ($doc_obj,$section_OID,$section,$edit_mode) = @_;
|
---|
[10398] | 308 |
|
---|
[12889] | 309 | my @metadata_groups = @{$self->{'metadata_groups'}};
|
---|
[10398] | 310 |
|
---|
[12896] | 311 | # Only classify the section if it has a value for one of the metadata elements in the first group
|
---|
| 312 | my $classify_section = 0;
|
---|
| 313 | my $first_metadata_group = $metadata_groups[0];
|
---|
[20008] | 314 | foreach my $first_metadata_group_element (split(/\;|,/, $first_metadata_group)) {
|
---|
[20424] | 315 | my $real_first_metadata_group_element = $self->strip_ex_from_metadata($first_metadata_group_element);
|
---|
[20421] | 316 | my $first_metadata_group_element_value = $doc_obj->get_metadata_element($section, $real_first_metadata_group_element);
|
---|
[18619] | 317 |
|
---|
| 318 | # Remove prefix/suffix if requested
|
---|
| 319 | my $remove_prefix_expr = $self->{$first_metadata_group_element . ".remove_prefix_expr"};
|
---|
| 320 | if (defined $remove_prefix_expr && $remove_prefix_expr ne "") {
|
---|
| 321 | $first_metadata_group_element_value =~ s/^$remove_prefix_expr//;
|
---|
| 322 | }
|
---|
| 323 | my $remove_suffix_expr = $self->{$first_metadata_group_element . ".remove_suffix_expr"};
|
---|
| 324 | if (defined $remove_suffix_expr && $remove_suffix_expr ne "") {
|
---|
| 325 | $first_metadata_group_element_value =~ s/$remove_suffix_expr$//;
|
---|
| 326 | }
|
---|
| 327 |
|
---|
[12896] | 328 | if (defined($first_metadata_group_element_value) && $first_metadata_group_element_value ne "") {
|
---|
| 329 | # This section must be included in the classifier
|
---|
| 330 | $classify_section = 1;
|
---|
| 331 | last;
|
---|
[18619] | 332 | }
|
---|
[12896] | 333 | }
|
---|
[10398] | 334 |
|
---|
[12896] | 335 | # We're not classifying this section because it doesn't have the required metadata
|
---|
| 336 | return if (!$classify_section);
|
---|
[10398] | 337 |
|
---|
[18508] | 338 | if (($edit_mode eq "delete") || ($edit_mode eq "update")) {
|
---|
[18455] | 339 | $self->oid_array_delete($section_OID,'OIDs');
|
---|
[18508] | 340 | if ($edit_mode eq "delete") {
|
---|
| 341 | return;
|
---|
| 342 | }
|
---|
[18455] | 343 | }
|
---|
| 344 |
|
---|
[12896] | 345 | # Otherwise, include this section in the classifier
|
---|
| 346 | push(@{$self->{'OIDs'}}, $section_OID);
|
---|
| 347 |
|
---|
| 348 | # Create a hash for the metadata values of each metadata element we're interested in
|
---|
| 349 | my %metadata_groups_done = ();
|
---|
| 350 | foreach my $metadata_group (@metadata_groups, @{$self->{'sort_leaf_nodes_using_metadata_groups'}}) {
|
---|
| 351 | # Take care not to do a metadata group more than once
|
---|
| 352 | unless ($metadata_groups_done{$metadata_group}) {
|
---|
[20653] | 353 | if ($edit_mode eq "update") {
|
---|
| 354 | # if we are updating, we delete all the old values before
|
---|
| 355 | # adding the new ones, otherwise, the section will end up in
|
---|
| 356 | # the classifier twice.
|
---|
| 357 | delete $self->{$metadata_group . ".list"}->{$section_OID};
|
---|
| 358 | }
|
---|
[20008] | 359 | foreach my $metadata_element (split(/\;|,/, $metadata_group)) {
|
---|
[20424] | 360 | my $real_metadata_element = $self->strip_ex_from_metadata($metadata_element);
|
---|
| 361 |
|
---|
[18619] | 362 | my $remove_prefix_expr = $self->{$metadata_element . ".remove_prefix_expr"};
|
---|
| 363 | my $remove_suffix_expr = $self->{$metadata_element . ".remove_suffix_expr"};
|
---|
[20421] | 364 | my @metadata_values = @{$doc_obj->get_metadata($section, $real_metadata_element)};
|
---|
[12896] | 365 | foreach my $metadata_value (@metadata_values) {
|
---|
| 366 | # Strip leading and trailing whitespace
|
---|
| 367 | $metadata_value =~ s/^\s*//;
|
---|
| 368 | $metadata_value =~ s/\s*$//;
|
---|
[13550] | 369 |
|
---|
[18619] | 370 | # Remove prefix/suffix if requested
|
---|
| 371 | if (defined $remove_prefix_expr && $remove_prefix_expr ne "") {
|
---|
| 372 | $metadata_value =~ s/^$remove_prefix_expr//;
|
---|
| 373 | }
|
---|
| 374 | if (defined $remove_suffix_expr && $remove_suffix_expr ne "") {
|
---|
| 375 | $metadata_value =~ s/$remove_suffix_expr$//;
|
---|
| 376 | }
|
---|
| 377 |
|
---|
[13550] | 378 | # Convert the metadata value from a UTF-8 string to a Unicode string
|
---|
| 379 | # This means that length() and substr() work properly
|
---|
| 380 | # We need to be careful to convert classifier node title values back to UTF-8, however
|
---|
[14173] | 381 | my $metadata_value_unicode_string = $self->convert_utf8_string_to_unicode_string($metadata_value);
|
---|
[13550] | 382 |
|
---|
| 383 | # Add the metadata value into the list for this combination of metadata group and section
|
---|
| 384 | push(@{$self->{$metadata_group . ".list"}->{$section_OID}}, $metadata_value_unicode_string);
|
---|
[10398] | 385 | }
|
---|
[12896] | 386 | last if (@metadata_values > 0);
|
---|
[10398] | 387 | }
|
---|
| 388 |
|
---|
[12896] | 389 | $metadata_groups_done{$metadata_group} = 1;
|
---|
[10398] | 390 | }
|
---|
| 391 | }
|
---|
| 392 | }
|
---|
| 393 |
|
---|
| 394 |
|
---|
| 395 | sub get_classify_info
|
---|
| 396 | {
|
---|
| 397 | my $self = shift(@_);
|
---|
| 398 |
|
---|
[12896] | 399 | # The metadata groups to classify by
|
---|
[12889] | 400 | my @metadata_groups = @{$self->{'metadata_groups'}};
|
---|
| 401 | my $first_metadata_group = $metadata_groups[0];
|
---|
[10398] | 402 |
|
---|
[12896] | 403 | # The OID values of the documents to include in the classifier
|
---|
[12889] | 404 | my @OIDs = @{$self->{'OIDs'}};
|
---|
[10398] | 405 |
|
---|
[12896] | 406 | # Create the root node of the classification hierarchy
|
---|
[12893] | 407 | my %classifier_node = ( 'thistype' => "Invisible",
|
---|
[12895] | 408 | 'childtype' => $self->{$first_metadata_group . ".list_type"},
|
---|
[12894] | 409 | 'Title' => $self->{'buttonname'},
|
---|
[13271] | 410 | 'contains' => [],
|
---|
| 411 | 'mdtype' => $first_metadata_group );
|
---|
[10398] | 412 |
|
---|
[12895] | 413 | # Recursively create the classification hierarchy, one level for each metadata group
|
---|
[14173] | 414 | $self->add_level(\@metadata_groups, \@OIDs, \%classifier_node);
|
---|
[12893] | 415 | return \%classifier_node;
|
---|
[10398] | 416 | }
|
---|
| 417 |
|
---|
| 418 |
|
---|
[12895] | 419 | sub add_level
|
---|
[10398] | 420 | {
|
---|
| 421 | my $self = shift(@_);
|
---|
[12889] | 422 | my @metadata_groups = @{shift(@_)};
|
---|
| 423 | my @OIDs = @{shift(@_)};
|
---|
[12893] | 424 | my $classifier_node = shift(@_);
|
---|
| 425 | # print STDERR "\nAdding AZ list for " . $classifier_node->{'Title'} . "\n";
|
---|
[10398] | 426 |
|
---|
[12889] | 427 | my $metadata_group = $metadata_groups[0];
|
---|
| 428 | # print STDERR "Processing metadata group: " . $metadata_group . "\n";
|
---|
| 429 | # print STDERR "Number of OID values: " . @OIDs . "\n";
|
---|
[10398] | 430 |
|
---|
[13340] | 431 | if (!defined($self->{$metadata_group . ".list"})) {
|
---|
| 432 | print STDERR "Warning: No metadata values assigned to $metadata_group.\n";
|
---|
| 433 | return;
|
---|
| 434 | }
|
---|
[10398] | 435 |
|
---|
| 436 | # Create a mapping from metadata value to OID
|
---|
[14845] | 437 | my $OID_to_metadata_values_hash_ref = $self->{$metadata_group . ".list"};
|
---|
[18619] | 438 | my %metadata_value_to_OIDs_hash = ();
|
---|
[14845] | 439 | foreach my $OID (@OIDs)
|
---|
| 440 | {
|
---|
| 441 | if ($OID_to_metadata_values_hash_ref->{$OID})
|
---|
| 442 | {
|
---|
| 443 | my @metadata_values = @{$OID_to_metadata_values_hash_ref->{$OID}};
|
---|
| 444 | foreach my $metadata_value (@metadata_values)
|
---|
| 445 | {
|
---|
| 446 | push(@{$metadata_value_to_OIDs_hash{$metadata_value}}, $OID);
|
---|
[10398] | 447 | }
|
---|
| 448 | }
|
---|
| 449 | }
|
---|
[14845] | 450 | # print STDERR "Number of distinct values: " . scalar(keys %metadata_value_to_OIDs_hash) . "\n";
|
---|
[10398] | 451 |
|
---|
| 452 | # Partition the values (if necessary)
|
---|
[18619] | 453 | my $partition_type_within_level = $self->{$metadata_group . ".partition_type_within_level"};
|
---|
[10498] | 454 | if ($partition_type_within_level =~ /^per_letter$/i) {
|
---|
[10398] | 455 | # Generate one hlist for each letter
|
---|
[14845] | 456 | my @sortedmetadata_values = $self->sort_metadata_values_array(keys(%metadata_value_to_OIDs_hash));
|
---|
| 457 | my %metadata_value_to_OIDs_subhash = ();
|
---|
[10398] | 458 |
|
---|
[14845] | 459 | my $lastpartition = substr($sortedmetadata_values[0], 0, 1);
|
---|
| 460 | foreach my $metadata_value (@sortedmetadata_values) {
|
---|
| 461 | my $metadata_valuepartition = substr($metadata_value, 0, 1);
|
---|
[10398] | 462 |
|
---|
| 463 | # Is this the start of a new partition?
|
---|
[14845] | 464 | if ($metadata_valuepartition ne $lastpartition) {
|
---|
| 465 | $self->add_hlist_partition(\@metadata_groups, $classifier_node, $lastpartition, \%metadata_value_to_OIDs_subhash);
|
---|
| 466 | %metadata_value_to_OIDs_subhash = ();
|
---|
| 467 | $lastpartition = $metadata_valuepartition;
|
---|
[10398] | 468 | }
|
---|
| 469 |
|
---|
[14845] | 470 | $metadata_value_to_OIDs_subhash{$metadata_value} = $metadata_value_to_OIDs_hash{$metadata_value};
|
---|
[10398] | 471 | }
|
---|
| 472 |
|
---|
| 473 | # Don't forget to add the last partition
|
---|
[14845] | 474 | $self->add_hlist_partition(\@metadata_groups, $classifier_node, $lastpartition, \%metadata_value_to_OIDs_subhash);
|
---|
[10398] | 475 |
|
---|
| 476 | # The partitions are stored in an HList
|
---|
[12893] | 477 | $classifier_node->{'childtype'} = "HList";
|
---|
[10398] | 478 | }
|
---|
[18619] | 479 | elsif ($partition_type_within_level =~ /^per_letter_fixed_size$/i) {
|
---|
| 480 | # Generate hlist based on the first letter of the metadata value (like per_letter) but with restriction on the partition size
|
---|
| 481 | # If a letter has fewer items than specified by the "partition_size_within_level", then group them together if possible
|
---|
| 482 | # If a letter has more items than specified, split into several hlists.
|
---|
| 483 | # Depends on the bookshelf_type, one item can be either a document (when bookshelf_type is "never") or a metadata value (otherwise)
|
---|
| 484 | my $partition_size_within_level = $self->{$metadata_group . ".partition_size_within_level"};
|
---|
| 485 | my @sortedmetadata_values = $self->sort_metadata_values_array(keys(%metadata_value_to_OIDs_hash));
|
---|
| 486 | my $bookshelf_type = $self->{$metadata_group . ".bookshelf_type"};
|
---|
| 487 |
|
---|
| 488 | # Separate values by their first letter, each form a bucket, like the per_letter partition type
|
---|
| 489 | my $last_partition = substr($sortedmetadata_values[0], 0, 1);
|
---|
| 490 | my %partition_buckets = ();
|
---|
| 491 | my @metadata_values_in_bucket = ();
|
---|
| 492 | my $num_items_in_bucket = 0;
|
---|
| 493 | foreach my $metadata_value (@sortedmetadata_values) {
|
---|
| 494 | my $metadata_valuepartition = substr($metadata_value, 0, 1);
|
---|
| 495 | if ($metadata_valuepartition ne $last_partition) {
|
---|
| 496 | my @temp_array = @metadata_values_in_bucket;
|
---|
| 497 | # Cache the values that belong to this bucket, and the number of items in this bucket, not necessary to be the same number as the metadata values
|
---|
| 498 | my %partition_info = ();
|
---|
| 499 | $partition_info{'metadata_values'} = \@temp_array;
|
---|
| 500 | $partition_info{'size'} = $num_items_in_bucket;
|
---|
| 501 | $partition_buckets{$last_partition} = \%partition_info;
|
---|
| 502 |
|
---|
| 503 | @metadata_values_in_bucket = ($metadata_value);
|
---|
| 504 | $num_items_in_bucket = $bookshelf_type eq "never" ? scalar(@{$metadata_value_to_OIDs_hash{$metadata_value}}) : scalar(@metadata_values_in_bucket);
|
---|
| 505 | $last_partition = $metadata_valuepartition;
|
---|
| 506 | } else {
|
---|
| 507 | $num_items_in_bucket += $bookshelf_type eq "never" ? scalar(@{$metadata_value_to_OIDs_hash{$metadata_value}}) : scalar(@metadata_values_in_bucket);
|
---|
| 508 | push (@metadata_values_in_bucket, $metadata_value);
|
---|
| 509 | }
|
---|
| 510 | }
|
---|
| 511 | # Last one
|
---|
| 512 | my %partition_info = ();
|
---|
| 513 | $partition_info{'metadata_values'} = \@metadata_values_in_bucket;
|
---|
| 514 | $partition_info{'size'} = $num_items_in_bucket;
|
---|
| 515 | $partition_buckets{$last_partition} = \%partition_info;
|
---|
| 516 |
|
---|
| 517 | my @partition_keys = $self->sort_metadata_values_array(keys(%partition_buckets));
|
---|
| 518 | for (my $i = 0; $i < scalar(@partition_keys) - 1; $i++) {
|
---|
| 519 | my $partition = $partition_keys[$i];
|
---|
| 520 | my $items_in_partition = $partition_buckets{$partition}->{'size'};
|
---|
| 521 | # Merge small buckets together, but keep the numeric bucket apart
|
---|
| 522 | if ($items_in_partition < $partition_size_within_level) {
|
---|
| 523 | my $items_in_next_partition = $partition_buckets{$partition_keys[$i+1]}->{'size'};
|
---|
| 524 | if ($items_in_partition + $items_in_next_partition <= $partition_size_within_level
|
---|
| 525 | && !(($partition =~ /^[^0-9]/ && $partition_keys[$i+1] =~ /^[0-9]/)
|
---|
| 526 | || ($partition =~ /^[0-9]/ && $partition_keys[$i+1] =~ /^[^0-9]/))) {
|
---|
| 527 | foreach my $metadata_value_to_merge (@{$partition_buckets{$partition}->{'metadata_values'}}) {
|
---|
| 528 | push(@{$partition_buckets{$partition_keys[$i+1]}->{'metadata_values'}}, $metadata_value_to_merge);
|
---|
| 529 | }
|
---|
| 530 | $partition_buckets{$partition_keys[$i+1]}->{'size'} += $items_in_partition;
|
---|
| 531 | delete $partition_buckets{$partition};
|
---|
| 532 | }
|
---|
| 533 | }
|
---|
| 534 | }
|
---|
| 535 | @partition_keys = $self->sort_metadata_values_array(keys(%partition_buckets));
|
---|
| 536 |
|
---|
| 537 | # Add partitions, and divide big bucket into several
|
---|
| 538 | my $last_partition_end = "";
|
---|
| 539 | my $partition_start = "";
|
---|
| 540 | foreach my $partition (@partition_keys) {
|
---|
| 541 | my @metadata_values = $self->sort_metadata_values_array(@{$partition_buckets{$partition}->{'metadata_values'}});
|
---|
| 542 | my $items_in_partition = $partition_buckets{$partition}->{'size'};
|
---|
| 543 | $partition_start = $self->generate_partition_start($metadata_values[0], $last_partition_end, $self->{"partition_name_length"});
|
---|
| 544 |
|
---|
| 545 | if ($items_in_partition > $partition_size_within_level) {
|
---|
| 546 | my $items_done = 0;
|
---|
| 547 | my %metadata_values_to_OIDs_subhashes = ();
|
---|
| 548 | for (my $i = 0; $i < scalar(@metadata_values); $i++) {
|
---|
| 549 | my $metadata_value = $metadata_values[$i];
|
---|
| 550 | # If the bookshelf_type is "never", count the documents, otherwise count the distinct metadata values
|
---|
| 551 | my $items_for_this_md_value = $bookshelf_type eq "never" ? scalar(@{$metadata_value_to_OIDs_hash{$metadata_value}}) : 1;
|
---|
[10398] | 552 |
|
---|
[18619] | 553 | my $partitionend = $self->generate_partition_end($metadata_value, $partition_start, $self->{"partition_name_length"});
|
---|
| 554 | my $partitionname = $partition_start;
|
---|
| 555 | if ($partitionend ne $partition_start) {
|
---|
| 556 | $partitionname = $partitionname . "-" . $partitionend;
|
---|
| 557 | }
|
---|
| 558 |
|
---|
| 559 | # Start a new partition
|
---|
| 560 | if ($items_done + $items_for_this_md_value > $partition_size_within_level && $items_done != 0) {
|
---|
| 561 | $self->add_hlist_partition(\@metadata_groups, $classifier_node, $partitionname, \%metadata_values_to_OIDs_subhashes);
|
---|
| 562 | $last_partition_end = $partitionend;
|
---|
| 563 | $partition_start = $self->generate_partition_start($metadata_value, $last_partition_end, $self->{"partition_name_length"});
|
---|
| 564 | $items_done = 0;
|
---|
| 565 | %metadata_values_to_OIDs_subhashes = ();
|
---|
| 566 | }
|
---|
| 567 |
|
---|
| 568 | # If bookshelf_type is "never" and the current metadata value holds too many items, need to split into several partitions
|
---|
| 569 | if ($bookshelf_type eq "never" && $items_for_this_md_value > $partition_size_within_level) {
|
---|
| 570 | my $partitionname_for_this_value = $self->generate_partition_start($metadata_value, $last_partition_end, $self->{"partition_name_length"});
|
---|
| 571 | # Get the number of partitions needed for this value
|
---|
| 572 | my $num_splits = int($items_for_this_md_value / $partition_size_within_level);
|
---|
| 573 | $num_splits++ if ($items_for_this_md_value / $partition_size_within_level > $num_splits);
|
---|
| 574 |
|
---|
| 575 | my @OIDs_for_this_value = @{$metadata_value_to_OIDs_hash{$metadata_value}};
|
---|
| 576 | for (my $i = 0; $i < $num_splits; $i++) {
|
---|
| 577 | my %OIDs_subhashes_for_this_value = ();
|
---|
| 578 | my @OIDs_for_this_partition = ();
|
---|
| 579 | for (my $d = $i * $partition_size_within_level; $d < (($i+1) * $partition_size_within_level > $items_for_this_md_value ? $items_for_this_md_value : ($i+1) * $partition_size_within_level); $d++) {
|
---|
| 580 | push (@OIDs_for_this_partition, $OIDs_for_this_value[$d]);
|
---|
| 581 | }
|
---|
| 582 |
|
---|
| 583 | # The last bucket might have only a few items and need to be merged with buckets for subsequent metadata values
|
---|
| 584 | if ($i == $num_splits - 1 && scalar(@OIDs_for_this_partition) < $partition_size_within_level) {
|
---|
| 585 | $metadata_values_to_OIDs_subhashes{$metadata_value} = \@OIDs_for_this_partition;
|
---|
| 586 | $items_done += scalar(@OIDs_for_this_partition);
|
---|
| 587 | next;
|
---|
| 588 | }
|
---|
| 589 |
|
---|
| 590 | # Add an HList for this bucket
|
---|
| 591 | $OIDs_subhashes_for_this_value{$metadata_value} = \@OIDs_for_this_partition;
|
---|
| 592 | $self->add_hlist_partition(\@metadata_groups, $classifier_node, $partitionname_for_this_value, \%OIDs_subhashes_for_this_value);
|
---|
| 593 | $last_partition_end = $partitionname_for_this_value;
|
---|
| 594 | }
|
---|
| 595 | next;
|
---|
| 596 | }
|
---|
| 597 |
|
---|
| 598 | $metadata_values_to_OIDs_subhashes{$metadata_value} = $metadata_value_to_OIDs_hash{$metadata_value};
|
---|
| 599 | $items_done += $bookshelf_type eq "never" ? scalar(@{$metadata_values_to_OIDs_subhashes{$metadata_value}}) : 1;
|
---|
| 600 |
|
---|
| 601 | # The last partition
|
---|
| 602 | if($i == scalar(@metadata_values) - 1) {
|
---|
| 603 | $self->add_hlist_partition(\@metadata_groups, $classifier_node, $partitionname, \%metadata_values_to_OIDs_subhashes);
|
---|
| 604 | }
|
---|
| 605 | }
|
---|
| 606 | }
|
---|
| 607 | else {
|
---|
| 608 | # The easier case, just add a partition
|
---|
| 609 | my %metadata_values_to_OIDs_subhashes = ();
|
---|
| 610 | for (my $i = 0; $i < scalar(@metadata_values); $i++) {
|
---|
| 611 | my $metadata_value = $metadata_values[$i];
|
---|
| 612 | $metadata_values_to_OIDs_subhashes{$metadata_value} = $metadata_value_to_OIDs_hash{$metadata_value};
|
---|
| 613 | }
|
---|
| 614 | my $last_metadata_value = $metadata_values[scalar(@metadata_values)-1];
|
---|
| 615 | my $partitionend = $self->generate_partition_end($last_metadata_value, $partition_start, $self->{"partition_name_length"});
|
---|
| 616 | my $partitionname = $partition_start;
|
---|
| 617 | if ($partitionend ne $partition_start) {
|
---|
| 618 | $partitionname = $partitionname . "-" . $partitionend;
|
---|
| 619 | }
|
---|
| 620 | $self->add_hlist_partition(\@metadata_groups, $classifier_node, $partitionname, \%metadata_values_to_OIDs_subhashes);
|
---|
| 621 | $last_partition_end = $partitionend;
|
---|
| 622 | }
|
---|
| 623 | }
|
---|
| 624 | }
|
---|
[10398] | 625 | else {
|
---|
| 626 | # Generate hlists of a certain size
|
---|
[12889] | 627 | my $partition_size_within_level = $self->{$metadata_group . ".partition_size_within_level"};
|
---|
[14845] | 628 | if ($partition_type_within_level =~ /^constant_size$/i && scalar(keys %metadata_value_to_OIDs_hash) > $partition_size_within_level) {
|
---|
| 629 | my @sortedmetadata_values = $self->sort_metadata_values_array(keys(%metadata_value_to_OIDs_hash));
|
---|
[10398] | 630 | my $itemsdone = 0;
|
---|
[14845] | 631 | my %metadata_value_to_OIDs_subhash = ();
|
---|
[10398] | 632 | my $lastpartitionend = "";
|
---|
| 633 | my $partitionstart;
|
---|
[14845] | 634 | foreach my $metadata_value (@sortedmetadata_values) {
|
---|
| 635 | $metadata_value_to_OIDs_subhash{$metadata_value} = $metadata_value_to_OIDs_hash{$metadata_value};
|
---|
[10398] | 636 | $itemsdone++;
|
---|
[14845] | 637 | my $itemsinpartition = scalar(keys %metadata_value_to_OIDs_subhash);
|
---|
[10398] | 638 |
|
---|
| 639 | # Is this the start of a new partition?
|
---|
| 640 | if ($itemsinpartition == 1) {
|
---|
[14845] | 641 | $partitionstart = $self->generate_partition_start($metadata_value, $lastpartitionend, $self->{"partition_name_length"});
|
---|
[10398] | 642 | }
|
---|
| 643 |
|
---|
| 644 | # Is this the end of the partition?
|
---|
[14845] | 645 | if ($itemsinpartition == $partition_size_within_level || $itemsdone == @sortedmetadata_values) {
|
---|
| 646 | my $partitionend = $self->generate_partition_end($metadata_value, $partitionstart, $self->{"partition_name_length"});
|
---|
[10398] | 647 | my $partitionname = $partitionstart;
|
---|
| 648 | if ($partitionend ne $partitionstart) {
|
---|
| 649 | $partitionname = $partitionname . "-" . $partitionend;
|
---|
| 650 | }
|
---|
| 651 |
|
---|
[14845] | 652 | $self->add_hlist_partition(\@metadata_groups, $classifier_node, $partitionname, \%metadata_value_to_OIDs_subhash);
|
---|
| 653 | %metadata_value_to_OIDs_subhash = ();
|
---|
[10398] | 654 | $lastpartitionend = $partitionend;
|
---|
| 655 | }
|
---|
| 656 | }
|
---|
| 657 |
|
---|
| 658 | # The partitions are stored in an HList
|
---|
[12893] | 659 | $classifier_node->{'childtype'} = "HList";
|
---|
[10398] | 660 | }
|
---|
| 661 |
|
---|
| 662 | # Otherwise just add all the values to a VList
|
---|
| 663 | else {
|
---|
[14845] | 664 | $self->add_vlist(\@metadata_groups, $classifier_node, \%metadata_value_to_OIDs_hash);
|
---|
[10398] | 665 | }
|
---|
| 666 | }
|
---|
| 667 | }
|
---|
| 668 |
|
---|
| 669 |
|
---|
[13550] | 670 | sub convert_utf8_string_to_unicode_string
|
---|
[10398] | 671 | {
|
---|
[14173] | 672 | my $self = shift(@_);
|
---|
[13550] | 673 | my $utf8_string = shift(@_);
|
---|
[10398] | 674 |
|
---|
[13550] | 675 | my $unicode_string = "";
|
---|
| 676 | foreach my $unicode_value (@{&unicode::utf82unicode($utf8_string)}) {
|
---|
| 677 | $unicode_string .= chr($unicode_value);
|
---|
| 678 | }
|
---|
| 679 | return $unicode_string;
|
---|
[10398] | 680 | }
|
---|
| 681 |
|
---|
| 682 |
|
---|
[13550] | 683 | sub convert_unicode_string_to_utf8_string
|
---|
| 684 | {
|
---|
[14173] | 685 | my $self = shift(@_);
|
---|
[13550] | 686 | my $unicode_string = shift(@_);
|
---|
| 687 |
|
---|
| 688 | my @unicode_array;
|
---|
| 689 | for (my $i = 0; $i < length($unicode_string); $i++) {
|
---|
| 690 | push(@unicode_array, ord(substr($unicode_string, $i, 1)));
|
---|
| 691 | }
|
---|
| 692 | return &unicode::unicode2utf8(\@unicode_array);
|
---|
| 693 | }
|
---|
| 694 |
|
---|
| 695 |
|
---|
[10398] | 696 | sub generate_partition_start
|
---|
| 697 | {
|
---|
[14173] | 698 | my $self = shift(@_);
|
---|
[14845] | 699 | my $metadata_value = shift(@_);
|
---|
[10398] | 700 | my $lastpartitionend = shift(@_);
|
---|
[14084] | 701 | my $partition_name_length = shift(@_);
|
---|
[10398] | 702 |
|
---|
[14084] | 703 | if ($partition_name_length) {
|
---|
[14845] | 704 | return substr($metadata_value, 0, $partition_name_length);
|
---|
[14084] | 705 | }
|
---|
| 706 |
|
---|
[14845] | 707 | my $partitionstart = substr($metadata_value, 0, 1);
|
---|
[10398] | 708 | if ($partitionstart le $lastpartitionend) {
|
---|
[14845] | 709 | $partitionstart = substr($metadata_value, 0, 2);
|
---|
[10398] | 710 | # Give up after three characters
|
---|
| 711 | if ($partitionstart le $lastpartitionend) {
|
---|
[14845] | 712 | $partitionstart = substr($metadata_value, 0, 3);
|
---|
[10398] | 713 | }
|
---|
| 714 | }
|
---|
| 715 |
|
---|
| 716 | return $partitionstart;
|
---|
| 717 | }
|
---|
| 718 |
|
---|
| 719 |
|
---|
| 720 | sub generate_partition_end
|
---|
| 721 | {
|
---|
[14173] | 722 | my $self = shift(@_);
|
---|
[14845] | 723 | my $metadata_value = shift(@_);
|
---|
[10398] | 724 | my $partitionstart = shift(@_);
|
---|
[14084] | 725 | my $partition_name_length = shift(@_);
|
---|
[10398] | 726 |
|
---|
[14084] | 727 | if ($partition_name_length) {
|
---|
[14845] | 728 | return substr($metadata_value, 0, $partition_name_length);
|
---|
[14084] | 729 | }
|
---|
| 730 |
|
---|
[14845] | 731 | my $partitionend = substr($metadata_value, 0, length($partitionstart));
|
---|
[10398] | 732 | if ($partitionend gt $partitionstart) {
|
---|
[14845] | 733 | $partitionend = substr($metadata_value, 0, 1);
|
---|
[10398] | 734 | if ($partitionend le $partitionstart) {
|
---|
[14845] | 735 | $partitionend = substr($metadata_value, 0, 2);
|
---|
[10398] | 736 | # Give up after three characters
|
---|
| 737 | if ($partitionend le $partitionstart) {
|
---|
[14845] | 738 | $partitionend = substr($metadata_value, 0, 3);
|
---|
[10398] | 739 | }
|
---|
| 740 | }
|
---|
| 741 | }
|
---|
| 742 |
|
---|
| 743 | return $partitionend;
|
---|
| 744 | }
|
---|
| 745 |
|
---|
| 746 |
|
---|
| 747 | sub add_hlist_partition
|
---|
| 748 | {
|
---|
| 749 | my $self = shift(@_);
|
---|
[12889] | 750 | my @metadata_groups = @{shift(@_)};
|
---|
[12893] | 751 | my $classifier_node = shift(@_);
|
---|
[10398] | 752 | my $partitionname = shift(@_);
|
---|
[14845] | 753 | my $metadata_value_to_OIDs_hash_ref = shift(@_);
|
---|
[10398] | 754 |
|
---|
| 755 | # Create an hlist partition
|
---|
[14173] | 756 | my %child_classifier_node = ( 'Title' => $self->convert_unicode_string_to_utf8_string($partitionname),
|
---|
[12893] | 757 | 'childtype' => "VList",
|
---|
| 758 | 'contains' => [] );
|
---|
[10398] | 759 |
|
---|
| 760 | # Add the children to the hlist partition
|
---|
[14845] | 761 | $self->add_vlist(\@metadata_groups, \%child_classifier_node, $metadata_value_to_OIDs_hash_ref);
|
---|
[12893] | 762 | push(@{$classifier_node->{'contains'}}, \%child_classifier_node);
|
---|
[10398] | 763 | }
|
---|
| 764 |
|
---|
| 765 |
|
---|
| 766 | sub add_vlist
|
---|
| 767 | {
|
---|
| 768 | my $self = shift(@_);
|
---|
[12889] | 769 | my @metadata_groups = @{shift(@_)};
|
---|
[12893] | 770 | my $classifier_node = shift(@_);
|
---|
[14845] | 771 | my $metadata_value_to_OIDs_hash_ref = shift(@_);
|
---|
[10398] | 772 |
|
---|
[12889] | 773 | my $metadata_group = shift(@metadata_groups);
|
---|
[13287] | 774 | $classifier_node->{'mdtype'} = $metadata_group;
|
---|
[10398] | 775 |
|
---|
| 776 | # Create an entry in the vlist for each value
|
---|
[14845] | 777 | foreach my $metadata_value ($self->sort_metadata_values_array(keys(%{$metadata_value_to_OIDs_hash_ref})))
|
---|
| 778 | {
|
---|
| 779 | my @OIDs = @{$metadata_value_to_OIDs_hash_ref->{$metadata_value}};
|
---|
[10398] | 780 |
|
---|
[18619] | 781 | # If there is only one item and 'bookshelf_type' is not always (ie. never or duplicate_only), add the item to the list
|
---|
| 782 | if (@OIDs == 1 && $self->{$metadata_group . ".bookshelf_type"} ne "always") {
|
---|
[13271] | 783 | my $OID = $OIDs[0];
|
---|
| 784 |
|
---|
| 785 | # Find the offset of this metadata value
|
---|
| 786 | my $offset = 0;
|
---|
[14845] | 787 | my $OID_to_metadata_values_hash_ref = $self->{$metadata_group . ".list"};
|
---|
| 788 | my @metadata_values = @{$OID_to_metadata_values_hash_ref->{$OID}};
|
---|
| 789 | for (my $i = 0; $i < scalar(@metadata_values); $i++) {
|
---|
| 790 | if ($metadata_value eq $metadata_values[$i]) {
|
---|
[13271] | 791 | $offset = $i;
|
---|
| 792 | last;
|
---|
| 793 | }
|
---|
| 794 | }
|
---|
| 795 | push(@{$classifier_node->{'contains'}}, { 'OID' => $OID, 'offset' => $offset });
|
---|
[18619] | 796 | }
|
---|
| 797 | # If 'bookshelf_type' is 'never', list all the items even if there are duplicated values
|
---|
| 798 | elsif ($self->{$metadata_group . ".bookshelf_type"} eq "never") {
|
---|
| 799 | $self->add_sorted_leaf_items(\@OIDs, $classifier_node);
|
---|
[10398] | 800 | }
|
---|
| 801 | # Otherwise create a sublist (bookshelf) for the metadata value
|
---|
[18619] | 802 | else {
|
---|
[14845] | 803 | my %child_classifier_node = ( 'Title' => $self->convert_unicode_string_to_utf8_string($metadata_value),
|
---|
[12893] | 804 | 'childtype' => "VList",
|
---|
| 805 | 'contains' => [] );
|
---|
[10398] | 806 |
|
---|
| 807 | # If there are metadata elements remaining, recursively apply the process
|
---|
[12889] | 808 | if (@metadata_groups > 0) {
|
---|
| 809 | my $next_metadata_group = $metadata_groups[0];
|
---|
[12895] | 810 | $child_classifier_node{'childtype'} = $self->{$next_metadata_group . ".list_type"};
|
---|
[14173] | 811 | $self->add_level(\@metadata_groups, \@OIDs, \%child_classifier_node);
|
---|
[10398] | 812 | }
|
---|
| 813 | # Otherwise just add the documents as children of this list
|
---|
| 814 | else {
|
---|
[18619] | 815 | $self->add_sorted_leaf_items(\@OIDs, \%child_classifier_node);
|
---|
[10398] | 816 | }
|
---|
| 817 |
|
---|
| 818 | # Add the sublist to the list
|
---|
[12893] | 819 | push(@{$classifier_node->{'contains'}}, \%child_classifier_node);
|
---|
[10398] | 820 | }
|
---|
| 821 | }
|
---|
| 822 | }
|
---|
| 823 |
|
---|
[18619] | 824 | sub add_sorted_leaf_items
|
---|
| 825 | {
|
---|
| 826 | my $self = shift(@_);
|
---|
| 827 | my @OIDs = @{shift(@_)};
|
---|
| 828 | my $classifier_node = shift(@_);
|
---|
| 829 |
|
---|
| 830 | # Sort leaf nodes and add to list
|
---|
[20825] | 831 | my @sort_leaf_nodes_using_metadata_groups = @{$self->{'sort_leaf_nodes_using_metadata_groups'}};
|
---|
| 832 | foreach my $sort_leaf_nodes_usingmetaelem (reverse @sort_leaf_nodes_using_metadata_groups) {
|
---|
[18619] | 833 | my $OID_to_metadata_values_hash_ref = $self->{$sort_leaf_nodes_usingmetaelem . ".list"};
|
---|
| 834 | # Force a stable sort (Perl 5.6's sort isn't stable)
|
---|
| 835 | # !! The [0] bits aren't ideal (multiple metadata values) !!
|
---|
| 836 | @OIDs = @OIDs[ sort { $OID_to_metadata_values_hash_ref->{$OIDs[$a]}[0] cmp $OID_to_metadata_values_hash_ref->{$OIDs[$b]}[0] || $a <=> $b; } 0..$#OIDs ];
|
---|
| 837 | }
|
---|
[10398] | 838 |
|
---|
[18619] | 839 | foreach my $OID (@OIDs) {
|
---|
| 840 | push(@{$classifier_node->{'contains'}}, { 'OID' => $OID });
|
---|
| 841 | }
|
---|
| 842 | }
|
---|
| 843 |
|
---|
| 844 |
|
---|
[13551] | 845 | sub sort_metadata_values_array
|
---|
| 846 | {
|
---|
| 847 | my $self = shift(@_);
|
---|
| 848 | my @metadata_values = @_;
|
---|
| 849 |
|
---|
| 850 | if ($self->{'unicode_collator'}) {
|
---|
| 851 | return $self->{'unicode_collator'}->sort(@metadata_values);
|
---|
| 852 | }
|
---|
| 853 | else {
|
---|
| 854 | return sort(@metadata_values);
|
---|
| 855 | }
|
---|
| 856 | }
|
---|
| 857 |
|
---|
| 858 |
|
---|
[10398] | 859 | 1;
|
---|