[10398] | 1 | ###########################################################################
|
---|
| 2 | #
|
---|
| 3 | # GenericList.pm -- A general and flexible list classifier with most of
|
---|
| 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 |
|
---|
| 34 | package GenericList;
|
---|
| 35 |
|
---|
| 36 |
|
---|
[17209] | 37 | use BaseClassifier;
|
---|
[10398] | 38 |
|
---|
| 39 | use strict;
|
---|
| 40 |
|
---|
| 41 |
|
---|
| 42 | sub BEGIN {
|
---|
[17209] | 43 | @GenericList::ISA = ('BaseClassifier');
|
---|
[10398] | 44 | }
|
---|
| 45 |
|
---|
| 46 |
|
---|
| 47 | my $arguments =
|
---|
| 48 | [ { 'name' => "metadata",
|
---|
| 49 | 'desc' => "{GenericList.metadata}",
|
---|
| 50 | 'type' => "metadata",
|
---|
| 51 | 'reqd' => "yes" },
|
---|
[12889] | 52 |
|
---|
[10398] | 53 | # The interesting options
|
---|
[10498] | 54 | { 'name' => "always_bookshelf_last_level",
|
---|
| 55 | 'desc' => "{GenericList.always_bookshelf_last_level}",
|
---|
| 56 | 'type' => "flag" },
|
---|
| 57 | { 'name' => "classify_sections",
|
---|
| 58 | 'desc' => "{GenericList.classify_sections}",
|
---|
| 59 | 'type' => "flag" },
|
---|
| 60 | { 'name' => "partition_type_within_level",
|
---|
| 61 | 'desc' => "{GenericList.partition_type_within_level}",
|
---|
[10398] | 62 | 'type' => "string",
|
---|
| 63 | 'deft' => "none" },
|
---|
[10498] | 64 | { 'name' => "partition_size_within_level",
|
---|
| 65 | 'desc' => "{GenericList.partition_size_within_level}",
|
---|
[10398] | 66 | 'type' => "string" },
|
---|
[14084] | 67 | { 'name' => "partition_name_length",
|
---|
| 68 | 'desc' => "{GenericList.partition_name_length}",
|
---|
| 69 | 'type' => "string" },
|
---|
[10498] | 70 | { 'name' => "sort_leaf_nodes_using",
|
---|
| 71 | 'desc' => "{GenericList.sort_leaf_nodes_using}",
|
---|
[10398] | 72 | 'type' => "metadata",
|
---|
[10499] | 73 | 'deft' => "Title" },
|
---|
[13551] | 74 | { 'name' => "sort_using_unicode_collation",
|
---|
| 75 | 'desc' => "{GenericList.sort_using_unicode_collation}",
|
---|
| 76 | 'type' => "flag" },
|
---|
[10499] | 77 | { 'name' => "use_hlist_for",
|
---|
| 78 | 'desc' => "{GenericList.use_hlist_for}",
|
---|
| 79 | 'type' => "string" } ];
|
---|
[10398] | 80 |
|
---|
| 81 | my $options = { 'name' => "GenericList",
|
---|
| 82 | 'desc' => "{GenericList.desc}",
|
---|
[10502] | 83 | 'abstract' => "no",
|
---|
[10398] | 84 | 'inherits' => "Yes",
|
---|
| 85 | 'args' => $arguments };
|
---|
| 86 |
|
---|
| 87 |
|
---|
| 88 | sub new
|
---|
| 89 | {
|
---|
| 90 | my ($class) = shift(@_);
|
---|
| 91 | my ($classifierslist, $inputargs, $hashArgOptLists) = @_;
|
---|
| 92 | push(@$classifierslist, $class);
|
---|
| 93 |
|
---|
[17209] | 94 | push(@{$hashArgOptLists->{"ArgList"}}, @{$arguments});
|
---|
| 95 | push(@{$hashArgOptLists->{"OptList"}}, $options);
|
---|
[10398] | 96 |
|
---|
[17209] | 97 | my $self = new BaseClassifier($classifierslist, $inputargs, $hashArgOptLists);
|
---|
[10398] | 98 |
|
---|
| 99 | if ($self->{'info_only'}) {
|
---|
| 100 | # don't worry about any options etc
|
---|
| 101 | return bless $self, $class;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | # The metadata elements to use (required)
|
---|
[12894] | 105 | if (!$self->{'metadata'}) {
|
---|
[10398] | 106 | die "Error: No metadata fields specified for GenericList.\n";
|
---|
| 107 | }
|
---|
[12894] | 108 | my @metadata_groups = split(/\//, $self->{'metadata'});
|
---|
[12889] | 109 | $self->{'metadata_groups'} = \@metadata_groups;
|
---|
[10398] | 110 |
|
---|
[12894] | 111 | # The classifier button name (default: the first metadata element specified)
|
---|
| 112 | if (!$self->{'buttonname'}) {
|
---|
| 113 | my $first_metadata_group = $metadata_groups[0];
|
---|
| 114 | my $first_metadata_element = (split(/\;/, $first_metadata_group))[0];
|
---|
| 115 | $self->{'buttonname'} = $self->generate_title_from_metadata($first_metadata_element);
|
---|
[10398] | 116 | }
|
---|
| 117 |
|
---|
| 118 | # Whether to group single items into a bookshelf (must be true for all metadata fields except the last)
|
---|
[12889] | 119 | foreach my $metadata_group (@metadata_groups) {
|
---|
| 120 | $self->{$metadata_group . ".always_bookshelf"} = "t";
|
---|
[10398] | 121 | }
|
---|
[10498] | 122 | if (!$self->{'always_bookshelf_last_level'}) {
|
---|
[12894] | 123 | # Default: leave leaf nodes ungrouped (equivalent to AZCompactList -mingroup 2)
|
---|
[12889] | 124 | my $last_metadata_group = $metadata_groups[$#metadata_groups];
|
---|
| 125 | $self->{$last_metadata_group . ".always_bookshelf"} = "f";
|
---|
[10398] | 126 | }
|
---|
| 127 |
|
---|
[12892] | 128 | # Whether to use an hlist or a vlist for each level in the hierarchy (default: vlist)
|
---|
[12889] | 129 | foreach my $metadata_group (@metadata_groups) {
|
---|
[12892] | 130 | $self->{$metadata_group . ".list_type"} = "VList";
|
---|
[10499] | 131 | }
|
---|
[12892] | 132 | foreach my $metadata_group (split(/\,/, $self->{'use_hlist_for'})) {
|
---|
| 133 | $self->{$metadata_group . ".list_type"} = "HList";
|
---|
[10499] | 134 | }
|
---|
| 135 |
|
---|
[12894] | 136 | # How the items are grouped into partitions (default: no partition)
|
---|
| 137 | if (!$self->{"partition_type_within_level"}) {
|
---|
| 138 | $self->{"partition_type_within_level"} = "none";
|
---|
[10398] | 139 | }
|
---|
| 140 |
|
---|
[10499] | 141 | # The number of items in each partition
|
---|
[12894] | 142 | if (!$self->{'partition_size_within_level'}) {
|
---|
[10398] | 143 | # Default: 20
|
---|
[12889] | 144 | foreach my $metadata_group (@metadata_groups) {
|
---|
| 145 | $self->{$metadata_group . ".partition_size_within_level"} = 20;
|
---|
[10398] | 146 | }
|
---|
| 147 | }
|
---|
| 148 | else {
|
---|
[12894] | 149 | my @partition_size_within_levellist = split(/\//, $self->{'partition_size_within_level'});
|
---|
[10398] | 150 |
|
---|
[10498] | 151 | # Assign values based on the partition_size_within_level parameter
|
---|
[12889] | 152 | foreach my $metadata_group (@metadata_groups) {
|
---|
[10498] | 153 | my $partition_size_within_levelelem = shift(@partition_size_within_levellist);
|
---|
| 154 | if (defined($partition_size_within_levelelem)) {
|
---|
[12889] | 155 | $self->{$metadata_group . ".partition_size_within_level"} = $partition_size_within_levelelem;
|
---|
[10398] | 156 | }
|
---|
| 157 | else {
|
---|
[12889] | 158 | $self->{$metadata_group . ".partition_size_within_level"} = $self->{$metadata_groups[0] . ".partition_size_within_level"};
|
---|
[10398] | 159 | }
|
---|
| 160 | }
|
---|
| 161 | }
|
---|
| 162 |
|
---|
[12894] | 163 | # The metadata elements to use to sort the leaf nodes (default: Title)
|
---|
| 164 | my @sort_leaf_nodes_using_metadata_groups = ( "Title" );
|
---|
| 165 | if ($self->{'sort_leaf_nodes_using'}) {
|
---|
| 166 | @sort_leaf_nodes_using_metadata_groups = split(/\|/, $self->{'sort_leaf_nodes_using'});
|
---|
[10398] | 167 | }
|
---|
[12894] | 168 | $self->{'sort_leaf_nodes_using_metadata_groups'} = \@sort_leaf_nodes_using_metadata_groups;
|
---|
[10398] | 169 |
|
---|
[13551] | 170 | # Create an instance of the Unicode::Collate object if better Unicode sorting is desired
|
---|
| 171 | if ($self->{'sort_using_unicode_collation'}) {
|
---|
[13791] | 172 | # To use this you first need to download the allkeys.txt file from
|
---|
| 173 | # http://www.unicode.org/Public/UCA/latest/allkeys.txt and put it in the Perl
|
---|
| 174 | # Unicode/Collate directory.
|
---|
[13551] | 175 | require Unicode::Collate;
|
---|
| 176 | $self->{'unicode_collator'} = Unicode::Collate->new();
|
---|
| 177 | }
|
---|
| 178 |
|
---|
[12894] | 179 | # An empty array for the document OIDs
|
---|
| 180 | $self->{'OIDs'} = [];
|
---|
| 181 |
|
---|
[10398] | 182 | return bless $self, $class;
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 |
|
---|
| 186 | sub init
|
---|
| 187 | {
|
---|
| 188 | # Nothing to do...
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 |
|
---|
[12896] | 192 | # Called for each document in the collection
|
---|
[10398] | 193 | sub classify
|
---|
| 194 | {
|
---|
| 195 | my $self = shift(@_);
|
---|
[18455] | 196 | my ($doc_obj,$edit_mode) = @_;
|
---|
[10398] | 197 |
|
---|
[12896] | 198 | # If "-classify_sections" is set, classify every section of the document
|
---|
[10398] | 199 | if ($self->{'classify_sections'}) {
|
---|
| 200 | my $section = $doc_obj->get_next_section($doc_obj->get_top_section());
|
---|
| 201 | while (defined $section) {
|
---|
[18455] | 202 | $self->classify_section($doc_obj, $doc_obj->get_OID() . ".$section", $section, $edit_mode);
|
---|
[10398] | 203 | $section = $doc_obj->get_next_section($section);
|
---|
| 204 | }
|
---|
| 205 | }
|
---|
[12896] | 206 | # Otherwise just classify the top document section
|
---|
[10398] | 207 | else {
|
---|
[18455] | 208 | $self->classify_section($doc_obj, $doc_obj->get_OID(), $doc_obj->get_top_section(), $edit_mode);
|
---|
[10398] | 209 | }
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 |
|
---|
| 213 | sub classify_section
|
---|
| 214 | {
|
---|
| 215 | my $self = shift(@_);
|
---|
[18455] | 216 | my ($doc_obj,$section_OID,$section,$edit_mode) = @_;
|
---|
[10398] | 217 |
|
---|
[12889] | 218 | my @metadata_groups = @{$self->{'metadata_groups'}};
|
---|
[10398] | 219 |
|
---|
[12896] | 220 | # Only classify the section if it has a value for one of the metadata elements in the first group
|
---|
| 221 | my $classify_section = 0;
|
---|
| 222 | my $first_metadata_group = $metadata_groups[0];
|
---|
| 223 | foreach my $first_metadata_group_element (split(/\;/, $first_metadata_group)) {
|
---|
| 224 | my $first_metadata_group_element_value = $doc_obj->get_metadata_element($section, $first_metadata_group_element);
|
---|
| 225 | if (defined($first_metadata_group_element_value) && $first_metadata_group_element_value ne "") {
|
---|
| 226 | # This section must be included in the classifier
|
---|
| 227 | $classify_section = 1;
|
---|
| 228 | last;
|
---|
| 229 | }
|
---|
| 230 | }
|
---|
[10398] | 231 |
|
---|
[12896] | 232 | # We're not classifying this section because it doesn't have the required metadata
|
---|
| 233 | return if (!$classify_section);
|
---|
[10398] | 234 |
|
---|
[18508] | 235 |
|
---|
| 236 | if (($edit_mode eq "delete") || ($edit_mode eq "update")) {
|
---|
[18455] | 237 | $self->oid_array_delete($section_OID,'OIDs');
|
---|
[18508] | 238 | if ($edit_mode eq "delete") {
|
---|
| 239 | return;
|
---|
| 240 | }
|
---|
[18455] | 241 | }
|
---|
| 242 |
|
---|
[12896] | 243 | # Otherwise, include this section in the classifier
|
---|
| 244 | push(@{$self->{'OIDs'}}, $section_OID);
|
---|
| 245 |
|
---|
| 246 | # Create a hash for the metadata values of each metadata element we're interested in
|
---|
| 247 | my %metadata_groups_done = ();
|
---|
| 248 | foreach my $metadata_group (@metadata_groups, @{$self->{'sort_leaf_nodes_using_metadata_groups'}}) {
|
---|
| 249 | # Take care not to do a metadata group more than once
|
---|
| 250 | unless ($metadata_groups_done{$metadata_group}) {
|
---|
| 251 | foreach my $metadata_element (split(/\;/, $metadata_group)) {
|
---|
| 252 | my @metadata_values = @{$doc_obj->get_metadata($section, $metadata_element)};
|
---|
| 253 | foreach my $metadata_value (@metadata_values) {
|
---|
| 254 | # Strip leading and trailing whitespace
|
---|
| 255 | $metadata_value =~ s/^\s*//;
|
---|
| 256 | $metadata_value =~ s/\s*$//;
|
---|
[13550] | 257 |
|
---|
| 258 | # Convert the metadata value from a UTF-8 string to a Unicode string
|
---|
| 259 | # This means that length() and substr() work properly
|
---|
| 260 | # We need to be careful to convert classifier node title values back to UTF-8, however
|
---|
[14173] | 261 | my $metadata_value_unicode_string = $self->convert_utf8_string_to_unicode_string($metadata_value);
|
---|
[13550] | 262 |
|
---|
| 263 | # Add the metadata value into the list for this combination of metadata group and section
|
---|
| 264 | push(@{$self->{$metadata_group . ".list"}->{$section_OID}}, $metadata_value_unicode_string);
|
---|
[10398] | 265 | }
|
---|
[12896] | 266 | last if (@metadata_values > 0);
|
---|
[10398] | 267 | }
|
---|
| 268 |
|
---|
[12896] | 269 | $metadata_groups_done{$metadata_group} = 1;
|
---|
[10398] | 270 | }
|
---|
| 271 | }
|
---|
| 272 | }
|
---|
| 273 |
|
---|
| 274 |
|
---|
| 275 | sub get_classify_info
|
---|
| 276 | {
|
---|
| 277 | my $self = shift(@_);
|
---|
| 278 |
|
---|
[12896] | 279 | # The metadata groups to classify by
|
---|
[12889] | 280 | my @metadata_groups = @{$self->{'metadata_groups'}};
|
---|
| 281 | my $first_metadata_group = $metadata_groups[0];
|
---|
[10398] | 282 |
|
---|
[12896] | 283 | # The OID values of the documents to include in the classifier
|
---|
[12889] | 284 | my @OIDs = @{$self->{'OIDs'}};
|
---|
[10398] | 285 |
|
---|
[12896] | 286 | # Create the root node of the classification hierarchy
|
---|
[12893] | 287 | my %classifier_node = ( 'thistype' => "Invisible",
|
---|
[12895] | 288 | 'childtype' => $self->{$first_metadata_group . ".list_type"},
|
---|
[12894] | 289 | 'Title' => $self->{'buttonname'},
|
---|
[13271] | 290 | 'contains' => [],
|
---|
| 291 | 'mdtype' => $first_metadata_group );
|
---|
[10398] | 292 |
|
---|
[12895] | 293 | # Recursively create the classification hierarchy, one level for each metadata group
|
---|
[14173] | 294 | $self->add_level(\@metadata_groups, \@OIDs, \%classifier_node);
|
---|
[12893] | 295 | return \%classifier_node;
|
---|
[10398] | 296 | }
|
---|
| 297 |
|
---|
| 298 |
|
---|
[12895] | 299 | sub add_level
|
---|
[10398] | 300 | {
|
---|
| 301 | my $self = shift(@_);
|
---|
[12889] | 302 | my @metadata_groups = @{shift(@_)};
|
---|
| 303 | my @OIDs = @{shift(@_)};
|
---|
[12893] | 304 | my $classifier_node = shift(@_);
|
---|
| 305 | # print STDERR "\nAdding AZ list for " . $classifier_node->{'Title'} . "\n";
|
---|
[10398] | 306 |
|
---|
[12889] | 307 | my $metadata_group = $metadata_groups[0];
|
---|
| 308 | # print STDERR "Processing metadata group: " . $metadata_group . "\n";
|
---|
| 309 | # print STDERR "Number of OID values: " . @OIDs . "\n";
|
---|
[10398] | 310 |
|
---|
[13340] | 311 | if (!defined($self->{$metadata_group . ".list"})) {
|
---|
| 312 | print STDERR "Warning: No metadata values assigned to $metadata_group.\n";
|
---|
| 313 | return;
|
---|
| 314 | }
|
---|
[10398] | 315 |
|
---|
| 316 | # Create a mapping from metadata value to OID
|
---|
[14845] | 317 | my $OID_to_metadata_values_hash_ref = $self->{$metadata_group . ".list"};
|
---|
| 318 | my %metadata_value_to_OIDs_hash = ();
|
---|
| 319 | foreach my $OID (@OIDs)
|
---|
| 320 | {
|
---|
| 321 | if ($OID_to_metadata_values_hash_ref->{$OID})
|
---|
| 322 | {
|
---|
| 323 | my @metadata_values = @{$OID_to_metadata_values_hash_ref->{$OID}};
|
---|
| 324 | foreach my $metadata_value (@metadata_values)
|
---|
| 325 | {
|
---|
| 326 | push(@{$metadata_value_to_OIDs_hash{$metadata_value}}, $OID);
|
---|
[10398] | 327 | }
|
---|
| 328 | }
|
---|
| 329 | }
|
---|
[14845] | 330 | # print STDERR "Number of distinct values: " . scalar(keys %metadata_value_to_OIDs_hash) . "\n";
|
---|
[10398] | 331 |
|
---|
| 332 | # Partition the values (if necessary)
|
---|
[10498] | 333 | my $partition_type_within_level = $self->{"partition_type_within_level"};
|
---|
| 334 | if ($partition_type_within_level =~ /^per_letter$/i) {
|
---|
[10398] | 335 | # Generate one hlist for each letter
|
---|
[14845] | 336 | my @sortedmetadata_values = $self->sort_metadata_values_array(keys(%metadata_value_to_OIDs_hash));
|
---|
| 337 | my %metadata_value_to_OIDs_subhash = ();
|
---|
[10398] | 338 |
|
---|
[14845] | 339 | my $lastpartition = substr($sortedmetadata_values[0], 0, 1);
|
---|
| 340 | foreach my $metadata_value (@sortedmetadata_values) {
|
---|
| 341 | my $metadata_valuepartition = substr($metadata_value, 0, 1);
|
---|
[10398] | 342 |
|
---|
| 343 | # Is this the start of a new partition?
|
---|
[14845] | 344 | if ($metadata_valuepartition ne $lastpartition) {
|
---|
| 345 | $self->add_hlist_partition(\@metadata_groups, $classifier_node, $lastpartition, \%metadata_value_to_OIDs_subhash);
|
---|
| 346 | %metadata_value_to_OIDs_subhash = ();
|
---|
| 347 | $lastpartition = $metadata_valuepartition;
|
---|
[10398] | 348 | }
|
---|
| 349 |
|
---|
[14845] | 350 | $metadata_value_to_OIDs_subhash{$metadata_value} = $metadata_value_to_OIDs_hash{$metadata_value};
|
---|
[10398] | 351 | }
|
---|
| 352 |
|
---|
| 353 | # Don't forget to add the last partition
|
---|
[14845] | 354 | $self->add_hlist_partition(\@metadata_groups, $classifier_node, $lastpartition, \%metadata_value_to_OIDs_subhash);
|
---|
[10398] | 355 |
|
---|
| 356 | # The partitions are stored in an HList
|
---|
[12893] | 357 | $classifier_node->{'childtype'} = "HList";
|
---|
[10398] | 358 | }
|
---|
| 359 |
|
---|
| 360 | else {
|
---|
| 361 | # Generate hlists of a certain size
|
---|
[12889] | 362 | my $partition_size_within_level = $self->{$metadata_group . ".partition_size_within_level"};
|
---|
[14845] | 363 | if ($partition_type_within_level =~ /^constant_size$/i && scalar(keys %metadata_value_to_OIDs_hash) > $partition_size_within_level) {
|
---|
| 364 | my @sortedmetadata_values = $self->sort_metadata_values_array(keys(%metadata_value_to_OIDs_hash));
|
---|
[10398] | 365 | my $itemsdone = 0;
|
---|
[14845] | 366 | my %metadata_value_to_OIDs_subhash = ();
|
---|
[10398] | 367 | my $lastpartitionend = "";
|
---|
| 368 | my $partitionstart;
|
---|
[14845] | 369 | foreach my $metadata_value (@sortedmetadata_values) {
|
---|
| 370 | $metadata_value_to_OIDs_subhash{$metadata_value} = $metadata_value_to_OIDs_hash{$metadata_value};
|
---|
[10398] | 371 | $itemsdone++;
|
---|
[14845] | 372 | my $itemsinpartition = scalar(keys %metadata_value_to_OIDs_subhash);
|
---|
[10398] | 373 |
|
---|
| 374 | # Is this the start of a new partition?
|
---|
| 375 | if ($itemsinpartition == 1) {
|
---|
[14845] | 376 | $partitionstart = $self->generate_partition_start($metadata_value, $lastpartitionend, $self->{"partition_name_length"});
|
---|
[10398] | 377 | }
|
---|
| 378 |
|
---|
| 379 | # Is this the end of the partition?
|
---|
[14845] | 380 | if ($itemsinpartition == $partition_size_within_level || $itemsdone == @sortedmetadata_values) {
|
---|
| 381 | my $partitionend = $self->generate_partition_end($metadata_value, $partitionstart, $self->{"partition_name_length"});
|
---|
[10398] | 382 | my $partitionname = $partitionstart;
|
---|
| 383 | if ($partitionend ne $partitionstart) {
|
---|
| 384 | $partitionname = $partitionname . "-" . $partitionend;
|
---|
| 385 | }
|
---|
| 386 |
|
---|
[14845] | 387 | $self->add_hlist_partition(\@metadata_groups, $classifier_node, $partitionname, \%metadata_value_to_OIDs_subhash);
|
---|
| 388 | %metadata_value_to_OIDs_subhash = ();
|
---|
[10398] | 389 | $lastpartitionend = $partitionend;
|
---|
| 390 | }
|
---|
| 391 | }
|
---|
| 392 |
|
---|
| 393 | # The partitions are stored in an HList
|
---|
[12893] | 394 | $classifier_node->{'childtype'} = "HList";
|
---|
[10398] | 395 | }
|
---|
| 396 |
|
---|
| 397 | # Otherwise just add all the values to a VList
|
---|
| 398 | else {
|
---|
[14845] | 399 | $self->add_vlist(\@metadata_groups, $classifier_node, \%metadata_value_to_OIDs_hash);
|
---|
[10398] | 400 | }
|
---|
| 401 | }
|
---|
| 402 | }
|
---|
| 403 |
|
---|
| 404 |
|
---|
[13550] | 405 | sub convert_utf8_string_to_unicode_string
|
---|
[10398] | 406 | {
|
---|
[14173] | 407 | my $self = shift(@_);
|
---|
[13550] | 408 | my $utf8_string = shift(@_);
|
---|
[10398] | 409 |
|
---|
[13550] | 410 | my $unicode_string = "";
|
---|
| 411 | foreach my $unicode_value (@{&unicode::utf82unicode($utf8_string)}) {
|
---|
| 412 | $unicode_string .= chr($unicode_value);
|
---|
| 413 | }
|
---|
| 414 | return $unicode_string;
|
---|
[10398] | 415 | }
|
---|
| 416 |
|
---|
| 417 |
|
---|
[13550] | 418 | sub convert_unicode_string_to_utf8_string
|
---|
| 419 | {
|
---|
[14173] | 420 | my $self = shift(@_);
|
---|
[13550] | 421 | my $unicode_string = shift(@_);
|
---|
| 422 |
|
---|
| 423 | my @unicode_array;
|
---|
| 424 | for (my $i = 0; $i < length($unicode_string); $i++) {
|
---|
| 425 | push(@unicode_array, ord(substr($unicode_string, $i, 1)));
|
---|
| 426 | }
|
---|
| 427 | return &unicode::unicode2utf8(\@unicode_array);
|
---|
| 428 | }
|
---|
| 429 |
|
---|
| 430 |
|
---|
[10398] | 431 | sub generate_partition_start
|
---|
| 432 | {
|
---|
[14173] | 433 | my $self = shift(@_);
|
---|
[14845] | 434 | my $metadata_value = shift(@_);
|
---|
[10398] | 435 | my $lastpartitionend = shift(@_);
|
---|
[14084] | 436 | my $partition_name_length = shift(@_);
|
---|
[10398] | 437 |
|
---|
[14084] | 438 | if ($partition_name_length) {
|
---|
[14845] | 439 | return substr($metadata_value, 0, $partition_name_length);
|
---|
[14084] | 440 | }
|
---|
| 441 |
|
---|
[14845] | 442 | my $partitionstart = substr($metadata_value, 0, 1);
|
---|
[10398] | 443 | if ($partitionstart le $lastpartitionend) {
|
---|
[14845] | 444 | $partitionstart = substr($metadata_value, 0, 2);
|
---|
[10398] | 445 | # Give up after three characters
|
---|
| 446 | if ($partitionstart le $lastpartitionend) {
|
---|
[14845] | 447 | $partitionstart = substr($metadata_value, 0, 3);
|
---|
[10398] | 448 | }
|
---|
| 449 | }
|
---|
| 450 |
|
---|
| 451 | return $partitionstart;
|
---|
| 452 | }
|
---|
| 453 |
|
---|
| 454 |
|
---|
| 455 | sub generate_partition_end
|
---|
| 456 | {
|
---|
[14173] | 457 | my $self = shift(@_);
|
---|
[14845] | 458 | my $metadata_value = shift(@_);
|
---|
[10398] | 459 | my $partitionstart = shift(@_);
|
---|
[14084] | 460 | my $partition_name_length = shift(@_);
|
---|
[10398] | 461 |
|
---|
[14084] | 462 | if ($partition_name_length) {
|
---|
[14845] | 463 | return substr($metadata_value, 0, $partition_name_length);
|
---|
[14084] | 464 | }
|
---|
| 465 |
|
---|
[14845] | 466 | my $partitionend = substr($metadata_value, 0, length($partitionstart));
|
---|
[10398] | 467 | if ($partitionend gt $partitionstart) {
|
---|
[14845] | 468 | $partitionend = substr($metadata_value, 0, 1);
|
---|
[10398] | 469 | if ($partitionend le $partitionstart) {
|
---|
[14845] | 470 | $partitionend = substr($metadata_value, 0, 2);
|
---|
[10398] | 471 | # Give up after three characters
|
---|
| 472 | if ($partitionend le $partitionstart) {
|
---|
[14845] | 473 | $partitionend = substr($metadata_value, 0, 3);
|
---|
[10398] | 474 | }
|
---|
| 475 | }
|
---|
| 476 | }
|
---|
| 477 |
|
---|
| 478 | return $partitionend;
|
---|
| 479 | }
|
---|
| 480 |
|
---|
| 481 |
|
---|
| 482 | sub add_hlist_partition
|
---|
| 483 | {
|
---|
| 484 | my $self = shift(@_);
|
---|
[12889] | 485 | my @metadata_groups = @{shift(@_)};
|
---|
[12893] | 486 | my $classifier_node = shift(@_);
|
---|
[10398] | 487 | my $partitionname = shift(@_);
|
---|
[14845] | 488 | my $metadata_value_to_OIDs_hash_ref = shift(@_);
|
---|
[10398] | 489 |
|
---|
| 490 | # Create an hlist partition
|
---|
[14173] | 491 | my %child_classifier_node = ( 'Title' => $self->convert_unicode_string_to_utf8_string($partitionname),
|
---|
[12893] | 492 | 'childtype' => "VList",
|
---|
| 493 | 'contains' => [] );
|
---|
[10398] | 494 |
|
---|
| 495 | # Add the children to the hlist partition
|
---|
[14845] | 496 | $self->add_vlist(\@metadata_groups, \%child_classifier_node, $metadata_value_to_OIDs_hash_ref);
|
---|
[12893] | 497 | push(@{$classifier_node->{'contains'}}, \%child_classifier_node);
|
---|
[10398] | 498 | }
|
---|
| 499 |
|
---|
| 500 |
|
---|
| 501 | sub add_vlist
|
---|
| 502 | {
|
---|
| 503 | my $self = shift(@_);
|
---|
[12889] | 504 | my @metadata_groups = @{shift(@_)};
|
---|
[12893] | 505 | my $classifier_node = shift(@_);
|
---|
[14845] | 506 | my $metadata_value_to_OIDs_hash_ref = shift(@_);
|
---|
[10398] | 507 |
|
---|
[12889] | 508 | my $metadata_group = shift(@metadata_groups);
|
---|
[13287] | 509 | $classifier_node->{'mdtype'} = $metadata_group;
|
---|
[10398] | 510 |
|
---|
| 511 | # Create an entry in the vlist for each value
|
---|
[14845] | 512 | foreach my $metadata_value ($self->sort_metadata_values_array(keys(%{$metadata_value_to_OIDs_hash_ref})))
|
---|
| 513 | {
|
---|
| 514 | my @OIDs = @{$metadata_value_to_OIDs_hash_ref->{$metadata_value}};
|
---|
[10398] | 515 |
|
---|
[10498] | 516 | # If there is only one item and 'always_bookshelf' is false, add the item to the list
|
---|
[12889] | 517 | if (@OIDs == 1 && $self->{$metadata_group . ".always_bookshelf"} eq "f") {
|
---|
[13271] | 518 | my $OID = $OIDs[0];
|
---|
| 519 |
|
---|
| 520 | # Find the offset of this metadata value
|
---|
| 521 | my $offset = 0;
|
---|
[14845] | 522 | my $OID_to_metadata_values_hash_ref = $self->{$metadata_group . ".list"};
|
---|
| 523 | my @metadata_values = @{$OID_to_metadata_values_hash_ref->{$OID}};
|
---|
| 524 | for (my $i = 0; $i < scalar(@metadata_values); $i++) {
|
---|
| 525 | if ($metadata_value eq $metadata_values[$i]) {
|
---|
[13271] | 526 | $offset = $i;
|
---|
| 527 | last;
|
---|
| 528 | }
|
---|
| 529 | }
|
---|
| 530 | push(@{$classifier_node->{'contains'}}, { 'OID' => $OID, 'offset' => $offset });
|
---|
[10398] | 531 | }
|
---|
| 532 |
|
---|
| 533 | # Otherwise create a sublist (bookshelf) for the metadata value
|
---|
| 534 | else {
|
---|
[14845] | 535 | my %child_classifier_node = ( 'Title' => $self->convert_unicode_string_to_utf8_string($metadata_value),
|
---|
[12893] | 536 | 'childtype' => "VList",
|
---|
| 537 | 'contains' => [] );
|
---|
[10398] | 538 |
|
---|
| 539 | # If there are metadata elements remaining, recursively apply the process
|
---|
[12889] | 540 | if (@metadata_groups > 0) {
|
---|
| 541 | my $next_metadata_group = $metadata_groups[0];
|
---|
[12895] | 542 | $child_classifier_node{'childtype'} = $self->{$next_metadata_group . ".list_type"};
|
---|
[14173] | 543 | $self->add_level(\@metadata_groups, \@OIDs, \%child_classifier_node);
|
---|
[10398] | 544 | }
|
---|
| 545 | # Otherwise just add the documents as children of this list
|
---|
| 546 | else {
|
---|
[10498] | 547 | # Sort the leaf nodes by the metadata elements specified with -sort_leaf_nodes_using
|
---|
[12894] | 548 | my @sort_leaf_nodes_usingmetadata_groups = @{$self->{'sort_leaf_nodes_using_metadata_groups'}};
|
---|
[12889] | 549 | foreach my $sort_leaf_nodes_usingmetaelem (reverse @sort_leaf_nodes_usingmetadata_groups) {
|
---|
[14845] | 550 | my $OID_to_metadata_values_hash_ref = $self->{$sort_leaf_nodes_usingmetaelem . ".list"};
|
---|
[10398] | 551 | # Force a stable sort (Perl 5.6's sort isn't stable)
|
---|
| 552 | # !! The [0] bits aren't ideal (multiple metadata values) !!
|
---|
[14845] | 553 | @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 ];
|
---|
[10398] | 554 | }
|
---|
| 555 |
|
---|
[12889] | 556 | foreach my $OID (@OIDs) {
|
---|
[12893] | 557 | push(@{$child_classifier_node{'contains'}}, { 'OID' => $OID });
|
---|
[10398] | 558 | }
|
---|
| 559 | }
|
---|
| 560 |
|
---|
| 561 | # Add the sublist to the list
|
---|
[12893] | 562 | push(@{$classifier_node->{'contains'}}, \%child_classifier_node);
|
---|
[10398] | 563 | }
|
---|
| 564 | }
|
---|
| 565 | }
|
---|
| 566 |
|
---|
| 567 |
|
---|
[13551] | 568 | sub sort_metadata_values_array
|
---|
| 569 | {
|
---|
| 570 | my $self = shift(@_);
|
---|
| 571 | my @metadata_values = @_;
|
---|
| 572 |
|
---|
| 573 | if ($self->{'unicode_collator'}) {
|
---|
| 574 | return $self->{'unicode_collator'}->sort(@metadata_values);
|
---|
| 575 | }
|
---|
| 576 | else {
|
---|
| 577 | return sort(@metadata_values);
|
---|
| 578 | }
|
---|
| 579 | }
|
---|
| 580 |
|
---|
| 581 |
|
---|
[10398] | 582 | 1;
|
---|