[30332] | 1 | ###############################################################################
|
---|
| 2 | #
|
---|
| 3 | # 70HyphenFormat.pm -- The parent class of drivers that use the basic GS format
|
---|
| 4 | # of a text obeying these rules:
|
---|
| 5 | #
|
---|
| 6 | # <line> := <uniqueid> <metadata>+ <separator>
|
---|
| 7 | # <uniqueid> := \[[a-z][a-z0-9]*\]\n
|
---|
| 8 | # <metadata> := <[a-z][a-z0-9]*>(^-{70})+\n
|
---|
| 9 | # <separator> := -{70}\n
|
---|
| 10 | #
|
---|
| 11 | # Contains some utility functions useful to any driver
|
---|
| 12 | # that makes use of this format.
|
---|
| 13 | #
|
---|
| 14 | # A component of the Greenstone digital library software from the New Zealand
|
---|
| 15 | # Digital Library Project at the University of Waikato, New Zealand.
|
---|
| 16 | #
|
---|
| 17 | # Copyright (C) 1999-2015 New Zealand Digital Library Project
|
---|
| 18 | #
|
---|
| 19 | # This program is free software; you can redistribute it and/or modify it under
|
---|
| 20 | # the terms of the GNU General Public License as published by the Free Software
|
---|
| 21 | # Foundation; either version 2 of the License, or (at your option) any later
|
---|
| 22 | # version.
|
---|
| 23 | #
|
---|
| 24 | # This program is distributed in the hope that it will be useful, but WITHOUT
|
---|
| 25 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
---|
| 26 | # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
---|
| 27 | # more details.
|
---|
| 28 | #
|
---|
| 29 | # You should have received a copy of the GNU General Public License along with
|
---|
| 30 | # this program; if not, write to the Free Software Foundation, Inc., 675 Mass
|
---|
| 31 | # Ave, Cambridge, MA 02139, USA.
|
---|
| 32 | #
|
---|
| 33 | ###############################################################################
|
---|
| 34 |
|
---|
[30347] | 35 | # Note: This driver may be a candidate for further splitting, maybe into a
|
---|
| 36 | # PipedExecutableDriver and a 70HyphenFormatDriver... but for now all piped
|
---|
| 37 | # drivers are 70 hyphen format ones, so, yeah.
|
---|
| 38 |
|
---|
[30332] | 39 | package DBDrivers::70HyphenFormat;
|
---|
| 40 |
|
---|
| 41 | # Pragma
|
---|
| 42 | use strict;
|
---|
| 43 |
|
---|
| 44 | # Libraries
|
---|
| 45 | use ghtml;
|
---|
| 46 | use util;
|
---|
| 47 | use FileUtils;
|
---|
| 48 | use parent 'DBDrivers::BaseDBDriver';
|
---|
| 49 |
|
---|
| 50 | use constant {
|
---|
| 51 | RWMODE_READ => '-|',
|
---|
| 52 | RWMODE_WRITE => '|-',
|
---|
| 53 | };
|
---|
| 54 |
|
---|
| 55 | ## @function constructor
|
---|
| 56 | #
|
---|
| 57 | sub new
|
---|
| 58 | {
|
---|
| 59 | my $class = shift(@_);
|
---|
| 60 | my $self = DBDrivers::BaseDBDriver->new(@_);
|
---|
| 61 | $self->{'executable_path'} = 'error';
|
---|
| 62 | $self->{'keyread_executable'} = 'error';
|
---|
| 63 | $self->{'read_executable'} = 'error';
|
---|
| 64 | $self->{'write_executable'} = 'error';
|
---|
[30347] | 65 | $self->{'forced_affinity'} = -1; # Set to processor number for forced affinity
|
---|
[30332] | 66 | bless($self, $class);
|
---|
| 67 | return $self;
|
---|
| 68 | }
|
---|
| 69 | ## new(void) => 70HyphenFormat ##
|
---|
| 70 |
|
---|
| 71 |
|
---|
| 72 | ################################## Protected ##################################
|
---|
| 73 |
|
---|
| 74 |
|
---|
| 75 | ## @function close_infodb_write_handle(filehandle)
|
---|
| 76 | #
|
---|
| 77 | sub close_infodb_write_handle
|
---|
| 78 | {
|
---|
| 79 | my $self = shift(@_);
|
---|
[30347] | 80 | $self->debugPrintFunctionHeader(@_);
|
---|
| 81 | my $handle = shift(@_);
|
---|
| 82 | my $force_close = shift(@_); # Undefined most of the time
|
---|
| 83 | my $continue_close = $self->removeConnectionIfPersistent($handle, $force_close);
|
---|
| 84 | if ($continue_close) {
|
---|
| 85 | close($handle);
|
---|
| 86 | }
|
---|
| 87 | return;
|
---|
[30332] | 88 | }
|
---|
| 89 | ## close_infodb_write_handle(filehandle) => void ##
|
---|
| 90 |
|
---|
| 91 |
|
---|
| 92 | ## @function delete_infodb_entry(filehandle, string)
|
---|
| 93 | #
|
---|
| 94 | sub delete_infodb_entry
|
---|
| 95 | {
|
---|
| 96 | my $self = shift(@_);
|
---|
[30347] | 97 | $self->debugPrintFunctionHeader(@_);
|
---|
[30332] | 98 | my $infodb_handle = shift(@_);
|
---|
| 99 | my $infodb_key = shift(@_);
|
---|
| 100 | # A minus at the end of a key (after the ]) signifies 'delete'
|
---|
[30347] | 101 | print $infodb_handle '[' . $infodb_key . ']-' . "\n";
|
---|
[30332] | 102 | # The 70 minus signs are also needed, to help make the parsing by db2txt simple
|
---|
| 103 | print $infodb_handle '-' x 70, "\n";
|
---|
| 104 | }
|
---|
| 105 | ## delete_infodb_entry(filehandle, string) => void ##
|
---|
| 106 |
|
---|
| 107 |
|
---|
| 108 | ## @function open_infodb_write_handle(string, string)
|
---|
| 109 | #
|
---|
| 110 | sub open_infodb_write_handle
|
---|
| 111 | {
|
---|
| 112 | my $self = shift(@_);
|
---|
| 113 | $self->debugPrintFunctionHeader(@_);
|
---|
[30347] | 114 | my $path = shift(@_);
|
---|
| 115 | my $append = shift(@_);
|
---|
| 116 | my $infodb_file_handle = $self->retrieveConnectionIfPersistent($path, $append);;
|
---|
| 117 | # No available existing connection
|
---|
| 118 | if (!defined $infodb_file_handle || !$infodb_file_handle) {
|
---|
| 119 | $infodb_file_handle = $self->openWriteHandle($path, $append, @_);
|
---|
| 120 | $self->registerConnectionIfPersistent($infodb_file_handle, $path, $append);
|
---|
| 121 | }
|
---|
[30332] | 122 | return $infodb_file_handle;
|
---|
| 123 | }
|
---|
| 124 | ## open_infodb_write_handle(string, string) => filehandle ##
|
---|
| 125 |
|
---|
| 126 |
|
---|
| 127 | ## @function openPipedHandle(integer, string, string, string*) => filehandle
|
---|
| 128 | #
|
---|
| 129 | sub openPipedHandle
|
---|
| 130 | {
|
---|
| 131 | my $self = shift(@_);
|
---|
| 132 | my $mode = shift(@_);
|
---|
[30341] | 133 | my $executable_and_default_args = shift(@_);
|
---|
[30332] | 134 | my $infodb_file_path = shift(@_);
|
---|
[30341] | 135 | my ($executable, $default_args) = $executable_and_default_args =~ /^([a-z0-9]+)\s*(.*)$/;
|
---|
[30332] | 136 | my $exe = &FileUtils::filenameConcatenate($self->{'executable_path'}, $executable . &util::get_os_exe());
|
---|
| 137 | if (!-e $exe) {
|
---|
[30341] | 138 | # Hope it's on path
|
---|
[30332] | 139 | $exe = $executable . &util::get_os_exe();
|
---|
| 140 | }
|
---|
| 141 | my $infodb_file_handle = undef;
|
---|
[30347] | 142 | my $cmd = '';
|
---|
| 143 | if ($self->{'forced_affinity'} >= 0)
|
---|
| 144 | {
|
---|
| 145 | $cmd = 'taskset -c ' . $self->{'forced_affinity'} . ' ';
|
---|
| 146 | }
|
---|
| 147 | $cmd .= '"' . $exe . '" ' . $default_args;
|
---|
[30332] | 148 | foreach my $open_arg (@_) {
|
---|
[30347] | 149 | # Special - append is typically missing a hyphen
|
---|
| 150 | if ($open_arg eq 'append') {
|
---|
| 151 | $open_arg = '-append';
|
---|
| 152 | }
|
---|
[30332] | 153 | $cmd .= ' ' . $open_arg;
|
---|
| 154 | }
|
---|
| 155 | $cmd .= ' "' . $infodb_file_path . '"';
|
---|
| 156 | $self->debugPrint("CMD: '" . $cmd . "'\n");
|
---|
| 157 | if(!open($infodb_file_handle, $mode . ':utf8', $cmd)) {
|
---|
| 158 | print STDERR "Error: Failed to open pipe to '$cmd'\n";
|
---|
| 159 | print STDERR " $!\n";
|
---|
| 160 | return undef;
|
---|
| 161 | }
|
---|
| 162 | #binmode($infodb_file_handle,":utf8");
|
---|
| 163 | return $infodb_file_handle;
|
---|
| 164 | }
|
---|
| 165 | ## openPipedHandle(integer, string, string, string*) => filehandle ##
|
---|
| 166 |
|
---|
| 167 |
|
---|
| 168 | ## @function openReadHandle(string, string) => filehandle
|
---|
[30347] | 169 | #
|
---|
[30332] | 170 | sub openReadHandle
|
---|
| 171 | {
|
---|
| 172 | my $self = shift(@_);
|
---|
| 173 | return $self->openPipedHandle(RWMODE_READ, $self->{'read_executable'}, @_);
|
---|
| 174 | }
|
---|
| 175 | ## openReadHandle(string, string) => filehandle
|
---|
| 176 |
|
---|
| 177 |
|
---|
[30347] | 178 | ## @function openWriteHandle(*) => filehandle
|
---|
| 179 | #
|
---|
[30332] | 180 | sub openWriteHandle
|
---|
| 181 | {
|
---|
| 182 | my $self = shift(@_);
|
---|
| 183 | return $self->openPipedHandle(RWMODE_WRITE, $self->{'write_executable'}, @_);
|
---|
| 184 | }
|
---|
[30347] | 185 | ## openWriteHandle(*) => filehandle ##
|
---|
[30332] | 186 |
|
---|
[30347] | 187 |
|
---|
[30332] | 188 | ## @function read_infodb_entry(string, string) => hashmap
|
---|
| 189 | #
|
---|
| 190 | sub read_infodb_entry
|
---|
| 191 | {
|
---|
| 192 | my $self = shift(@_);
|
---|
| 193 | my $raw_string = $self->read_infodb_rawentry(@_);
|
---|
| 194 | my $infodb_rec = $self->convert_infodb_string_to_hash($raw_string);
|
---|
| 195 | return $infodb_rec;
|
---|
| 196 | }
|
---|
| 197 | ## read_infodb_entry(string, string) => hashmap ##
|
---|
| 198 |
|
---|
| 199 |
|
---|
| 200 | ## @function read_infodb_file(string, hashmap) => void
|
---|
| 201 | #
|
---|
| 202 | sub read_infodb_file
|
---|
| 203 | {
|
---|
| 204 | my $self = shift(@_);
|
---|
| 205 | my $infodb_file_path = shift(@_);
|
---|
| 206 | my $infodb_map = shift(@_);
|
---|
[30347] | 207 | $self->debugPrintFunctionHeader($infodb_file_path, $infodb_map);
|
---|
[30332] | 208 | my $infodb_file_handle = $self->openReadHandle($infodb_file_path);
|
---|
| 209 | my $infodb_line = "";
|
---|
| 210 | my $infodb_key = "";
|
---|
| 211 | my $infodb_value = "";
|
---|
| 212 | while (defined ($infodb_line = <$infodb_file_handle>)) {
|
---|
| 213 | $infodb_line =~ s/(\r\n)+$//; # more general than chomp
|
---|
| 214 | if ($infodb_line =~ /^\[([^\]]+)\]$/) {
|
---|
| 215 | $infodb_key = $1;
|
---|
| 216 | }
|
---|
| 217 | elsif ($infodb_line =~ /^-{70}$/) {
|
---|
| 218 | $infodb_map->{$infodb_key} = $infodb_value;
|
---|
| 219 | $infodb_key = "";
|
---|
| 220 | $infodb_value = "";
|
---|
| 221 | }
|
---|
| 222 | else {
|
---|
| 223 | $infodb_value .= $infodb_line;
|
---|
| 224 | }
|
---|
| 225 | }
|
---|
[30347] | 226 | $self->close_infodb_write_handle($infodb_file_handle);
|
---|
[30332] | 227 | }
|
---|
| 228 | ## read_infodb_file(string, hashmap) => void ##
|
---|
| 229 |
|
---|
| 230 |
|
---|
| 231 | ## @function read_infodb_keys(string, hashmap) => void
|
---|
| 232 | #
|
---|
| 233 | sub read_infodb_keys
|
---|
| 234 | {
|
---|
| 235 | my $self = shift(@_);
|
---|
| 236 | my $infodb_file_path = shift(@_);
|
---|
| 237 | my $infodb_map = shift(@_);
|
---|
| 238 | my $infodb_file_handle = $self->openPipedHandle(RWMODE_READ, $self->{'keyread_executable'}, $infodb_file_path);
|
---|
| 239 | if (!$infodb_file_handle) {
|
---|
| 240 | die("Couldn't open pipe from gdbmkeys: " . $infodb_file_path . "\n");
|
---|
| 241 | }
|
---|
| 242 | my $infodb_line = "";
|
---|
| 243 | my $infodb_key = "";
|
---|
| 244 | my $infodb_value = "";
|
---|
| 245 | # Simple case - dedicated keyread exe, so keys are strings
|
---|
| 246 | if ($self->{'keyread_executable'} ne $self->{'read_executable'}) {
|
---|
| 247 | while (defined ($infodb_line = <$infodb_file_handle>)) {
|
---|
| 248 | $infodb_line =~ s/[\r\n]+$//;
|
---|
| 249 | $infodb_map->{$infodb_line} = 1;
|
---|
| 250 | }
|
---|
| 251 | }
|
---|
| 252 | # Slightly more difficult - have to parse keys out of 70hyphen format
|
---|
| 253 | else {
|
---|
| 254 | while (defined ($infodb_line = <$infodb_file_handle>)) {
|
---|
[30341] | 255 | if ($infodb_line =~ /^\[([^\]]+)\](-)?[\r\n]*$/) {
|
---|
| 256 | my $key = $1;
|
---|
| 257 | my $delete_flag = $2;
|
---|
| 258 | if (defined $delete_flag) {
|
---|
| 259 | delete $infodb_map->{$key}
|
---|
| 260 | }
|
---|
| 261 | else {
|
---|
| 262 | $infodb_map->{$key} = 1;
|
---|
| 263 | }
|
---|
[30332] | 264 | }
|
---|
| 265 | }
|
---|
| 266 | }
|
---|
[30347] | 267 | $self->close_infodb_write_handle($infodb_file_handle);
|
---|
[30332] | 268 | }
|
---|
| 269 | ## read_infodb_keys(string, hashmap) => void ##
|
---|
| 270 |
|
---|
| 271 |
|
---|
| 272 | ## @function read_infodb_rawentry(string, string) => string
|
---|
| 273 | #
|
---|
| 274 | # !! TEMPORARY: Slow and naive implementation that just reads the entire file
|
---|
| 275 | # and picks out the one value. This should one day be replaced with database-
|
---|
| 276 | # specific versions that will use dbget etc.
|
---|
| 277 | #
|
---|
| 278 | sub read_infodb_rawentry
|
---|
| 279 | {
|
---|
| 280 | my $self = shift(@_);
|
---|
| 281 | my $infodb_file_path = shift(@_);
|
---|
| 282 | my $infodb_key = shift(@_);
|
---|
| 283 | # temporary hashmap... we're only interested in one entry
|
---|
| 284 | my $infodb_map = {};
|
---|
| 285 | $self->read_infodb_file($infodb_file_path, $infodb_map);
|
---|
| 286 | return $infodb_map->{$infodb_key};
|
---|
| 287 | }
|
---|
| 288 | ## read_infodb_rawentry(string, string) => string ##
|
---|
| 289 |
|
---|
| 290 |
|
---|
| 291 | ## @function set_infodb_entry(string, string, hashmap)
|
---|
| 292 | #
|
---|
| 293 | sub set_infodb_entry
|
---|
| 294 | {
|
---|
| 295 | my $self = shift(@_);
|
---|
| 296 | my $infodb_file_path = shift(@_);
|
---|
| 297 | my $infodb_key = shift(@_);
|
---|
| 298 | my $infodb_map = shift(@_);
|
---|
| 299 |
|
---|
| 300 | # HTML escape anything that is not part of the "contains" metadata value
|
---|
| 301 | foreach my $k (keys %$infodb_map) {
|
---|
| 302 | my @escaped_v = ();
|
---|
| 303 | foreach my $v (@{$infodb_map->{$k}}) {
|
---|
| 304 | if ($k eq "contains") {
|
---|
| 305 | push(@escaped_v, $v);
|
---|
| 306 | }
|
---|
| 307 | else {
|
---|
| 308 | my $ev = &ghtml::unescape_html($v);
|
---|
| 309 | push(@escaped_v, $ev);
|
---|
| 310 | }
|
---|
| 311 | }
|
---|
| 312 | $infodb_map->{$k} = \@escaped_v;
|
---|
| 313 | }
|
---|
| 314 |
|
---|
| 315 | # Generate the record string
|
---|
| 316 | my $serialized_infodb_map = $self->convert_infodb_hash_to_string($infodb_map);
|
---|
| 317 |
|
---|
| 318 | # Store it into DB using '... -append' which despite its name actually
|
---|
| 319 | # replaces the record if it already exists
|
---|
| 320 | my $status = undef;
|
---|
| 321 | my $infodb_file_handle = $self->openWriteHandle($infodb_file_path, '-append');
|
---|
| 322 | if (!$infodb_file_handle) {
|
---|
| 323 | print STDERR "Error: set_infodb_entry() failed to open pipe to: " . $infodb_file_handle ."\n";
|
---|
| 324 | print STDERR " $!\n";
|
---|
| 325 | $status = -1;
|
---|
| 326 | }
|
---|
| 327 | else {
|
---|
| 328 | print $infodb_file_handle "[$infodb_key]\n";
|
---|
| 329 | print $infodb_file_handle "$serialized_infodb_map\n";
|
---|
[30347] | 330 | $self->close_infodb_write_handle($infodb_file_handle);
|
---|
[30332] | 331 | $status = 0; # as in exit status of cmd OK
|
---|
| 332 | }
|
---|
| 333 | return $status;
|
---|
| 334 | }
|
---|
| 335 | ## set_infodb_entry(string, string, hashmap) => integer ##
|
---|
| 336 |
|
---|
| 337 |
|
---|
| 338 | ## @function write_infodb_entry(filehandle, string, hashmap)
|
---|
| 339 | #
|
---|
| 340 | sub write_infodb_entry
|
---|
| 341 | {
|
---|
| 342 | my $self = shift(@_);
|
---|
| 343 | my $infodb_handle = shift(@_);
|
---|
| 344 | my $infodb_key = shift(@_);
|
---|
| 345 | my $infodb_map = shift(@_);
|
---|
| 346 |
|
---|
| 347 | print $infodb_handle "[$infodb_key]\n";
|
---|
| 348 | foreach my $infodb_value_key (sort keys(%$infodb_map)) {
|
---|
| 349 | foreach my $infodb_value (@{$infodb_map->{$infodb_value_key}}) {
|
---|
| 350 | if ($infodb_value =~ /-{70,}/) {
|
---|
| 351 | # if value contains 70 or more hyphens in a row we need to escape them
|
---|
| 352 | # to prevent txt2db from treating them as a separator
|
---|
| 353 | $infodb_value =~ s/-/&\#045;/gi;
|
---|
| 354 | }
|
---|
| 355 | print $infodb_handle "<$infodb_value_key>" . $infodb_value . "\n";
|
---|
| 356 | }
|
---|
| 357 | }
|
---|
| 358 | print $infodb_handle '-' x 70, "\n";
|
---|
| 359 | }
|
---|
| 360 | ## write_infodb_entry(filehandle, string, hashmap) => void ##
|
---|
| 361 |
|
---|
| 362 |
|
---|
| 363 | ## @function write_infodb_rawentry(filehandle, string, string)
|
---|
| 364 | #
|
---|
| 365 | sub write_infodb_rawentry
|
---|
| 366 | {
|
---|
| 367 | my $self = shift(@_);
|
---|
| 368 | my $infodb_handle = shift(@_);
|
---|
| 369 | my $infodb_key = shift(@_);
|
---|
| 370 | my $infodb_val = shift(@_);
|
---|
| 371 |
|
---|
| 372 | print $infodb_handle "[$infodb_key]\n";
|
---|
| 373 | print $infodb_handle "$infodb_val\n";
|
---|
| 374 | print $infodb_handle '-' x 70, "\n";
|
---|
| 375 | }
|
---|
| 376 | ## write_infodb_rawentry(filehandle, string, string) ##
|
---|
| 377 |
|
---|
| 378 |
|
---|
| 379 | 1;
|
---|