source: trunk/gsdl/perllib/classify/HFileHierarchy.pm@ 11293

Last change on this file since 11293 was 10663, checked in by kjdon, 19 years ago

added documents_last - display document nodes after classifier nodes in the lists

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 9.6 KB
Line 
1###########################################################################
2#
3# HFileHierarchy.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 generating hierarchical classifications
27
28
29package HFileHierarchy;
30
31use BasClas;
32use util;
33use cfgread;
34use sorttools;
35
36use strict;
37no strict 'refs'; # allow filehandles to be variables and viceversa
38
39sub BEGIN {
40 @HFileHierarchy::ISA = ('BasClas');
41}
42
43my $arguments =
44 [ { 'name' => "metadata",
45 'desc' => "{AZCompactList.metadata}",
46 'type' => "metadata",
47 'reqd' => "yes" },
48 { 'name' => "firstvalueonly",
49 'desc' => "{AZCompactList.firstvalueonly}",
50 'type' => "flag",
51 'reqd' => "no" },
52 { 'name' => "allvalues",
53 'desc' => "{AZCompactList.allvalues}",
54 'type' => "flag",
55 'reqd' => "no" },
56 { 'name' => "hfile",
57 'desc' => "{Hierarchy.hfile}",
58 'type' => "string",
59 'deft' => "",
60 'reqd' => "no" },
61 { 'name' => "buttonname",
62 'desc' => "{BasClas.buttonname}",
63 'type' => "string",
64 'reqd' => "no" },
65 { 'name' => "sort",
66 'desc' => "{Hierarchy.sort}",
67 'type' => "string",
68 'reqd' => "no" },
69 { 'name' => "reverse_sort",
70 'desc' => "{Hierarchy.reverse_sort}",
71 'type' => "flag",
72 'reqd' => "no" },
73 { 'name' => "hlist_at_top",
74 'desc' => "{Hierarchy.hlist_at_top}",
75 'type' => "flag",
76 'reqd' => "no" },
77 { 'name' => "documents_last",
78 'desc' => "{Hierarchy.documents_last}",
79 'type' => "flag",
80 'reqd' => "no"}
81 ];
82
83my $options =
84{ 'name' => "HFileHierarchy",
85 'desc' => "{HFileHierarchy.desc}",
86 'abstract' => "yes",
87 'inherits' => "yes",
88 'args' => $arguments };
89
90
91sub new {
92 my ($class) = shift (@_);
93 my ($classifierslist,$inputargs,$hashArgOptLists) = @_;
94 push(@$classifierslist, $class);
95
96 if(defined $arguments){ push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});}
97 if(defined $options) { push(@{$hashArgOptLists->{"OptList"}},$options)};
98
99 my $self = (defined $hashArgOptLists)? new BasClas($classifierslist,$inputargs,$hashArgOptLists): new BasClas($classifierslist,$inputargs);
100
101 if ($self->{'info_only'}) {
102 # don't worry about any options etc
103 return bless $self, $class;
104 }
105
106 my $metadata = $self->{'metadata'};
107 if (!$metadata) {
108 print STDERR "$class Error: required option -metadata not supplied\n";
109 $self->print_txt_usage(""); # Use default resource bundle
110
111 die "$class Error: required option -metadata not supplied\n";
112 }
113
114 $self->{'buttonname'} = $self->generate_title_from_metadata($metadata) unless ($self->{'buttonname'});
115
116 #$self->{'metaname'} = $metadata;
117 my @meta_list = split(/,/, $metadata);
118 $self->{'meta_list'} = \@meta_list;
119
120 # sort = undef in this case is the same as sort=nosort
121 if ($self->{'sort'} eq "nosort") {
122 $self->{'sort'} = undef;
123 }
124
125 # sortname is handled a bit differently - kjdon
126 # undef means to sort, but use the metadata value from -metadata
127 # because there is no one metadata value to get for sorting when
128 # we have a list of possible metadata.
129 # To get no sorting, set sortname = 'nosort'
130 # we don't need to set it to undef if its not defined do we???
131
132# if (!$self->{'sort'}) {
133# if (defined ($metadata)) {
134# $sortname = undef;
135# } else {
136# $sortname = "nosort";
137# }
138# }
139 # $self->{'sortname'} = $sortname;
140
141 #if (defined $self->{'sort'} && $reverse_sort) {
142# $self->{'reverse_sort'} = 1;
143 # }
144
145 if ($self->{'hfile'}) {
146 my $hfile = $self->{'hfile'};
147 my $subjectfile;
148 $subjectfile = &util::filename_cat($ENV{'GSDLCOLLECTDIR'},"etc", $hfile);
149 if (!-e $subjectfile) {
150 my $collfile = $subjectfile;
151 $subjectfile = &util::filename_cat($ENV{'GSDLHOME'},"etc", $hfile);
152 if (!-e $subjectfile) {
153 my $outhandle = $self->{'outhandle'};
154 print STDERR "\nHFileHierarchy Error: Can't locate subject file $hfile\n";
155 print STDERR "This file should be in $collfile or $subjectfile\n";
156 $self->print_txt_usage(""); # Use default resource bundle
157 print STDERR "\nHFileHierarchy Error: Can't locate subject file $hfile\n";
158 print STDERR "This file should be in $collfile or $subjectfile\n";
159 die "\n";
160 }
161 }
162 $self->{'descriptorlist'} = {}; # first field in subject file
163 $self->{'locatorlist'} = {}; # second field in subject file
164 $self->{'subjectfile'} = $subjectfile;
165 }
166
167
168 # $self->{'firstvalueonly'} = $firstvalueonly;
169 # $self->{'allvalues'} = $allvalues;
170
171 #$self->{'hlist_at_top'} = $hlist_at_top;
172
173 # Clean out the unused keys
174 delete $self->{'metadata'};
175 delete $self->{'hfile'};
176
177 return bless $self, $class;
178}
179
180sub init {
181 my $self = shift (@_);
182
183 my $subjectfile = $self->{'subjectfile'};
184 if (defined $subjectfile) {
185 # read in the subject file
186 my $list = &cfgread::read_cfg_file ($self->{'subjectfile'}, undef, '^[^#]?\S');
187 # $list is a hash that is indexed by the descriptor. The contents of this
188 # hash is a list of two items. The first item is the OID and the second item
189 # is the title
190 foreach my $descriptor (keys (%$list)) {
191 $self->{'descriptorlist'}->{$descriptor} = $list->{$descriptor}->[0];
192 unless (defined $self->{'locatorlist'}->{$list->{$descriptor}->[0]}) {
193 $self->{'locatorlist'}->{$list->{$descriptor}->[0]}->{'title'} = $list->{$descriptor}->[1];
194 $self->{'locatorlist'}->{$list->{$descriptor}->[0]}->{'contents'} = [];
195 }
196 }
197 }
198}
199
200sub hfile_classify
201{
202 my $self = shift (@_);
203 my ($doc_obj,$sortmeta,$metavalues) = @_;
204
205 my $doc_OID = $doc_obj->get_OID();
206
207 foreach my $metaelement (@$metavalues) {
208 if ((defined $self->{'descriptorlist'}->{$metaelement}) &&
209 (defined $self->{'locatorlist'}->{$self->{'descriptorlist'}->{$metaelement}})) {
210 push (@{$self->{'locatorlist'}->{$self->{'descriptorlist'}->{$metaelement}}->{'contents'}},
211 [$doc_OID, $sortmeta]);
212 my $localid = $self->{'descriptorlist'}->{$metaelement};
213 my $classid = $self->get_number();
214
215 $doc_obj->add_metadata($doc_obj->get_top_section(), "memberof", "CL$classid.$localid");
216 }
217 }
218}
219
220
221
222
223sub hfile_get_classify_info {
224 my $self = shift (@_);
225
226 my ($classifyinfo) = @_;
227
228 my $list = $self->{'locatorlist'};
229
230 # sorted the keys - otherwise funny things happen - kjdon 03/01/03
231 foreach my $OID (sort keys (%$list)) {
232 my $tempinfo = $self->get_OID_entry ($OID, $classifyinfo, $list->{$OID}->{'title'}, "VList");
233
234 if (defined $self->{'sort'}) {
235 if ($self->{'reverse_sort'}) {
236 foreach my $subOID (sort {$b->[1] cmp $a->[1];} @{$list->{$OID}->{'contents'}}) {
237 push (@{$tempinfo->{'contains'}}, {'OID'=>$subOID->[0]});
238 }
239 }
240 else {
241 foreach my $subOID (sort {$a->[1] cmp $b->[1];} @{$list->{$OID}->{'contents'}}) {
242 push (@{$tempinfo->{'contains'}}, {'OID'=>$subOID->[0]});
243 }
244 }
245 }
246 else {
247 foreach my $subOID (@{$list->{$OID}->{'contents'}}) {
248 push (@{$tempinfo->{'contains'}}, {'OID'=>$subOID->[0]});
249 }
250 }
251 }
252
253 return $classifyinfo;
254}
255
256
257sub supports_memberof {
258 my $self = shift(@_);
259
260 return "true";
261}
262
263sub get_OID_entry {
264 my $self = shift (@_);
265 my ($OID, $classifyinfo, $title, $classifytype) = @_;
266
267 $OID = "" unless defined $OID;
268 $OID =~ s/^\.+//;
269
270 my ($headOID, $tailOID) = $OID =~ /^(\d+)(.*)$/;
271 $tailOID = "" unless defined $tailOID;
272
273 if (!defined $headOID) {
274 $classifyinfo->{'Title'} = $title;
275 $classifyinfo->{'classifytype'} = $classifytype;
276 return $classifyinfo;
277 }
278
279 $classifyinfo->{'contains'} = [] unless defined $classifyinfo->{'contains'};
280 if ($self->{'documents_last'}) {
281 # documents should come after nodes in the classifier
282
283 my $doc_pos = 0;
284 foreach my $thing (@{$classifyinfo->{'contains'}}) {
285 last if defined $thing->{'OID'};
286 $doc_pos++;
287 }
288
289 while ($doc_pos < $headOID) {
290 splice(@{$classifyinfo->{'contains'}}, $doc_pos, 0, $self->get_entry("", $classifytype));
291 $doc_pos++;
292 }
293
294 return $self->get_OID_entry ($tailOID, $classifyinfo->{'contains'}->[($headOID-1)], $title, $classifytype);
295
296 }
297
298 # else, documents come before nodes
299 my $offset = 0;
300 foreach my $thing (@{$classifyinfo->{'contains'}}) {
301 $offset ++ if defined $thing->{'OID'};
302 }
303
304 while (scalar(@{$classifyinfo->{'contains'}}) < ($headOID+$offset)) {
305 push (@{$classifyinfo->{'contains'}}, $self->get_entry("", $classifytype));
306 }
307
308 return $self->get_OID_entry ($tailOID, $classifyinfo->{'contains'}->[($headOID+$offset-1)], $title, $classifytype);
309}
310
311sub get_entry {
312 my $self = shift (@_);
313 my ($title, $childtype, $thistype) = @_;
314 my $memberof = &supports_memberof();
315
316 # organise into classification structure
317 my %classifyinfo = ('childtype'=>$childtype,
318 'Title'=>$title,
319 'supportsmemberof'=>$memberof,
320 'contains'=>[]);
321 $classifyinfo{'thistype'} = $thistype
322 if defined $thistype && $thistype =~ /\w/;
323
324 return \%classifyinfo;
325}
326
327
3281;
Note: See TracBrowser for help on using the repository browser.