source: main/trunk/greenstone2/perllib/classify/AZCompactList.pm@ 24691

Last change on this file since 24691 was 24691, checked in by davidb, 13 years ago

Improved multi-lingual support

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