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

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

removed debug statements that I had forgotten about

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