[537] | 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 |
|
---|
[287] | 26 | # simple list classifier plugin
|
---|
[426] | 27 | # options are:
|
---|
| 28 | # metadata=Metaname -- (optional) all documents with Metaname metadata
|
---|
[545] | 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
|
---|
[2022] | 36 | # buttonname=Title -- (optional) the title field for this classification.
|
---|
[426] | 37 | # if not included title field will be Metaname.
|
---|
| 38 | # if metadata is also not included title will be 'List'
|
---|
[287] | 39 |
|
---|
[3540] | 40 | # 12/05/02 Added usage datastructure - John Thompson
|
---|
| 41 |
|
---|
[1839] | 42 | use BasClas;
|
---|
[287] | 43 | package List;
|
---|
| 44 |
|
---|
[385] | 45 | use sorttools;
|
---|
| 46 |
|
---|
[1483] | 47 | sub BEGIN {
|
---|
| 48 | @ISA = ('BasClas');
|
---|
| 49 | }
|
---|
| 50 |
|
---|
[3540] | 51 | my $arguments =
|
---|
[4759] | 52 | [ { 'name' => "metadata",
|
---|
[4873] | 53 | 'desc' => "{List.metadata}",
|
---|
[3540] | 54 | 'type' => "metadata",
|
---|
[4873] | 55 | 'reqd' => "yes" },
|
---|
[4759] | 56 | { 'name' => "buttonname",
|
---|
[4873] | 57 | 'desc' => "{List.buttonname}",
|
---|
[3540] | 58 | 'type' => "string",
|
---|
[4759] | 59 | 'deft' => "Metadata element specified with -metadata",
|
---|
| 60 | 'reqd' => "no" },
|
---|
| 61 | { 'name' => "sort",
|
---|
[4873] | 62 | 'desc' => "{List.sort}",
|
---|
[3540] | 63 | 'type' => "string",
|
---|
[4759] | 64 | 'deft' => "Metadata field specified with -metadata",
|
---|
| 65 | 'reqd' => "no" } ];
|
---|
[3540] | 66 |
|
---|
[4759] | 67 | my $options = { 'name' => "List",
|
---|
[5645] | 68 | 'desc' => "{List.desc}",
|
---|
[4759] | 69 | 'inherits' => "Yes",
|
---|
| 70 | 'args' => $arguments };
|
---|
[3540] | 71 |
|
---|
[4786] | 72 | # sub print_usage {
|
---|
| 73 | # print STDERR "
|
---|
| 74 | # usage: classify List [options]
|
---|
| 75 | # options:
|
---|
[1839] | 76 |
|
---|
[4786] | 77 | # -metadata X Metadata field used for classification,
|
---|
| 78 | # list will be sorted by this element.
|
---|
[1839] | 79 |
|
---|
[4786] | 80 | # -buttonname X (optional) Title field for this classification.
|
---|
| 81 | # if not included title field will be Metaname.
|
---|
[1839] | 82 |
|
---|
[4786] | 83 | # -sort X (optional) Sort documents in list by this metadata field.
|
---|
| 84 | # By default it will sort by Metaname, or (if this is not
|
---|
| 85 | # set) in build (random) order.
|
---|
| 86 | # ";
|
---|
| 87 | # }
|
---|
[1839] | 88 |
|
---|
[287] | 89 | sub new {
|
---|
[1839] | 90 | my $class = shift (@_);
|
---|
| 91 | my $self = new BasClas($class, @_);
|
---|
[3540] | 92 |
|
---|
[4759] | 93 | # 14-05-02 To allow for proper inheritance of arguments - John Thompson
|
---|
| 94 | my $option_list = $self->{'option_list'};
|
---|
| 95 | push( @{$option_list}, $options );
|
---|
| 96 |
|
---|
[1839] | 97 | my ($metaname, $title, $sortname, $list);
|
---|
[287] | 98 |
|
---|
[1839] | 99 | if (!parsargv::parse(\@_,
|
---|
| 100 | q^metadata/.*/^, \$metaname,
|
---|
[2022] | 101 | q^buttonname/.*/^, \$title,
|
---|
[1839] | 102 | q^sort/.*/^, \$sortname,
|
---|
| 103 | "allow_extra_options")) {
|
---|
| 104 |
|
---|
| 105 | print STDERR "\nIncorrect options passed to $class, check your collect.cfg file\n";
|
---|
[4873] | 106 | $self->print_txt_usage(""); # Use default resource bundle
|
---|
[1839] | 107 | die "\n";
|
---|
[287] | 108 | }
|
---|
| 109 |
|
---|
[1839] | 110 | if (!$title) {
|
---|
| 111 | if ($metaname) {
|
---|
[426] | 112 | $title = $metaname;
|
---|
| 113 | } else {
|
---|
| 114 | $title = 'List';
|
---|
| 115 | }
|
---|
| 116 | }
|
---|
| 117 |
|
---|
[1839] | 118 | if (!$sortname) {
|
---|
| 119 | if ($metaname) {
|
---|
| 120 | $sortname = $metaname;
|
---|
| 121 | } else {
|
---|
| 122 | $sortname = undef;
|
---|
| 123 | }
|
---|
[545] | 124 | }
|
---|
| 125 |
|
---|
[1839] | 126 | if ($metaname) {
|
---|
| 127 | $list = {};
|
---|
| 128 | } else {
|
---|
| 129 | $list = [];
|
---|
| 130 | $metaname = undef;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
[1483] | 133 | $self->{'list'} = $list;
|
---|
| 134 | $self->{'metaname'} = $metaname;
|
---|
| 135 | $self->{'title'} = $title;
|
---|
| 136 | $self->{'sortname'} = $sortname;
|
---|
| 137 |
|
---|
| 138 | return bless $self, $class;
|
---|
[287] | 139 | }
|
---|
| 140 |
|
---|
| 141 | sub init {
|
---|
| 142 | my $self = shift (@_);
|
---|
| 143 |
|
---|
[545] | 144 | if (defined $self->{'sortname'}) {
|
---|
[287] | 145 | $self->{'list'} = {};
|
---|
| 146 | } else {
|
---|
| 147 | $self->{'list'} = [];
|
---|
| 148 | }
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | sub classify {
|
---|
| 152 | my $self = shift (@_);
|
---|
| 153 | my ($doc_obj) = @_;
|
---|
| 154 |
|
---|
| 155 | my $doc_OID = $doc_obj->get_OID();
|
---|
| 156 |
|
---|
[545] | 157 | my $sortmeta = "";
|
---|
| 158 | if (defined $self->{'sortname'}) {
|
---|
| 159 | if ($self->{'sortname'} =~ /^filename$/i) {
|
---|
| 160 | $sortmeta = $doc_obj->get_source_filename();
|
---|
| 161 | } else {
|
---|
| 162 | $sortmeta = $doc_obj->get_metadata_element($doc_obj->get_top_section(),
|
---|
| 163 | $self->{'sortname'});
|
---|
[641] | 164 | if (defined $sortmeta) {
|
---|
[3719] | 165 |
|
---|
| 166 | my $lang = $doc_obj->get_metadata_element ($doc_obj->get_top_section(), 'Language');
|
---|
[3728] | 167 | $lang = 'en' unless defined $lang;
|
---|
[3719] | 168 | if ($lang eq 'en') {
|
---|
| 169 | if ($self->{'sortname'} eq "Creator") {
|
---|
| 170 | &sorttools::format_string_name_english (\$sortmeta);
|
---|
| 171 | } else {
|
---|
| 172 | &sorttools::format_string_english (\$sortmeta);
|
---|
| 173 | }
|
---|
[641] | 174 | }
|
---|
[287] | 175 | }
|
---|
[545] | 176 | }
|
---|
| 177 | $sortmeta = "" unless defined $sortmeta;
|
---|
| 178 |
|
---|
| 179 | if (defined $self->{'metaname'}) {
|
---|
| 180 | my $metavalue = $doc_obj->get_metadata_element ($doc_obj->get_top_section(),
|
---|
| 181 | $self->{'metaname'});
|
---|
| 182 | if (defined $metavalue) {
|
---|
| 183 | if (defined $self->{'list'}->{$doc_OID}) {
|
---|
[1483] | 184 | my $outhandle = $self->{'outhandle'};
|
---|
| 185 | print $outhandle "WARNING: List::classify called multiple times for $doc_OID\n";
|
---|
[545] | 186 | }
|
---|
| 187 | $self->{'list'}->{$doc_OID} = $sortmeta;
|
---|
| 188 | }
|
---|
| 189 | } else {
|
---|
[287] | 190 | if (defined $self->{'list'}->{$doc_OID}) {
|
---|
[1483] | 191 | my $outhandle = $self->{'outhandle'};
|
---|
| 192 | print $outhandle "WARNING: List::classify called multiple times for $doc_OID\n";
|
---|
[287] | 193 | }
|
---|
[545] | 194 | $self->{'list'}->{$doc_OID} = $sortmeta;
|
---|
[287] | 195 | }
|
---|
| 196 | } else {
|
---|
[545] | 197 | if (defined $self->{'metaname'}) {
|
---|
| 198 | my $metavalue = $doc_obj->get_metadata_element ($doc_obj->get_top_section(),
|
---|
| 199 | $self->{'metaname'});
|
---|
| 200 | if (defined $metavalue) {
|
---|
| 201 | push (@{$self->{'list'}}, $doc_OID);
|
---|
| 202 | }
|
---|
| 203 | } else {
|
---|
| 204 | push (@{$self->{'list'}}, $doc_OID);
|
---|
| 205 | }
|
---|
[287] | 206 | }
|
---|
| 207 | }
|
---|
| 208 |
|
---|
[316] | 209 | sub get_classify_info {
|
---|
[287] | 210 | my $self = shift (@_);
|
---|
[1086] | 211 | my ($no_thistype) = @_;
|
---|
| 212 | $no_thistype = 0 unless defined $no_thistype;
|
---|
[287] | 213 |
|
---|
[316] | 214 | my @list = ();
|
---|
[545] | 215 | if (defined $self->{'sortname'}) {
|
---|
[287] | 216 | if (keys %{$self->{'list'}}) {
|
---|
[316] | 217 | @list = sort {$self->{'list'}->{$a}
|
---|
| 218 | cmp $self->{'list'}->{$b};} keys %{$self->{'list'}};
|
---|
[287] | 219 | }
|
---|
| 220 | } else {
|
---|
[316] | 221 | @list = @{$self->{'list'}};
|
---|
[287] | 222 | }
|
---|
| 223 |
|
---|
[316] | 224 | # organise into classification structure
|
---|
[5645] | 225 | print STDERR "List::get_classify_info::title = " . $self->{'title'} . "\n";
|
---|
[1086] | 226 | my %classifyinfo = ('childtype'=>'VList',
|
---|
[426] | 227 | 'Title'=>$self->{'title'},
|
---|
[316] | 228 | 'contains'=>[]);
|
---|
[1086] | 229 | $classifyinfo{'thistype'} = 'Invisible' unless $no_thistype;
|
---|
| 230 |
|
---|
[316] | 231 | foreach $OID (@list) {
|
---|
| 232 | push (@{$classifyinfo{'contains'}}, {'OID'=>$OID});
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 | return \%classifyinfo;
|
---|
[287] | 236 | }
|
---|
| 237 |
|
---|
| 238 |
|
---|
| 239 | 1;
|
---|