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