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
RevLine 
[1086]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
[6975]26
[1086]27package AZCompactList;
28
[17209]29use BaseClassifier;
[1086]30use sorttools;
31
[10253]32use strict;
33no strict 'refs'; # allow filehandles to be variables and viceversa
34
[1483]35sub BEGIN {
[17209]36 @AZCompactList::ISA = ('BaseClassifier');
[1483]37}
38
[3540]39my $doclevel_list =
[4759]40 [ { 'name' => "top",
[4873]41 'desc' => "{AZCompactList.doclevel.top}" },
[4759]42 { 'name' => "section",
[4873]43 'desc' => "{AZCompactList.doclevel.section}" } ];
[3540]44
45my $arguments =
[4759]46 [ { 'name' => "metadata",
[6975]47 'desc' => "{AZCompactList.metadata}",
[3540]48 'type' => "metadata",
[4759]49 'reqd' => "yes" },
[7548]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" },
[6281]58 { 'name' => "sort",
59 'desc' => "{AZCompactList.sort}",
[11665]60 'type' => "metadata",
[10218]61# 'deft' => "Title",
[6281]62 'reqd' => "no" },
[4759]63 { 'name' => "removeprefix",
[9064]64 'desc' => "{BasClas.removeprefix}",
[6408]65 'type' => "regexp",
[4759]66 'deft' => "",
67 'reqd' => "no" },
[4840]68 { 'name' => "removesuffix",
[9064]69 'desc' => "{BasClas.removesuffix}",
[6408]70 'type' => "regexp",
[4840]71 'deft' => "",
72 'reqd' => "no" },
[4759]73 { 'name' => "mingroup",
[4873]74 'desc' => "{AZCompactList.mingroup}",
[3540]75 'type' => "int",
[11407]76 'deft' => "1",
[7577]77 'range' => "1,",
[4759]78 'reqd' => "no" },
79 { 'name' => "minnesting",
[4873]80 'desc' => "{AZCompactList.minnesting}",
[3540]81 'type' => "int",
[4759]82 'deft' => "20",
[7577]83 'range' => "2,",
[4759]84 'reqd' => "no" },
85 { 'name' => "mincompact",
[4873]86 'desc' => "{AZCompactList.mincompact}",
[3540]87 'type' => "int",
[4759]88 'deft' => "10",
[7577]89 'range' => "1,",
[4759]90 'reqd' => "no" },
91 { 'name' => "maxcompact",
[4873]92 'desc' => "{AZCompactList.maxcompact}",
[3540]93 'type' => "int",
[4759]94 'deft' => "30",
[7577]95 'range' => "1,",
[4759]96 'reqd' => "no" },
97 { 'name' => "doclevel",
[4873]98 'desc' => "{AZCompactList.doclevel}",
[3540]99 'type' => "enum",
100 'list' => $doclevel_list,
[4759]101 'deft' => "top",
102 'reqd' => "no" },
103 { 'name' => "freqsort",
[4873]104 'desc' => "{AZCompactList.freqsort}",
[5615]105 'type' => "flag"},
106 { 'name' => "recopt",
107 'desc' => "{AZCompactList.recopt}",
108 'type' => "flag",
109 'reqd' => "no" } ];
[3540]110
111my $options =
112{ 'name' => "AZCompactList",
[5729]113 'desc' => "{AZCompactList.desc}",
[6408]114 'abstract' => "no",
115 'inherits' => "yes",
[3540]116 'args' => $arguments };
117
[1839]118
[1086]119sub new {
[10218]120 my ($class) = shift (@_);
121 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
122 push(@$pluginlist, $class);
[1086]123
[17209]124 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
125 push(@{$hashArgOptLists->{"OptList"}},$options);
[3540]126
[17209]127 my $self = new BaseClassifier($pluginlist, $inputargs, $hashArgOptLists);
[3540]128
[10253]129 if ($self->{'info_only'}) {
130 # don't worry about any options etc
131 return bless $self, $class;
132 }
133
[10218]134 if (!$self->{"metadata"}) {
[1483]135 my $outhandle = $self->{'outhandle'};
[6985]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";
[1483]139 }
140
[10218]141 # Manually set $self parameters.
[1483]142 $self->{'list'} = {};
143 $self->{'listmetavalue'} = {};
[7406]144 $self->{'list_mvpair'} = {};
[1483]145 $self->{'reclassify'} = {};
146 $self->{'reclassifylist'} = {};
[9461]147
[10218]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 ^
[2955]152 }
[10218]153 if (defined($self->{"removesuffix"}) && $self->{"removesuffix"}) {
154 $self->{"removesuffix"} =~ s/\$$//; # don't need a trailing $
[4840]155 }
[10218]156
157 $self->{'recopt'} = ($self->{'recopt'} == 0) ? undef : "on";
[1839]158
[10218]159 # Clean out the unused keys
160 if($self->{"removeprefix"} eq "") {delete $self->{"removeprefix"};}
161 if($self->{"removesuffix"} eq "") {delete $self->{"removesuffix"};}
[3303]162
[1483]163 return bless $self, $class;
[1086]164}
165
166sub init
167{
168 my $self = shift (@_);
169
170 $self->{'list'} = {};
171 $self->{'listmetavalue'} = {};
[7406]172 $self->{'list_mvpair'} = {};
[1086]173 $self->{'reclassify'} = {};
174 $self->{'reclassifylist'} = {};
175}
176
[10253]177my $tmp = 0;
[1086]178
[7835]179sub classify
[1086]180{
181 my $self = shift (@_);
182 my ($doc_obj) = @_;
183
184 my $doc_OID = $doc_obj->get_OID();
185
[1313]186 my @sectionlist = ();
187 my $topsection = $doc_obj->get_top_section();
[10218]188 my $metaname = $self->{'metadata'};
[1483]189 my $outhandle = $self->{'outhandle'};
[1086]190
[9461]191 $metaname =~ s/(\/|\|).*//; # grab first name in n1/n2/n3 or n1|n2|n3 list
[7406]192 my @commameta_list = split(/,/, $metaname);
[1086]193
[1313]194 if ($self->{'doclevel'} =~ /^top(level)?/i)
[1086]195 {
[1313]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 }
[1086]207
[1313]208 my $thissection;
209 foreach $thissection (@sectionlist)
210 {
211 my $full_doc_OID
212 = ($thissection ne "") ? "$doc_OID.$thissection" : $doc_OID;
[7406]213 if (defined $self->{'list_mvpair'}->{$full_doc_OID})
[1086]214 {
[1483]215 print $outhandle "WARNING: AZCompactList::classify called multiple times for $full_doc_OID\n";
[1086]216 }
[1313]217 $self->{'list'}->{$full_doc_OID} = [];
218 $self->{'listmetavalue'}->{$full_doc_OID} = [];
[7406]219 $self->{'list_mvpair'}->{$full_doc_OID} = [];
[1313]220
[7406]221 my $metavalues = [];
222 foreach my $cmn (@commameta_list) {
223 my $cmvalues = $doc_obj->get_metadata($thissection,$cmn);
[7557]224 push(@$metavalues,@$cmvalues) if (@{$cmvalues});
225 last if (@{$cmvalues} && !$self->{'allvalues'});
[7406]226 }
227
[1313]228 my $metavalue;
229 foreach $metavalue (@$metavalues)
[1086]230 {
[7042]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
[1313]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 {
[4840]241 if (defined($self->{'removeprefix'}) &&
242 length($self->{'removeprefix'})) {
[2955]243 $metavalue =~ s/^$self->{'removeprefix'}//;
[4225]244
245 # check that it's not now empty
246 if (!$metavalue) {next;}
[2955]247 }
248
[4840]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
[10630]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 }
[1313]263
[2955]264 #### prefix-str
265 if (! defined($formatted_metavalue)) {
266 print $outhandle "Warning: AZCompactList: metavalue is ";
267 print $outhandle "empty\n";
268 $formatted_metavalue="";
269 }
[7406]270
271 my $mv_pair = { 'mv' => $metavalue, 'fmv' => $formatted_metavalue };
[1313]272 push(@{$self->{'list'}->{$full_doc_OID}},$formatted_metavalue);
273 push(@{$self->{'listmetavalue'}->{$full_doc_OID}} ,$metavalue);
[7406]274 push(@{$self->{'list_mvpair'}->{$full_doc_OID}},$mv_pair);
[1313]275
[7406]276
[7548]277 last if ($self->{'firstvalueonly'});
[1313]278 }
[1086]279 }
[9519]280
281 # This is used in reclassify below for AZCompactSectionList
[10218]282 my $sortmeta = $doc_obj->get_metadata_element($thissection, $self->{'sort'});
[9519]283 $self->{'reclassify'}->{$full_doc_OID} = [$doc_obj,$sortmeta];
[1086]284 }
285}
286
287sub reinit
288{
289 my ($self,$classlist_ref) = @_;
[2955]290 my $outhandle = $self->{'outhandle'};
[1086]291
292 my %mtfreq = ();
293 my @single_classlist = ();
294 my @multiple_classlist = ();
295
296 # find out how often each metavalue occurs
[1313]297 map
298 {
[7406]299 foreach my $mvp (@{$self->{'list_mvpair'}->{$_}} )
[1313]300 {
[7406]301### print STDERR "*** plain mv = $mvp->{'mv'}\n";
302### print STDERR "*** format mv = $mvp->{'fmv'}\n";
303
304 my $metavalue = $mvp->{'mv'};
[7702]305 $metavalue =~ s!^-!\\-!; # in case it starts with "-"
306 $mtfreq{$metavalue}++;
[1313]307 }
308 } @$classlist_ref;
[1086]309
310 # use this information to split the list: single metavalue/repeated value
311 map
312 {
[1313]313 my $i = 1;
314 my $metavalue;
[7406]315 foreach my $mvp (@{$self->{'list_mvpair'}->{$_}})
[1086]316 {
[7406]317 my $metavalue = $mvp->{'mv'};
[7702]318 $metavalue =~ s!^-!\\-!; # in case it starts with "-"
[7406]319 my $cs_metavalue = $mvp->{'mv'}; # case sensative
[1313]320 if ($mtfreq{$metavalue} >= $self->{'mingroup'})
321 {
[7406]322### print STDERR "*** pushing on $cs_metavalue\n";
[1313]323 push(@multiple_classlist,[$_,$i,$metavalue]);
324 }
325 else
326 {
[7406]327 push(@single_classlist,[$_,$cs_metavalue]);
[1313]328 $metavalue =~ tr/[A-Z]/[a-z]/;
329 $self->{'reclassifylist'}->{"Metavalue_$i.$_"} = $metavalue;
330 }
331 $i++;
[1086]332 }
333 } @$classlist_ref;
334
335
336 # Setup sub-classifiers for multiple list
337
338 $self->{'classifiers'} = {};
339
[1313]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 {
[1483]348 print $outhandle "AZCompactList ERROR - couldn't find classifier \"$listname\"\n";
349 die "\n";
[1313]350 }
[1086]351 }
352
[1313]353 # Create classifiers objects for each entry >= mingroup
[1086]354 my $metavalue;
[8728]355 my $doclevel = $self->{'doclevel'};
356 my $mingroup = $self->{'mingroup'};
[10218]357 my @metaname_list = split(/\/|\|/,$self->{'metadata'});
[8728]358 my $metaname = shift(@metaname_list);
359 my $hierarchical = 0;
[9461]360 if (scalar(@metaname_list) > 1) {
[8728]361 $hierarchical = 1;
362 $metaname = join('/',@metaname_list);
363 }
[1086]364 foreach $metavalue (keys %mtfreq)
365 {
[8728]366 if ($mtfreq{$metavalue} >= $mingroup)
[1086]367 {
[7835]368 # occurs more often than minimum required to compact into a group
[1086]369 my $listclassobj;
[8728]370
371 if (!$hierarchical)
372 {
[1948]373 my @args;
374 push @args, ("-metadata", "$metaname");
[5615]375 # buttonname is also used for the node's title
[2080]376 push @args, ("-buttonname", "$metavalue");
[10218]377 push @args, ("-sort", $self->{'sort'});
[1948]378
[10218]379 my $ptArgs = \@args;
[1313]380 if ($doclevel =~ m/^top(level)?/i)
381 {
[10218]382 eval ("\$listclassobj = new List([],\$ptArgs)");
[1313]383 }
384 else
385 {
[10218]386 eval ("\$listclassobj = new SectionList([],\$ptArgs)");
[1313]387 }
388 }
389 else
390 {
[1948]391
392 my @args;
393 push @args, ("-metadata", "$metaname");
[5615]394 # buttonname is also used for the node's title
[2080]395 push @args, ("-buttonname", "$metavalue");
[10218]396 push @args, ("-sort", $self->{'sort'});
[10113]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
[1948]405 push @args, ("-doclevel", "$doclevel");
[5615]406 push @args, ("-mingroup", $mingroup);
407 push @args, "-recopt ";
[1313]408
[10218]409 my $ptArgs = \@args;
410 eval ("\$listclassobj = new AZCompactList([],\$ptArgs)");
[1313]411 }
[10218]412
[1483]413 if ($@) {
414 print $outhandle "$@";
415 die "\n";
416 }
[1086]417
418 $listclassobj->init();
419
420 if (defined $metavalue && $metavalue =~ /\w/)
421 {
422 my $formatted_node = $metavalue;
[2955]423
[4840]424 if (defined($self->{'removeprefix'}) &&
425 length($self->{'removeprefix'})) {
[2955]426 $formatted_node =~ s/^$self->{'removeprefix'}//;
[4225]427 # check that it's not now empty
428 if (!$formatted_node) {next;}
[2955]429 }
[4840]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 }
[7406]436
[10630]437 $formatted_node = &sorttools::format_metadata_for_sorting($self->{'metadata'}, $formatted_node) unless $self->{'no_metadata_formatting'};
[2955]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
[6482]446 # use the lower case, for speed of lookup.
447 my $meta_lc=lc($metavalue);
448 $self->{'classifiers'}->{$meta_lc}
[1086]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
[1313]464 # Entries in the current classify list that are "book nodes"
465 # should be recursively classified.
466 #--
[10253]467 foreach my $dm_pair (@$multiple_cl_ref)
[1086]468 {
[7406]469 my ($doc_OID,$mdoffset,$metavalue,$cs_metavalue) = @$dm_pair;
[1086]470 my $listclassobj;
[1313]471
[1086]472 # find metavalue in list of sub-classifiers
[6482]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})
[1086]476 {
[9519]477 my ($doc_obj, $sortmeta) = @{$self->{'reclassify'}->{$doc_OID}};
[1086]478
[7835]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
[6482]483 if ($doc_OID =~ m/^[^\.]*\.([\d\.]+)$/)
484 {
485 my $section=$1;
486 if ($self->{'doclevel'} =~ m/^top/i) { # toplevel
[1313]487 $self->{'classifiers'}->{$node_name}->{'classifyobj'}
[6482]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'}
[9519]493 ->classify_section($section, $doc_obj, $sortmeta);
[1313]494 }
[1086]495 }
[6482]496 else
497 {
498 $self->{'classifiers'}->{$node_name}->{'classifyobj'}
499 ->classify($doc_obj);
500 }
501 } else { # this key is not in the hash
[2889]502 my $outhandle=$self->{outhandle};
[1483]503 print $outhandle "Warning: AZCompactList::reclassify ";
[2955]504 print $outhandle "could not find sub-node for metadata=`$metavalue' with doc_OID $doc_OID\n";
[1086]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'}
[1313]520 ->get_classify_info();
[1086]521 $self->{'classifiers'}->{$node_name}->{'classifyinfo'}
522 = $classifyinfo;
523 $self->{'reclassifylist'}->{"CLASSIFY.$node_name"}
524 = $self->{'classifiers'}->{$node_name}->{'formattednode'};
525 }
526}
527
528
[1313]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};
[7175]535
[1313]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
[3303]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
[1086]580sub get_classify_info {
581 my $self = shift (@_);
582
[7406]583 my @classlist =keys %{$self->{'list_mvpair'}}; # list all doc oids
[1086]584
585 my ($single_cl_ref,$multiple_cl_ref) = $self->reinit(\@classlist);
586 $self->reclassify($multiple_cl_ref);
587 $self->get_reclassify_info();
588
[3303]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'}};
[1086]601
[3529]602 # alpha_numeric_cmp is slower than "cmp" but handles numbers better ...
[1313]603
[3303]604 @reclassified_classlist
[3529]605 = sort { $self->alpha_numeric_cmp($a,$b) } keys %{$self->{'reclassifylist'}};
[3303]606
607 }
608
[1086]609 return $self->splitlist (\@reclassified_classlist);
610}
611
[1313]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.
[1086]631sub splitlist {
632 my $self = shift (@_);
633 my ($classlistref) = @_;
634 my $classhash = {};
635
636 # top level
[10218]637 my @metanames = split(/\/|\|/,$self->{'metadata'});
[1313]638 my $metaname = shift(@metanames);
639
[1086]640 my $childtype = "HList";
[1313]641 $childtype = "VList" if (scalar (@$classlistref) <= $self->{'minnesting'});
[1086]642
[10218]643 my $title = $self->{'buttonname'}; # should always be defined by now.
[1313]644 my $classifyinfo;
645 if (!defined($self->{'recopt'}))
646 {
647 $classifyinfo
[1716]648 = $self->get_entry ($title, $childtype, $metaname, "Invisible");
[1313]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'}) {
[10253]658 foreach my $subOID (@$classlistref) {
[1086]659 if ($subOID =~ /^CLASSIFY\.(.*)$/
660 && defined $self->{'classifiers'}->{$1})
661 {
662 push (@{$classifyinfo->{'contains'}},
663 $self->{'classifiers'}->{$1}->{'classifyinfo'});
664 }
665 else
666 {
[1313]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);
[1086]671 }
672 }
673 return $classifyinfo;
674 }
675
676 # first split up the list into separate A-Z and 0-9 classifications
[10253]677 foreach my $classification (@$classlistref) {
[1086]678 my $title = $self->{'reclassifylist'}->{$classification};
679 $title =~ s/&(.){2,4};//g; # remove any HTML special chars
[6482]680 $title =~ s/^\W+//g; # remove leading non-word chars
[3109]681
[7406]682 # only want first character for classification
[7835]683 $title =~ m/^(.)/; # always a char, or first byte of wide char?
[7704]684 if (defined($1) && $1 ne "") {
[6482]685 $title=$1;
686 } else {
687 print STDERR "no first character found for \"$title\" - \"" .
688 $self->{'reclassifylist'}->{$classification} . "\"\n";
689 }
[1086]690 $title =~ tr/[a-z]/[A-Z]/;
691
692 if ($title =~ /^[0-9]$/) {$title = '0-9';}
693 elsif ($title !~ /^[A-Z]$/) {
[1483]694 my $outhandle = $self->{'outhandle'};
695 print $outhandle "AZCompactList: WARNING $classification has badly formatted title ($title)\n";
[1086]696 }
697 $classhash->{$title} = [] unless defined $classhash->{$title};
698 push (@{$classhash->{$title}}, $classification);
699 }
700 $classhash = $self->compactlist ($classhash);
701
702 my @tmparr = ();
[10253]703 foreach my $subsection (sort keys (%$classhash)) {
[1086]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 }
[10253]713 foreach my $subclass (@tmparr)
[1086]714 {
[3302]715 my $tempclassify
716 = (scalar(@tmparr)==1)
717 ? ($self->get_entry(" ", "VList", $metaname))
718 : ($self->get_entry($subclass, "VList", $metaname));
719
720
[10253]721 foreach my $subsubOID (@{$classhash->{$subclass}})
[1086]722 {
723 if ($subsubOID =~ /^CLASSIFY\.(.*)$/
724 && defined $self->{'classifiers'}->{$1})
725 {
[7835]726 # this is a "bookshelf" node... >1 entry compacted
[1086]727 push (@{$tempclassify->{'contains'}},
728 $self->{'classifiers'}->{$1}->{'classifyinfo'});
[7835]729 # set the metadata field name, for mdoffset to work
730 $self->{'classifiers'}->{$1}->{'classifyinfo'}->{'mdtype'}=
731 $metaname;
[1086]732 }
733 else
734 {
[1313]735 $subsubOID =~ s/^Metavalue_(\d+)\.//;
[7835]736 # record the offset if this metadata type has multiple values
[1313]737 my $metaname_offset = $1 -1;
738 my $oid_rec = {'OID'=>$subsubOID, 'offset'=>$metaname_offset};
739 push (@{$tempclassify->{'contains'}}, $oid_rec);
[1086]740 }
741 }
742 push (@{$classifyinfo->{'contains'}}, $tempclassify);
743 }
744
745 return $classifyinfo;
746}
747
[1313]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
[10253]765 foreach my $subsection (sort keys %$classhashref) {
[1313]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 = "";
[6084]788 $lastkey=$subsection;
[1313]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
[5790]799
800 # don't add if there aren't any oids
801 if (! scalar (@currentOIDs)) {return $compactedhash;}
802
[1313]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
[1086]8191;
[1313]820
821
Note: See TracBrowser for help on using the repository browser.