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