[537] | 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 |
|
---|
[18566] | 26 | # Same as SimpleList classifier but includes all sections of document
|
---|
[425] | 27 | # (excluding top level) rather than just top level document
|
---|
| 28 | # itself
|
---|
| 29 |
|
---|
[3540] | 30 |
|
---|
[425] | 31 | package SectionList;
|
---|
| 32 |
|
---|
[18566] | 33 | use SimpleList;
|
---|
[425] | 34 | use sorttools;
|
---|
| 35 |
|
---|
[10253] | 36 | use strict;
|
---|
| 37 | no strict 'refs'; # allow filehandles to be variables and viceversa
|
---|
| 38 |
|
---|
[1250] | 39 | sub BEGIN {
|
---|
[18566] | 40 | @SectionList::ISA = ('SimpleList');
|
---|
[425] | 41 | }
|
---|
| 42 |
|
---|
[10253] | 43 | my $arguments = [];
|
---|
[4759] | 44 | my $options = { 'name' => "SectionList",
|
---|
[5645] | 45 | 'desc' => "{SectionList.desc}",
|
---|
[6408] | 46 | 'abstract' => "no",
|
---|
| 47 | 'inherits' => "yes" };
|
---|
[3540] | 48 |
|
---|
[1839] | 49 |
|
---|
[10218] | 50 | sub new {
|
---|
| 51 | my ($class) = shift (@_);
|
---|
| 52 | my ($classifierslist,$inputargs,$hashArgOptLists) = @_;
|
---|
| 53 | push(@$classifierslist, $class);
|
---|
[3540] | 54 |
|
---|
[17209] | 55 | push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
|
---|
| 56 | push(@{$hashArgOptLists->{"OptList"}},$options);
|
---|
[4786] | 57 |
|
---|
[18566] | 58 | my $self = new SimpleList($classifierslist, $inputargs, $hashArgOptLists);
|
---|
[10218] | 59 |
|
---|
[1611] | 60 | return bless $self, $class;
|
---|
[1483] | 61 | }
|
---|
| 62 |
|
---|
[425] | 63 | sub classify {
|
---|
| 64 | my $self = shift (@_);
|
---|
[23116] | 65 | my ($doc_obj, @options) = @_;
|
---|
[838] | 66 |
|
---|
[6974] | 67 | # @options used by AZCompactList when is uses SectionList internally
|
---|
[6970] | 68 | # are we sorting the list??
|
---|
| 69 | my $nosort = 0;
|
---|
[10218] | 70 | if (defined $self->{'sort'} && $self->{'sort'} eq "nosort") {
|
---|
[6970] | 71 | $nosort = 1;
|
---|
[838] | 72 | }
|
---|
[425] | 73 |
|
---|
[6974] | 74 | my $thissection = undef;
|
---|
[6970] | 75 |
|
---|
[10253] | 76 | foreach my $option (@options)
|
---|
[6974] | 77 | {
|
---|
| 78 | if ($option =~ m/^section=(\d+)$/i)
|
---|
| 79 | {
|
---|
| 80 | $thissection = $1;
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
[6970] | 83 |
|
---|
[425] | 84 | my $sortmeta = "";
|
---|
[10218] | 85 | if (!$nosort && defined $self->{'sort'}) {
|
---|
| 86 | if ($self->{'sort'} =~ /^filename$/i) {
|
---|
[425] | 87 | $sortmeta = $doc_obj->get_source_filename();
|
---|
| 88 | } else {
|
---|
[10218] | 89 | $sortmeta = $doc_obj->get_metadata_element($doc_obj->get_top_section(), $self->{'sort'});
|
---|
[10630] | 90 | if (defined $sortmeta && !$self->{'no_metadata_formatting'}) {
|
---|
[10218] | 91 | $sortmeta = &sorttools::format_metadata_for_sorting($self->{'sort'}, $sortmeta, $doc_obj);
|
---|
[425] | 92 | }
|
---|
| 93 | }
|
---|
| 94 | $sortmeta = "" unless defined $sortmeta;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
[6974] | 97 | if (defined $thissection) {
|
---|
| 98 | # just classify the one section
|
---|
[23116] | 99 | $self->classify_section($thissection, $doc_obj, $sortmeta, $nosort);
|
---|
[6974] | 100 | } else
|
---|
| 101 | {
|
---|
| 102 | $thissection = $doc_obj->get_next_section ($doc_obj->get_top_section());
|
---|
| 103 | while (defined $thissection) {
|
---|
[23116] | 104 | $self->classify_section($thissection, $doc_obj, $sortmeta, $nosort);
|
---|
[6974] | 105 | $thissection = $doc_obj->get_next_section ($thissection);
|
---|
| 106 | }
|
---|
[838] | 107 | }
|
---|
[425] | 108 | }
|
---|
| 109 |
|
---|
| 110 | sub classify_section {
|
---|
| 111 | my $self = shift (@_);
|
---|
[23116] | 112 | my ($section, $doc_obj, $sortmeta, $nosort) = @_;
|
---|
[425] | 113 |
|
---|
| 114 | my $doc_OID = $doc_obj->get_OID();
|
---|
[6970] | 115 | $nosort = 0 unless defined $nosort;
|
---|
[425] | 116 | $sortmeta = "" unless defined $sortmeta;
|
---|
| 117 |
|
---|
[6970] | 118 | my $metavalue;
|
---|
| 119 | my $metaname;
|
---|
| 120 | if (defined $self->{'meta_list'}) {
|
---|
| 121 | # find the first available metadata
|
---|
[10253] | 122 | foreach my $m (@{$self->{'meta_list'}}) {
|
---|
[6970] | 123 | $metavalue = $doc_obj->get_metadata_element($section, $m);
|
---|
| 124 | $metaname = $m;
|
---|
| 125 | last if defined $metavalue;
|
---|
| 126 | }
|
---|
| 127 | #if we haven't found a metavalue here, then the section shouldn't be included
|
---|
| 128 | return unless defined $metavalue;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | # we know the section should be included, add it now if we are not sorting
|
---|
| 132 | if ($nosort) {
|
---|
| 133 | push (@{$self->{'list'}}, "$doc_OID.$section");
|
---|
| 134 | return;
|
---|
| 135 | }
|
---|
| 136 | # check that it hasn't been added already
|
---|
| 137 | if (defined $self->{'list'}->{"$doc_OID.$section"}) {
|
---|
| 138 | my $outhandle = $self->{'outhandle'};
|
---|
| 139 | print $outhandle "WARNING: SectionList::classify called multiple times for $doc_OID.$section\n";
|
---|
| 140 | }
|
---|
| 141 |
|
---|
[10218] | 142 | if (defined $self->{'sort'}) {
|
---|
[6970] | 143 | # sorting on alternative metadata
|
---|
| 144 | $self->{'list'}->{"$doc_OID.$section"} = $sortmeta;
|
---|
[425] | 145 | } else {
|
---|
[10218] | 146 | # sorting on the classification metadata
|
---|
[6970] | 147 | # do the same formatting on the meta value as for sort meta
|
---|
[10630] | 148 | $metavalue = &sorttools::format_metadata_for_sorting($metaname, $metavalue, $doc_obj) unless $self->{'no_metadata_formatting'};
|
---|
[6970] | 149 | $self->{'list'}->{"$doc_OID.$section"} = $metavalue;
|
---|
[425] | 150 | }
|
---|
| 151 | }
|
---|
| 152 | 1;
|
---|