1 | ###########################################################################
|
---|
2 | #
|
---|
3 | # SectionList.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 | # Same as List classifier but includes all sections of document
|
---|
27 | # (excluding top level) rather than just top level document
|
---|
28 | # itself
|
---|
29 | # options are:
|
---|
30 | # metadata=Metaname -- (optional) all documents with Metaname metadata
|
---|
31 | # will be included in list. if not included all documents
|
---|
32 | # will be included in list.
|
---|
33 | # sort=Meta -- (optional) sort documents in list alphabetically by
|
---|
34 | # Meta. by default it will sort by Metaname, if neither
|
---|
35 | # are set documents will be in build (random) order.
|
---|
36 | # Meta may be Filename to sort by original filename or
|
---|
37 | # nosort to force not to sort
|
---|
38 | # title=Title -- (optional) the title field for this classification.
|
---|
39 | # if not included title field will be Metaname.
|
---|
40 | # if metadata is also not included title will be 'List'
|
---|
41 |
|
---|
42 | package SectionList;
|
---|
43 |
|
---|
44 | use sorttools;
|
---|
45 |
|
---|
46 | sub new {
|
---|
47 | my ($class, @options) = @_;
|
---|
48 |
|
---|
49 | my $list = [];
|
---|
50 | my ($metaname, $title, $sortname);
|
---|
51 |
|
---|
52 | foreach $option (@options) {
|
---|
53 | if ($option =~ /^metadata=(.*)$/i) {
|
---|
54 | $metaname = $1;
|
---|
55 | $list = {};
|
---|
56 | } elsif ($option =~ /^title=(.*)$/i) {
|
---|
57 | $title = $1;
|
---|
58 | } elsif ($option =~ /^sort=(.*)$/i) {
|
---|
59 | $sortname = $1;
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | if (!defined $title) {
|
---|
64 | if (defined $metaname) {
|
---|
65 | $title = $metaname;
|
---|
66 | } else {
|
---|
67 | $title = 'List';
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | if (defined $sortname && $sortname =~ /^nosort$/i) {
|
---|
72 | $sortname = undef;
|
---|
73 | } elsif (!defined $sortname && defined $metaname) {
|
---|
74 | $sortname = $metaname;
|
---|
75 | }
|
---|
76 |
|
---|
77 | return bless {
|
---|
78 | 'list'=>$list,
|
---|
79 | 'metaname' => $metaname,
|
---|
80 | 'title' => $title,
|
---|
81 | 'sortname' => $sortname
|
---|
82 | }, $class;
|
---|
83 | }
|
---|
84 |
|
---|
85 | sub init {
|
---|
86 | my $self = shift (@_);
|
---|
87 |
|
---|
88 | if (defined $self->{'sortname'}) {
|
---|
89 | $self->{'list'} = {};
|
---|
90 | } else {
|
---|
91 | $self->{'list'} = [];
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | sub classify {
|
---|
96 | my $self = shift (@_);
|
---|
97 | my ($doc_obj,@options) = @_;
|
---|
98 |
|
---|
99 | my $thissection = undef;
|
---|
100 |
|
---|
101 | my $option;
|
---|
102 | foreach $option (@options)
|
---|
103 | {
|
---|
104 | if ($option =~ m/^section=(\d+)$/i)
|
---|
105 | {
|
---|
106 | $thissection = $1;
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | my $sortmeta = "";
|
---|
111 | if (defined $self->{'sortname'}) {
|
---|
112 | if ($self->{'sortname'} =~ /^filename$/i) {
|
---|
113 | $sortmeta = $doc_obj->get_source_filename();
|
---|
114 | } else {
|
---|
115 | $sortmeta = $doc_obj->get_metadata_element($doc_obj->get_top_section(),
|
---|
116 | $self->{'sortname'});
|
---|
117 | if ($self->{'sortname'} eq "Creator") {
|
---|
118 | &sorttools::format_string_name_english (\$sortmeta);
|
---|
119 | } else {
|
---|
120 | &sorttools::format_string_english (\$sortmeta);
|
---|
121 | }
|
---|
122 | }
|
---|
123 | $sortmeta = "" unless defined $sortmeta;
|
---|
124 | }
|
---|
125 |
|
---|
126 | if (!defined $thissection)
|
---|
127 | {
|
---|
128 | $thissection = $doc_obj->get_next_section ($doc_obj->get_top_section());
|
---|
129 | while (defined $thissection) {
|
---|
130 | $self->classify_section($thissection, $doc_obj, $sortmeta);
|
---|
131 | $thissection = $doc_obj->get_next_section ($thissection);
|
---|
132 | }
|
---|
133 | }
|
---|
134 | else
|
---|
135 | {
|
---|
136 | $self->classify_section($thissection, $doc_obj, $sortmeta);
|
---|
137 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 | sub classify_section {
|
---|
141 | my $self = shift (@_);
|
---|
142 | my ($section, $doc_obj, $sortmeta) = @_;
|
---|
143 |
|
---|
144 | my $doc_OID = $doc_obj->get_OID();
|
---|
145 | $sortmeta = "" unless defined $sortmeta;
|
---|
146 |
|
---|
147 | if (defined $self->{'sortname'}) {
|
---|
148 | if (defined $self->{'metaname'}) {
|
---|
149 |
|
---|
150 | my $metavalue = $doc_obj->get_metadata_element ($section, $self->{'metaname'});
|
---|
151 | if (defined $metavalue) {
|
---|
152 | if (defined $self->{'list'}->{"$doc_OID.$section"}) {
|
---|
153 | print STDERR "WARNING: SectionList::classify called multiple times for $doc_OID.$section\n";
|
---|
154 | }
|
---|
155 | $self->{'list'}->{"$doc_OID.$section"} = $sortmeta;
|
---|
156 | }
|
---|
157 | } else {
|
---|
158 | if (defined $self->{'list'}->{"$doc_OID.$section"}) {
|
---|
159 | print STDERR "WARNING: SectionList::classify called multiple times for $doc_OID.$section\n";
|
---|
160 | }
|
---|
161 | $self->{'list'}->{"$doc_OID.$section"} = $sortmeta;
|
---|
162 | }
|
---|
163 |
|
---|
164 | } else {
|
---|
165 | if (defined $self->{'metaname'}) {
|
---|
166 |
|
---|
167 | my $metavalue = $doc_obj->get_metadata_element ($section, $self->{'metaname'});
|
---|
168 | if (defined $metavalue) {
|
---|
169 | push (@{$self->{'list'}}, "$doc_OID.$section");
|
---|
170 | }
|
---|
171 | } else {
|
---|
172 | push (@{$self->{'list'}}, "$doc_OID.$section");
|
---|
173 | }
|
---|
174 | }
|
---|
175 | }
|
---|
176 |
|
---|
177 | sub get_classify_info {
|
---|
178 | my $self = shift (@_);
|
---|
179 |
|
---|
180 | my @list = ();
|
---|
181 | if (defined $self->{'sortname'}) {
|
---|
182 | if (keys %{$self->{'list'}}) {
|
---|
183 | @list = sort {$self->{'list'}->{$a}
|
---|
184 | cmp $self->{'list'}->{$b};} keys %{$self->{'list'}};
|
---|
185 | }
|
---|
186 | } else {
|
---|
187 | @list = @{$self->{'list'}};
|
---|
188 | }
|
---|
189 |
|
---|
190 | # organise into classification structure
|
---|
191 | my %classifyinfo = ('thistype'=>'Invisible',
|
---|
192 | 'childtype'=>'VList',
|
---|
193 | 'Title'=>$self->{'title'},
|
---|
194 | 'contains'=>[]);
|
---|
195 | foreach $OID (@list) {
|
---|
196 | push (@{$classifyinfo{'contains'}}, {'OID'=>$OID});
|
---|
197 | }
|
---|
198 |
|
---|
199 | return \%classifyinfo;
|
---|
200 | }
|
---|
201 |
|
---|
202 |
|
---|
203 | 1;
|
---|