Line 0
Link Here
|
|
|
1 |
package Koha::OAI::Client::Record; |
2 |
|
3 |
# Copyright 2016 Prosentient Systems |
4 |
# |
5 |
# This file is part of Koha. |
6 |
# |
7 |
# Koha is free software; you can redistribute it and/or modify it |
8 |
# under the terms of the GNU General Public License as published by |
9 |
# the Free Software Foundation; either version 3 of the License, or |
10 |
# (at your option) any later version. |
11 |
# |
12 |
# Koha is distributed in the hope that it will be useful, but |
13 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
# GNU General Public License for more details. |
16 |
# |
17 |
# You should have received a copy of the GNU General Public License |
18 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
19 |
# |
20 |
|
21 |
use Modern::Perl; |
22 |
use XML::LibXML; |
23 |
use XML::LibXSLT; |
24 |
use MARC::Record; |
25 |
|
26 |
use C4::Context; |
27 |
use C4::Biblio; |
28 |
use C4::ImportBatch; |
29 |
use C4::Matcher; |
30 |
|
31 |
use constant MAX_MATCHES => 99999; #NOTE: This is an arbitrary value. We want to get all matches. |
32 |
|
33 |
sub new { |
34 |
my ($class, $args) = @_; |
35 |
$args = {} unless defined $args; |
36 |
|
37 |
if (my $inxml = $args->{xml_string}){ |
38 |
|
39 |
#Parse the XML string into a XML::LibXML object |
40 |
my $doc = XML::LibXML->load_xml(string => $inxml, { no_blanks => 1 }); |
41 |
$args->{doc} = $doc; |
42 |
#NOTE: Don't load blank nodes... |
43 |
|
44 |
#Get the root element |
45 |
my $root = $doc->documentElement; |
46 |
|
47 |
#Register namespaces for searching purposes |
48 |
my $xpc = XML::LibXML::XPathContext->new(); |
49 |
$xpc->registerNs('oai','http://www.openarchives.org/OAI/2.0/'); |
50 |
|
51 |
my $xpath_identifier = XML::LibXML::XPathExpression->new("oai:header/oai:identifier"); |
52 |
my $identifier = $xpc->findnodes($xpath_identifier,$root)->shift; |
53 |
#my $identifier_string = $identifier->textContent; |
54 |
$args->{header_identifier} = $identifier->textContent; |
55 |
|
56 |
my $xpath_datestamp = XML::LibXML::XPathExpression->new("oai:header/oai:datestamp"); |
57 |
my $datestamp = $xpc->findnodes($xpath_datestamp,$root)->shift; |
58 |
#my $datestamp_string = $datestamp->textContent; |
59 |
$args->{header_datestamp} = $datestamp->textContent; |
60 |
|
61 |
my $xpath_status = XML::LibXML::XPathExpression->new(q{oai:header/@status}); |
62 |
my $status_node = $xpc->findnodes($xpath_status,$root)->shift; |
63 |
#my $status_string = $status_node ? $status_node->textContent : ""; |
64 |
$args->{header_status} = $status_node ? $status_node->textContent : ""; |
65 |
} |
66 |
|
67 |
return bless ($args, $class); |
68 |
} |
69 |
|
70 |
sub is_deleted_upstream { |
71 |
my ($self, $args) = @_; |
72 |
if ($self->{header_status}){ |
73 |
if ($self->{header_status} eq "deleted"){ |
74 |
return 1; |
75 |
} |
76 |
} |
77 |
return 0; |
78 |
} |
79 |
|
80 |
sub filter { |
81 |
my ($self, $args) = @_; |
82 |
my $doc = $self->{doc}; |
83 |
my $filter = $args->{filter}; |
84 |
$self->{filter} = $filter; #FIXME |
85 |
#FIXME: Check that it's an XSLT here... |
86 |
if ( -f $filter ){ |
87 |
#Filter is a valid filepath |
88 |
|
89 |
#FIXME: Ideally, it would be good to use Koha::XSLT_Handler here... (especially for persistent environments...) |
90 |
my $xslt = XML::LibXSLT->new(); |
91 |
my $style_doc = XML::LibXML->load_xml(location => $filter); |
92 |
my $stylesheet = $xslt->parse_stylesheet($style_doc); |
93 |
if ($stylesheet){ |
94 |
my $results = $stylesheet->transform($doc); |
95 |
my $metadata_xml = $stylesheet->output_as_bytes($results); |
96 |
#If the XSLT outputs nothing, then we don't meet the following condition, and we'll return 0 instead. |
97 |
if ($metadata_xml){ |
98 |
$self->{filtered_record} = $metadata_xml; |
99 |
return 1; |
100 |
} |
101 |
} |
102 |
} |
103 |
return 0; |
104 |
} |
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
sub import_record { |
112 |
my ($self, $args) = @_; |
113 |
my $koha_record_numbers = ""; |
114 |
my $errors = []; |
115 |
my $import_status = "error"; |
116 |
my $match_status = "no_match"; |
117 |
|
118 |
my $batch_id = $args->{import_batch_id}; |
119 |
$self->{import_batch_id} = $batch_id; #FIXME |
120 |
my $matcher = $args->{matcher}; |
121 |
my $framework = $args->{framework}; |
122 |
my $import_mode = $args->{import_mode}; |
123 |
|
124 |
my $metadata_xml = $self->{filtered_record}; |
125 |
|
126 |
if ($metadata_xml){ |
127 |
#Convert MARCXML into MARC::Record object |
128 |
my $marcflavour = C4::Context->preference('marcflavour') || 'MARC21'; |
129 |
my $marc_record = eval {MARC::Record::new_from_xml( $metadata_xml, "utf8", $marcflavour)}; |
130 |
if ($@) { |
131 |
warn "Error converting OAI-PMH filtered metadata into MARC::Record object: $@"; |
132 |
#FIXME: Improve error handling |
133 |
} |
134 |
|
135 |
if ($self->is_deleted_upstream){ |
136 |
|
137 |
=pod |
138 |
my @matches = $matcher->get_matches($marc_record, MAX_MATCHES); |
139 |
if (@matches){ |
140 |
$match_status = "matched"; |
141 |
} |
142 |
my $delete_error; |
143 |
foreach my $match (@matches){ |
144 |
if (my $record_id = $match->{record_id}){ |
145 |
#FIXME: This is biblio specific... what about authority records? |
146 |
my $error = C4::Biblio::DelBiblio($record_id); |
147 |
if ($error){ |
148 |
$delete_error++; |
149 |
$koha_record_numbers = []; |
150 |
push(@$koha_record_numbers,$record_id); |
151 |
|
152 |
#FIXME: Find a better way of sending the errors in a predictable way... |
153 |
push(@$errors,{"record_id" => $record_id, "error" => $error, }); |
154 |
} |
155 |
} |
156 |
|
157 |
} |
158 |
|
159 |
#If there are no delete errors, then the import was ok |
160 |
if ( ! $delete_error){ |
161 |
$import_status = "ok"; |
162 |
} |
163 |
#Deleted records will never actually have an records in them, so always mark them as cleaned so that other imports don't try to pick up the same batch. |
164 |
C4::ImportBatch::SetImportBatchStatus($batch_id, 'cleaned'); |
165 |
=cut |
166 |
my $import_record_id = AddBiblioToBatch($batch_id, 0, $marc_record, "utf8", int(rand(99999))); |
167 |
my $number_of_matches = BatchFindDuplicates($batch_id, $matcher, MAX_MATCHES); |
168 |
if ($number_of_matches > 0){ |
169 |
$match_status = "auto_match"; #See `import_records` table for other options... but this should be the right one. |
170 |
} |
171 |
my $results = GetImportRecordMatches($import_record_id); #Only works for biblio... |
172 |
my $delete_error; |
173 |
|
174 |
my @result_record_numbers = (); |
175 |
foreach my $result (@$results){ |
176 |
if (my $record_id = $result->{biblionumber}){ |
177 |
push(@result_record_numbers,$record_id); |
178 |
|
179 |
#FIXME: This is biblio specific... what about authority records? |
180 |
my $error = C4::Biblio::DelBiblio($record_id); |
181 |
if ($error){ |
182 |
$delete_error++; |
183 |
push(@$errors, { type => 'delete_failed', error_msg => $error, record_id => $record_id, }) ; |
184 |
} |
185 |
} |
186 |
} |
187 |
$koha_record_numbers = join(",",@result_record_numbers); #FIXME: check that this works... |
188 |
|
189 |
if ($delete_error){ |
190 |
$import_status = "error"; |
191 |
C4::ImportBatch::SetImportBatchStatus($batch_id, 'importing'); |
192 |
} else { |
193 |
$import_status = "ok"; |
194 |
#Ideally, it would be nice to say what records were deleted, but Koha doesn't have that capacity at the moment, so just clean the batch. |
195 |
CleanBatch($batch_id); |
196 |
} |
197 |
|
198 |
|
199 |
|
200 |
|
201 |
} else { |
202 |
#Import the MARCXML record into Koha |
203 |
my $import_record_id = AddBiblioToBatch($batch_id, 0, $marc_record, "utf8", int(rand(99999))); |
204 |
#FIXME: Don't allow item imports do to the nature of OAI-PMH records updating over time... |
205 |
#my @import_items_ids = AddItemsToImportBiblio($batch_id, $import_record_id, $marc_record, 'UPDATE COUNTS'); |
206 |
my $number_of_matches = BatchFindDuplicates($batch_id, $matcher); |
207 |
|
208 |
# XXX we are ignoring the result of this; |
209 |
BatchCommitRecords($batch_id, $framework) if lc($import_mode) eq 'direct'; |
210 |
|
211 |
my $dbh = C4::Context->dbh(); |
212 |
my $sth = $dbh->prepare("SELECT matched_biblionumber FROM import_biblios WHERE import_record_id =?"); |
213 |
$sth->execute($import_record_id); |
214 |
$koha_record_numbers = $sth->fetchrow_arrayref->[0] || ''; |
215 |
$sth = $dbh->prepare("SELECT overlay_status FROM import_records WHERE import_record_id =?"); |
216 |
$sth->execute($import_record_id); |
217 |
|
218 |
$match_status = $sth->fetchrow_arrayref->[0] || 'no_match'; |
219 |
$import_status = "ok"; |
220 |
} |
221 |
} else { |
222 |
#There's no filtered metadata... |
223 |
#Clean the batch, so future imports don't use the same batch. |
224 |
CleanBatch($batch_id); |
225 |
} |
226 |
$self->{status} = $import_status; #FIXME |
227 |
#$self->save_to_database(); |
228 |
return ($import_status,$match_status,$koha_record_numbers, $errors); |
229 |
} |
230 |
|
231 |
sub save_to_database { |
232 |
my ($self,$args) = @_; |
233 |
|
234 |
my $header_identifier = $self->{header_identifier}; |
235 |
my $header_datestamp = $self->{header_datestamp}; |
236 |
my $header_status = $self->{header_status}; |
237 |
my $metadata = $self->{doc}->toString(1); |
238 |
my $import_batch_id = $self->{import_batch_id}; |
239 |
my $filter = $self->{filter}; |
240 |
my $status = $self->{status}; |
241 |
|
242 |
my $dbh = C4::Context->dbh; |
243 |
my $sql = "INSERT INTO import_oai (header_identifier, header_datestamp, header_status, metadata, import_batch_id, filter, status) VALUES (?, ?, ?, ?, ?, ?, ?)"; |
244 |
my $sth = $dbh->prepare($sql); |
245 |
$sth->execute($header_identifier,$header_datestamp,$header_status,$metadata, $import_batch_id, $filter, $status); |
246 |
} |
247 |
|
248 |
|
249 |
1; |