[4429] | 1 | ###########################################################################
|
---|
| 2 | #
|
---|
[17720] | 3 | # DatabasePlugin.pm -- plugin to import records from a database
|
---|
[4429] | 4 | #
|
---|
| 5 | # A component of the Greenstone digital library software
|
---|
| 6 | # from the New Zealand Digital Library Project at the
|
---|
| 7 | # University of Waikato, New Zealand.
|
---|
| 8 | #
|
---|
| 9 | # Copyright (C) 2003 New Zealand Digital Library Project
|
---|
| 10 | #
|
---|
| 11 | # This program is free software; you can redistribute it and/or modify
|
---|
| 12 | # it under the terms of the GNU General Public License as published by
|
---|
| 13 | # the Free Software Foundation; either version 2 of the License, or
|
---|
| 14 | # (at your option) any later version.
|
---|
| 15 | #
|
---|
| 16 | # This program is distributed in the hope that it will be useful,
|
---|
| 17 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 18 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 19 | # GNU General Public License for more details.
|
---|
| 20 | #
|
---|
| 21 | # You should have received a copy of the GNU General Public License
|
---|
| 22 | # along with this program; if not, write to the Free Software
|
---|
| 23 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 24 | #
|
---|
| 25 | ###########################################################################
|
---|
| 26 |
|
---|
[12741] | 27 | # A plugin that imports records from a database. This uses perl's DBI module,
|
---|
| 28 | # which includes back-ends for mysql, postgresql, comma separated values (CSV),
|
---|
| 29 | # MS Excel, ODBC, sybase, etc... Extra modules may need to be installed to
|
---|
| 30 | # use this. See <GSDLHOME>/etc/packages/example.dbi for an example config file.
|
---|
[4429] | 31 | #
|
---|
| 32 |
|
---|
| 33 | # Written by John McPherson for the NZDL project
|
---|
| 34 | # Mar, Apr 2003
|
---|
| 35 |
|
---|
[17720] | 36 | package DatabasePlugin;
|
---|
[4429] | 37 |
|
---|
[7683] | 38 | use strict;
|
---|
| 39 | no strict 'refs'; # allow variable as a filehandle
|
---|
| 40 |
|
---|
[15872] | 41 | use AutoExtractMetadata;
|
---|
[24548] | 42 | use MetadataRead;
|
---|
[4429] | 43 | use unicode;
|
---|
| 44 |
|
---|
| 45 | sub BEGIN {
|
---|
[24548] | 46 | @DatabasePlugin::ISA = ('MetadataRead', 'AutoExtractMetadata');
|
---|
[4429] | 47 | }
|
---|
| 48 |
|
---|
[4744] | 49 | my $arguments =
|
---|
| 50 | [ { 'name' => "process_exp",
|
---|
[31492] | 51 | 'desc' => "{BaseImporter.process_exp}",
|
---|
[6408] | 52 | 'type' => "regexp",
|
---|
[4744] | 53 | 'deft' => &get_default_process_exp(),
|
---|
| 54 | 'reqd' => "no" }];
|
---|
[4429] | 55 |
|
---|
[17720] | 56 | my $options = { 'name' => "DatabasePlugin",
|
---|
[17744] | 57 | 'desc' => "{DatabasePlugin.desc}",
|
---|
[6408] | 58 | 'abstract' => "no",
|
---|
[4744] | 59 | 'inherits' => "yes",
|
---|
| 60 | 'args' => $arguments };
|
---|
[4429] | 61 |
|
---|
| 62 | sub new {
|
---|
[10218] | 63 | my ($class) = shift (@_);
|
---|
| 64 | my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
|
---|
| 65 | push(@$pluginlist, $class);
|
---|
[4429] | 66 |
|
---|
[15872] | 67 | push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
|
---|
| 68 | push(@{$hashArgOptLists->{"OptList"}},$options);
|
---|
[4429] | 69 |
|
---|
[15872] | 70 | my $self = new AutoExtractMetadata($pluginlist, $inputargs, $hashArgOptLists);
|
---|
[4429] | 71 |
|
---|
| 72 | return bless $self, $class;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | sub get_default_process_exp {
|
---|
| 76 | my $self = shift (@_);
|
---|
| 77 |
|
---|
| 78 | return q^(?i)\.dbi$^;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | sub read {
|
---|
| 82 | my $self = shift (@_);
|
---|
[16392] | 83 | my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $processor, $maxdocs,$total_count,$gli) = @_;
|
---|
[13315] | 84 |
|
---|
| 85 | #see if we can handle the passed file...
|
---|
[16392] | 86 | my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
|
---|
| 87 | return undef unless $self->can_process_this_file($filename_full_path);
|
---|
| 88 |
|
---|
[4429] | 89 | my $outhandle = $self->{'outhandle'};
|
---|
| 90 | my $verbosity = $self->{'verbosity'};
|
---|
| 91 |
|
---|
[17720] | 92 | print STDERR "<Processing n='$file' p='DatabasePlugin'>\n" if ($gli);
|
---|
| 93 | print $outhandle "DatabasePlugin: processing $file\n"
|
---|
[4429] | 94 | if $self->{'verbosity'} > 1;
|
---|
[13315] | 95 |
|
---|
[12687] | 96 | require DBI; # database independent stuff
|
---|
| 97 |
|
---|
[4429] | 98 | # calculate the document hash, for document ids
|
---|
[13315] | 99 | my $hash="0";
|
---|
[4429] | 100 |
|
---|
[13315] | 101 |
|
---|
[4429] | 102 | # default options - may be overridden by config file
|
---|
| 103 | my $language=undef;
|
---|
| 104 | my $encoding=undef;
|
---|
| 105 | my $dbplug_debug=0;
|
---|
| 106 | my $username='';
|
---|
| 107 | my $password='';
|
---|
| 108 |
|
---|
| 109 | # these settings must be set by the config file:
|
---|
| 110 | my $db=undef;
|
---|
| 111 |
|
---|
[15872] | 112 | # get id of pages from "nonempty", get latest version number from
|
---|
| 113 | # "recent", and then get pagename from "page" and content from "version" !
|
---|
[4429] | 114 |
|
---|
[10923] | 115 | my $sql_query_prime = undef ;
|
---|
[4429] | 116 | my $sql_query = undef ;
|
---|
| 117 |
|
---|
| 118 | my %db_to_greenstone_fields=();
|
---|
[8119] | 119 | my %callbacks=();
|
---|
[4429] | 120 |
|
---|
[13315] | 121 |
|
---|
[4429] | 122 | # read in config file.
|
---|
[16392] | 123 | if (!open (CONF, $filename_full_path)) {
|
---|
[17720] | 124 | print $outhandle "DatabasePlugin: can't read $filename_full_path: $!\n";
|
---|
[7681] | 125 | return 0;
|
---|
[13315] | 126 | }
|
---|
| 127 |
|
---|
[7683] | 128 | my $line;
|
---|
| 129 | my $statement="";
|
---|
| 130 | my $callback="";
|
---|
| 131 | while (defined($line=<CONF>)) {
|
---|
| 132 | chomp $line;
|
---|
[10923] | 133 | $line .= " "; # for multi-line statements - don't conjoin!
|
---|
[8119] | 134 | $line =~ s/\s*\#.*$//mg; # remove comments
|
---|
[7683] | 135 | $statement .= $line;
|
---|
[8119] | 136 |
|
---|
| 137 | if ($line =~ /^\}\s*$/ && $callback) { # ends the callback
|
---|
| 138 | $callback .= $statement ; $statement = "";
|
---|
| 139 | # try to check that the function is "safe"
|
---|
| 140 | if ($callback =~ /\b(?:system|open|pipe|readpipe|qx|kill|eval|do|use|require|exec|fork)\b/ ||
|
---|
| 141 | $callback =~ /[\`]|\|\-/) {
|
---|
| 142 | # no backticks or functions that start new processes allowed
|
---|
[17720] | 143 | print $outhandle "DatabasePlugin: bad function in callback\n";
|
---|
[8119] | 144 | return 0;
|
---|
| 145 | }
|
---|
| 146 | $callback =~ s/sub (\w+?)_callback/sub/;
|
---|
[10956] | 147 | my $fieldname = $1;
|
---|
| 148 | my $ret = eval "\$callbacks{'$fieldname'} = $callback ; 1";
|
---|
| 149 | if (!defined($ret)) {
|
---|
[17720] | 150 | print $outhandle "DatabasePlugin: error eval'ing callback: $@\n";
|
---|
[10956] | 151 | exit(1);
|
---|
| 152 | }
|
---|
[8119] | 153 | $callback="";
|
---|
[17720] | 154 | print $outhandle "DatabasePlugin: callback registered for '$fieldname'\n"
|
---|
[10956] | 155 | if $dbplug_debug;
|
---|
[8119] | 156 | } elsif ($callback) {
|
---|
| 157 | # add this line to the callback function
|
---|
| 158 | $callback .= $statement;
|
---|
| 159 | $statement = "";
|
---|
| 160 | } elsif ($statement =~ m/;\s*$/) { # ends with ";"
|
---|
[7683] | 161 | # check that it is safe
|
---|
| 162 | # assignment
|
---|
[8119] | 163 | if ($statement =~ m~(\$\w+)\s* = \s*
|
---|
[7683] | 164 | (\d # digits
|
---|
| 165 | | ".*?(?<!\\)" # " up to the next " not preceded by a \
|
---|
| 166 | | '.*?(?<!\\)' # ' up to the next ' not preceded by a \
|
---|
[8119] | 167 | )\s*;~x || # /x means ignore comments and whitespace in rx
|
---|
| 168 | $statement =~ m~(\%\w+)\s*=\s*(\([\w\s\"\',:=>]+\))\s*;~ ) {
|
---|
| 169 | # evaluate the assignment, return 1 on success "
|
---|
| 170 | if (!eval "$1=$2; 1") {
|
---|
[7683] | 171 | my $err=$@;
|
---|
| 172 | chomp $err;
|
---|
| 173 | $err =~ s/\.$//; # remove a trailing .
|
---|
[17720] | 174 | print $outhandle "DatabasePlugin: error evaluating `$statement'\n";
|
---|
[16392] | 175 | print $outhandle " $err (in $filename_full_path)\n";
|
---|
[7683] | 176 | return 0; # there was an error reading the config file
|
---|
| 177 | }
|
---|
[8119] | 178 | } elsif ($statement =~ /sub \w+_callback/) {
|
---|
| 179 | # this is the start of a callback function definition
|
---|
[7683] | 180 | $callback = $statement;
|
---|
[8119] | 181 | $statement = "";
|
---|
| 182 | } else {
|
---|
[17720] | 183 | print $outhandle "DatabasePlugin: skipping statement `$statement'\n";
|
---|
[7683] | 184 | }
|
---|
| 185 | $statement = "";
|
---|
| 186 | }
|
---|
| 187 | }
|
---|
[7681] | 188 | close CONF;
|
---|
[4429] | 189 |
|
---|
[13315] | 190 |
|
---|
[4429] | 191 | if (!defined($db)) {
|
---|
[17720] | 192 | print $outhandle "DatabasePlugin: error: $filename_full_path does not specify a db!\n";
|
---|
[4429] | 193 | return 0;
|
---|
| 194 | }
|
---|
| 195 | if (!defined($sql_query)) {
|
---|
[17720] | 196 | print $outhandle "DatabasePlugin: error: no SQL query specified!\n";
|
---|
[4429] | 197 | return 0;
|
---|
| 198 | }
|
---|
| 199 | # connect to database
|
---|
| 200 | my $dbhandle=DBI->connect($db, $username, $password);
|
---|
| 201 |
|
---|
| 202 | if (!defined($dbhandle)) {
|
---|
[17720] | 203 | die "DatabasePlugin: could not connect to database, exiting.\n";
|
---|
[4429] | 204 | }
|
---|
| 205 | if (defined($dbplug_debug) && $dbplug_debug==1) {
|
---|
[17720] | 206 | print $outhandle "DatabasePlugin (debug): connected ok\n";
|
---|
[4429] | 207 | }
|
---|
| 208 |
|
---|
[10923] | 209 | my $statement_hand;
|
---|
| 210 |
|
---|
| 211 | # The user gave 2 sql statements to execute?
|
---|
| 212 | if ($sql_query_prime) {
|
---|
| 213 | $statement_hand=$dbhandle->prepare($sql_query_prime);
|
---|
| 214 | $statement_hand->execute;
|
---|
| 215 | if ($statement_hand->err) {
|
---|
| 216 | print $outhandle "Error: " . $statement_hand->errstr . "\n";
|
---|
| 217 | return undef;
|
---|
| 218 | }
|
---|
| 219 | }
|
---|
| 220 |
|
---|
[13315] | 221 |
|
---|
[10923] | 222 | $statement_hand=$dbhandle->prepare($sql_query);
|
---|
[4429] | 223 | $statement_hand->execute;
|
---|
[10923] | 224 | if ($statement_hand->err) {
|
---|
[13315] | 225 | print $outhandle "Error:" . $statement_hand->errstr . "\n";
|
---|
[10923] | 226 | return undef;
|
---|
| 227 | }
|
---|
[4429] | 228 |
|
---|
| 229 | # get the array-ref for the field names and cast it to array
|
---|
| 230 | my @field_names;
|
---|
| 231 | @field_names=@{ $statement_hand->{NAME} };
|
---|
| 232 |
|
---|
| 233 | foreach my $fieldname (@field_names) {
|
---|
| 234 | if (defined($db_to_greenstone_fields{$fieldname})) {
|
---|
| 235 | if (defined($dbplug_debug) && $dbplug_debug==1) {
|
---|
[17720] | 236 | print $outhandle "DatabasePlugin (debug): mapping db field "
|
---|
[4429] | 237 | . "'$fieldname' to "
|
---|
| 238 | . $db_to_greenstone_fields{$fieldname} . "\n";
|
---|
| 239 | }
|
---|
| 240 | $fieldname=$db_to_greenstone_fields{$fieldname};
|
---|
| 241 | }
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 | # get rows
|
---|
| 245 |
|
---|
| 246 | my $count = 0;
|
---|
| 247 | my @row_array;
|
---|
| 248 |
|
---|
| 249 | @row_array=$statement_hand->fetchrow_array; # fetchrow_hashref?
|
---|
[17026] | 250 |
|
---|
| 251 | my $base_oid = undef;
|
---|
[4429] | 252 | while (scalar(@row_array)) {
|
---|
| 253 | if (defined($dbplug_debug) && $dbplug_debug==1) {
|
---|
[17720] | 254 | print $outhandle "DatabasePlugin (debug): retrieved a row from query\n";
|
---|
[4429] | 255 | }
|
---|
| 256 |
|
---|
| 257 | # create a new document
|
---|
[18327] | 258 | my $doc_obj = new doc ($filename_full_path, "indexed_doc", $self->{'file_rename_method'});
|
---|
[17026] | 259 |
|
---|
[4429] | 260 | my $cursection = $doc_obj->get_top_section();
|
---|
| 261 |
|
---|
[31492] | 262 | # if $language not set in config file, will use BaseImporter's default
|
---|
[4429] | 263 | if (defined($language)) {
|
---|
| 264 | $doc_obj->add_utf8_metadata($cursection, "Language", $language);
|
---|
| 265 | }
|
---|
[31492] | 266 | # if $encoding not set in config file, will use BaseImporter's default
|
---|
[4429] | 267 | if (defined($encoding)) {
|
---|
[7701] | 268 | # allow some common aliases
|
---|
| 269 | if ($encoding =~ m/^utf[-_]8$/i) {$encoding="utf8"}
|
---|
| 270 | $encoding =~ s/-/_/g; # greenstone uses eg iso_8859_1
|
---|
[4429] | 271 | $doc_obj->add_utf8_metadata($cursection, "Encoding", $encoding);
|
---|
| 272 | }
|
---|
[15872] | 273 |
|
---|
[23349] | 274 | my $plugin_filename_encoding = $self->{'filename_encoding'};
|
---|
[23352] | 275 | my $filename_encoding = $self->deduce_filename_encoding($file,$metadata,$plugin_filename_encoding);
|
---|
| 276 | $self->set_Source_metadata($doc_obj, $filename_full_path, $filename_encoding);
|
---|
[23349] | 277 |
|
---|
[4429] | 278 | if ($self->{'cover_image'}) {
|
---|
[16392] | 279 | $self->associate_cover_image($doc_obj, $filename_full_path);
|
---|
[4429] | 280 | }
|
---|
[7508] | 281 | $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "Plugin", "$self->{'plugin_type'}");
|
---|
[4429] | 282 |
|
---|
[8121] | 283 | $doc_obj->add_metadata($doc_obj->get_top_section(), "FileFormat", "DB");
|
---|
[5919] | 284 |
|
---|
[4429] | 285 | # include any metadata passed in from previous plugins
|
---|
| 286 | # note that this metadata is associated with the top level section
|
---|
| 287 | $self->extra_metadata ($doc_obj, $cursection,
|
---|
| 288 | $metadata);
|
---|
| 289 |
|
---|
| 290 | # do any automatic metadata extraction
|
---|
| 291 | $self->auto_extract_metadata ($doc_obj);
|
---|
| 292 |
|
---|
| 293 | my $unique_id=undef;
|
---|
| 294 |
|
---|
| 295 | foreach my $fieldname (@field_names) {
|
---|
| 296 | my $fielddata=shift @row_array;
|
---|
[10923] | 297 |
|
---|
| 298 | if (! defined($fielddata) ) {
|
---|
| 299 | next; # this field was "" or NULL
|
---|
| 300 | }
|
---|
[4429] | 301 | # use the specified encoding, defaulting to utf-8
|
---|
| 302 | if (defined($encoding) && $encoding ne "ascii"
|
---|
| 303 | && $encoding ne "utf8") {
|
---|
| 304 | $fielddata=&unicode::unicode2utf8(
|
---|
| 305 | &unicode::convert2unicode($encoding, \$fielddata)
|
---|
| 306 | );
|
---|
| 307 | }
|
---|
[8119] | 308 | # see if we have a ****_callback() function defined
|
---|
| 309 | if (exists $callbacks{$fieldname}) {
|
---|
| 310 | my $funcptr = $callbacks{$fieldname};
|
---|
| 311 | $fielddata = &$funcptr($fielddata);
|
---|
| 312 | }
|
---|
| 313 |
|
---|
[4429] | 314 | if ($fieldname eq "text") {
|
---|
| 315 | # add as document text
|
---|
| 316 | $fielddata=~s@<@<@g;
|
---|
| 317 | $fielddata=~s@>@>@g; # for xml protection...
|
---|
| 318 | $fielddata=~s@_@\\_@g; # for macro language protection...
|
---|
| 319 | $doc_obj->add_utf8_text($cursection, $fielddata);
|
---|
| 320 | } elsif ($fieldname eq "Identifier") {
|
---|
| 321 | # use as greenstone's unique record id
|
---|
| 322 | if ($fielddata =~ /^\d+$/) {
|
---|
| 323 | # don't allow IDs that are completely numeric
|
---|
| 324 | $unique_id="id" . $fielddata;
|
---|
| 325 | } else {
|
---|
| 326 | $unique_id=$fielddata;
|
---|
| 327 | }
|
---|
| 328 | } else {
|
---|
| 329 | # add as document metadata
|
---|
| 330 | $fielddata=~s/\[/[/g;
|
---|
| 331 | $fielddata=~s/\]/]/g;
|
---|
| 332 | $doc_obj->add_utf8_metadata($cursection,
|
---|
| 333 | $fieldname, $fielddata);
|
---|
| 334 |
|
---|
| 335 | }
|
---|
| 336 | }
|
---|
| 337 |
|
---|
[13315] | 338 |
|
---|
[4429] | 339 | if (!defined $unique_id) {
|
---|
[17026] | 340 | if (!defined $base_oid) {
|
---|
| 341 | $self->add_OID($doc_obj);
|
---|
| 342 | $base_oid = $doc_obj->get_OID();
|
---|
| 343 | }
|
---|
| 344 | $doc_obj->set_OID($base_oid."s$count");
|
---|
[4429] | 345 | } else {
|
---|
| 346 | # use our id from the database...
|
---|
| 347 | $doc_obj->set_OID($unique_id);
|
---|
| 348 | }
|
---|
| 349 |
|
---|
| 350 |
|
---|
| 351 | # process the document
|
---|
| 352 | $processor->process($doc_obj);
|
---|
| 353 |
|
---|
| 354 | $count++;
|
---|
| 355 |
|
---|
| 356 | # get next row
|
---|
| 357 | @row_array=$statement_hand->fetchrow_array; # fetchrow_hashref?
|
---|
| 358 | } # end of row_array is not empty
|
---|
| 359 |
|
---|
| 360 | # check "$sth->err" if empty array for error
|
---|
| 361 | if ($statement_hand->err) {
|
---|
[17720] | 362 | print $outhandle "DatabasePlugin: received error: \"" .
|
---|
[7685] | 363 | $statement_hand->errstr . "\"\n";
|
---|
[4429] | 364 | }
|
---|
| 365 |
|
---|
| 366 | # clean up connection to database
|
---|
| 367 | $statement_hand->finish();
|
---|
| 368 | $dbhandle->disconnect();
|
---|
| 369 |
|
---|
| 370 | # num of input files, rather than documents created?
|
---|
| 371 | $self->{'num_processed'}++;
|
---|
| 372 |
|
---|
[8119] | 373 | if (defined($dbplug_debug) && $dbplug_debug==1) {
|
---|
[17720] | 374 | print $outhandle "DatabasePlugin: imported $count DB records as documents.\n";
|
---|
[8119] | 375 | }
|
---|
| 376 | $count;
|
---|
[4429] | 377 | }
|
---|
| 378 |
|
---|
| 379 | 1;
|
---|