[11783] | 1 | ###########################################################################
|
---|
| 2 | #
|
---|
[22086] | 3 | # OAIDownload.pm -- base class for all the import plugins
|
---|
[11783] | 4 | # A component of the Greenstone digital library software
|
---|
| 5 | # from the New Zealand Digital Library Project at the
|
---|
| 6 | # University of Waikato, New Zealand.
|
---|
| 7 | #
|
---|
| 8 | # Copyright (C) 1999 New Zealand Digital Library Project
|
---|
| 9 | #
|
---|
| 10 | # This program is free software; you can redistribute it and/or modify
|
---|
| 11 | # it under the terms of the GNU General Public License as published by
|
---|
| 12 | # the Free Software Foundation; either version 2 of the License, or
|
---|
| 13 | # (at your option) any later version.
|
---|
| 14 | #
|
---|
| 15 | # This program is distributed in the hope that it will be useful,
|
---|
| 16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 18 | # GNU General Public License for more details.
|
---|
| 19 | #
|
---|
| 20 | # You should have received a copy of the GNU General Public License
|
---|
| 21 | # along with this program; if not, write to the Free Software
|
---|
| 22 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 23 | #
|
---|
| 24 | ###########################################################################
|
---|
| 25 |
|
---|
| 26 | package OAIDownload;
|
---|
| 27 |
|
---|
| 28 | eval {require bytes};
|
---|
| 29 |
|
---|
| 30 | # suppress the annoying "subroutine redefined" warning that various
|
---|
| 31 | # plugins cause under perl 5.6
|
---|
| 32 | $SIG{__WARN__} = sub {warn($_[0]) unless ($_[0] =~ /Subroutine\s+\S+\sredefined/)};
|
---|
| 33 |
|
---|
| 34 | use strict;
|
---|
| 35 |
|
---|
| 36 | use WgetDownload;
|
---|
| 37 | use XMLParser;
|
---|
| 38 |
|
---|
| 39 | use POSIX qw(tmpnam);
|
---|
[12465] | 40 | use util;
|
---|
[11783] | 41 |
|
---|
| 42 | sub BEGIN {
|
---|
| 43 | @OAIDownload::ISA = ('WgetDownload');
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | my $arguments =
|
---|
| 47 | [ { 'name' => "url",
|
---|
| 48 | 'disp' => "{OAIDownload.url_disp}",
|
---|
| 49 | 'desc' => "{OAIDownload.url}",
|
---|
| 50 | 'type' => "string",
|
---|
| 51 | 'reqd' => "yes"},
|
---|
[14941] | 52 | { 'name' => "metadata_prefix",
|
---|
| 53 | 'disp' => "{OAIDownload.metadata_prefix_disp}",
|
---|
| 54 | 'desc' => "{OAIDownload.metadata_prefix}",
|
---|
| 55 | 'type' => "string",
|
---|
| 56 | 'deft' => "oai_dc",
|
---|
| 57 | 'reqd' => "no"},
|
---|
[11783] | 58 | { 'name' => "set",
|
---|
| 59 | 'disp' => "{OAIDownload.set_disp}",
|
---|
| 60 | 'desc' => "{OAIDownload.set}",
|
---|
| 61 | 'type' => "string",
|
---|
| 62 | 'reqd' => "no"},
|
---|
| 63 | { 'name' => "get_doc",
|
---|
[12465] | 64 | 'disp' => "{OAIDownload.get_doc_disp}",
|
---|
[11783] | 65 | 'desc' => "{OAIDownload.get_doc}",
|
---|
| 66 | 'type' => "flag",
|
---|
| 67 | 'reqd' => "no"},
|
---|
[16791] | 68 | { 'name' => "get_doc_exts",
|
---|
| 69 | 'disp' => "{OAIDownload.get_doc_exts_disp}",
|
---|
| 70 | 'desc' => "{OAIDownload.get_doc_exts}",
|
---|
| 71 | 'type' => "string",
|
---|
| 72 | 'deft' => "doc,pdf,ppt",
|
---|
| 73 | 'reqd' => "no"},
|
---|
[11783] | 74 | { 'name' => "max_records",
|
---|
| 75 | 'disp' => "{OAIDownload.max_records_disp}",
|
---|
| 76 | 'desc' => "{OAIDownload.max_records}",
|
---|
| 77 | 'type' => "int",
|
---|
| 78 | 'range' => "1,",
|
---|
| 79 | 'reqd' => "no"} ];
|
---|
| 80 |
|
---|
| 81 | my $options = { 'name' => "OAIDownload",
|
---|
| 82 | 'desc' => "{OAIDownload.desc}",
|
---|
| 83 | 'abstract' => "no",
|
---|
| 84 | 'inherits' => "yes",
|
---|
| 85 | 'args' => $arguments };
|
---|
| 86 |
|
---|
| 87 | sub new
|
---|
| 88 | {
|
---|
| 89 | my ($class) = shift (@_);
|
---|
| 90 | my ($getlist,$inputargs,$hashArgOptLists) = @_;
|
---|
| 91 | push(@$getlist, $class);
|
---|
| 92 |
|
---|
[17207] | 93 | push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
|
---|
| 94 | push(@{$hashArgOptLists->{"OptList"}},$options);
|
---|
[11783] | 95 |
|
---|
[17207] | 96 | my $self = new WgetDownload($getlist,$inputargs,$hashArgOptLists);
|
---|
[11783] | 97 |
|
---|
| 98 | if ($self->{'info_only'}) {
|
---|
| 99 | # don't worry about any options etc
|
---|
| 100 | return bless $self, $class;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | my $parser = new XML::Parser('Style' => 'Stream',
|
---|
[16791] | 104 | 'PluginObj' => $self,
|
---|
[11783] | 105 | 'Handlers' => {'Char' => \&Char,
|
---|
| 106 | 'Start' => \&OAI_StartTag,
|
---|
| 107 | 'End' => \&OAI_EndTag
|
---|
| 108 | });
|
---|
| 109 | $self->{'parser'} = $parser;
|
---|
| 110 |
|
---|
[13961] | 111 | # make sure the tmp directory that we will use later exists
|
---|
| 112 | my $tmp_dir = "$ENV{GSDLHOME}/tmp";
|
---|
| 113 | if (! -e $tmp_dir) {
|
---|
[28250] | 114 | &FileUtils::makeDirectory($tmp_dir);
|
---|
[13961] | 115 | }
|
---|
[16791] | 116 |
|
---|
[25224] | 117 | # if max_records not specified, parsing will have set it to ""
|
---|
| 118 | undef $self->{'max_records'} if $self->{'max_records'} eq "";
|
---|
| 119 |
|
---|
[16791] | 120 | # set up hashmap for individual items in get_doc_exts
|
---|
| 121 | # to make testing for matches easier
|
---|
| 122 |
|
---|
| 123 | $self->{'lookup_exts'} = {};
|
---|
| 124 | my $get_doc_exts = $self->{'get_doc_exts'};
|
---|
[25224] | 125 |
|
---|
[16791] | 126 | if ((defined $get_doc_exts) && ($get_doc_exts ne "")) {
|
---|
| 127 | my @exts = split(/,\s*/,$get_doc_exts);
|
---|
| 128 | foreach my $e (@exts) {
|
---|
| 129 | $self->{'lookup_exts'}->{lc($e)} = 1;
|
---|
| 130 | }
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 |
|
---|
[11783] | 134 | return bless $self, $class;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | sub download
|
---|
| 138 | {
|
---|
| 139 | my ($self) = shift (@_);
|
---|
| 140 | my ($hashGeneralOptions) = @_;
|
---|
| 141 |
|
---|
[16791] | 142 | ## my $cmdWget = $strWgetOptions;
|
---|
[12465] | 143 |
|
---|
| 144 | my $strOutputDir ="";
|
---|
| 145 | $strOutputDir = $hashGeneralOptions->{"cache_dir"};
|
---|
[11783] | 146 | my $strBasURL = $self->{'url'};
|
---|
| 147 | my $blnDownloadDoc = $self->{'get_doc'};
|
---|
| 148 |
|
---|
| 149 | print STDERR "<<Defined Maximum>>\n";
|
---|
[12465] | 150 |
|
---|
| 151 | my $strIDs = $self->getOAIIDs($strBasURL);
|
---|
| 152 |
|
---|
[16791] | 153 | if($strIDs eq "")
|
---|
[11783] | 154 | {
|
---|
[16791] | 155 | print STDERR "Error: No IDs found\n";
|
---|
[11783] | 156 | return 0;
|
---|
| 157 | }
|
---|
[16791] | 158 |
|
---|
[12465] | 159 | my $aryIDs = $self->parseOAIIDs($strIDs);
|
---|
[11783] | 160 | my $intIDs = 0;
|
---|
[25224] | 161 | if(defined $self->{'max_records'} && $self->{'max_records'} < scalar(@$aryIDs))
|
---|
[11783] | 162 | {
|
---|
| 163 | $intIDs = $self->{'max_records'};
|
---|
| 164 | }
|
---|
| 165 | else
|
---|
| 166 | {
|
---|
| 167 | $intIDs = scalar(@$aryIDs);
|
---|
| 168 | }
|
---|
| 169 | print STDERR "<<Total number of record(s):$intIDs>>\n";
|
---|
| 170 |
|
---|
[25224] | 171 | $self->getOAIRecords($aryIDs, $strOutputDir, $strBasURL, $self->{'max_records'}, $blnDownloadDoc);
|
---|
[11783] | 172 |
|
---|
[28250] | 173 | # my $tmp_file = &FileUtils::filenameConcatenate($ENV{'GSDLHOME'},"tmp","oai.tmp");
|
---|
| 174 | # &FileUtils::removeFiles($tmp_file);
|
---|
[12465] | 175 |
|
---|
[11783] | 176 | return 1;
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | sub getOAIIDs
|
---|
| 180 | {
|
---|
| 181 | my ($self,$strBasURL) = @_;
|
---|
[16791] | 182 | ## my ($cmdWget);
|
---|
[12465] | 183 |
|
---|
| 184 | my $wgetOptions = $self->getWgetOptions();
|
---|
| 185 |
|
---|
[16791] | 186 | my $cmdWget = $wgetOptions;
|
---|
[12465] | 187 |
|
---|
[11783] | 188 | print STDERR "Gathering OAI identifiers.....\n";
|
---|
[25224] | 189 |
|
---|
[14941] | 190 | my $metadata_prefix = $self->{'metadata_prefix'};
|
---|
| 191 | $cmdWget .= " -q -O - \"$strBasURL?verb=ListIdentifiers&metadataPrefix=$metadata_prefix";
|
---|
[12465] | 192 |
|
---|
[14941] | 193 | # if $set specified, add it in to URL
|
---|
| 194 | my $set = $self->{'set'};
|
---|
| 195 | $cmdWget .= "&set=$set" if ($set ne "");
|
---|
| 196 |
|
---|
| 197 | $cmdWget .= "\" ";
|
---|
| 198 |
|
---|
[14948] | 199 | my $accumulated_strIDs = "";
|
---|
[12465] | 200 | my $strIDs = $self->useWget($cmdWget);
|
---|
| 201 |
|
---|
| 202 | if (!defined $strIDs or $strIDs eq "" ){
|
---|
| 203 | print STDERR "Server information is unavailable.\n";
|
---|
| 204 | print STDERR "<<Finished>>\n";
|
---|
| 205 | return;
|
---|
| 206 | }
|
---|
[17549] | 207 | if ($self->{'forced_quit'}) {
|
---|
| 208 | return $strIDs;
|
---|
| 209 | }
|
---|
[12465] | 210 |
|
---|
| 211 | print STDERR "<<Download Information>>\n";
|
---|
[14948] | 212 |
|
---|
[12465] | 213 | $self->parse_xml($strIDs);
|
---|
| 214 |
|
---|
[14948] | 215 | $accumulated_strIDs = $strIDs;
|
---|
[25224] | 216 | my $max_recs = $self->{'max_records'};
|
---|
[25199] | 217 | while ($strIDs =~ m/<resumptionToken.*?>\s*(.*?)\s*<\/resumptionToken>/s) {
|
---|
[14948] | 218 | # top up list with further requests for IDs
|
---|
| 219 |
|
---|
| 220 | my $resumption_token = $1;
|
---|
| 221 |
|
---|
| 222 | $cmdWget = $wgetOptions;
|
---|
| 223 |
|
---|
[16725] | 224 | $cmdWget .= " -q -O - \"$strBasURL?verb=ListIdentifiers&resumptionToken=$resumption_token\"";
|
---|
[14948] | 225 |
|
---|
| 226 | $strIDs = $self->useWget($cmdWget);
|
---|
[17549] | 227 | if ($self->{'forced_quit'}) {
|
---|
| 228 | return $accumulated_strIDs;
|
---|
| 229 | }
|
---|
[14948] | 230 |
|
---|
| 231 | $self->parse_xml($strIDs);
|
---|
| 232 |
|
---|
| 233 | $accumulated_strIDs .= $strIDs;
|
---|
| 234 |
|
---|
| 235 | my @accumulated_identifiers
|
---|
| 236 | = ($accumulated_strIDs =~ m/<identifier>(.*?)<\/identifier>/sg);
|
---|
| 237 |
|
---|
| 238 | my $num_acc_identifiers = scalar(@accumulated_identifiers);
|
---|
[25224] | 239 | if (defined $max_recs && $num_acc_identifiers > $max_recs ) {
|
---|
[14948] | 240 | last;
|
---|
| 241 | }
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 | return $accumulated_strIDs;
|
---|
[11783] | 245 | }
|
---|
| 246 |
|
---|
| 247 | sub parseOAIIDs
|
---|
| 248 | {
|
---|
[12465] | 249 | my ($self,$strIDs) = @_;
|
---|
[11783] | 250 |
|
---|
| 251 | print STDERR "Parsing OAI identifiers.....\n";
|
---|
| 252 | $strIDs =~ s/^.*?<identifier>/<identifier>/s;
|
---|
| 253 | $strIDs =~ s/^(.*<\/identifier>).*$/$1/s;
|
---|
| 254 |
|
---|
| 255 | my @aryIDs = ();
|
---|
| 256 |
|
---|
| 257 | while ($strIDs =~ m/<identifier>(.*?)<\/identifier>(.*)$/s)
|
---|
| 258 | {
|
---|
| 259 | $strIDs = $2;
|
---|
| 260 | push(@aryIDs,$1);
|
---|
| 261 | }
|
---|
| 262 |
|
---|
| 263 | return \@aryIDs;
|
---|
| 264 | }
|
---|
| 265 |
|
---|
| 266 | sub dirFileSplit
|
---|
| 267 | {
|
---|
[12465] | 268 | my ($self,$strFile) = @_;
|
---|
[11783] | 269 |
|
---|
[12465] | 270 | my @aryDirs = split("[/\]",$strFile);
|
---|
| 271 |
|
---|
[11783] | 272 | my $strLocalFile = pop(@aryDirs);
|
---|
| 273 | my $strSubDirs = join("/",@aryDirs);
|
---|
| 274 |
|
---|
| 275 | return ($strSubDirs,$strLocalFile);
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 | sub getOAIDoc
|
---|
| 279 | {
|
---|
[16791] | 280 | my ($self,$strRecord, $oai_rec_filename) = @_;
|
---|
[12465] | 281 |
|
---|
[11783] | 282 | print STDERR "Gathering source documents.....\n";
|
---|
| 283 | # look out for identifier tag in metadata section
|
---|
[12465] | 284 |
|
---|
[11783] | 285 | if ($strRecord =~ m/<metadata>(.*)<\/metadata>/s)
|
---|
| 286 | {
|
---|
| 287 | my $strMetaTag = $1;
|
---|
[16791] | 288 | my $had_valid_url = 0;
|
---|
[17668] | 289 | my $count = 1;
|
---|
[16791] | 290 | while ($strMetaTag =~ s/<(dc:)?identifier>(.*?)<\/(dc:)?identifier>//is)
|
---|
[11783] | 291 | {
|
---|
[16791] | 292 | my $doc_id_url = $2;
|
---|
[20926] | 293 | print STDERR "Found doc url: $doc_id_url\n";
|
---|
[16791] | 294 | next if ($doc_id_url !~ m/^(https?|ftp):\/\//);
|
---|
[11783] | 295 |
|
---|
[16791] | 296 | my $orig_doc_id_url = $doc_id_url;
|
---|
| 297 | $had_valid_url = 1;
|
---|
[12465] | 298 |
|
---|
[16791] | 299 | my ($doc_dir_url_prefix,$doc_id_tail) = ($doc_id_url =~ m/^(.*)\/(.*?)$/);
|
---|
| 300 | my $faked_ext = 0;
|
---|
| 301 | my $primary_doc_match = 0;
|
---|
[12465] | 302 |
|
---|
[16791] | 303 | my ($id_file_ext) = ($doc_id_tail =~ m/\.([^\.]+)$/);
|
---|
[11783] | 304 |
|
---|
[16791] | 305 | if (defined $id_file_ext) {
|
---|
| 306 | # cross-check this filename extension with get_doc_exts option
|
---|
| 307 | # if provided
|
---|
| 308 | my $lookup_exts = $self->{'lookup_exts'};
|
---|
[11783] | 309 |
|
---|
[16791] | 310 | if (defined $lookup_exts->{lc($id_file_ext)}) {
|
---|
| 311 | # this initial URL matches requirement
|
---|
| 312 | $primary_doc_match = 1;
|
---|
| 313 | }
|
---|
| 314 | }
|
---|
| 315 | else {
|
---|
| 316 | $faked_ext = 1;
|
---|
| 317 | $id_file_ext = "html";
|
---|
| 318 | }
|
---|
| 319 |
|
---|
[28250] | 320 |
|
---|
| 321 | my $is_page_html = 0;
|
---|
| 322 | if($id_file_ext =~ m/^html?$/i) {
|
---|
| 323 | $is_page_html = 1;
|
---|
| 324 | } elsif ($doc_id_url) { # important: if no doc id has currently been typed into the url field, skip this block
|
---|
[16791] | 325 |
|
---|
[28250] | 326 | # get the page and check the header's content-type and see if this is text/html
|
---|
| 327 | # if so, $is_page_html is true
|
---|
| 328 | # See http://superuser.com/questions/197009/wget-head-request
|
---|
| 329 |
|
---|
| 330 | my $wget_opts3 = $self->getWgetOptions();
|
---|
| 331 | my $wget_header_cmd = "$wget_opts3 -S --spider \"$doc_id_url\"";
|
---|
| 332 | my $page_content = $self->useWget($wget_header_cmd);
|
---|
| 333 |
|
---|
| 334 | if($page_content && $page_content =~ m@Content-Type:\s*text/html@i) {
|
---|
| 335 | $is_page_html = 1;
|
---|
| 336 | }
|
---|
| 337 | }
|
---|
| 338 |
|
---|
| 339 | if (!$primary_doc_match && $is_page_html) {
|
---|
| 340 |
|
---|
[16791] | 341 | # Download this doc if HTML, scan through it looking for a link
|
---|
| 342 | # that does match get_doc_exts
|
---|
| 343 |
|
---|
| 344 |
|
---|
| 345 | # 1. Generate a tmp name
|
---|
| 346 | my $tmp_filename = &util::get_tmp_filename();
|
---|
| 347 |
|
---|
| 348 | # 2. Download it
|
---|
| 349 | my $wget_opts2 = $self->getWgetOptions();
|
---|
| 350 | my $wget_cmd2 = "$wget_opts2 --convert-links -O \"$tmp_filename\" \"$doc_id_url\"";
|
---|
| 351 | my ($stdout_and_err2,$error2,$follow2) = $self->useWgetMonitored($wget_cmd2);
|
---|
[17549] | 352 | return $strRecord if $self->{'forced_quit'};
|
---|
[16791] | 353 |
|
---|
| 354 | if($error2 ne "")
|
---|
| 355 | {
|
---|
[20926] | 356 | print STDERR "Error occured while retrieving OAI source documents (1): $error2\n";
|
---|
| 357 | #exit(-1);
|
---|
| 358 | next;
|
---|
[16791] | 359 | }
|
---|
| 360 |
|
---|
| 361 | if (defined $follow2) {
|
---|
| 362 | # src url was "redirected" to another place
|
---|
| 363 | # => pick up on this and make it the new doc_id_url
|
---|
| 364 | $doc_id_url = $follow2;
|
---|
| 365 | }
|
---|
| 366 |
|
---|
| 367 | my $primary_doc_html = "";
|
---|
| 368 | if (open(HIN,"<$tmp_filename")) {
|
---|
| 369 | my $line;
|
---|
| 370 | while (defined ($line = <HIN>)) {
|
---|
| 371 | $primary_doc_html .= $line;
|
---|
| 372 | }
|
---|
| 373 | close(HIN);
|
---|
| 374 |
|
---|
| 375 | # 3. Scan through it looking for match
|
---|
| 376 | #
|
---|
| 377 | # if got match, change $doc_id_url to this new URL and
|
---|
| 378 | # $id_file_ext to 'match'
|
---|
| 379 |
|
---|
| 380 | my @href_links = ($primary_doc_html =~ m/href="(.*?)"/gsi);
|
---|
| 381 |
|
---|
| 382 | my $lookup_exts = $self->{'lookup_exts'};
|
---|
| 383 |
|
---|
| 384 | foreach my $href (@href_links) {
|
---|
| 385 | my ($ext) = ($href =~ m/\.([^\.]+)$/);
|
---|
| 386 |
|
---|
| 387 | if ((defined $ext) && (defined $lookup_exts->{$ext})) {
|
---|
| 388 |
|
---|
| 389 | if ($href !~ m/^(https?|ftp):\/\//) {
|
---|
| 390 | # link is within current site
|
---|
| 391 | my ($site_domain) = ($doc_id_url =~ m/^((?:https?|ftp):\/\/.*?)\//);
|
---|
| 392 |
|
---|
| 393 | $href = "$site_domain$href";
|
---|
| 394 | }
|
---|
| 395 |
|
---|
| 396 | $doc_id_url = $href;
|
---|
| 397 | $id_file_ext = $ext;
|
---|
| 398 | last;
|
---|
| 399 | }
|
---|
| 400 | }
|
---|
| 401 | }
|
---|
| 402 | else {
|
---|
[20926] | 403 | print STDERR "Error occurred while retrieving OAI source documents (2):\n";
|
---|
[16791] | 404 | print STDERR "$!\n";
|
---|
| 405 | }
|
---|
| 406 |
|
---|
| 407 | if (-e $tmp_filename) {
|
---|
[28250] | 408 | &FileUtils::removeFiles($tmp_filename);
|
---|
[16791] | 409 | }
|
---|
| 410 | }
|
---|
| 411 |
|
---|
| 412 | my $download_doc_filename = $oai_rec_filename;
|
---|
[27012] | 413 | # As requested by John Rose, don't suffix -$count for the very first document (so no "-1" suffix):
|
---|
| 414 | my $new_extension = ($count > 1) ? "\-$count\.$id_file_ext" : "\.$id_file_ext";
|
---|
[17668] | 415 | $count++;
|
---|
| 416 | #$download_doc_filename =~ s/\.oai$/\.$id_file_ext/;
|
---|
| 417 | $download_doc_filename =~ s/\.oai$/$new_extension/;
|
---|
[16791] | 418 | my ($unused,$download_doc_file) = $self->dirFileSplit($download_doc_filename);
|
---|
| 419 |
|
---|
[20926] | 420 | # may have ' in url - others??
|
---|
| 421 | my $safe_doc_id_url = $doc_id_url;
|
---|
| 422 | $safe_doc_id_url =~ s/'/\'/g;
|
---|
| 423 |
|
---|
[16791] | 424 | my $wget_opts = $self->getWgetOptions();
|
---|
[20926] | 425 | my $wget_cmd = "$wget_opts --convert-links -O \"$download_doc_filename\" \"$safe_doc_id_url\"";
|
---|
| 426 |
|
---|
[16791] | 427 | my ($stdout_and_err,$errors,$follow) = $self->useWgetMonitored($wget_cmd);
|
---|
[17549] | 428 | return $strRecord if $self->{'forced_quit'};
|
---|
[16791] | 429 |
|
---|
| 430 | if($errors ne "")
|
---|
[11783] | 431 | {
|
---|
[20926] | 432 | print STDERR "Error occured while retriving OAI souce documents (3):\n";
|
---|
[16791] | 433 | print STDERR "$errors\n";
|
---|
[20926] | 434 | #exit(-1);
|
---|
| 435 | next;
|
---|
[11783] | 436 | }
|
---|
| 437 |
|
---|
[16791] | 438 |
|
---|
[20926] | 439 | $strRecord =~ s/<metadata>(.*?)<((?:dc:)?identifier)>$orig_doc_id_url<\/((?:dc:)?identifier)>(.*?)<\/metadata>/<metadata>$1<${2}>$orig_doc_id_url<\/${2}>\n <gi.Sourcedoc>$download_doc_file<\/gi.Sourcedoc>$4<\/metadata>/s;
|
---|
[11783] | 440 | }
|
---|
[16791] | 441 |
|
---|
| 442 | if (!$had_valid_url)
|
---|
[11783] | 443 | {
|
---|
[17549] | 444 | print STDERR "\tNo source document URL is specified in the OAI record (No (dc:)?identifier is provided)\n";
|
---|
[11783] | 445 | }
|
---|
| 446 | }
|
---|
| 447 | else
|
---|
| 448 | {
|
---|
[17549] | 449 | print STDERR "\tNo source document URL is specified in the OAI record (No metadata field is provided)\n";
|
---|
[11783] | 450 | }
|
---|
[16791] | 451 |
|
---|
| 452 | return $strRecord;
|
---|
[11783] | 453 | }
|
---|
| 454 |
|
---|
| 455 | sub getOAIRecords
|
---|
| 456 | {
|
---|
[12465] | 457 | my ($self,$aryIDs, $strOutputDir, $strBasURL, $intMaxRecords, $blnDownloadDoc) = @_;
|
---|
[11783] | 458 | my $intDocCounter = 0;
|
---|
| 459 |
|
---|
[14941] | 460 | my $metadata_prefix = $self->{'metadata_prefix'};
|
---|
| 461 |
|
---|
[11783] | 462 | foreach my $strID ( @$aryIDs)
|
---|
| 463 | {
|
---|
[16791] | 464 | print STDERR "Gathering OAI record with ID $strID.....\n";
|
---|
[12465] | 465 |
|
---|
[16791] | 466 | my $wget_opts = $self->getWgetOptions();
|
---|
| 467 | my $cmdWget= "$wget_opts -q -O - \"$strBasURL?verb=GetRecord&metadataPrefix=$metadata_prefix&identifier=$strID\"";
|
---|
[20926] | 468 |
|
---|
[12465] | 469 | my $strRecord = $self->useWget($cmdWget);
|
---|
| 470 |
|
---|
[16791] | 471 | my @fileDirs = split(":",$strID);
|
---|
| 472 | my $local_id = pop @fileDirs;
|
---|
[12465] | 473 |
|
---|
[11783] | 474 | # setup directories
|
---|
| 475 |
|
---|
[12580] | 476 | $strOutputDir =~ s/"//g; #"
|
---|
[13065] | 477 |
|
---|
| 478 | my $host =$self->{'url'};
|
---|
[12465] | 479 |
|
---|
[25224] | 480 | $host =~ s@https?:\/\/@@g;
|
---|
[13065] | 481 |
|
---|
| 482 | $host =~ s/:.*//g;
|
---|
| 483 |
|
---|
[25224] | 484 | my $strFileURL = "";
|
---|
| 485 | if ($strOutputDir ne "") {
|
---|
| 486 | $strFileURL = "$strOutputDir/";
|
---|
| 487 | }
|
---|
| 488 | $strFileURL .= "$host/$local_id.oai";
|
---|
[14179] | 489 |
|
---|
[11783] | 490 | # prepare subdirectory for record (if needed)
|
---|
[12465] | 491 | my ($strSubDirPath,$unused) = ("", "");
|
---|
| 492 |
|
---|
| 493 | ($strSubDirPath,$unused) = $self->dirFileSplit($strFileURL);
|
---|
| 494 |
|
---|
[28250] | 495 | &FileUtils::makeAllDirectories($strSubDirPath);
|
---|
[11783] | 496 |
|
---|
| 497 | my $ds = &util::get_dirsep();
|
---|
[12465] | 498 |
|
---|
[11783] | 499 | if($blnDownloadDoc)
|
---|
| 500 | {
|
---|
[16791] | 501 | $strRecord = $self->getOAIDoc($strRecord,$strFileURL);
|
---|
[11783] | 502 | }
|
---|
| 503 |
|
---|
| 504 | # save record
|
---|
[12465] | 505 | open (OAIOUT,">$strFileURL")
|
---|
[11783] | 506 | || die "Unable to save oai metadata record: $!\n";
|
---|
| 507 | print OAIOUT $strRecord;
|
---|
| 508 | close(OAIOUT);
|
---|
| 509 |
|
---|
[25224] | 510 | print STDERR "Saving record to $strFileURL\n";
|
---|
[12465] | 511 | print STDERR "<<Done>>\n";
|
---|
| 512 | $intDocCounter ++;
|
---|
[25224] | 513 | last if (defined $intMaxRecords && $intDocCounter >= $intMaxRecords);
|
---|
[11783] | 514 | }
|
---|
[12465] | 515 |
|
---|
[25224] | 516 | (defined $intMaxRecords && $intDocCounter >= $intMaxRecords) ?
|
---|
[14926] | 517 | print STDERR "Reached maximum download records, use -max_records to set the maximum.\n":
|
---|
[11783] | 518 | print STDERR "Complete download meta record from $strBasURL\n";
|
---|
| 519 |
|
---|
[12465] | 520 | print STDERR "<<Finished>>\n";
|
---|
[11783] | 521 | }
|
---|
| 522 |
|
---|
| 523 | sub url_information
|
---|
| 524 | {
|
---|
| 525 | my ($self) = shift (@_);
|
---|
| 526 | if(!defined $self){ die "System Error: No \$self defined for url_information in OAIDownload\n";}
|
---|
| 527 |
|
---|
[12465] | 528 | my $wgetOptions = $self->getWgetOptions();
|
---|
| 529 | my $strBaseCMD = $wgetOptions." -q -O - \"$self->{'url'}?_OPTS_\"";
|
---|
[11783] | 530 |
|
---|
| 531 | my $strIdentify = "verb=Identify";
|
---|
| 532 | my $strListSets = "verb=ListSets";
|
---|
[16791] | 533 | my $strListMdFormats = "verb=ListMetadataFormats";
|
---|
[11783] | 534 |
|
---|
| 535 | my $strIdentifyCMD = $strBaseCMD;
|
---|
| 536 | $strIdentifyCMD =~ s/_OPTS_/$strIdentify/;
|
---|
| 537 |
|
---|
[12465] | 538 | my $strIdentifyText = $self->useWget($strIdentifyCMD);
|
---|
[11783] | 539 |
|
---|
[12465] | 540 | if (!defined $strIdentifyText or $strIdentifyText eq "" ){
|
---|
| 541 | print STDERR "Server information is unavailable.\n";
|
---|
| 542 | print STDERR "<<Finished>>\n";
|
---|
| 543 | return;
|
---|
| 544 | }
|
---|
| 545 |
|
---|
[11783] | 546 | print STDERR "General information:\n";
|
---|
| 547 | $self->parse_xml($strIdentifyText);
|
---|
[16791] | 548 | print STDERR "\n";
|
---|
[11783] | 549 |
|
---|
[16791] | 550 | print STDERR "=" x 10, "\n";
|
---|
| 551 | print STDERR "Metadata Format Information (metadataPrefix):\n";
|
---|
| 552 | print STDERR "=" x 10, "\n";
|
---|
| 553 |
|
---|
| 554 | my $strListMdFormatsCMD = $strBaseCMD;
|
---|
| 555 | $strListMdFormatsCMD =~ s/_OPTS_/$strListMdFormats/;
|
---|
| 556 | my $strListMdFormatsText = $self->useWget($strListMdFormatsCMD);
|
---|
| 557 |
|
---|
| 558 | $self->parse_xml($strListMdFormatsText);
|
---|
| 559 | print STDERR "\n";
|
---|
| 560 |
|
---|
| 561 | print STDERR "=" x 10, "\n";
|
---|
| 562 | print STDERR "List Information:\n";
|
---|
| 563 | print STDERR "=" x 10, "\n";
|
---|
| 564 |
|
---|
[11783] | 565 | my $strListSetCMD = $strBaseCMD;
|
---|
| 566 | $strListSetCMD =~ s/_OPTS_/$strListSets/;
|
---|
[12465] | 567 | my $strListSetsText = $self->useWget($strListSetCMD);
|
---|
| 568 |
|
---|
[11783] | 569 | $self->parse_xml($strListSetsText);
|
---|
| 570 | }
|
---|
| 571 |
|
---|
| 572 | sub parse_xml
|
---|
| 573 | {
|
---|
| 574 | my ($self) = shift (@_);
|
---|
[16791] | 575 | my ($xml_text) = @_;
|
---|
[12465] | 576 |
|
---|
[16791] | 577 | #### change this to work directly from $xml_text
|
---|
| 578 |
|
---|
[11783] | 579 | #Open a temporary file to store OAI information, and store the information to the temp file
|
---|
[28250] | 580 | my $name = &FileUtils::filenameConcatenate($ENV{GSDLHOME},"tmp","oai.tmp");
|
---|
[11783] | 581 |
|
---|
[12465] | 582 | open(*OAIOUT,"> $name");
|
---|
| 583 |
|
---|
[16791] | 584 | print OAIOUT $xml_text;
|
---|
[12465] | 585 | close(OAIOUT);
|
---|
| 586 |
|
---|
[11783] | 587 | $self->{'temp_file_name'} = $name;
|
---|
| 588 |
|
---|
[16791] | 589 | ## print STDERR "**** xml text = $xml_text\n";
|
---|
| 590 |
|
---|
[11783] | 591 | eval {
|
---|
| 592 | $self->{'parser'}->parsefile("$name");
|
---|
[16791] | 593 | ## $self->{'parser'}->parse($xml_text);
|
---|
[11783] | 594 | };
|
---|
[25224] | 595 |
|
---|
[11783] | 596 | if ($@) {
|
---|
[16791] | 597 | die "OAI: Parsed file $name is not a well formed XML file ($@)\n";
|
---|
| 598 | ## die "OAI: Parsed text is not a well formed XML file ($@)\n";
|
---|
[11783] | 599 | }
|
---|
| 600 |
|
---|
[16791] | 601 | unlink($self->{'temp_file_name'}) or die "Could not unlink $self->{'temp_file_name'}: $!";
|
---|
[11783] | 602 | }
|
---|
| 603 |
|
---|
[16791] | 604 | ####END
|
---|
| 605 | #{
|
---|
| 606 | # if($self->{'info'})
|
---|
| 607 | # {
|
---|
| 608 | # unlink($self->{'temp_file_name'}) or die "Could not unlink $self->{'temp_file_name'}: $!";
|
---|
| 609 | # }
|
---|
| 610 | #}
|
---|
| 611 |
|
---|
[11783] | 612 | # This Char function overrides the one in XML::Parser::Stream to overcome a
|
---|
| 613 | # problem where $expat->{Text} is treated as the return value, slowing
|
---|
| 614 | # things down significantly in some cases.
|
---|
| 615 | sub Char {
|
---|
| 616 | use bytes; # Necessary to prevent encoding issues with XML::Parser 2.31+
|
---|
| 617 | $_[0]->{'Text'} .= $_[1];
|
---|
[16791] | 618 |
|
---|
| 619 | my $self = $_[0]->{'PluginObj'};
|
---|
[11783] | 620 | if ((defined $self->{'subfield'} && ($self->{'subfield'} ne ""))) {
|
---|
| 621 | $self->{'text'} .= $_[1];
|
---|
[12465] | 622 | $self->{'text'} =~ s/[\n]|([ ]{2,})//g;
|
---|
[11783] | 623 | if($self->{'text'} ne "")
|
---|
| 624 | {
|
---|
| 625 | print STDERR " $self->{'subfield'}:($self->{'text'})\n";
|
---|
| 626 | }
|
---|
| 627 | }
|
---|
| 628 | return undef;
|
---|
| 629 | }
|
---|
| 630 |
|
---|
| 631 | sub OAI_StartTag
|
---|
| 632 | {
|
---|
| 633 | my ($expat, $element, %attr) = @_;
|
---|
[12465] | 634 |
|
---|
[16791] | 635 | my $self = $expat->{'PluginObj'};
|
---|
[11783] | 636 | $self->{'subfield'} = $element;
|
---|
[12465] | 637 |
|
---|
[11783] | 638 | }
|
---|
| 639 |
|
---|
| 640 | sub OAI_EndTag
|
---|
| 641 | {
|
---|
| 642 | my ($expat, $element) = @_;
|
---|
[16791] | 643 |
|
---|
| 644 | my $self = $expat->{'PluginObj'};
|
---|
[11783] | 645 | $self->{'text'} = "";
|
---|
| 646 | $self->{'subfield'} = "";
|
---|
| 647 | }
|
---|
| 648 |
|
---|
| 649 | sub error
|
---|
| 650 | {
|
---|
[12465] | 651 | my ($self,$strFunctionName,$strError) = @_;
|
---|
[11783] | 652 | {
|
---|
| 653 | print "Error occoured in OAIDownload.pm\n".
|
---|
| 654 | "In Function:".$strFunctionName."\n".
|
---|
| 655 | "Error Message:".$strError."\n";
|
---|
| 656 | exit(-1);
|
---|
| 657 | }
|
---|
| 658 | }
|
---|
| 659 |
|
---|
| 660 |
|
---|
| 661 |
|
---|
| 662 | 1;
|
---|