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