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

Last change on this file since 11484 was 11407, checked in by kjdon, 18 years ago

made the mingroup default 1

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