[22841] | 1 |
|
---|
[2810] | 2 | ###########################################################################
|
---|
| 3 | #
|
---|
[17740] | 4 | # GreenstoneXMLPlugin.pm
|
---|
[2810] | 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) 2001 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 |
|
---|
[3540] | 27 | # Processes GreenstoneArchive XML documents. Note that this plugin does no
|
---|
| 28 | # syntax checking (though the XML::Parser module tests for
|
---|
| 29 | # well-formedness). It's assumed that the GreenstoneArchive files conform
|
---|
| 30 | # to their DTD.
|
---|
[2810] | 31 |
|
---|
[17740] | 32 | package GreenstoneXMLPlugin;
|
---|
[2810] | 33 |
|
---|
[22841] | 34 | use Encode;
|
---|
[23167] | 35 | use File::Basename;
|
---|
[22841] | 36 |
|
---|
[15872] | 37 | use ReadXMLFile;
|
---|
[27306] | 38 | use util;
|
---|
| 39 | use FileUtils;
|
---|
[2810] | 40 |
|
---|
[10254] | 41 | use strict;
|
---|
| 42 | no strict 'refs'; # allow filehandles to be variables and viceversa
|
---|
| 43 |
|
---|
[2810] | 44 | sub BEGIN {
|
---|
[17740] | 45 | @GreenstoneXMLPlugin::ISA = ('ReadXMLFile');
|
---|
[2810] | 46 | }
|
---|
| 47 |
|
---|
[7900] | 48 |
|
---|
[22841] | 49 |
|
---|
| 50 |
|
---|
[7900] | 51 | sub get_default_process_exp {
|
---|
| 52 | my $self = shift (@_);
|
---|
| 53 |
|
---|
[28265] | 54 | return q^(?i)doc(-\d+)?\.xml$^;
|
---|
[7900] | 55 | }
|
---|
| 56 |
|
---|
[14004] | 57 | my $arguments =
|
---|
| 58 | [ { 'name' => "process_exp",
|
---|
[31492] | 59 | 'desc' => "{BaseImporter.process_exp}",
|
---|
[14004] | 60 | 'type' => "regexp",
|
---|
| 61 | 'deft' => &get_default_process_exp(),
|
---|
| 62 | 'reqd' => "no" } ];
|
---|
[10254] | 63 |
|
---|
[17740] | 64 | my $options = { 'name' => "GreenstoneXMLPlugin",
|
---|
[17745] | 65 | 'desc' => "{GreenstoneXMLPlugin.desc}",
|
---|
[6408] | 66 | 'abstract' => "no",
|
---|
[14004] | 67 | 'inherits' => "yes",
|
---|
| 68 | 'args' => $arguments };
|
---|
[3540] | 69 |
|
---|
[2810] | 70 | sub new {
|
---|
[10218] | 71 | my ($class) = shift (@_);
|
---|
| 72 | my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
|
---|
| 73 | push(@$pluginlist, $class);
|
---|
[2810] | 74 |
|
---|
[15872] | 75 | push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
|
---|
| 76 | push(@{$hashArgOptLists->{"OptList"}},$options);
|
---|
[3540] | 77 |
|
---|
[15872] | 78 | my $self = new ReadXMLFile($pluginlist, $inputargs, $hashArgOptLists);
|
---|
[10218] | 79 |
|
---|
[2810] | 80 | $self->{'section'} = "";
|
---|
| 81 | $self->{'section_level'} = 0;
|
---|
| 82 | $self->{'metadata_name'} = "";
|
---|
| 83 | $self->{'metadata_value'} = "";
|
---|
| 84 | $self->{'content'} = "";
|
---|
[23167] | 85 | $self->{'metadata_read_store'} = {};
|
---|
[12844] | 86 |
|
---|
| 87 | # # Currently used to store information for previous values controls. In
|
---|
| 88 | # # the next contract I'll move to using information directly from Lucene.
|
---|
| 89 | # $self->{'sqlfh'} = 0;
|
---|
[7900] | 90 |
|
---|
[2810] | 91 | return bless $self, $class;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
[23167] | 94 |
|
---|
| 95 |
|
---|
| 96 |
|
---|
| 97 | sub metadata_read {
|
---|
| 98 | my $self = shift (@_);
|
---|
| 99 | my ($pluginfo, $base_dir, $file, $block_hash,
|
---|
| 100 | $extrametakeys, $extrametadata, $extrametafile,
|
---|
[23212] | 101 | $processor, $gli, $aux) = @_;
|
---|
[23167] | 102 |
|
---|
| 103 | my $outhandle = $self->{'outhandle'};
|
---|
| 104 |
|
---|
| 105 | # can we process this file??
|
---|
| 106 | my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
|
---|
[28267] | 107 |
|
---|
[23167] | 108 | return undef unless $self->can_process_this_file($filename_full_path);
|
---|
| 109 |
|
---|
| 110 | $file =~ s/^[\/\\]+//; # $file often begins with / so we'll tidy it up
|
---|
| 111 |
|
---|
[31480] | 112 | print $outhandle "GreenstoneXMLPlugin: setting up block list for $file\n"
|
---|
[23167] | 113 | if $self->{'verbosity'} > 1;
|
---|
| 114 |
|
---|
| 115 | my $line;
|
---|
| 116 | if (open(GIN,"<:utf8",$filename_full_path)) {
|
---|
| 117 |
|
---|
| 118 | while (defined($line=<GIN>)) {
|
---|
| 119 | if ($line =~ m@<Metadata\s+name="gsdlassocfile">([^:]*):(?:[^:]*):(?:[^:]*)</Metadata>@) {
|
---|
| 120 | my $gsdl_assoc_file = $1;
|
---|
| 121 |
|
---|
| 122 | my $dirname = dirname($filename_full_path);
|
---|
[27306] | 123 | my $full_gsdl_assoc_filename = &FileUtils::filenameConcatenate($dirname,$gsdl_assoc_file);
|
---|
[23167] | 124 | if ($self->{'verbosity'}>2) {
|
---|
| 125 | print $outhandle " Storing block list item: $full_gsdl_assoc_filename\n";
|
---|
| 126 | }
|
---|
| 127 |
|
---|
[31480] | 128 | # is this raw filename here, or unicode?? assuming unicode
|
---|
| 129 | # however we have concatenated raw directory???
|
---|
| 130 | $self->block_filename($block_hash,$full_gsdl_assoc_filename);
|
---|
[23167] | 131 | }
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | close(GIN);
|
---|
| 135 | }
|
---|
| 136 | else {
|
---|
| 137 |
|
---|
| 138 | print $outhandle "Error: Failed to open $file in GreenstoneXMLPlugin::metadata_read()\n";
|
---|
| 139 | print $outhandle " $!\n";
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 |
|
---|
| 143 | $self->{'metadata_read_store'}->{$filename_full_path} = 1;
|
---|
| 144 |
|
---|
| 145 | return 1;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 |
|
---|
[2810] | 149 | sub xml_start_document {
|
---|
[28267] | 150 |
|
---|
| 151 | my $self = shift(@_);
|
---|
| 152 |
|
---|
| 153 | my ($expat, $name, $sysid, $pubid, $internal) = @_;
|
---|
| 154 |
|
---|
| 155 | my $outhandle = $self->{'outhandle'};
|
---|
| 156 | print $outhandle "GreenstoneXMLPlugin: processing $self->{'file'}\n" if $self->{'verbosity'} > 1;
|
---|
| 157 | print STDERR "<Processing n='$self->{'file'}' p='GreenstoneXMLPlugin'>\n" if $self->{'gli'};
|
---|
| 158 |
|
---|
[2810] | 159 | }
|
---|
| 160 |
|
---|
| 161 | sub xml_end_document {
|
---|
| 162 | }
|
---|
| 163 |
|
---|
[13147] | 164 | sub get_doctype {
|
---|
| 165 | my $self = shift(@_);
|
---|
| 166 |
|
---|
| 167 | return "(Greenstone)?Archive";
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 |
|
---|
[2810] | 171 | sub xml_doctype {
|
---|
| 172 | my $self = shift(@_);
|
---|
[7900] | 173 |
|
---|
[2810] | 174 | my ($expat, $name, $sysid, $pubid, $internal) = @_;
|
---|
| 175 |
|
---|
[28267] | 176 | # Some doc.xml files that have been manipulated by XML::Rules
|
---|
| 177 | # no longer have the DOCTYPE line. No obvious way to fix
|
---|
| 178 | # the XML::Rules based code to keep DOCTYPE, so commenting
|
---|
| 179 | # out the code below to allow doc.xml files with DOCTYPE
|
---|
| 180 | # to be processed
|
---|
| 181 |
|
---|
[2925] | 182 | # allow the short-lived and badly named "GreenstoneArchive" files to be processed
|
---|
| 183 | # as well as the "Archive" files which should now be created by import.pl
|
---|
[28267] | 184 | ## die "" if ($name !~ /^(Greenstone)?Archive$/);
|
---|
[2925] | 185 |
|
---|
[28267] | 186 | # my $outhandle = $self->{'outhandle'};
|
---|
| 187 | # print $outhandle "GreenstoneXMLPlugin: processing $self->{'file'}\n" if $self->{'verbosity'} > 1;
|
---|
| 188 | # print STDERR "<Processing n='$self->{'file'}' p='GreenstoneXMLPlugin'>\n" if $self->{'gli'};
|
---|
[9468] | 189 |
|
---|
[2810] | 190 | }
|
---|
| 191 |
|
---|
| 192 |
|
---|
| 193 | sub xml_start_tag {
|
---|
| 194 | my $self = shift(@_);
|
---|
| 195 | my ($expat, $element) = @_;
|
---|
| 196 |
|
---|
| 197 | $self->{'element'} = $element;
|
---|
| 198 | if ($element eq "Section") {
|
---|
| 199 | if ($self->{'section_level'} == 0) {
|
---|
| 200 | $self->open_document();
|
---|
[7900] | 201 | } else {
|
---|
[2810] | 202 | my $doc_obj = $self->{'doc_obj'};
|
---|
| 203 | $self->{'section'} =
|
---|
| 204 | $doc_obj->insert_section($doc_obj->get_end_child($self->{'section'}));
|
---|
| 205 | }
|
---|
[7900] | 206 |
|
---|
[2810] | 207 | $self->{'section_level'} ++;
|
---|
| 208 | }
|
---|
| 209 | elsif ($element eq "Metadata") {
|
---|
| 210 | $self->{'metadata_name'} = $_{'name'};
|
---|
| 211 | }
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 | sub xml_end_tag {
|
---|
| 215 | my $self = shift(@_);
|
---|
| 216 | my ($expat, $element) = @_;
|
---|
| 217 |
|
---|
| 218 | if ($element eq "Section") {
|
---|
| 219 | $self->{'section_level'} --;
|
---|
| 220 | $self->{'section'} = $self->{'doc_obj'}->get_parent_section ($self->{'section'});
|
---|
| 221 | $self->close_document() if $self->{'section_level'} == 0;
|
---|
| 222 | }
|
---|
| 223 | elsif ($element eq "Metadata") {
|
---|
[22841] | 224 | # text read in by XML::Parser is in Perl's binary byte value
|
---|
| 225 | # form ... need to explicitly make it UTF-8
|
---|
| 226 |
|
---|
[23753] | 227 |
|
---|
[28836] | 228 | my $metadata_name = $self->{'metadata_name'};
|
---|
| 229 | my $metadata_value = $self->{'metadata_value'};
|
---|
| 230 | #my $metadata_name = decode("utf-8",$self->{'metadata_name'});
|
---|
| 231 | #my $metadata_value = decode("utf-8",$self->{'metadata_value'});
|
---|
[22841] | 232 |
|
---|
| 233 | $self->{'doc_obj'}->add_utf8_metadata($self->{'section'},
|
---|
| 234 | $metadata_name,$metadata_value);
|
---|
| 235 |
|
---|
[12844] | 236 | # Ensure this value is added to the allvalues database in gseditor.
|
---|
| 237 | # Note that the database constraints prevent multiple occurances of the
|
---|
| 238 | # same key-value pair.
|
---|
| 239 | # We write these out to a file, so they can all be commited in one
|
---|
| 240 | # transaction
|
---|
| 241 | #if (!$self->{'sqlfh'})
|
---|
| 242 | # {
|
---|
| 243 | # my $sql_file = $ENV{'GSDLHOME'} . "/collect/lld/tmp/gseditor.sql";
|
---|
| 244 | # # If the file doesn't already exist, open it and begin a transaction
|
---|
| 245 | # my $sql_fh;
|
---|
| 246 | # if (!-e $sql_file)
|
---|
| 247 | # {
|
---|
| 248 | # open($sql_fh, ">" . $sql_file);
|
---|
| 249 | # print $sql_fh "BEGIN TRANSACTION;\n";
|
---|
| 250 | # }
|
---|
| 251 | # else
|
---|
| 252 | # {
|
---|
| 253 | # open($sql_fh, ">>" . $sql_file);
|
---|
| 254 | # }
|
---|
| 255 | # print STDERR "Opened SQL log\n";
|
---|
| 256 | # $self->{'sqlfh'} = $sql_fh;
|
---|
| 257 | # }
|
---|
| 258 |
|
---|
| 259 | #my $mvalue = $self->{'metadata_value'};
|
---|
| 260 | #$mvalue =~ s/\'/\'\'/g;
|
---|
| 261 | #$mvalue =~ s/_claimantsep_/ \& /g;
|
---|
| 262 |
|
---|
| 263 | #my $fh = $self->{'sqlfh'};
|
---|
| 264 | #if ($fh)
|
---|
| 265 | # {
|
---|
| 266 | # print $fh "INSERT INTO allvalues (mkey, mvalue) VALUES ('" . $self->{'metadata_name'} . "', '" . $mvalue . "');\n";
|
---|
| 267 | # }
|
---|
| 268 |
|
---|
| 269 | # Clean Up
|
---|
[2810] | 270 | $self->{'metadata_name'} = "";
|
---|
| 271 | $self->{'metadata_value'} = "";
|
---|
| 272 | }
|
---|
| 273 | elsif ($element eq "Content" && $self->{'content'} ne "") {
|
---|
[22841] | 274 |
|
---|
| 275 | # text read in by XML::Parser is in Perl's binary byte value
|
---|
| 276 | # form ... need to explicitly make it UTF-8
|
---|
[28836] | 277 | #my $content = decode("utf-8",$self->{'content'});
|
---|
| 278 | my $content = $self->{'content'};
|
---|
[22841] | 279 |
|
---|
| 280 | $self->{'doc_obj'}->add_utf8_text($self->{'section'}, $content);
|
---|
[2810] | 281 | $self->{'content'} = "";
|
---|
| 282 | }
|
---|
| 283 | $self->{'element'} = "";
|
---|
| 284 | }
|
---|
| 285 |
|
---|
| 286 | sub xml_text {
|
---|
| 287 | my $self = shift(@_);
|
---|
| 288 | my ($expat) = @_;
|
---|
| 289 |
|
---|
| 290 | if ($self->{'element'} eq "Metadata") {
|
---|
| 291 | $self->{'metadata_value'} .= $_;
|
---|
| 292 | }
|
---|
| 293 | elsif ($self->{'element'} eq "Content") {
|
---|
| 294 | $self->{'content'} .= $_;
|
---|
| 295 | }
|
---|
| 296 | }
|
---|
| 297 |
|
---|
| 298 | sub open_document {
|
---|
| 299 | my $self = shift(@_);
|
---|
| 300 |
|
---|
[23167] | 301 | my $filename = $self->{'filename'};
|
---|
| 302 |
|
---|
[2810] | 303 | # create a new document
|
---|
[23167] | 304 | if (defined $self->{'metadata_read_store'}->{$filename}) {
|
---|
| 305 | # Being processed as part of *import* phase
|
---|
| 306 | # (i.e. was in import directory)
|
---|
| 307 | $self->SUPER::open_document(@_);
|
---|
| 308 | delete $self->{'metadata_read_store'}->{$filename};
|
---|
| 309 | }
|
---|
| 310 | else {
|
---|
| 311 | # Otherwise being processed as part of the *buildcol* phase
|
---|
| 312 | # (i.e. named directly by ArchiveInf plugin)
|
---|
| 313 | $self->{'doc_obj'} = new doc();
|
---|
| 314 | }
|
---|
| 315 |
|
---|
[2810] | 316 | $self->{'section'} = "";
|
---|
| 317 | }
|
---|
| 318 |
|
---|
| 319 | sub close_document {
|
---|
| 320 | my $self = shift(@_);
|
---|
[12844] | 321 |
|
---|
[2810] | 322 | # add the associated files
|
---|
| 323 | my $assoc_files =
|
---|
| 324 | $self->{'doc_obj'}->get_metadata($self->{'doc_obj'}->get_top_section(), "gsdlassocfile");
|
---|
| 325 |
|
---|
[3629] | 326 | # for when "assocfilepath" isn't the same directory that doc.xml is in...
|
---|
| 327 | my $assoc_filepath_list= $self->{'doc_obj'}->get_metadata($self->{'doc_obj'}->get_top_section(), "assocfilepath");
|
---|
[2810] | 328 |
|
---|
[3629] | 329 | my $assoc_filepath=shift (@$assoc_filepath_list);
|
---|
[12844] | 330 |
|
---|
| 331 | #rint STDERR "Filename is: " . $self->{'filename'} . "\n";
|
---|
| 332 | #rint STDERR "Initially my assoc_filepath is: $assoc_filepath\n";
|
---|
| 333 | #rint STDERR "Custom archive dir is: " . $self->{'base_dir'} . "\n";
|
---|
| 334 | # Correct the assoc filepath if one is defined
|
---|
| 335 | if (defined ($assoc_filepath))
|
---|
| 336 | {
|
---|
| 337 | # Check whether the assoc_filepath already includes the base dir
|
---|
| 338 | if (index($assoc_filepath, $self->{'base_dir'}) == -1)
|
---|
| 339 | {
|
---|
| 340 | # And if not, append it so as to make this absolute
|
---|
[27306] | 341 | $assoc_filepath = &FileUtils::filenameConcatenate($self->{'base_dir'}, $assoc_filepath);
|
---|
[12844] | 342 | }
|
---|
| 343 | }
|
---|
| 344 | else
|
---|
| 345 | {
|
---|
[3629] | 346 | $assoc_filepath = $self->{'filename'};
|
---|
| 347 | $assoc_filepath =~ s/[^\\\/]*$//;
|
---|
[12844] | 348 | }
|
---|
| 349 | #rint STDERR "Goned and made it absolute: $assoc_filepath\n";
|
---|
[3629] | 350 |
|
---|
[2810] | 351 | foreach my $assoc_file_info (@$assoc_files) {
|
---|
| 352 | my ($assoc_file, $mime_type, $dir) = split (":", $assoc_file_info);
|
---|
[12844] | 353 | #rint STDERR "assoc_file: $assoc_file\n";
|
---|
| 354 | #rint STDERR "mime_type: $mime_type\n";
|
---|
| 355 | #rint STDERR "dir: $dir\n";
|
---|
[27306] | 356 | my $real_dir = &FileUtils::filenameConcatenate($assoc_filepath, $assoc_file),
|
---|
[2810] | 357 | my $assoc_dir = (defined $dir && $dir ne "")
|
---|
[27306] | 358 | ? &FileUtils::filenameConcatenate($dir, $assoc_file) : $assoc_file;
|
---|
[2810] | 359 | $self->{'doc_obj'}->associate_file($real_dir, $assoc_dir, $mime_type);
|
---|
[12844] | 360 | #rint STDERR "According to me the real assoc_filepath is: $real_dir\n";
|
---|
[2810] | 361 | }
|
---|
| 362 | $self->{'doc_obj'}->delete_metadata($self->{'doc_obj'}->get_top_section(), "gsdlassocfile");
|
---|
| 363 |
|
---|
| 364 | # process the document
|
---|
| 365 | $self->{'processor'}->process($self->{'doc_obj'}, $self->{'file'});
|
---|
[23753] | 366 |
|
---|
| 367 | $self->{'num_processed'} ++;
|
---|
| 368 | undef $self->{'doc_obj'};
|
---|
[2810] | 369 | }
|
---|
| 370 |
|
---|
| 371 |
|
---|
| 372 | 1;
|
---|
[3540] | 373 |
|
---|
| 374 |
|
---|