[12844] | 1 | ###########################################################################
|
---|
| 2 | #
|
---|
| 3 | # IncrementalDocument.pm -- An object to encapsulate the Greenstone
|
---|
| 4 | # document retrieved from the GDBM database.
|
---|
| 5 | #
|
---|
| 6 | # A component of the Greenstone digital library software
|
---|
| 7 | # from the New Zealand Digital Library Project at the
|
---|
| 8 | # University of Waikato, New Zealand.
|
---|
| 9 | #
|
---|
| 10 | # Copyright (C) 2006 DL Consulting Ltd and New Zealand Digital Library Project
|
---|
| 11 | #
|
---|
| 12 | # This program is free software; you can redistribute it and/or modify
|
---|
| 13 | # it under the terms of the GNU General Public License as published by
|
---|
| 14 | # the Free Software Foundation; either version 2 of the License, or
|
---|
| 15 | # (at your option) any later version.
|
---|
| 16 | #
|
---|
| 17 | # This program is distributed in the hope that it will be useful,
|
---|
| 18 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 19 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 20 | # GNU General Public License for more details.
|
---|
| 21 | #
|
---|
| 22 | # You should have received a copy of the GNU General Public License
|
---|
| 23 | # along with this program; if not, write to the Free Software
|
---|
| 24 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 25 | #
|
---|
| 26 | ###########################################################################
|
---|
| 27 | package IncrementalDocument;
|
---|
| 28 |
|
---|
| 29 | use GDBMUtils;
|
---|
[15894] | 30 | use strict;
|
---|
[12844] | 31 |
|
---|
| 32 | # /**
|
---|
| 33 | # */
|
---|
| 34 | sub new()
|
---|
| 35 | {
|
---|
| 36 | my ($class, $collection, $oid) = @_;
|
---|
| 37 | #rint STDERR "IncrementalDocument::new($collection, $oid)\n";
|
---|
| 38 | # Test the parameters
|
---|
| 39 | die ("Error! Can't create a document that doesn't belong to a collection!") unless $collection;
|
---|
| 40 | die ("Error! Can't create a document that doesn't have a unique id (OID)!") unless $oid;
|
---|
| 41 | # Store the variables
|
---|
| 42 | my $self = {};
|
---|
| 43 | # The collection this document object has been loaded from.
|
---|
| 44 | $self->{'collection'} = $collection;
|
---|
| 45 | # An associative array of information retrieved from the GDBM database
|
---|
| 46 | # which maps a key string to a nested associative array listing values.
|
---|
| 47 | $self->{'data'} = {};
|
---|
| 48 | # The unique identifier of the document loaded
|
---|
| 49 | $self->{'oid'} = $oid;
|
---|
| 50 | # Stores the order in which metadata keys where discovered/added.
|
---|
| 51 | $self->{'order'} = {};
|
---|
| 52 | # Bless me father for I have sinned
|
---|
| 53 | bless $self, $class;
|
---|
| 54 | return $self;
|
---|
| 55 | }
|
---|
| 56 | # /** new() **/
|
---|
| 57 |
|
---|
| 58 | # /**
|
---|
| 59 | # */
|
---|
| 60 | sub addMetadata()
|
---|
| 61 | {
|
---|
| 62 | my ($self, $key, $value, $internal) = @_;
|
---|
| 63 | # Validate the arguments
|
---|
| 64 | die ("Error! Can't add a metadata value to a document without a valid key!") unless $key =~ /[\w]+/;
|
---|
| 65 | die ("Error! Can't add a metadata key to a document without a valid value!") unless $value =~ /[\w\d]+/;
|
---|
| 66 | # Is this a new key that we haven't encountered before? If so ensure an
|
---|
| 67 | # array exists for its values, and record the order in which we encountered
|
---|
| 68 | # this key.
|
---|
| 69 | if (!defined($self->{'data'}->{$key}))
|
---|
| 70 | {
|
---|
| 71 | # Determine how many data keys we're already storing, so we can add the next
|
---|
| 72 | # one at the appropriate index
|
---|
| 73 | my $index = scalar(keys %{$self->{'order'}});
|
---|
| 74 | $self->{'order'}->{$index} = $key;
|
---|
| 75 | $self->{'data'}->{$key} = {};
|
---|
| 76 | }
|
---|
| 77 | # Set the value of the associative path to 1.
|
---|
| 78 | $self->{'data'}->{$key}->{$value} = 1;
|
---|
| 79 | }
|
---|
| 80 | # /** addMetadata() **/
|
---|
| 81 |
|
---|
| 82 | # /** Retrieve all the metadata of this document as an array of pairs.
|
---|
| 83 | # *
|
---|
| 84 | # */
|
---|
| 85 | sub getAllMetadata()
|
---|
| 86 | {
|
---|
| 87 | print STDERR "IncrementalDocument.getAllMetadata()\n";
|
---|
| 88 | my ($self) = @_;
|
---|
| 89 | my @all_metadata;
|
---|
| 90 |
|
---|
| 91 | my $key_count = scalar(keys %{$self->{'order'}});
|
---|
| 92 | for (my $i = 0; $i < $key_count; $i++)
|
---|
| 93 | {
|
---|
| 94 | my $key = $self->{'order'}->{$i};
|
---|
| 95 | # Check if this key has been set
|
---|
| 96 | if ($self->{'data'}->{$key})
|
---|
| 97 | {
|
---|
| 98 | # Note: there may be zero values left
|
---|
| 99 | foreach my $value (sort keys %{$self->{'data'}->{$key}})
|
---|
| 100 | {
|
---|
| 101 | if ($self->{'data'}->{$key}->{$value})
|
---|
| 102 | {
|
---|
| 103 | print STDERR "* Storing $key => $value\n";
|
---|
| 104 | push(@all_metadata, [$key, $value]);
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
| 108 | }
|
---|
| 109 | print STDERR "Complete!\n";
|
---|
| 110 | return \@all_metadata;
|
---|
| 111 | }
|
---|
| 112 | # /** getAllMetadata() **/
|
---|
| 113 |
|
---|
| 114 | # /**
|
---|
| 115 | # */
|
---|
| 116 | sub getDocNum()
|
---|
| 117 | {
|
---|
| 118 | my ($self) = @_;
|
---|
| 119 | my $docnum = -1;
|
---|
| 120 | # Check the docnum path exists in the associated data
|
---|
| 121 | if(defined($self->{'data'}->{'docnum'}))
|
---|
| 122 | {
|
---|
| 123 | # Get the list of keys from that associative path
|
---|
| 124 | my @values = keys (%{$self->{'data'}->{'docnum'}});
|
---|
| 125 | # And since we know there will only ever be one value for docnum
|
---|
| 126 | $docnum = $values[0];
|
---|
| 127 | }
|
---|
| 128 | return $docnum;
|
---|
| 129 | }
|
---|
| 130 | # /** getDocNum() **/
|
---|
| 131 |
|
---|
| 132 | # /**
|
---|
| 133 | # */
|
---|
| 134 | sub loadDocument()
|
---|
| 135 | {
|
---|
| 136 | my ($self) = @_;
|
---|
| 137 | #rint STDERR "IncrementalDocument::loadDocument()\n";
|
---|
| 138 | # Load the raw text for the document object from GDBM
|
---|
| 139 | my $text = &GDBMUtils::gdbmGet($self->{'collection'}, $self->{'oid'});
|
---|
| 140 | # For each line in the raw text, extract the key (enclosed in angle
|
---|
| 141 | # brackets) and the value
|
---|
| 142 | $text =~ s/<([\w\d\.]+)>(.+?)\r?\n/&addMetadata($self, $1, $2, 1)/egs;
|
---|
| 143 | # Done
|
---|
| 144 | }
|
---|
| 145 | # /** loadDocument() **/
|
---|
| 146 |
|
---|
| 147 | # /** Locates and removes the given key/value mappings from this document
|
---|
| 148 | # * object.
|
---|
| 149 | # *
|
---|
| 150 | # * @param $self A reference to this IncrementalDocument object
|
---|
| 151 | # * @param $key The metadata key as a string
|
---|
| 152 | # * @param $value The obsolete metadata value as a string
|
---|
| 153 | # *
|
---|
| 154 | # * @author John Thompson, DL Consulting Ltd.
|
---|
| 155 | # */
|
---|
| 156 | sub removeMetadata()
|
---|
| 157 | {
|
---|
| 158 | my ($self, $key, $value) = @_;
|
---|
| 159 | # Ensure the value doesn't exist by simply setting to 0 the correct
|
---|
| 160 | # associative path
|
---|
| 161 | $self->{'data'}->{$key}->{$value} = 0;
|
---|
| 162 | }
|
---|
| 163 | # /*** removeMetadat() **/
|
---|
| 164 |
|
---|
| 165 | # /**
|
---|
| 166 | # */
|
---|
| 167 | sub saveDocument()
|
---|
| 168 | {
|
---|
| 169 | my ($self) = @_;
|
---|
| 170 | # Get a textual version of this object
|
---|
| 171 | my $text = $self->toString();
|
---|
| 172 | # Now store the object in the database using the GDBM utilities
|
---|
| 173 | &GDBMUtils::gdbmSet($self->{'collection'}, $self->{'oid'}, $text);
|
---|
| 174 | # There is a little bit of extra complexity when saving an incremental
|
---|
| 175 | # document in that we should ensure that a reverse lookup-from DocNum or
|
---|
| 176 | # nodeID to Greenstone document hash-exists in the database.
|
---|
| 177 | my $doc_num = $self->getDocNum();
|
---|
| 178 | if($doc_num >= 0)
|
---|
| 179 | {
|
---|
| 180 | my $text = &GDBMUtils::gdbmGet($self->{'collection'}, $doc_num);
|
---|
| 181 | # If there is no reverse lookup, then add one now
|
---|
| 182 | if($text !~ /<section>/)
|
---|
| 183 | {
|
---|
| 184 | &GDBMUtils::gdbmSet($self->{'collection'}, $doc_num, "<section>" . $self->{'oid'});
|
---|
| 185 | }
|
---|
| 186 | }
|
---|
| 187 | # Done
|
---|
| 188 | #rint STDERR "Stored document:\n[" . $self->{'oid'} . "]\n$text\n";
|
---|
| 189 | }
|
---|
| 190 | # /** saveDocument() **/
|
---|
| 191 |
|
---|
| 192 | # /** Produces a textual representation of this object.
|
---|
| 193 | # *
|
---|
| 194 | # * @return A string which describes this incremental document object
|
---|
| 195 | # *
|
---|
| 196 | # * @author John Thompson, DL Consulting Ltd.
|
---|
| 197 | # */
|
---|
| 198 | sub toString()
|
---|
| 199 | {
|
---|
| 200 | my ($self) = @_;
|
---|
| 201 | my $text = "";
|
---|
| 202 |
|
---|
| 203 | my $key_count = scalar(keys %{$self->{'order'}});
|
---|
| 204 | for (my $i = 0; $i < $key_count; $i++)
|
---|
| 205 | {
|
---|
| 206 | my $key = $self->{'order'}->{$i};
|
---|
| 207 | # Check if this key has been set
|
---|
| 208 | if ($self->{'data'}->{$key})
|
---|
| 209 | {
|
---|
| 210 | # Note: there may be zero values left
|
---|
| 211 | foreach my $value (sort keys %{$self->{'data'}->{$key}})
|
---|
| 212 | {
|
---|
| 213 | if ($self->{'data'}->{$key}->{$value})
|
---|
| 214 | {
|
---|
| 215 | $text .= "<$key>$value\n";
|
---|
| 216 | }
|
---|
| 217 | }
|
---|
| 218 | }
|
---|
| 219 | }
|
---|
| 220 | return $text;
|
---|
| 221 | }
|
---|
| 222 | # /** toString() **/
|
---|
| 223 |
|
---|
| 224 | 1;
|
---|