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

Last change on this file since 6980 was 6980, checked in by kjdon, 20 years ago

changed the type of the sort option to metadata

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