source: trunk/gsdl/perllib/classify/AZList.pm@ 741

Last change on this file since 741 was 741, checked in by sjboddie, 25 years ago

fixed up a bit of a bug - should fix this properly
some time

  • Property svn:keywords set to Author Date Id Revision
File size: 7.3 KB
Line 
1###########################################################################
2#
3# AZList.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
26# classifier plugin for sorting alphabetically
27# options are:
28# metadata=Metaname -- all documents with Metaname metadata
29# will be included in list, list will be sorted
30# by this element.
31# title=Title -- (optional) the title field for this classification.
32# if not included title field will be Metaname.
33
34package AZList;
35
36use sorttools;
37
38sub new {
39 my ($class, @options) = @_;
40
41 my ($metaname, $title);
42
43 foreach $option (@options) {
44 if ($option =~ /^metadata=(.*)$/i) {
45 $metaname = $1;
46 } elsif ($option =~ /^title=(.*)$/i) {
47 $title = $1;
48 }
49 }
50
51 die "AZList used with no metadata name to classify by\n"
52 unless defined $metaname;
53 $title = $metaname unless defined $title;
54
55 return bless {
56 'list'=>{},
57 'metaname' => $metaname,
58 'title' => $title
59 }, $class;
60}
61
62sub init {
63 my $self = shift (@_);
64
65 $self->{'list'} = {};
66}
67
68sub classify {
69 my $self = shift (@_);
70 my ($doc_obj) = @_;
71
72 my $doc_OID = $doc_obj->get_OID();
73 my $metavalue = $doc_obj->get_metadata_element ($doc_obj->get_top_section(),
74 $self->{'metaname'});
75
76 # if this document doesn't contain the metadata element we're
77 # sorting by we won't include it in this classification
78 if (defined $metavalue && $metavalue ne "") {
79 if ($self->{'metaname'} eq 'Creator') {
80 &sorttools::format_string_name_english (\$metavalue);
81 } else {
82 &sorttools::format_string_english (\$metavalue);
83 }
84 if (defined $self->{'list'}->{$doc_OID}) {
85 print STDERR "WARNING: AZList::classify called multiple times for $doc_OID\n";
86 }
87 $self->{'list'}->{$doc_OID} = $metavalue;
88 }
89}
90
91sub get_classify_info {
92 my $self = shift (@_);
93
94 my @classlist = sort {$self->{'list'}->{$a} cmp $self->{'list'}->{$b};} keys %{$self->{'list'}};
95
96 return $self->splitlist (\@classlist);
97}
98
99sub get_entry {
100 my $self = shift (@_);
101 my ($title, $childtype, $thistype) = @_;
102
103 # organise into classification structure
104 my %classifyinfo = ('childtype'=>$childtype,
105 'Title'=>$title,
106 'contains'=>[]);
107 $classifyinfo{'thistype'} = $thistype
108 if defined $thistype && $thistype =~ /\w/;
109
110 return \%classifyinfo;
111}
112
113# splitlist takes an ordered list of classifications (@$classlistref) and splits it
114# up into alphabetical sub-sections.
115sub splitlist {
116 my $self = shift (@_);
117 my ($classlistref) = @_;
118 my $classhash = {};
119
120 # top level
121 my $childtype = "HList";
122 if (scalar (@$classlistref) <= 39) {$childtype = "VList";}
123 my $classifyinfo = $self->get_entry ($self->{'metaname'}, $childtype, "Invisible");
124
125 # don't need to do any splitting if there are less than 39 (max + min -1) classifications
126 if ((scalar @$classlistref) <= 39) {
127 foreach $subOID (@$classlistref) {
128 push (@{$classifyinfo->{'contains'}}, {'OID'=>$subOID});
129 }
130 return $classifyinfo;
131 }
132
133 # first split up the list into separate A-Z and 0-9 classifications
134 foreach $classification (@$classlistref) {
135 my $title = $self->{'list'}->{$classification};
136 $title =~ s/^(&.{1,6};|<[^>]>|[^a-zA-Z0-9])//g; # remove any unwanted stuff
137 $title =~ s/^(.).*$/$1/;
138 $title =~ tr/[a-z]/[A-Z]/;
139 if ($title =~ /^[0-9]$/) {$title = '0-9';}
140 elsif ($title !~ /^[A-Z]$/) {
141 print STDERR "AZList: WARNING $classification has badly formatted title ($title)\n";
142 }
143 $classhash->{$title} = [] unless defined $classhash->{$title};
144 push (@{$classhash->{$title}}, $classification);
145 }
146 $classhash = $self->compactlist ($classhash);
147
148 my @tmparr = ();
149 foreach $subsection (sort keys (%$classhash)) {
150 push (@tmparr, $subsection);
151 }
152
153 # if there's a 0-9 section it will have been sorted to the beginning
154 # but we want it at the end
155 if ($tmparr[0] eq '0-9') {
156 shift @tmparr;
157 push (@tmparr, '0-9');
158 }
159
160 foreach $subclass (@tmparr) {
161 my $tempclassify = $self->get_entry($subclass, "VList");
162 foreach $subsubOID (@{$classhash->{$subclass}}) {
163 push (@{$tempclassify->{'contains'}}, {'OID'=>$subsubOID});
164 }
165 push (@{$classifyinfo->{'contains'}}, $tempclassify);
166 }
167
168 return $classifyinfo;
169}
170
171sub compactlist {
172 my $self = shift (@_);
173 my ($classhashref) = @_;
174 my $compactedhash = {};
175 my @currentOIDs = ();
176 my $currentfirstletter = "";
177 my $currentlastletter = "";
178 my $lastkey = "";
179
180 # minimum and maximum documents to be displayed per page.
181 # the actual maximum will be max + (min-1).
182 # the smallest sub-section is a single letter at present
183 # so in this case there may be many times max documents
184 # displayed on a page.
185 my $min = 10;
186 my $max = 30;
187
188 foreach $subsection (sort keys %$classhashref) {
189 if ($subsection eq '0-9') {
190 @{$compactedhash->{$subsection}} = @{$classhashref->{$subsection}};
191 next;
192 }
193 $currentfirstletter = $subsection if $currentfirstletter eq "";
194 if ((scalar (@currentOIDs) < $min) ||
195 ((scalar (@currentOIDs) + scalar (@{$classhashref->{$subsection}})) <= $max)) {
196 push (@currentOIDs, @{$classhashref->{$subsection}});
197 $currentlastletter = $subsection;
198 } else {
199
200 if ($currentfirstletter eq $currentlastletter) {
201 @{$compactedhash->{$currentfirstletter}} = @currentOIDs;
202 $lastkey = $currentfirstletter;
203 } else {
204 @{$compactedhash->{"$currentfirstletter-$currentlastletter"}} = @currentOIDs;
205 $lastkey = "$currentfirstletter-$currentlastletter";
206 }
207 if (scalar (@{$classhashref->{$subsection}}) >= $max) {
208 $compactedhash->{$subsection} = $classhashref->{$subsection};
209 @currentOIDs = ();
210 $currentfirstletter = "";
211 $lastkey = $subsection;
212 } else {
213 @currentOIDs = @{$classhashref->{$subsection}};
214 $currentfirstletter = $subsection;
215 $currentlastletter = $subsection;
216 }
217 }
218 }
219
220 # add final OIDs to last sub-classification if there aren't many otherwise
221 # add final sub-classification
222 if (scalar (@currentOIDs) < $min) {
223 my ($newkey) = $lastkey =~ /^(.)/;
224 @currentOIDs = (@{$compactedhash->{$lastkey}}, @currentOIDs);
225 delete $compactedhash->{$lastkey};
226 @{$compactedhash->{"$newkey-$currentlastletter"}} = @currentOIDs;
227 } else {
228 if ($currentfirstletter eq $currentlastletter) {
229 @{$compactedhash->{$currentfirstletter}} = @currentOIDs;
230 } else {
231 @{$compactedhash->{"$currentfirstletter-$currentlastletter"}} = @currentOIDs;
232 }
233 }
234
235 return $compactedhash;
236}
237
2381;
Note: See TracBrowser for help on using the repository browser.