|
Line 0
Link Here
|
|
|
1 |
package Koha::Prosentient::MQ::AuditAuthorities; |
| 2 |
|
| 3 |
use Modern::Perl; |
| 4 |
use Template; |
| 5 |
use Mail::Sendmail; |
| 6 |
use Email::Valid; |
| 7 |
use Archive::Zip qw( :ERROR_CODES :CONSTANTS ); |
| 8 |
use Encode; |
| 9 |
use MIME::QuotedPrint; |
| 10 |
use MIME::Base64; |
| 11 |
|
| 12 |
use C4::Context; |
| 13 |
use Koha::Database; |
| 14 |
use Koha::MarcSubfieldStructures; |
| 15 |
use Koha::Biblios; |
| 16 |
|
| 17 |
my $email_template = <<EMAIL_TEMPLATE; |
| 18 |
<html> |
| 19 |
<head> |
| 20 |
<style type="text/css"> |
| 21 |
table, tr, th, td { |
| 22 |
border-style: solid; |
| 23 |
} |
| 24 |
</style> |
| 25 |
</head> |
| 26 |
<body> |
| 27 |
<table> |
| 28 |
<tr> |
| 29 |
<th>Biblionumber</th> |
| 30 |
<th>Tag</th> |
| 31 |
<th>Auth type</th> |
| 32 |
<th>Bib heading</th> |
| 33 |
<th>Linkage in bib</th> |
| 34 |
<th>Description</th> |
| 35 |
<th>Matched authority id</th> |
| 36 |
<th>Matched authority heading</th> |
| 37 |
</tr> |
| 38 |
[% FOREACH result IN results %] |
| 39 |
<tr> |
| 40 |
<td>[% result.biblionumber %]</td> |
| 41 |
<td>[% result.field %]$[% result.subfield %]</td> |
| 42 |
<td>[% result.authtypecode %]</td> |
| 43 |
<td>[% result.bib_full %]</td> |
| 44 |
<td>[% result.linkage %]</td> |
| 45 |
<td>[% result.desc %]</td> |
| 46 |
<td>[% result.matched_id %]</td> |
| 47 |
<td>[% result.matched_heading %]</td> |
| 48 |
</tr> |
| 49 |
[% END %] |
| 50 |
</table> |
| 51 |
</body> |
| 52 |
</html> |
| 53 |
EMAIL_TEMPLATE |
| 54 |
|
| 55 |
sub do_task { |
| 56 |
my ($class,$args) = @_; |
| 57 |
my $completed = 1; |
| 58 |
my $debug = $args->{debug} // 0; |
| 59 |
my $field = $args->{field}; |
| 60 |
my $subfield = $args->{subfield}; |
| 61 |
my $authtypecode = $args->{authtypecode}; |
| 62 |
my $email = $args->{email}; |
| 63 |
$debug && warn __PACKAGE__ . " " . "starting task"; |
| 64 |
if ($field && $subfield && $authtypecode){ |
| 65 |
my $results = _get_results({ |
| 66 |
field => $field, |
| 67 |
subfield => $subfield, |
| 68 |
authtypecode => $authtypecode, |
| 69 |
}); |
| 70 |
if (@$results){ |
| 71 |
$debug && warn __PACKAGE__ . " " . scalar @$results . " results"; |
| 72 |
my $report = _create_report({ |
| 73 |
results => $results, |
| 74 |
}); |
| 75 |
if ($report){ |
| 76 |
$debug && warn __PACKAGE__ . " " . "report created"; |
| 77 |
my $sent = _email_report({ |
| 78 |
email => $email, |
| 79 |
report => $report, |
| 80 |
tag => $field . '$' . $subfield, |
| 81 |
}); |
| 82 |
if ($sent){ |
| 83 |
$debug && warn __PACKAGE__ . " " . "email sent"; |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
$debug && warn __PACKAGE__ . " " . "completed task"; |
| 89 |
return $completed; |
| 90 |
} |
| 91 |
|
| 92 |
sub _email_report { |
| 93 |
my ($args) = @_; |
| 94 |
my $sent; |
| 95 |
my $email = $args->{email}; |
| 96 |
my $report = $args->{report}; |
| 97 |
my $tag = $args->{tag}; |
| 98 |
if ($email && $report && $tag){ |
| 99 |
if ( Email::Valid->address($email) ){ |
| 100 |
my $zip = Archive::Zip->new(); |
| 101 |
$zip->addString(Encode::encode("UTF-8",$report),"authorityreport_$tag.html"); |
| 102 |
my $memory_file = ''; #scalar as a file |
| 103 |
open(my $fh, '>', \$memory_file); |
| 104 |
my $status = $zip->writeToFileHandle($fh); |
| 105 |
if ($status == AZ_OK){ |
| 106 |
$fh->close; |
| 107 |
my $zipfile = encode_base64($memory_file); |
| 108 |
|
| 109 |
my %mail = (); |
| 110 |
$mail{from} = 'ADMIN@DOMAIN.com'; |
| 111 |
$mail{to} = $email; |
| 112 |
$mail{subject} = Encode::encode("UTF-8","Authorities audit report for your catalogue on tag $tag"); |
| 113 |
my $boundary = "====" . time() . "===="; |
| 114 |
$mail{'content-type'} = "multipart/mixed; boundary=\"$boundary\""; |
| 115 |
$boundary = '--' . $boundary; |
| 116 |
my $body = encode_qp(Encode::encode("UTF-8","<html><body>Please find attached your authorities audit report for review.</body></html>")); |
| 117 |
$mail{body} = <<END_OF_BODY; |
| 118 |
$boundary |
| 119 |
Content-Type: text/html; charset="utf-8" |
| 120 |
Content-Transfer-Encoding: quoted-printable |
| 121 |
|
| 122 |
$body |
| 123 |
$boundary |
| 124 |
Content-Type: application/octet-stream; name="authorityreport_$tag.zip" |
| 125 |
Content-Transfer-Encoding: base64 |
| 126 |
Content-Disposition: attachment; filename="authorityreport_$tag.zip" |
| 127 |
|
| 128 |
$zipfile |
| 129 |
$boundary-- |
| 130 |
END_OF_BODY |
| 131 |
if ( sendmail %mail ) { |
| 132 |
$sent = 1; |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
return $sent; |
| 138 |
} |
| 139 |
|
| 140 |
sub _get_results { |
| 141 |
my ($args) = @_; |
| 142 |
my @results = (); |
| 143 |
my $field = $args->{field}; |
| 144 |
my $subfield = $args->{subfield}; |
| 145 |
my $authtypecode = $args->{authtypecode}; |
| 146 |
if ($field && $subfield && $authtypecode){ |
| 147 |
|
| 148 |
my $schema = Koha::Database->new->schema; |
| 149 |
my $authtype = $schema->resultset('AuthType')->search({ |
| 150 |
authtypecode => $authtypecode, |
| 151 |
})->first; |
| 152 |
|
| 153 |
my $auth_tag = $authtype->auth_tag_to_report; |
| 154 |
|
| 155 |
my %auth_lookup_by_id = (); |
| 156 |
my %auth_lookup_by_full = (); |
| 157 |
my %auth_lookup_by_primary = (); |
| 158 |
my $dbh = C4::Context->dbh; |
| 159 |
my $sql = qq~ |
| 160 |
SELECT |
| 161 |
authid, |
| 162 |
ExtractValue(marcxml,'//datafield[\@tag="$auth_tag"]/*') as full_header, |
| 163 |
ExtractValue(marcxml,'//datafield[\@tag="$auth_tag"]/subfield[\@code="a"]') as primary_header |
| 164 |
FROM auth_header |
| 165 |
WHERE authtypecode = ? |
| 166 |
~; |
| 167 |
my $sth = $dbh->prepare($sql); |
| 168 |
$sth->execute($authtypecode); |
| 169 |
while (my $row = $sth->fetchrow_hashref){ |
| 170 |
$auth_lookup_by_id{ $row->{authid} } = { full => $row->{full_header}, primary => $row->{primary_header}, }; |
| 171 |
if ( ! $auth_lookup_by_full{ $row->{full_header} } ){ |
| 172 |
$auth_lookup_by_full{ $row->{full_header} } = []; |
| 173 |
} |
| 174 |
push(@{$auth_lookup_by_full{ $row->{full_header} }},$row->{authid}); |
| 175 |
|
| 176 |
if ( ! $auth_lookup_by_primary{ $row->{primary_header} } ){ |
| 177 |
$auth_lookup_by_primary{ $row->{primary_header} }= []; |
| 178 |
} |
| 179 |
push(@{$auth_lookup_by_primary{ $row->{primary_header} }},$row->{authid}); |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
my @frameworks_to_audit = Koha::MarcSubfieldStructures->search( |
| 184 |
{ |
| 185 |
tagfield => $field, |
| 186 |
tagsubfield => $subfield, |
| 187 |
authtypecode => $authtypecode, |
| 188 |
} |
| 189 |
); |
| 190 |
my @search_conditions = (); |
| 191 |
foreach my $structure (@frameworks_to_audit){ |
| 192 |
my $frameworkcode = $structure->frameworkcode; |
| 193 |
my $condition = { frameworkcode => $frameworkcode }; |
| 194 |
push(@search_conditions,$condition); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
my @biblios = Koha::Biblios->search(\@search_conditions); |
| 200 |
foreach my $biblio (@biblios){ |
| 201 |
my $record = $biblio->metadata->record; |
| 202 |
if ($record){ |
| 203 |
my @marc_fields = $record->field($field); |
| 204 |
foreach my $marc_field (@marc_fields){ |
| 205 |
my $result = { |
| 206 |
biblionumber => $biblio->biblionumber, |
| 207 |
field => $field, |
| 208 |
subfield => $subfield, |
| 209 |
bib_full => '', |
| 210 |
authtypecode => $authtypecode, |
| 211 |
linkage => '', |
| 212 |
matched_heading => '', |
| 213 |
matched_id => '', |
| 214 |
desc => 'Unlinked and no match', |
| 215 |
}; |
| 216 |
#Check if it already has a $9 linkage |
| 217 |
my $bib_field = $marc_field->clone(); |
| 218 |
my $linkage = $bib_field->subfield('9'); |
| 219 |
$bib_field->delete_subfield('9'); |
| 220 |
my $bib_subfield = $bib_field->subfield($subfield); |
| 221 |
my $bib_full = $bib_field->as_string; |
| 222 |
$result->{bib_full} = $bib_full; |
| 223 |
if ( $linkage){ |
| 224 |
my $linked_auth = $auth_lookup_by_id{ $linkage }; |
| 225 |
if ($linked_auth){ |
| 226 |
$result->{linkage} = $linkage; |
| 227 |
if ( $linked_auth->{full} eq $bib_field->as_string){ |
| 228 |
$result->{desc} = "Linked heading 100% match"; |
| 229 |
$result->{matched_heading} = $linked_auth->{full}; |
| 230 |
} elsif ( $bib_subfield && $linked_auth->{primary} eq $bib_subfield ){ |
| 231 |
$result->{desc} = "Linked heading partial match"; |
| 232 |
$result->{matched_heading} = $linked_auth->{primary}; |
| 233 |
|
| 234 |
} else { |
| 235 |
$result->{desc} = "Linked heading does not match"; |
| 236 |
$result->{matched_heading} = $linked_auth->{full}; |
| 237 |
} |
| 238 |
} else { |
| 239 |
$result->{desc} = "Linked but unable to find authority record"; |
| 240 |
} |
| 241 |
} else { |
| 242 |
my $full_match = $auth_lookup_by_full{ $bib_full }; |
| 243 |
if ($full_match) { |
| 244 |
$result->{desc} = "Unlinked but found 100% match"; |
| 245 |
$result->{matched_id} = join(',',@$full_match); |
| 246 |
$result->{matched__heading} = $bib_full; |
| 247 |
} else { |
| 248 |
if ($bib_subfield){ |
| 249 |
my $primary_match = $auth_lookup_by_primary{ $bib_subfield }; |
| 250 |
if ($primary_match){ |
| 251 |
$result->{desc} = "Unlinked but found partial match"; |
| 252 |
$result->{matched_id} = join(',',@$primary_match); |
| 253 |
$result->{matched_heading} = $bib_subfield; |
| 254 |
} |
| 255 |
} |
| 256 |
} |
| 257 |
} |
| 258 |
push(@results,$result); |
| 259 |
} |
| 260 |
} |
| 261 |
} |
| 262 |
} |
| 263 |
return \@results; |
| 264 |
} |
| 265 |
|
| 266 |
1; |
| 267 |
|
| 268 |
sub _get_template { |
| 269 |
my $template; |
| 270 |
while (my $line = <DATA>){ |
| 271 |
$template .= $line; |
| 272 |
} |
| 273 |
return $template; |
| 274 |
} |
| 275 |
|
| 276 |
sub _create_report { |
| 277 |
my ($args) = @_; |
| 278 |
my $report; |
| 279 |
my $results = $args->{results}; |
| 280 |
if ($results){ |
| 281 |
my $template_engine = Template->new({ ENCODING => 'UTF-8', }); |
| 282 |
my $template = $email_template; |
| 283 |
$template_engine->process(\$template, { results => $results, }, \$report); |
| 284 |
} |
| 285 |
return $report; |
| 286 |
} |
| 287 |
|