########################################################################### # # List.pm -- # A component of the Greenstone digital library software # from the New Zealand Digital Library Project at the # University of Waikato, New Zealand. # # Copyright (C) 1999 New Zealand Digital Library Project # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. # ########################################################################### # simple list classifier plugin # options are: # metadata=Metaname -- (optional) all documents with Metaname metadata # will be included in list. if not included all documents # will be included in list. # sort=Meta -- (optional) sort documents in list alphabetically by # Meta. by default it will sort by Metaname, if neither # are set documents will be in build (random) order. # Meta may be Filename to sort by original filename or # nosort to force not to sort # title=Title -- (optional) the title field for this classification. # if not included title field will be Metaname. # if metadata is also not included title will be 'List' package List; use sorttools; sub new { my ($class, @options) = @_; my $list = []; my ($metaname, $title, $sortname); foreach $option (@options) { if ($option =~ /^metadata=(.*)$/i) { $metaname = $1; $list = {}; } elsif ($option =~ /^title=(.*)$/i) { $title = $1; } elsif ($option =~ /^sort=(.*)$/i) { $sortname = $1; } } if (!defined $title) { if (defined $metaname) { $title = $metaname; } else { $title = 'List'; } } if (defined $sortname && $sortname =~ /^nosort$/i) { $sortname = undef; } elsif (!defined $sortname && defined $metaname) { $sortname = $metaname; } return bless { 'list'=>$list, 'metaname' => $metaname, 'title' => $title, 'sortname' => $sortname }, $class; } sub init { my $self = shift (@_); if (defined $self->{'sortname'}) { $self->{'list'} = {}; } else { $self->{'list'} = []; } } sub classify { my $self = shift (@_); my ($doc_obj) = @_; my $doc_OID = $doc_obj->get_OID(); my $sortmeta = ""; if (defined $self->{'sortname'}) { if ($self->{'sortname'} =~ /^filename$/i) { $sortmeta = $doc_obj->get_source_filename(); } else { $sortmeta = $doc_obj->get_metadata_element($doc_obj->get_top_section(), $self->{'sortname'}); if (defined $sortmeta) { if ($self->{'sortname'} eq "Creator") { &sorttools::format_string_name_english (\$sortmeta); } else { &sorttools::format_string_english (\$sortmeta); } } } $sortmeta = "" unless defined $sortmeta; if (defined $self->{'metaname'}) { my $metavalue = $doc_obj->get_metadata_element ($doc_obj->get_top_section(), $self->{'metaname'}); if (defined $metavalue) { if (defined $self->{'list'}->{$doc_OID}) { print STDERR "WARNING: List::classify called multiple times for $doc_OID\n"; } $self->{'list'}->{$doc_OID} = $sortmeta; } } else { if (defined $self->{'list'}->{$doc_OID}) { print STDERR "WARNING: List::classify called multiple times for $doc_OID\n"; } $self->{'list'}->{$doc_OID} = $sortmeta; } } else { if (defined $self->{'metaname'}) { my $metavalue = $doc_obj->get_metadata_element ($doc_obj->get_top_section(), $self->{'metaname'}); if (defined $metavalue) { push (@{$self->{'list'}}, $doc_OID); } } else { push (@{$self->{'list'}}, $doc_OID); } } } sub get_classify_info { my $self = shift (@_); my @list = (); if (defined $self->{'sortname'}) { if (keys %{$self->{'list'}}) { @list = sort {$self->{'list'}->{$a} cmp $self->{'list'}->{$b};} keys %{$self->{'list'}}; } } else { @list = @{$self->{'list'}}; } # organise into classification structure my %classifyinfo = ('thistype'=>'Invisible', 'childtype'=>'VList', 'Title'=>$self->{'title'}, 'contains'=>[]); foreach $OID (@list) { push (@{$classifyinfo{'contains'}}, {'OID'=>$OID}); } return \%classifyinfo; } 1;