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

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

removed some commented out code. strip ex off metadata and sort arguments

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