1 | ###############################################################################
|
---|
2 | #
|
---|
3 | # STDOUTXML.pm -- A dbutil implementation that, hopefully unsurprisingly,
|
---|
4 | # writes infodb stuff as XML to STDOUT. This will be used as part of the Hadoop
|
---|
5 | # process - where this output will be passed through to the reduce phase to be
|
---|
6 | # merged with other documents.
|
---|
7 | #
|
---|
8 | # A component of the Greenstone digital library software from the New Zealand
|
---|
9 | # Digital Library Project at the University of Waikato, New Zealand.
|
---|
10 | #
|
---|
11 | # Copyright (c) 2015 New Zealand Digital Library Project
|
---|
12 | #
|
---|
13 | # This program is free software; you can redistribute it and/or modify it under
|
---|
14 | # the terms of the GNU General Public License as published by the Free Software
|
---|
15 | # Foundation; either version 2 of the License, or (at your option) any later
|
---|
16 | # version.
|
---|
17 | #
|
---|
18 | # This program is distributed in the hope that it will be useful, but WITHOUT
|
---|
19 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
---|
20 | # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
---|
21 | # details.
|
---|
22 | #
|
---|
23 | # You should have received a copy of the GNU General Public License along with
|
---|
24 | # this program; if not, write to the Free Software Foundation, Inc., 675 Mass
|
---|
25 | # Ave, Cambridge, MA 02139, USA.
|
---|
26 | #
|
---|
27 | ###############################################################################
|
---|
28 |
|
---|
29 | package DBDrivers::STDOUTXML;
|
---|
30 |
|
---|
31 | # Pragma
|
---|
32 | use strict;
|
---|
33 |
|
---|
34 | # Libraries
|
---|
35 | # - Perl built-in modules
|
---|
36 | use Time::HiRes qw( gettimeofday );
|
---|
37 | # - Greenstone modules
|
---|
38 | use FileUtils;
|
---|
39 | use parent 'DBDrivers::BaseDBDriver';
|
---|
40 |
|
---|
41 | ## @function
|
---|
42 | sub new
|
---|
43 | {
|
---|
44 | my $class = shift(@_);
|
---|
45 | my $self = DBDrivers::BaseDBDriver->new(@_);
|
---|
46 | $self->{'default_file_extension'} = 'gdb';
|
---|
47 | $self->{'type'} = 'doc';
|
---|
48 | # Optional Support
|
---|
49 | $self->{'supports_datestamp'} = 1;
|
---|
50 | $self->{'supports_rss'} = 1;
|
---|
51 | $self->{'supports_set'} = 1;
|
---|
52 | $self->{'write_only'} = 1;
|
---|
53 | bless($self, $class);
|
---|
54 | return $self;
|
---|
55 | }
|
---|
56 | ## new(integer) => STDOUTXML ##
|
---|
57 |
|
---|
58 | # -----------------------------------------------------------------------------
|
---|
59 | # STDOUT XML IMPLEMENTATION
|
---|
60 | # -----------------------------------------------------------------------------
|
---|
61 |
|
---|
62 |
|
---|
63 | ## @function open_infodb_write_handle()
|
---|
64 | #
|
---|
65 | # Since we are writing to STDOUT we don't have to open a handle
|
---|
66 | #
|
---|
67 | sub open_infodb_write_handle
|
---|
68 | {
|
---|
69 | my $self = shift(@_);
|
---|
70 | $self->debugPrintFunctionHeader(@_);
|
---|
71 | my $infodb_file_path = shift(@_);
|
---|
72 | my $opt_append = shift(@_);
|
---|
73 | # Store this for later
|
---|
74 | $self->{'type'} = $infodb_file_path;
|
---|
75 | return 1;
|
---|
76 | }
|
---|
77 | ## open_infodb_write_handle() ##
|
---|
78 |
|
---|
79 |
|
---|
80 | ## @function
|
---|
81 | #
|
---|
82 | # In this function we not close the handle we didn't open :P
|
---|
83 | #
|
---|
84 | sub close_infodb_write_handle
|
---|
85 | {
|
---|
86 | my $self = shift(@_);
|
---|
87 | $self->debugPrintFunctionHeader(@_);
|
---|
88 | my $infodb_handle = shift(@_);
|
---|
89 | }
|
---|
90 | ## close_infodb_write_handle() ##
|
---|
91 |
|
---|
92 |
|
---|
93 | ## @function delete_infodb_entry()
|
---|
94 | #
|
---|
95 | sub delete_infodb_entry
|
---|
96 | {
|
---|
97 | my $self = shift(@_);
|
---|
98 | $self->debugPrintFunctionHeader(@_);
|
---|
99 | my $infodb_handle = shift(@_);
|
---|
100 | my $infodb_key = shift(@_);
|
---|
101 | print STDERR "\@TODO: implement delete_infodb_entry()\n";
|
---|
102 | }
|
---|
103 | ## delete_infodb_entry() ##
|
---|
104 |
|
---|
105 |
|
---|
106 | ## @function get_infodb_file_path()
|
---|
107 | #
|
---|
108 | sub get_infodb_file_path
|
---|
109 | {
|
---|
110 | my $self = shift(@_);
|
---|
111 | $self->debugPrintFunctionHeader(@_);
|
---|
112 | my $collection_name = shift(@_);
|
---|
113 | my $infodb_directory_path = shift(@_);
|
---|
114 | my $infodb_type = 'error';
|
---|
115 | if ($collection_name =~ /(datestamp|doc|src|rss)/)
|
---|
116 | {
|
---|
117 | $infodb_type = $1;
|
---|
118 | }
|
---|
119 | return $infodb_type;
|
---|
120 | }
|
---|
121 | ## get_infodb_file_path(string, string) => string ##
|
---|
122 |
|
---|
123 |
|
---|
124 | ## @function read_infodb_entry()
|
---|
125 | #
|
---|
126 | sub read_infodb_entry
|
---|
127 | {
|
---|
128 | my $self = shift(@_);
|
---|
129 | $self->debugPrintFunctionHeader(@_);
|
---|
130 | my $infodb_file_path = shift(@_);
|
---|
131 | my $infodb_key = shift(@_);
|
---|
132 | print STDERR "\@TODO: implement read_infodb_entry()\n";
|
---|
133 | }
|
---|
134 | ## read_infodb_file() ##
|
---|
135 |
|
---|
136 |
|
---|
137 | ## @function read_infodb_file()
|
---|
138 | #
|
---|
139 | sub read_infodb_file
|
---|
140 | {
|
---|
141 | my $self = shift(@_);
|
---|
142 | $self->debugPrintFunctionHeader(@_);
|
---|
143 | my $infodb_file_path = shift(@_);
|
---|
144 | my $infodb_map = shift(@_);
|
---|
145 | print STDERR "\@TODO: implement read_infodb_file()\n";
|
---|
146 | }
|
---|
147 | ## read_infodb_file() ##
|
---|
148 |
|
---|
149 |
|
---|
150 | ## @function read_infodb_keys()
|
---|
151 | #
|
---|
152 | sub read_infodb_keys
|
---|
153 | {
|
---|
154 | my $self = shift(@_);
|
---|
155 | $self->debugPrintFunctionHeader(@_);
|
---|
156 | my $infodb_file_path = shift(@_);
|
---|
157 | my $infodb_map = shift(@_);
|
---|
158 | print "!implement read_infodb_keys()\n";
|
---|
159 | }
|
---|
160 | ## read_infodb_keys() ##
|
---|
161 |
|
---|
162 |
|
---|
163 | sub set_infodb_entry
|
---|
164 | {
|
---|
165 | my $self = shift(@_);
|
---|
166 | $self->debugPrintFunctionHeader(@_);
|
---|
167 | my $infodb_file_path = shift(@_);
|
---|
168 | my $infodb_key = shift(@_);
|
---|
169 | my $infodb_map = shift(@_);
|
---|
170 | my $status = undef;
|
---|
171 | print STDERR "\@TODO: implement set_infodb_entry()\n";
|
---|
172 | return $status;
|
---|
173 | }
|
---|
174 | ## set_infodb_entry() ##
|
---|
175 |
|
---|
176 |
|
---|
177 | ## @function write_infodb_entry()
|
---|
178 | #
|
---|
179 | sub write_infodb_entry
|
---|
180 | {
|
---|
181 | my $self = shift(@_);
|
---|
182 | $self->debugPrintFunctionHeader(@_);
|
---|
183 | my $infodb_handle = shift(@_);
|
---|
184 | my $infodb_key = shift(@_);
|
---|
185 | my $infodb_map = shift(@_);
|
---|
186 | my @values;
|
---|
187 | foreach my $infodb_value_key (sort keys(%$infodb_map))
|
---|
188 | {
|
---|
189 | foreach my $infodb_value (@{$infodb_map->{$infodb_value_key}})
|
---|
190 | {
|
---|
191 | if ($infodb_value =~ /-{70,}/)
|
---|
192 | {
|
---|
193 | # if value contains 70 or more hyphens in a row we need to escape them
|
---|
194 | # to prevent txt2db from treating them as a separator
|
---|
195 | $infodb_value =~ s/-/&\#045;/gi;
|
---|
196 | }
|
---|
197 | push(@values, '<' . $infodb_value_key . '>' . $infodb_value);
|
---|
198 | }
|
---|
199 | }
|
---|
200 | my $values_str = join("\n", @values);
|
---|
201 | $values_str =~ s/&/&/g;
|
---|
202 | $values_str =~ s/</</g;
|
---|
203 | $values_str =~ s/>/>/g;
|
---|
204 | $values_str =~ s/"/"/g;
|
---|
205 | $values_str =~ s/'/'/g;
|
---|
206 | my ($seconds, $microseconds) = gettimeofday();
|
---|
207 | print '<InfoDBEntry type="' . $self->{'type'} . '" key="' . $infodb_key . '" mode="set" timestamp="' . $seconds . '.' . $microseconds . '">' . $values_str . '</InfoDBEntry>' . "\n";
|
---|
208 | }
|
---|
209 | ## write_infodb_entry() ##
|
---|
210 |
|
---|
211 |
|
---|
212 | ## @function write_infodb_rawentry()
|
---|
213 | #
|
---|
214 | sub write_infodb_rawentry
|
---|
215 | {
|
---|
216 | my $self = shift(@_);
|
---|
217 | $self->debugPrintFunctionHeader(@_);
|
---|
218 | my $infodb_handle = shift(@_);
|
---|
219 | my $infodb_key = shift(@_);
|
---|
220 | my $infodb_val = shift(@_);
|
---|
221 | $infodb_val =~ s/&/&/g;
|
---|
222 | $infodb_val =~ s/</</g;
|
---|
223 | $infodb_val =~ s/>/>/g;
|
---|
224 | $infodb_val =~ s/"/"/g;
|
---|
225 | $infodb_val =~ s/'/'/g;
|
---|
226 | my ($seconds, $microseconds) = gettimeofday();
|
---|
227 | print '<InfoDBEntry type="' . $self->{'type'} . '" key="' . $infodb_key . '" mode="set" timestamp="' . $seconds . '.' . $microseconds . '">' . $infodb_val . '</InfoDBEntry>' . "\n";
|
---|
228 | }
|
---|
229 | ## write_infodb_rawentry(filehandle, string, string) => void ##
|
---|
230 |
|
---|
231 |
|
---|
232 | 1;
|
---|