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 |
|
---|
29 | package HFileHierarchy;
|
---|
30 |
|
---|
31 | use BaseClassifier;
|
---|
32 | use util;
|
---|
33 | use cfgread;
|
---|
34 | use sorttools;
|
---|
35 |
|
---|
36 | use strict;
|
---|
37 | no strict 'refs'; # allow filehandles to be variables and viceversa
|
---|
38 |
|
---|
39 | sub BEGIN {
|
---|
40 | @HFileHierarchy::ISA = ('BaseClassifier');
|
---|
41 | }
|
---|
42 |
|
---|
43 | my $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 |
|
---|
79 | my $options =
|
---|
80 | { 'name' => "HFileHierarchy",
|
---|
81 | 'desc' => "{HFileHierarchy.desc}",
|
---|
82 | 'abstract' => "yes",
|
---|
83 | 'inherits' => "yes",
|
---|
84 | 'args' => $arguments };
|
---|
85 |
|
---|
86 |
|
---|
87 | sub 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 = $self->strip_ex_from_metadata($metadata);
|
---|
113 |
|
---|
114 | my @meta_list = split(/,/, $metadata);
|
---|
115 | $self->{'meta_list'} = \@meta_list;
|
---|
116 |
|
---|
117 | # sort = undef in this case is the same as sort=nosort
|
---|
118 | if ($self->{'sort'} eq "nosort") {
|
---|
119 | $self->{'sort'} = undef;
|
---|
120 | }
|
---|
121 | if (defined $self->{'sort'}) { # remove ex. namespace
|
---|
122 | $self->{'sort'} = $self->strip_ex_from_metadata($self->{'sort'});
|
---|
123 | }
|
---|
124 |
|
---|
125 | if ($self->{'hfile'}) {
|
---|
126 | my $hfile = $self->{'hfile'};
|
---|
127 | my $subjectfile;
|
---|
128 | $subjectfile = &FileUtils::filenameConcatenate($ENV{'GSDLCOLLECTDIR'},"etc", $hfile);
|
---|
129 | if (!-e $subjectfile) {
|
---|
130 | my $collfile = $subjectfile;
|
---|
131 | $subjectfile = &FileUtils::filenameConcatenate($ENV{'GSDLHOME'},"etc", $hfile);
|
---|
132 | if (!-e $subjectfile) {
|
---|
133 | my $outhandle = $self->{'outhandle'};
|
---|
134 | print STDERR "\nHFileHierarchy Error: Can't locate subject file $hfile\n";
|
---|
135 | print STDERR "This file should be in $collfile or $subjectfile\n";
|
---|
136 | $self->print_txt_usage(""); # Use default resource bundle
|
---|
137 | print STDERR "\nHFileHierarchy Error: Can't locate subject file $hfile\n";
|
---|
138 | print STDERR "This file should be in $collfile or $subjectfile\n";
|
---|
139 | die "\n";
|
---|
140 | }
|
---|
141 | }
|
---|
142 | $self->{'descriptorlist'} = {}; # first field in subject file
|
---|
143 | $self->{'locatorlist'} = {}; # second field in subject file
|
---|
144 | $self->{'subjectfile'} = $subjectfile;
|
---|
145 | }
|
---|
146 |
|
---|
147 |
|
---|
148 | # $self->{'firstvalueonly'} = $firstvalueonly;
|
---|
149 | # $self->{'allvalues'} = $allvalues;
|
---|
150 |
|
---|
151 | #$self->{'hlist_at_top'} = $hlist_at_top;
|
---|
152 |
|
---|
153 | # Clean out the unused keys
|
---|
154 | delete $self->{'metadata'};
|
---|
155 | delete $self->{'hfile'};
|
---|
156 |
|
---|
157 | return bless $self, $class;
|
---|
158 | }
|
---|
159 |
|
---|
160 | sub init {
|
---|
161 | my $self = shift (@_);
|
---|
162 |
|
---|
163 | my $subjectfile = $self->{'subjectfile'};
|
---|
164 | if (defined $subjectfile) {
|
---|
165 | # read in the subject file, but read in unicode mode to preserve special characters
|
---|
166 | my $list = &cfgread::read_cfg_file_unicode ($self->{'subjectfile'}, undef, '^[^#]?\S');
|
---|
167 | # $list is a hash that is indexed by the descriptor. The contents of this
|
---|
168 | # hash is a list of two items. The first item is the OID and the second item
|
---|
169 | # is the title
|
---|
170 | foreach my $descriptor (keys (%$list)) {
|
---|
171 | $self->{'descriptorlist'}->{$descriptor} = $list->{$descriptor}->[0];
|
---|
172 | unless (defined $self->{'locatorlist'}->{$list->{$descriptor}->[0]}) {
|
---|
173 | $self->{'locatorlist'}->{$list->{$descriptor}->[0]}->{'title'} = $list->{$descriptor}->[1];
|
---|
174 | $self->{'locatorlist'}->{$list->{$descriptor}->[0]}->{'contents'} = [];
|
---|
175 | }
|
---|
176 | }
|
---|
177 | }
|
---|
178 | }
|
---|
179 |
|
---|
180 | sub hfile_classify
|
---|
181 | {
|
---|
182 | my $self = shift (@_);
|
---|
183 | my ($doc_obj,$sortmeta,$metavalues) = @_;
|
---|
184 |
|
---|
185 | my $outhandle = $self->{'outhandle'};
|
---|
186 |
|
---|
187 | my $doc_OID = $doc_obj->get_OID();
|
---|
188 |
|
---|
189 | foreach my $metaelement (@$metavalues) {
|
---|
190 | if ((defined $self->{'descriptorlist'}->{$metaelement}) &&
|
---|
191 | (defined $self->{'locatorlist'}->{$self->{'descriptorlist'}->{$metaelement}})) {
|
---|
192 |
|
---|
193 | push (@{$self->{'locatorlist'}->{$self->{'descriptorlist'}->{$metaelement}}->{'contents'}},
|
---|
194 | [$doc_OID, $sortmeta]);
|
---|
195 | my $localid = $self->{'descriptorlist'}->{$metaelement};
|
---|
196 | my $classid = $self->get_number();
|
---|
197 |
|
---|
198 | $doc_obj->add_metadata($doc_obj->get_top_section(), "memberof", "CL$classid.$localid");
|
---|
199 |
|
---|
200 | }
|
---|
201 | }
|
---|
202 | }
|
---|
203 |
|
---|
204 |
|
---|
205 |
|
---|
206 |
|
---|
207 | sub hfile_get_classify_info {
|
---|
208 | my $self = shift (@_);
|
---|
209 |
|
---|
210 | my ($classifyinfo) = @_;
|
---|
211 |
|
---|
212 | my $list = $self->{'locatorlist'};
|
---|
213 |
|
---|
214 | my $classifier_num = "CL".$self->get_number();
|
---|
215 | # sorted the keys - otherwise funny things happen - kjdon 03/01/03
|
---|
216 | foreach my $OID (sort keys (%$list)) {
|
---|
217 | my $tempinfo = $self->get_OID_entry ($OID, $classifyinfo, "$classifier_num.$OID", $list->{$OID}->{'title'}, "VList");
|
---|
218 | if (not defined ($tempinfo)) {
|
---|
219 | print STDERR "Error occurred for node $OID. Not creating the classifier \n";
|
---|
220 | return undef;
|
---|
221 | }
|
---|
222 | if (defined $self->{'sort'}) {
|
---|
223 | if ($self->{'reverse_sort'}) {
|
---|
224 | foreach my $subOID (sort {$b->[1] cmp $a->[1];} @{$list->{$OID}->{'contents'}}) {
|
---|
225 | push (@{$tempinfo->{'contains'}}, {'OID'=>$subOID->[0]});
|
---|
226 | }
|
---|
227 | }
|
---|
228 | else {
|
---|
229 | foreach my $subOID (sort {$a->[1] cmp $b->[1];} @{$list->{$OID}->{'contents'}}) {
|
---|
230 | push (@{$tempinfo->{'contains'}}, {'OID'=>$subOID->[0]});
|
---|
231 | }
|
---|
232 | }
|
---|
233 | }
|
---|
234 | else {
|
---|
235 | foreach my $subOID (@{$list->{$OID}->{'contents'}}) {
|
---|
236 | push (@{$tempinfo->{'contains'}}, {'OID'=>$subOID->[0]});
|
---|
237 | }
|
---|
238 | }
|
---|
239 | }
|
---|
240 |
|
---|
241 | return $classifyinfo;
|
---|
242 | }
|
---|
243 |
|
---|
244 |
|
---|
245 | sub supports_memberof {
|
---|
246 | my $self = shift(@_);
|
---|
247 |
|
---|
248 | return "true";
|
---|
249 | }
|
---|
250 |
|
---|
251 | sub get_OID_entry {
|
---|
252 | my $self = shift (@_);
|
---|
253 | my ($OID, $classifyinfo, $classifyOID, $title, $classifytype) = @_;
|
---|
254 |
|
---|
255 | $OID = "" unless defined $OID;
|
---|
256 | $OID =~ s/^\.+//;
|
---|
257 |
|
---|
258 | my ($headOID, $tailOID) = $OID =~ /^(\d+)(.*)$/;
|
---|
259 | $tailOID = "" unless defined $tailOID;
|
---|
260 |
|
---|
261 | if (!defined $headOID) {
|
---|
262 | $classifyinfo->{'Title'} = $title;
|
---|
263 | $classifyinfo->{'classifyOID'} = $classifyOID;
|
---|
264 | $classifyinfo->{'classifytype'} = $classifytype;
|
---|
265 | return $classifyinfo;
|
---|
266 | }
|
---|
267 | if ($headOID eq "0") {
|
---|
268 | print STDERR "Error: Hierarchy numbering must not contain 0\n";
|
---|
269 | return undef;
|
---|
270 | }
|
---|
271 | $classifyinfo->{'contains'} = [] unless defined $classifyinfo->{'contains'};
|
---|
272 | if ($self->{'documents_last'}) {
|
---|
273 | # documents should come after nodes in the classifier
|
---|
274 |
|
---|
275 | my $doc_pos = 0;
|
---|
276 | foreach my $thing (@{$classifyinfo->{'contains'}}) {
|
---|
277 | last if defined $thing->{'OID'};
|
---|
278 | $doc_pos++;
|
---|
279 | }
|
---|
280 |
|
---|
281 | while ($doc_pos < $headOID) {
|
---|
282 | splice(@{$classifyinfo->{'contains'}}, $doc_pos, 0, $self->get_entry("", $classifytype));
|
---|
283 | $doc_pos++;
|
---|
284 | }
|
---|
285 |
|
---|
286 | return $self->get_OID_entry ($tailOID, $classifyinfo->{'contains'}->[($headOID-1)], $classifyOID, $title, $classifytype);
|
---|
287 |
|
---|
288 | }
|
---|
289 |
|
---|
290 | # else, documents come before nodes
|
---|
291 | my $offset = 0;
|
---|
292 | foreach my $thing (@{$classifyinfo->{'contains'}}) {
|
---|
293 | $offset ++ if defined $thing->{'OID'};
|
---|
294 | }
|
---|
295 |
|
---|
296 | while (scalar(@{$classifyinfo->{'contains'}}) < ($headOID+$offset)) {
|
---|
297 | push (@{$classifyinfo->{'contains'}}, $self->get_entry("", $classifytype));
|
---|
298 | }
|
---|
299 |
|
---|
300 | return $self->get_OID_entry ($tailOID, $classifyinfo->{'contains'}->[($headOID+$offset-1)], $classifyOID, $title, $classifytype);
|
---|
301 | }
|
---|
302 |
|
---|
303 | sub get_entry {
|
---|
304 | my $self = shift (@_);
|
---|
305 | my ($title, $childtype, $thistype) = @_;
|
---|
306 | my $memberof = &supports_memberof();
|
---|
307 |
|
---|
308 | # organise into classification structure
|
---|
309 | my %classifyinfo = ('childtype'=>$childtype,
|
---|
310 | 'Title'=>$title,
|
---|
311 | 'supportsmemberof'=>$memberof,
|
---|
312 | 'contains'=>[]);
|
---|
313 | $classifyinfo{'thistype'} = $thistype
|
---|
314 | if defined $thistype && $thistype =~ /\w/;
|
---|
315 |
|
---|
316 | return \%classifyinfo;
|
---|
317 | }
|
---|
318 |
|
---|
319 |
|
---|
320 | 1;
|
---|