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

Last change on this file since 20828 was 20429, checked in by kjdon, 15 years ago

strip ex from metadata and sort arguments

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