source: trunk/gsdl/perllib/classify/List.pm@ 1483

Last change on this file since 1483 was 1483, checked in by sjboddie, 24 years ago

added -out option to classifiers

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 5.1 KB
Line 
1###########################################################################
2#
3# List.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# simple list classifier plugin
27# options are:
28# metadata=Metaname -- (optional) all documents with Metaname metadata
29# will be included in list. if not included all documents
30# will be included in list.
31# sort=Meta -- (optional) sort documents in list alphabetically by
32# Meta. by default it will sort by Metaname, if neither
33# are set documents will be in build (random) order.
34# Meta may be Filename to sort by original filename or
35# nosort to force not to sort
36# title=Title -- (optional) the title field for this classification.
37# if not included title field will be Metaname.
38# if metadata is also not included title will be 'List'
39
40package List;
41
42use sorttools;
43
44sub BEGIN {
45 @ISA = ('BasClas');
46}
47
48sub new {
49 my ($class, @options) = @_;
50 my $self = new BasClas ($class, @_);
51
52 my $list = [];
53 my ($metaname, $title, $sortname);
54
55 foreach $option (@options) {
56 if ($option =~ /^metadata=(.*)$/i) {
57 $metaname = $1;
58 $list = {};
59 } elsif ($option =~ /^title=(.*)$/i) {
60 $title = $1;
61 } elsif ($option =~ /^sort=(.*)$/i) {
62 $sortname = $1;
63 }
64 }
65
66 if (!defined $title) {
67 if (defined $metaname) {
68 $title = $metaname;
69 } else {
70 $title = 'List';
71 }
72 }
73
74 if (defined $sortname && $sortname =~ /^nosort$/i) {
75 $sortname = undef;
76 } elsif (!defined $sortname && defined $metaname) {
77 $sortname = $metaname;
78 }
79
80 $self->{'list'} = $list;
81 $self->{'metaname'} = $metaname;
82 $self->{'title'} = $title;
83 $self->{'sortname'} = $sortname;
84
85 return bless $self, $class;
86}
87
88sub init {
89 my $self = shift (@_);
90
91 if (defined $self->{'sortname'}) {
92 $self->{'list'} = {};
93 } else {
94 $self->{'list'} = [];
95 }
96}
97
98sub classify {
99 my $self = shift (@_);
100 my ($doc_obj) = @_;
101
102 my $doc_OID = $doc_obj->get_OID();
103
104 my $sortmeta = "";
105 if (defined $self->{'sortname'}) {
106 if ($self->{'sortname'} =~ /^filename$/i) {
107 $sortmeta = $doc_obj->get_source_filename();
108 } else {
109 $sortmeta = $doc_obj->get_metadata_element($doc_obj->get_top_section(),
110 $self->{'sortname'});
111 if (defined $sortmeta) {
112 if ($self->{'sortname'} eq "Creator") {
113 &sorttools::format_string_name_english (\$sortmeta);
114 } else {
115 &sorttools::format_string_english (\$sortmeta);
116 }
117 }
118 }
119 $sortmeta = "" unless defined $sortmeta;
120
121 if (defined $self->{'metaname'}) {
122 my $metavalue = $doc_obj->get_metadata_element ($doc_obj->get_top_section(),
123 $self->{'metaname'});
124 if (defined $metavalue) {
125 if (defined $self->{'list'}->{$doc_OID}) {
126 my $outhandle = $self->{'outhandle'};
127 print $outhandle "WARNING: List::classify called multiple times for $doc_OID\n";
128 }
129 $self->{'list'}->{$doc_OID} = $sortmeta;
130 }
131 } else {
132 if (defined $self->{'list'}->{$doc_OID}) {
133 my $outhandle = $self->{'outhandle'};
134 print $outhandle "WARNING: List::classify called multiple times for $doc_OID\n";
135 }
136 $self->{'list'}->{$doc_OID} = $sortmeta;
137 }
138 } else {
139 if (defined $self->{'metaname'}) {
140 my $metavalue = $doc_obj->get_metadata_element ($doc_obj->get_top_section(),
141 $self->{'metaname'});
142 if (defined $metavalue) {
143 push (@{$self->{'list'}}, $doc_OID);
144 }
145 } else {
146 push (@{$self->{'list'}}, $doc_OID);
147 }
148 }
149}
150
151sub get_classify_info {
152 my $self = shift (@_);
153 my ($no_thistype) = @_;
154 $no_thistype = 0 unless defined $no_thistype;
155
156 my @list = ();
157 if (defined $self->{'sortname'}) {
158 if (keys %{$self->{'list'}}) {
159 @list = sort {$self->{'list'}->{$a}
160 cmp $self->{'list'}->{$b};} keys %{$self->{'list'}};
161 }
162 } else {
163 @list = @{$self->{'list'}};
164 }
165
166 # organise into classification structure
167 my %classifyinfo = ('childtype'=>'VList',
168 'Title'=>$self->{'title'},
169 'contains'=>[]);
170 $classifyinfo{'thistype'} = 'Invisible' unless $no_thistype;
171
172 foreach $OID (@list) {
173 push (@{$classifyinfo{'contains'}}, {'OID'=>$OID});
174 }
175
176 return \%classifyinfo;
177}
178
179
1801;
Note: See TracBrowser for help on using the repository browser.