From 9dd6b3b6a202e5f6172b88ddfb85a81ae3107a2e Mon Sep 17 00:00:00 2001 From: Baptiste Wojtkowski Date: Thu, 27 Apr 2017 15:44:09 +0000 Subject: [PATCH] Bug 18618: Mana - Add scripts to send readings - There are 2 differents scripts: misc/cronjobs/mana_send_pairs.pl -> please use it if Mana is installed misc/cronjobs/mana_send_pairs_without_mana.pl -> designed to work if mana isn't enabled - Both of scripts anonymize the datas and send them to mana. Signed-off-by: Brendan A Gallagher --- misc/cronjobs/mana_send_pairs.pl | 105 +++++++++++++++++ misc/cronjobs/mana_send_pairs_without_mana.pl | 106 ++++++++++++++++++ 2 files changed, 211 insertions(+) create mode 100644 misc/cronjobs/mana_send_pairs.pl create mode 100644 misc/cronjobs/mana_send_pairs_without_mana.pl diff --git a/misc/cronjobs/mana_send_pairs.pl b/misc/cronjobs/mana_send_pairs.pl new file mode 100644 index 0000000000..50ea86b7a8 --- /dev/null +++ b/misc/cronjobs/mana_send_pairs.pl @@ -0,0 +1,105 @@ +use Modern::Perl; + +use C4::Context; +use Getopt::Long; +use Koha; +use C4::Koha; +use Koha; + +use JSON; +use HTTP::Request; +use Koha::SharedContent; + +my $help = 0; +my $man = 0; +my $verbose = 0; +my $date=DateTime->now()->strftime("%F"); + +print $date; + + +GetOptions( + 'help|?' => \$help, + 'man' => \$man, + 'v' => \$verbose, + 'date|d:s' => \$date +); + + +$date.="%"; +my $dbh = C4::Context->dbh; +my $query = q{ +SELECT + ExtractValue(biblio_metadata1.metadata, '//datafield[@tag="010"]/subfield[@code="a"]') as isbn1, + ExtractValue(biblio_metadata2.metadata, '//datafield[@tag="010"]/subfield[@code="a"]') as isbn2, + ExtractValue(biblio_metadata1.metadata, '//datafield[@tag="011"]/subfield[@code="a"]') as issn1, + ExtractValue(biblio_metadata2.metadata, '//datafield[@tag="011"]/subfield[@code="a"]') as issn2, + ExtractValue(biblio_metadata1.metadata, '//datafield[@tag="013"]/subfield[@code="a"]') as ismn1, + ExtractValue(biblio_metadata2.metadata, '//datafield[@tag="013"]/subfield[@code="a"]') as ismn2, + ExtractValue(biblio_metadata1.metadata, '//datafield[@tag="015"]/subfield[@code="a"]') as isrn1, + ExtractValue(biblio_metadata2.metadata, '//datafield[@tag="015"]/subfield[@code="a"]') as isrn2, + ExtractValue(biblio_metadata1.metadata, '//datafield[@tag="016"]/subfield[@code="a"]') as isrc1, + ExtractValue(biblio_metadata2.metadata, '//datafield[@tag="016"]/subfield[@code="a"]') as isrc2, + ExtractValue(biblio_metadata1.metadata, '//datafield[@tag="072"]/subfield[@code="a"]') as upc1, + ExtractValue(biblio_metadata2.metadata, '//datafield[@tag="072"]/subfield[@code="a"]') as upc2, + ExtractValue(biblio_metadata1.metadata, '//datafield[@tag="073"]/subfield[@code="a"]') as ean1, + ExtractValue(biblio_metadata2.metadata, '//datafield[@tag="073"]/subfield[@code="a"]') as ean2 +FROM + issues AS issues1 +LEFT JOIN + issues AS issues2 ON issues1.borrowernumber=issues2.borrowernumber +LEFT JOIN + items AS items1 ON issues1.itemnumber=items1.itemnumber +LEFT JOIN + biblio_metadata AS biblio_metadata1 ON items1.biblionumber=biblio_metadata1.biblionumber +LEFT JOIN + items AS items2 ON issues2.itemnumber=items2.itemnumber +LEFT JOIN + biblio_metadata AS biblio_metadata2 ON items2.biblionumber=biblio_metadata2.biblionumber +WHERE + issues1.issuedate LIKE '}.$date. q{%' AND biblio_metadata1.biblionumber>biblio_metadata2.biblionumber AND biblio_metadata1.format='marcxml' AND biblio_metadata2.format='marcxml' +ORDER BY + issues1.borrowernumber,biblio_metadata1.biblionumber +}; + +my $sth = $dbh->prepare( $query ); +$sth->execute; + +my $row; +my @reading_pairs; +my $cter = 0; +while ( $row = $sth->fetchrow_hashref ){ +$row->{isbn1} = NormalizeISBN({ isbn => $row->{isbn1}, format => 'ISBN-13', strip_hyphens => 1 }); +$row->{isbn2} = NormalizeISBN({ isbn => $row->{isbn2}, format => 'ISBN-13', strip_hyphens => 1 }); + my $currentpair; + foreach my $key1 ( qw ( isbn issn ismn isrn ean upc isrc ) ){ + foreach my $key2 ( qw ( isbn issn ismn isrn ean upc isrc ) ){ + if ( $row->{ $key1."1" } and $row->{ $key2."2" } ){ + $currentpair->{ idtype1 } = $key1; + $currentpair->{ documentid1 } = $row->{ $key1."1" }; + $currentpair->{ idtype2 } = $key2; + $currentpair->{ documentid2 } = $row->{ $key2."2" }; + push @reading_pairs, $currentpair; + + } + } + } +} + +my $securitytoken = C4::Context->preference('ManaToken'); +my $MANA_IP; +my $content; +$MANA_IP=C4::Context->config("mana_config"); +print $MANA_IP; +$content->{ securitytoken } = $securitytoken; +$content->{ reading_pairs } = \@reading_pairs; + +my $url = "$MANA_IP/bulk/reading_pair.json"; +my $request = HTTP::Request->new( POST => $url ); + +my $json = to_json( $content, { utf8 => 1 } ); +#print $json; + +$request->content($json); +my $result = Koha::SharedContent::manaRequest( $request ); +print Data::Dumper::Dumper( $result ); diff --git a/misc/cronjobs/mana_send_pairs_without_mana.pl b/misc/cronjobs/mana_send_pairs_without_mana.pl new file mode 100644 index 0000000000..c853832d65 --- /dev/null +++ b/misc/cronjobs/mana_send_pairs_without_mana.pl @@ -0,0 +1,106 @@ +use Modern::Perl; + +use C4::Context; +use Getopt::Long; +use Koha; +use C4::Koha; +use Koha; + +use JSON; +use HTTP::Request; +use LWP::UserAgent; + +my $help = 0; +my $man = 0; +my $verbose = 0; +my $date=DateTime->now()->strftime("%F"); + +print $date."\n"; + + +GetOptions( + 'help|?' => \$help, + 'man' => \$man, + 'v' => \$verbose, + 'date|d:s' => \$date +); + + +$date.="%"; +my $dbh = C4::Context->dbh; +my $query = q{ +SELECT + ExtractValue(biblio_metadata1.metadata, '//datafield[@tag="010"]/subfield[@code="a"]') as isbn1, + ExtractValue(biblio_metadata2.metadata, '//datafield[@tag="010"]/subfield[@code="a"]') as isbn2, + ExtractValue(biblio_metadata1.metadata, '//datafield[@tag="011"]/subfield[@code="a"]') as issn1, + ExtractValue(biblio_metadata2.metadata, '//datafield[@tag="011"]/subfield[@code="a"]') as issn2, + ExtractValue(biblio_metadata1.metadata, '//datafield[@tag="013"]/subfield[@code="a"]') as ismn1, + ExtractValue(biblio_metadata2.metadata, '//datafield[@tag="013"]/subfield[@code="a"]') as ismn2, + ExtractValue(biblio_metadata1.metadata, '//datafield[@tag="015"]/subfield[@code="a"]') as isrn1, + ExtractValue(biblio_metadata2.metadata, '//datafield[@tag="015"]/subfield[@code="a"]') as isrn2, + ExtractValue(biblio_metadata1.metadata, '//datafield[@tag="016"]/subfield[@code="a"]') as isrc1, + ExtractValue(biblio_metadata2.metadata, '//datafield[@tag="016"]/subfield[@code="a"]') as isrc2, + ExtractValue(biblio_metadata1.metadata, '//datafield[@tag="072"]/subfield[@code="a"]') as upc1, + ExtractValue(biblio_metadata2.metadata, '//datafield[@tag="072"]/subfield[@code="a"]') as upc2, + ExtractValue(biblio_metadata1.metadata, '//datafield[@tag="073"]/subfield[@code="a"]') as ean1, + ExtractValue(biblio_metadata2.metadata, '//datafield[@tag="073"]/subfield[@code="a"]') as ean2 +FROM + issues AS issues1 +LEFT JOIN + issues AS issues2 ON issues1.borrowernumber=issues2.borrowernumber +LEFT JOIN + items AS items1 ON issues1.itemnumber=items1.itemnumber +LEFT JOIN + biblio_metadata AS biblio_metadata1 ON items1.biblionumber=biblio_metadata1.biblionumber +LEFT JOIN + items AS items2 ON issues2.itemnumber=items2.itemnumber +LEFT JOIN + biblio_metadata AS biblio_metadata2 ON items2.biblionumber=biblio_metadata2.biblionumber +WHERE + issues1.issuedate LIKE '}.$date. q{' AND biblio_metadata1.biblionumber>biblio_metadata2.biblionumber AND biblio_metadata1.format='marcxml' AND biblio_metadata2.format='marcxml' +ORDER BY + issues1.borrowernumber,biblio_metadata1.biblionumber +}; + +my $sth = $dbh->prepare( $query ); +$sth->execute; + +my $row; +my @reading_pairs; +my $cter = 0; +while ( $row = $sth->fetchrow_hashref ){ +$row->{isbn1} = NormalizeISBN({ isbn => $row->{isbn1}, format => 'ISBN-13', strip_hyphens => 1 }); +$row->{isbn2} = NormalizeISBN({ isbn => $row->{isbn2}, format => 'ISBN-13', strip_hyphens => 1 }); + my $currentpair; + foreach my $key1 ( qw ( isbn issn ismn isrn ean upc isrc ) ){ + foreach my $key2 ( qw ( isbn issn ismn isrn ean upc isrc ) ){ + if ( $row->{ $key1."1" } and $row->{ $key2."2" } ){ + $currentpair->{ idtype1 } = $key1; + $currentpair->{ documentid1 } = $row->{ $key1."1" }; + $currentpair->{ idtype2 } = $key2; + $currentpair->{ documentid2 } = $row->{ $key2."2" }; + push @reading_pairs, $currentpair; + + } + } + } +} + +my $securitytoken = 'THIS_IS_A_TEST_ID'; +my $MANA_IP; +my $content; +$MANA_IP='https://mana-kb.koha-community.org'; +$content->{ securitytoken } = $securitytoken; +$content->{ reading_pairs } = \@reading_pairs; + +my $url = "$MANA_IP/bulk/reading_pair.json"; +my $request = HTTP::Request->new( POST => $url ); + +my $json = to_json( $content, { utf8 => 1 } ); +#print $json; + +$request->content($json); +$request->content_type('aplication/json'); +my $userAgent = LWP::UserAgent->new; +my $response = $userAgent->request( $request ); +print Data::Dumper::Dumper( $response )."\n"; -- 2.17.1