source: gsdl/trunk/perllib/classify/AZCompactList.pm@ 17209

Last change on this file since 17209 was 17209, checked in by kjdon, 16 years ago

BasClas renamed to BaseClassifier, tidied up constructors

  • Property svn:keywords set to Author Date Id Revision
File size: 23.0 KB
Line 
1###########################################################################
2#
3# AZCompactList.pm --
4# A component of the Greenstone digital library software
5# from the New Zealand Digital Library Project at the
6# University of Waikato, New Zealand.
7#
8# Copyright (C) 1999 New Zealand Digital Library Project
9#
10# This program is free software; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program; if not, write to the Free Software
22# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23#
24###########################################################################
25# classifier plugin for sorting alphabetically
26
27package AZCompactList;
28
29use BaseClassifier;
30use sorttools;
31
32use strict;
33no strict 'refs'; # allow filehandles to be variables and viceversa
34
35sub BEGIN {
36 @AZCompactList::ISA = ('BaseClassifier');
37}
38
39my $doclevel_list =
40 [ { 'name' => "top",
41 'desc' => "{AZCompactList.doclevel.top}" },
42 { 'name' => "section",
43 'desc' => "{AZCompactList.doclevel.section}" } ];
44
45my $arguments =
46 [ { 'name' => "metadata",
47 'desc' => "{AZCompactList.metadata}",
48 'type' => "metadata",
49 'reqd' => "yes" },
50 { 'name' => "firstvalueonly",
51 'desc' => "{AZCompactList.firstvalueonly}",
52 'type' => "flag",
53 'reqd' => "no" },
54 { 'name' => "allvalues",
55 'desc' => "{AZCompactList.allvalues}",
56 'type' => "flag",
57 'reqd' => "no" },
58 { 'name' => "sort",
59 'desc' => "{AZCompactList.sort}",
60 'type' => "metadata",
61# 'deft' => "Title",
62 'reqd' => "no" },
63 { 'name' => "removeprefix",
64 'desc' => "{BasClas.removeprefix}",
65 'type' => "regexp",
66 'deft' => "",
67 'reqd' => "no" },
68 { 'name' => "removesuffix",
69 'desc' => "{BasClas.removesuffix}",
70 'type' => "regexp",
71 'deft' => "",
72 'reqd' => "no" },
73 { 'name' => "mingroup",
74 'desc' => "{AZCompactList.mingroup}",
75 'type' => "int",
76 'deft' => "1",
77 'range' => "1,",
78 'reqd' => "no" },
79 { 'name' => "minnesting",
80 'desc' => "{AZCompactList.minnesting}",
81 'type' => "int",
82 'deft' => "20",
83 'range' => "2,",
84 'reqd' => "no" },
85 { 'name' => "mincompact",
86 'desc' => "{AZCompactList.mincompact}",
87 'type' => "int",
88 'deft' => "10",
89 'range' => "1,",
90 'reqd' => "no" },
91 { 'name' => "maxcompact",
92 'desc' => "{AZCompactList.maxcompact}",
93 'type' => "int",
94 'deft' => "30",
95 'range' => "1,",
96 'reqd' => "no" },
97 { 'name' => "doclevel",
98 'desc' => "{AZCompactList.doclevel}",
99 'type' => "enum",
100 'list' => $doclevel_list,
101 'deft' => "top",
102 'reqd' => "no" },
103 { 'name' => "freqsort",
104 'desc' => "{AZCompactList.freqsort}",
105 'type' => "flag"},
106 { 'name' => "recopt",
107 'desc' => "{AZCompactList.recopt}",
108 'type' => "flag",
109 'reqd' => "no" } ];
110
111my $options =
112{ 'name' => "AZCompactList",
113 'desc' => "{AZCompactList.desc}",
114 'abstract' => "no",
115 'inherits' => "yes",
116 'args' => $arguments };
117
118
119sub new {
120 my ($class) = shift (@_);
121 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
122 push(@$pluginlist, $class);
123
124 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
125 push(@{$hashArgOptLists->{"OptList"}},$options);
126
127 my $self = new BaseClassifier($pluginlist, $inputargs, $hashArgOptLists);
128
129 if ($self->{'info_only'}) {
130 # don't worry about any options etc
131 return bless $self, $class;
132 }
133
134 if (!$self->{"metadata"}) {
135 my $outhandle = $self->{'outhandle'};
136 print $outhandle "AZCompactList Error: required option -metadata not supplied\n";
137 $self->print_txt_usage("");
138 die "AZCompactList Error: required option -metadata not supplied\n";
139 }
140
141 # Manually set $self parameters.
142 $self->{'list'} = {};
143 $self->{'listmetavalue'} = {};
144 $self->{'list_mvpair'} = {};
145 $self->{'reclassify'} = {};
146 $self->{'reclassifylist'} = {};
147
148 $self->{'buttonname'} = $self->generate_title_from_metadata($self->{'metadata'}) unless ($self->{'buttonname'});
149
150 if (defined($self->{"removeprefix"}) && $self->{"removeprefix"}) {
151 $self->{"removeprefix"} =~ s/^\^//; # don't need a leading ^
152 }
153 if (defined($self->{"removesuffix"}) && $self->{"removesuffix"}) {
154 $self->{"removesuffix"} =~ s/\$$//; # don't need a trailing $
155 }
156
157 $self->{'recopt'} = ($self->{'recopt'} == 0) ? undef : "on";
158
159 # Clean out the unused keys
160 if($self->{"removeprefix"} eq "") {delete $self->{"removeprefix"};}
161 if($self->{"removesuffix"} eq "") {delete $self->{"removesuffix"};}
162
163 return bless $self, $class;
164}
165
166sub init
167{
168 my $self = shift (@_);
169
170 $self->{'list'} = {};
171 $self->{'listmetavalue'} = {};
172 $self->{'list_mvpair'} = {};
173 $self->{'reclassify'} = {};
174 $self->{'reclassifylist'} = {};
175}
176
177my $tmp = 0;
178
179sub classify
180{
181 my $self = shift (@_);
182 my ($doc_obj) = @_;
183
184 my $doc_OID = $doc_obj->get_OID();
185
186 my @sectionlist = ();
187 my $topsection = $doc_obj->get_top_section();
188 my $metaname = $self->{'metadata'};
189 my $outhandle = $self->{'outhandle'};
190
191 $metaname =~ s/(\/|\|).*//; # grab first name in n1/n2/n3 or n1|n2|n3 list
192 my @commameta_list = split(/,/, $metaname);
193
194 if ($self->{'doclevel'} =~ /^top(level)?/i)
195 {
196 push(@sectionlist,$topsection);
197 }
198 else
199 {
200 my $thissection = $doc_obj->get_next_section($topsection);
201 while (defined $thissection)
202 {
203 push(@sectionlist,$thissection);
204 $thissection = $doc_obj->get_next_section ($thissection);
205 }
206 }
207
208 my $thissection;
209 foreach $thissection (@sectionlist)
210 {
211 my $full_doc_OID
212 = ($thissection ne "") ? "$doc_OID.$thissection" : $doc_OID;
213 if (defined $self->{'list_mvpair'}->{$full_doc_OID})
214 {
215 print $outhandle "WARNING: AZCompactList::classify called multiple times for $full_doc_OID\n";
216 }
217 $self->{'list'}->{$full_doc_OID} = [];
218 $self->{'listmetavalue'}->{$full_doc_OID} = [];
219 $self->{'list_mvpair'}->{$full_doc_OID} = [];
220
221 my $metavalues = [];
222 foreach my $cmn (@commameta_list) {
223 my $cmvalues = $doc_obj->get_metadata($thissection,$cmn);
224 push(@$metavalues,@$cmvalues) if (@{$cmvalues});
225 last if (@{$cmvalues} && !$self->{'allvalues'});
226 }
227
228 my $metavalue;
229 foreach $metavalue (@$metavalues)
230 {
231 # Tidy up use of white space in metavalue for classifying
232 $metavalue =~ s/^\s*//s;
233 $metavalue =~ s/\s*$//s;
234 $metavalue =~ s/\n/ /s;
235 $metavalue =~ s/\s{2,}/ /s;
236
237 # if this document doesn't contain the metadata element we're
238 # sorting by we won't include it in this classification
239 if (defined $metavalue && $metavalue =~ /\w/)
240 {
241 if (defined($self->{'removeprefix'}) &&
242 length($self->{'removeprefix'})) {
243 $metavalue =~ s/^$self->{'removeprefix'}//;
244
245 # check that it's not now empty
246 if (!$metavalue) {next;}
247 }
248
249 if (defined($self->{'removesuffix'}) &&
250 length($self->{'removesuffix'})) {
251 $metavalue =~ s/$self->{'removesuffix'}$//;
252
253 # check that it's not now empty
254 if (!$metavalue) {next;}
255 }
256
257 my $formatted_metavalue;
258 if ($self->{'no_metadata_formatting'}) {
259 $formatted_metavalue = $metavalue;
260 } else {
261 $formatted_metavalue = &sorttools::format_metadata_for_sorting($self->{'metadata'}, $metavalue, $doc_obj);
262 }
263
264 #### prefix-str
265 if (! defined($formatted_metavalue)) {
266 print $outhandle "Warning: AZCompactList: metavalue is ";
267 print $outhandle "empty\n";
268 $formatted_metavalue="";
269 }
270
271 my $mv_pair = { 'mv' => $metavalue, 'fmv' => $formatted_metavalue };
272 push(@{$self->{'list'}->{$full_doc_OID}},$formatted_metavalue);
273 push(@{$self->{'listmetavalue'}->{$full_doc_OID}} ,$metavalue);
274 push(@{$self->{'list_mvpair'}->{$full_doc_OID}},$mv_pair);
275
276
277 last if ($self->{'firstvalueonly'});
278 }
279 }
280
281 # This is used in reclassify below for AZCompactSectionList
282 my $sortmeta = $doc_obj->get_metadata_element($thissection, $self->{'sort'});
283 $self->{'reclassify'}->{$full_doc_OID} = [$doc_obj,$sortmeta];
284 }
285}
286
287sub reinit
288{
289 my ($self,$classlist_ref) = @_;
290 my $outhandle = $self->{'outhandle'};
291
292 my %mtfreq = ();
293 my @single_classlist = ();
294 my @multiple_classlist = ();
295
296 # find out how often each metavalue occurs
297 map
298 {
299 foreach my $mvp (@{$self->{'list_mvpair'}->{$_}} )
300 {
301### print STDERR "*** plain mv = $mvp->{'mv'}\n";
302### print STDERR "*** format mv = $mvp->{'fmv'}\n";
303
304 my $metavalue = $mvp->{'mv'};
305 $metavalue =~ s!^-!\\-!; # in case it starts with "-"
306 $mtfreq{$metavalue}++;
307 }
308 } @$classlist_ref;
309
310 # use this information to split the list: single metavalue/repeated value
311 map
312 {
313 my $i = 1;
314 my $metavalue;
315 foreach my $mvp (@{$self->{'list_mvpair'}->{$_}})
316 {
317 my $metavalue = $mvp->{'mv'};
318 $metavalue =~ s!^-!\\-!; # in case it starts with "-"
319 my $cs_metavalue = $mvp->{'mv'}; # case sensative
320 if ($mtfreq{$metavalue} >= $self->{'mingroup'})
321 {
322### print STDERR "*** pushing on $cs_metavalue\n";
323 push(@multiple_classlist,[$_,$i,$metavalue]);
324 }
325 else
326 {
327 push(@single_classlist,[$_,$cs_metavalue]);
328 $metavalue =~ tr/[A-Z]/[a-z]/;
329 $self->{'reclassifylist'}->{"Metavalue_$i.$_"} = $metavalue;
330 }
331 $i++;
332 }
333 } @$classlist_ref;
334
335
336 # Setup sub-classifiers for multiple list
337
338 $self->{'classifiers'} = {};
339
340 my $pm;
341 foreach $pm ("List", "SectionList")
342 {
343 my $listname
344 = &util::filename_cat($ENV{'GSDLHOME'},"perllib/classify/$pm.pm");
345 if (-e $listname) { require $listname; }
346 else
347 {
348 print $outhandle "AZCompactList ERROR - couldn't find classifier \"$listname\"\n";
349 die "\n";
350 }
351 }
352
353 # Create classifiers objects for each entry >= mingroup
354 my $metavalue;
355 my $doclevel = $self->{'doclevel'};
356 my $mingroup = $self->{'mingroup'};
357 my @metaname_list = split(/\/|\|/,$self->{'metadata'});
358 my $metaname = shift(@metaname_list);
359 my $hierarchical = 0;
360 if (scalar(@metaname_list) > 1) {
361 $hierarchical = 1;
362 $metaname = join('/',@metaname_list);
363 }
364 foreach $metavalue (keys %mtfreq)
365 {
366 if ($mtfreq{$metavalue} >= $mingroup)
367 {
368 # occurs more often than minimum required to compact into a group
369 my $listclassobj;
370
371 if (!$hierarchical)
372 {
373 my @args;
374 push @args, ("-metadata", "$metaname");
375 # buttonname is also used for the node's title
376 push @args, ("-buttonname", "$metavalue");
377 push @args, ("-sort", $self->{'sort'});
378
379 my $ptArgs = \@args;
380 if ($doclevel =~ m/^top(level)?/i)
381 {
382 eval ("\$listclassobj = new List([],\$ptArgs)");
383 }
384 else
385 {
386 eval ("\$listclassobj = new SectionList([],\$ptArgs)");
387 }
388 }
389 else
390 {
391
392 my @args;
393 push @args, ("-metadata", "$metaname");
394 # buttonname is also used for the node's title
395 push @args, ("-buttonname", "$metavalue");
396 push @args, ("-sort", $self->{'sort'});
397
398 if (defined $self->{'removeprefix'}) {
399 push @args, ("-removeprefix", $self->{'removeprefix'});
400 }
401 if (defined $self->{'removesuffix'}) {
402 push @args, ("-removesuffix", $self->{'removesuffix'});
403 }
404
405 push @args, ("-doclevel", "$doclevel");
406 push @args, ("-mingroup", $mingroup);
407 push @args, "-recopt ";
408
409 my $ptArgs = \@args;
410 eval ("\$listclassobj = new AZCompactList([],\$ptArgs)");
411 }
412
413 if ($@) {
414 print $outhandle "$@";
415 die "\n";
416 }
417
418 $listclassobj->init();
419
420 if (defined $metavalue && $metavalue =~ /\w/)
421 {
422 my $formatted_node = $metavalue;
423
424 if (defined($self->{'removeprefix'}) &&
425 length($self->{'removeprefix'})) {
426 $formatted_node =~ s/^$self->{'removeprefix'}//;
427 # check that it's not now empty
428 if (!$formatted_node) {next;}
429 }
430 if (defined($self->{'removesuffix'}) &&
431 length($self->{'removesuffix'})) {
432 $formatted_node =~ s/$self->{'removesuffix'}$//;
433 # check that it's not now empty
434 if (!$formatted_node) {next;}
435 }
436
437 $formatted_node = &sorttools::format_metadata_for_sorting($self->{'metadata'}, $formatted_node) unless $self->{'no_metadata_formatting'};
438
439 # In case our formatted string is empty...
440 if (! defined($formatted_node)) {
441 print $outhandle "Warning: AZCompactList: metavalue is ";
442 print $outhandle "empty\n";
443 $formatted_node="";
444 }
445
446 # use the lower case, for speed of lookup.
447 my $meta_lc=lc($metavalue);
448 $self->{'classifiers'}->{$meta_lc}
449 = { 'classifyobj' => $listclassobj,
450 'formattednode' => $formatted_node };
451 }
452 }
453 }
454
455
456 return (\@single_classlist,\@multiple_classlist);
457}
458
459
460sub reclassify
461{
462 my ($self,$multiple_cl_ref) = @_;
463
464 # Entries in the current classify list that are "book nodes"
465 # should be recursively classified.
466 #--
467 foreach my $dm_pair (@$multiple_cl_ref)
468 {
469 my ($doc_OID,$mdoffset,$metavalue,$cs_metavalue) = @$dm_pair;
470 my $listclassobj;
471
472 # find metavalue in list of sub-classifiers
473 # check if we have a key (lower case) for this metadata value
474 my $node_name=lc($metavalue);
475 if (exists $self->{'classifiers'}->{$node_name})
476 {
477 my ($doc_obj, $sortmeta) = @{$self->{'reclassify'}->{$doc_OID}};
478
479 # record the metadata value offset temporarily, so eg AZList can
480 # get the correct metadata value (for multi-valued metadata fields)
481 $doc_obj->{'mdoffset'}=$mdoffset;
482
483 if ($doc_OID =~ m/^[^\.]*\.([\d\.]+)$/)
484 {
485 my $section=$1;
486 if ($self->{'doclevel'} =~ m/^top/i) { # toplevel
487 $self->{'classifiers'}->{$node_name}->{'classifyobj'}
488 ->classify($doc_obj, "Section=$section");
489 } else { # section level
490 # Thanks to Don Gourley for this...
491 # classify can't handle multi-level section
492 $self->{'classifiers'}->{$node_name}->{'classifyobj'}
493 ->classify_section($section, $doc_obj, $sortmeta);
494 }
495 }
496 else
497 {
498 $self->{'classifiers'}->{$node_name}->{'classifyobj'}
499 ->classify($doc_obj);
500 }
501 } else { # this key is not in the hash
502 my $outhandle=$self->{outhandle};
503 print $outhandle "Warning: AZCompactList::reclassify ";
504 print $outhandle "could not find sub-node for metadata=`$metavalue' with doc_OID $doc_OID\n";
505 }
506 }
507}
508
509
510
511sub get_reclassify_info
512{
513 my $self = shift (@_);
514
515 my $node_name;
516 foreach $node_name (keys %{$self->{'classifiers'}})
517 {
518 my $classifyinfo
519 = $self->{'classifiers'}->{$node_name}->{'classifyobj'}
520 ->get_classify_info();
521 $self->{'classifiers'}->{$node_name}->{'classifyinfo'}
522 = $classifyinfo;
523 $self->{'reclassifylist'}->{"CLASSIFY.$node_name"}
524 = $self->{'classifiers'}->{$node_name}->{'formattednode'};
525 }
526}
527
528
529sub alpha_numeric_cmp
530{
531 my ($self,$a,$b) = @_;
532
533 my $title_a = $self->{'reclassifylist'}->{$a};
534 my $title_b = $self->{'reclassifylist'}->{$b};
535
536 if ($title_a =~ m/^(\d+(\.\d+)?)/)
537 {
538 my $val_a = $1;
539 if ($title_b =~ m/^(\d+(\.\d+)?)/)
540 {
541 my $val_b = $1;
542 if ($val_a != $val_b)
543 {
544 return ($val_a <=> $val_b);
545 }
546 }
547 }
548
549 return ($title_a cmp $title_b);
550}
551
552sub frequency_cmp
553{
554 my ($self,$a,$b) = @_;
555
556
557 my $title_a = $self->{'reclassifylist'}->{$a};
558 my $title_b = $self->{'reclassifylist'}->{$b};
559
560 my $a_freq = 1;
561 my $b_freq = 1;
562
563 if ($a =~ m/^CLASSIFY\.(.*)$/)
564 {
565 my $a_node = $1;
566 my $a_nodeinfo = $self->{'classifiers'}->{$a_node}->{'classifyinfo'};
567 $a_freq = scalar(@{$a_nodeinfo->{'contains'}});
568 }
569
570 if ($b =~ m/^CLASSIFY\.(.*)$/)
571 {
572 my $b_node = $1;
573 my $b_nodeinfo = $self->{'classifiers'}->{$b_node}->{'classifyinfo'};
574 $b_freq = scalar(@{$b_nodeinfo->{'contains'}});
575 }
576
577 return $b_freq <=> $a_freq;
578}
579
580sub get_classify_info {
581 my $self = shift (@_);
582
583 my @classlist =keys %{$self->{'list_mvpair'}}; # list all doc oids
584
585 my ($single_cl_ref,$multiple_cl_ref) = $self->reinit(\@classlist);
586 $self->reclassify($multiple_cl_ref);
587 $self->get_reclassify_info();
588
589 my @reclassified_classlist;
590 if ($self->{'freqsort'})
591 {
592 @reclassified_classlist
593 = sort { $self->frequency_cmp($a,$b) } keys %{$self->{'reclassifylist'}};
594 # supress sub-grouping by alphabet
595 map { $self->{'reclassifylist'}->{$_} = "A".$self->{'reclassifylist'}; } keys %{$self->{'reclassifylist'}};
596 }
597 else
598 {
599# @reclassified_classlist
600# = sort {$self->{'reclassifylist'}->{$a} cmp $self->{'reclassifylist'}->{$b};} keys %{$self->{'reclassifylist'}};
601
602 # alpha_numeric_cmp is slower than "cmp" but handles numbers better ...
603
604 @reclassified_classlist
605 = sort { $self->alpha_numeric_cmp($a,$b) } keys %{$self->{'reclassifylist'}};
606
607 }
608
609 return $self->splitlist (\@reclassified_classlist);
610}
611
612sub get_entry {
613 my $self = shift (@_);
614 my ($title, $childtype, $metaname, $thistype) = @_;
615 # organise into classification structure
616 my %classifyinfo = ('childtype'=>$childtype,
617 'Title'=>$title,
618 'contains'=>[],
619 'mdtype'=>$metaname);
620
621 $classifyinfo{'thistype'} = $thistype
622 if defined $thistype && $thistype =~ /\w/;
623
624 return \%classifyinfo;
625}
626
627
628
629# splitlist takes an ordered list of classifications (@$classlistref) and
630# splits it up into alphabetical sub-sections.
631sub splitlist {
632 my $self = shift (@_);
633 my ($classlistref) = @_;
634 my $classhash = {};
635
636 # top level
637 my @metanames = split(/\/|\|/,$self->{'metadata'});
638 my $metaname = shift(@metanames);
639
640 my $childtype = "HList";
641 $childtype = "VList" if (scalar (@$classlistref) <= $self->{'minnesting'});
642
643 my $title = $self->{'buttonname'}; # should always be defined by now.
644 my $classifyinfo;
645 if (!defined($self->{'recopt'}))
646 {
647 $classifyinfo
648 = $self->get_entry ($title, $childtype, $metaname, "Invisible");
649 }
650 else
651 {
652 $classifyinfo
653 = $self->get_entry ($title, $childtype, $metaname, "VList");
654 }
655
656 # don't need to do any splitting if there are less than 'minnesting' classifications
657 if ((scalar @$classlistref) <= $self->{'minnesting'}) {
658 foreach my $subOID (@$classlistref) {
659 if ($subOID =~ /^CLASSIFY\.(.*)$/
660 && defined $self->{'classifiers'}->{$1})
661 {
662 push (@{$classifyinfo->{'contains'}},
663 $self->{'classifiers'}->{$1}->{'classifyinfo'});
664 }
665 else
666 {
667 $subOID =~ s/^Metavalue_(\d+)\.//;
668 my $metaname_offset = $1 -1;
669 my $oid_rec = {'OID'=>$subOID, 'offset'=>$metaname_offset};
670 push (@{$classifyinfo->{'contains'}}, $oid_rec);
671 }
672 }
673 return $classifyinfo;
674 }
675
676 # first split up the list into separate A-Z and 0-9 classifications
677 foreach my $classification (@$classlistref) {
678 my $title = $self->{'reclassifylist'}->{$classification};
679 $title =~ s/&(.){2,4};//g; # remove any HTML special chars
680 $title =~ s/^\W+//g; # remove leading non-word chars
681
682 # only want first character for classification
683 $title =~ m/^(.)/; # always a char, or first byte of wide char?
684 if (defined($1) && $1 ne "") {
685 $title=$1;
686 } else {
687 print STDERR "no first character found for \"$title\" - \"" .
688 $self->{'reclassifylist'}->{$classification} . "\"\n";
689 }
690 $title =~ tr/[a-z]/[A-Z]/;
691
692 if ($title =~ /^[0-9]$/) {$title = '0-9';}
693 elsif ($title !~ /^[A-Z]$/) {
694 my $outhandle = $self->{'outhandle'};
695 print $outhandle "AZCompactList: WARNING $classification has badly formatted title ($title)\n";
696 }
697 $classhash->{$title} = [] unless defined $classhash->{$title};
698 push (@{$classhash->{$title}}, $classification);
699 }
700 $classhash = $self->compactlist ($classhash);
701
702 my @tmparr = ();
703 foreach my $subsection (sort keys (%$classhash)) {
704 push (@tmparr, $subsection);
705 }
706
707 # if there's a 0-9 section it will have been sorted to the beginning
708 # but we want it at the end
709 if ($tmparr[0] eq '0-9') {
710 shift @tmparr;
711 push (@tmparr, '0-9');
712 }
713 foreach my $subclass (@tmparr)
714 {
715 my $tempclassify
716 = (scalar(@tmparr)==1)
717 ? ($self->get_entry(" ", "VList", $metaname))
718 : ($self->get_entry($subclass, "VList", $metaname));
719
720
721 foreach my $subsubOID (@{$classhash->{$subclass}})
722 {
723 if ($subsubOID =~ /^CLASSIFY\.(.*)$/
724 && defined $self->{'classifiers'}->{$1})
725 {
726 # this is a "bookshelf" node... >1 entry compacted
727 push (@{$tempclassify->{'contains'}},
728 $self->{'classifiers'}->{$1}->{'classifyinfo'});
729 # set the metadata field name, for mdoffset to work
730 $self->{'classifiers'}->{$1}->{'classifyinfo'}->{'mdtype'}=
731 $metaname;
732 }
733 else
734 {
735 $subsubOID =~ s/^Metavalue_(\d+)\.//;
736 # record the offset if this metadata type has multiple values
737 my $metaname_offset = $1 -1;
738 my $oid_rec = {'OID'=>$subsubOID, 'offset'=>$metaname_offset};
739 push (@{$tempclassify->{'contains'}}, $oid_rec);
740 }
741 }
742 push (@{$classifyinfo->{'contains'}}, $tempclassify);
743 }
744
745 return $classifyinfo;
746}
747
748sub compactlist {
749 my $self = shift (@_);
750 my ($classhashref) = @_;
751 my $compactedhash = {};
752 my @currentOIDs = ();
753 my $currentfirstletter = "";
754 my $currentlastletter = "";
755 my $lastkey = "";
756
757 # minimum and maximum documents to be displayed per page.
758 # the actual maximum will be max + (min-1).
759 # the smallest sub-section is a single letter at present
760 # so in this case there may be many times max documents
761 # displayed on a page.
762 my $min = $self->{'mincompact'};
763 my $max = $self->{'maxcompact'};
764
765 foreach my $subsection (sort keys %$classhashref) {
766 if ($subsection eq '0-9') {
767 @{$compactedhash->{$subsection}} = @{$classhashref->{$subsection}};
768 next;
769 }
770 $currentfirstletter = $subsection if $currentfirstletter eq "";
771 if ((scalar (@currentOIDs) < $min) ||
772 ((scalar (@currentOIDs) + scalar (@{$classhashref->{$subsection}})) <= $max)) {
773 push (@currentOIDs, @{$classhashref->{$subsection}});
774 $currentlastletter = $subsection;
775 } else {
776
777 if ($currentfirstletter eq $currentlastletter) {
778 @{$compactedhash->{$currentfirstletter}} = @currentOIDs;
779 $lastkey = $currentfirstletter;
780 } else {
781 @{$compactedhash->{"$currentfirstletter-$currentlastletter"}} = @currentOIDs;
782 $lastkey = "$currentfirstletter-$currentlastletter";
783 }
784 if (scalar (@{$classhashref->{$subsection}}) >= $max) {
785 $compactedhash->{$subsection} = $classhashref->{$subsection};
786 @currentOIDs = ();
787 $currentfirstletter = "";
788 $lastkey=$subsection;
789 } else {
790 @currentOIDs = @{$classhashref->{$subsection}};
791 $currentfirstletter = $subsection;
792 $currentlastletter = $subsection;
793 }
794 }
795 }
796
797 # add final OIDs to last sub-classification if there aren't many otherwise
798 # add final sub-classification
799
800 # don't add if there aren't any oids
801 if (! scalar (@currentOIDs)) {return $compactedhash;}
802
803 if (scalar (@currentOIDs) < $min) {
804 my ($newkey) = $lastkey =~ /^(.)/;
805 @currentOIDs = (@{$compactedhash->{$lastkey}}, @currentOIDs);
806 delete $compactedhash->{$lastkey};
807 @{$compactedhash->{"$newkey-$currentlastletter"}} = @currentOIDs;
808 } else {
809 if ($currentfirstletter eq $currentlastletter) {
810 @{$compactedhash->{$currentfirstletter}} = @currentOIDs;
811 } else {
812 @{$compactedhash->{"$currentfirstletter-$currentlastletter"}} = @currentOIDs;
813 }
814 }
815
816 return $compactedhash;
817}
818
8191;
820
821
Note: See TracBrowser for help on using the repository browser.