Lines 12-107
use HTTP::Request;
Link Here
|
12 |
use Koha::SharedContent; |
12 |
use Koha::SharedContent; |
13 |
|
13 |
|
14 |
my $help = 0; |
14 |
my $help = 0; |
15 |
my $man = 0; |
|
|
16 |
my $verbose = 0; |
17 |
my $date=DateTime->now()->strftime("%F"); |
18 |
|
19 |
my $securitytoken; |
20 |
my $MANA_IP; |
21 |
|
15 |
|
22 |
GetOptions( |
16 |
GetOptions( |
23 |
'help|?' => \$help, |
17 |
'help|?' => \$help, |
24 |
'man' => \$man, |
|
|
25 |
'v' => \$verbose, |
26 |
'date|d:s' => \$date, |
27 |
'security_token|s:s' => \$securitytoken, |
28 |
'mana_ip|i:s' => \$MANA_IP |
29 |
); |
18 |
); |
30 |
|
19 |
|
31 |
$securitytoken = C4::Context->preference('ManaToken'); |
20 |
if ($help){ |
32 |
my $dbh = C4::Context->dbh; |
21 |
print "This script is used to send reading pairs to Mana KB.\nReading pairs are automatically extracted from Koha database.\n"; |
33 |
my $query = q{ |
22 |
exit; |
34 |
SELECT |
|
|
35 |
ExtractValue(biblio_metadata1.metadata, '//datafield[@tag="010"]/subfield[@code="a"]') as isbn1, |
36 |
ExtractValue(biblio_metadata2.metadata, '//datafield[@tag="010"]/subfield[@code="a"]') as isbn2, |
37 |
ExtractValue(biblio_metadata1.metadata, '//datafield[@tag="011"]/subfield[@code="a"]') as issn1, |
38 |
ExtractValue(biblio_metadata2.metadata, '//datafield[@tag="011"]/subfield[@code="a"]') as issn2, |
39 |
ExtractValue(biblio_metadata1.metadata, '//datafield[@tag="013"]/subfield[@code="a"]') as ismn1, |
40 |
ExtractValue(biblio_metadata2.metadata, '//datafield[@tag="013"]/subfield[@code="a"]') as ismn2, |
41 |
ExtractValue(biblio_metadata1.metadata, '//datafield[@tag="015"]/subfield[@code="a"]') as isrn1, |
42 |
ExtractValue(biblio_metadata2.metadata, '//datafield[@tag="015"]/subfield[@code="a"]') as isrn2, |
43 |
ExtractValue(biblio_metadata1.metadata, '//datafield[@tag="016"]/subfield[@code="a"]') as isrc1, |
44 |
ExtractValue(biblio_metadata2.metadata, '//datafield[@tag="016"]/subfield[@code="a"]') as isrc2, |
45 |
ExtractValue(biblio_metadata1.metadata, '//datafield[@tag="072"]/subfield[@code="a"]') as upc1, |
46 |
ExtractValue(biblio_metadata2.metadata, '//datafield[@tag="072"]/subfield[@code="a"]') as upc2, |
47 |
ExtractValue(biblio_metadata1.metadata, '//datafield[@tag="073"]/subfield[@code="a"]') as ean1, |
48 |
ExtractValue(biblio_metadata2.metadata, '//datafield[@tag="073"]/subfield[@code="a"]') as ean2 |
49 |
FROM |
50 |
issues AS issues1 |
51 |
LEFT JOIN |
52 |
issues AS issues2 ON issues1.borrowernumber=issues2.borrowernumber |
53 |
LEFT JOIN |
54 |
items AS items1 ON issues1.itemnumber=items1.itemnumber |
55 |
LEFT JOIN |
56 |
biblio_metadata AS biblio_metadata1 ON items1.biblionumber=biblio_metadata1.biblionumber |
57 |
LEFT JOIN |
58 |
items AS items2 ON issues2.itemnumber=items2.itemnumber |
59 |
LEFT JOIN |
60 |
biblio_metadata AS biblio_metadata2 ON items2.biblionumber=biblio_metadata2.biblionumber |
61 |
WHERE |
62 |
issues1.issuedate LIKE '}.$date. q{%' AND biblio_metadata1.biblionumber>biblio_metadata2.biblionumber AND biblio_metadata1.format='marcxml' AND biblio_metadata2.format='marcxml' |
63 |
ORDER BY |
64 |
issues1.borrowernumber,biblio_metadata1.biblionumber |
65 |
}; |
23 |
}; |
66 |
|
24 |
|
67 |
my $sth = $dbh->prepare( $query ); |
25 |
#if mana is activated |
68 |
$sth->execute; |
26 |
if (C4::Context->preference("Mana") == 1){ |
69 |
|
27 |
#choose the field to select depending on marc flavour |
70 |
my $row; |
28 |
my $date=DateTime->now()->strftime("%F")."%"; |
71 |
my @reading_pairs; |
29 |
|
72 |
|
30 |
#define the max number of simultaneous issues for a single borrower |
73 |
#for each reading pair, processes the id pairs |
31 |
#if a sigle borrower have more issues than this, no pairs will be created for this borrower |
74 |
while ( $row = $sth->fetchrow_hashref ){ |
32 |
#It is used to exlude huge borrower such as libraries or instution (because the issues won't have any connection) |
75 |
$row->{isbn1} = C4::Koha::NormalizeISBN({ isbn => $row->{isbn1}, format => 'ISBN-13', strip_hyphens => 1 }); |
33 |
my $current_issues_max = 50; |
76 |
$row->{isbn2} = C4::Koha::NormalizeISBN({ isbn => $row->{isbn2}, format => 'ISBN-13', strip_hyphens => 1 }); |
34 |
|
77 |
my $currentpair; |
35 |
my $dbh = C4::Context->dbh; |
78 |
foreach my $key1 ( qw ( isbn issn ismn isrn ean upc isrc ) ){ |
36 |
my $query_part1 = " |
79 |
foreach my $key2 ( qw ( isbn issn ismn isrn ean upc isrc ) ){ |
37 |
SELECT |
80 |
if ( $row->{ $key1."1" } and $row->{ $key2."2" } ){ |
38 |
issues1.borrowernumber as borrowernumber, |
81 |
$currentpair->{ idtype1 } = $key1; |
39 |
biblio_metadata1.biblionumber as biblionumber1, |
82 |
$currentpair->{ documentid1 } = $row->{ $key1."1" }; |
40 |
biblio_metadata2.biblionumber as biblionumber2, |
83 |
$currentpair->{ idtype2 } = $key2; |
41 |
ExtractValue(biblio_metadata1.metadata, '//datafield[\@tag=\"010\"]/subfield[\@code=\"a\"]') as isbn1, |
84 |
$currentpair->{ documentid2 } = $row->{ $key2."2" }; |
42 |
ExtractValue(biblio_metadata2.metadata, '//datafield[\@tag=\"010\"]/subfield[\@code=\"a\"]') as isbn2, |
85 |
push @reading_pairs, $currentpair; |
43 |
ExtractValue(biblio_metadata1.metadata, '//datafield[\@tag=\"011\"]/subfield[\@code=\"a\"]') as issn1, |
|
|
44 |
ExtractValue(biblio_metadata2.metadata, '//datafield[\@tag=\"011\"]/subfield[\@code=\"a\"]') as issn2, |
45 |
ExtractValue(biblio_metadata1.metadata, '//datafield[\@tag=\"013\"]/subfield[\@code=\"a\"]') as ismn1, |
46 |
ExtractValue(biblio_metadata2.metadata, '//datafield[\@tag=\"013\"]/subfield[\@code=\"a\"]') as ismn2, |
47 |
ExtractValue(biblio_metadata1.metadata, '//datafield[\@tag=\"015\"]/subfield[\@code=\"a\"]') as isrn1, |
48 |
ExtractValue(biblio_metadata2.metadata, '//datafield[\@tag=\"015\"]/subfield[\@code=\"a\"]') as isrn2, |
49 |
ExtractValue(biblio_metadata1.metadata, '//datafield[\@tag=\"016\"]/subfield[\@code=\"a\"]') as isrc1, |
50 |
ExtractValue(biblio_metadata2.metadata, '//datafield[\@tag=\"016\"]/subfield[\@code=\"a\"]') as isrc2, |
51 |
ExtractValue(biblio_metadata1.metadata, '//datafield[\@tag=\"072\"]/subfield[\@code=\"a\"]') as upc1, |
52 |
ExtractValue(biblio_metadata2.metadata, '//datafield[\@tag=\"072\"]/subfield[\@code=\"a\"]') as upc2, |
53 |
ExtractValue(biblio_metadata1.metadata, '//datafield[\@tag=\"073\"]/subfield[\@code=\"a\"]') as ean1, |
54 |
ExtractValue(biblio_metadata2.metadata, '//datafield[\@tag=\"073\"]/subfield[\@code=\"a\"]') as ean2 |
55 |
FROM |
56 |
issues AS issues1 |
57 |
JOIN |
58 |
(SELECT borrowernumber FROM issues GROUP BY borrowernumber HAVING COUNT(*) < $current_issues_max) AS number ON number.borrowernumber=issues1.borrowernumber |
59 |
JOIN |
60 |
issues AS issues2 ON issues1.borrowernumber=issues2.borrowernumber |
61 |
JOIN |
62 |
items AS items1 ON issues1.itemnumber=items1.itemnumber |
63 |
JOIN |
64 |
biblio_metadata AS biblio_metadata1 ON items1.biblionumber=biblio_metadata1.biblionumber |
65 |
JOIN |
66 |
items AS items2 ON issues2.itemnumber=items2.itemnumber |
67 |
JOIN |
68 |
biblio_metadata AS biblio_metadata2 ON items2.biblionumber=biblio_metadata2.biblionumber |
69 |
WHERE |
70 |
issues1.issuedate LIKE ? |
71 |
AND items1.biblionumber != items2.biblionumber |
72 |
AND (DATE(issues1.issuedate) != DATE(issues2.issuedate) OR items1.biblionumber < items2.biblionumber) |
73 |
AND biblio_metadata1.format='marcxml' |
74 |
AND biblio_metadata2.format='marcxml' |
75 |
"; |
76 |
my $query_part2 = $query_part1; |
77 |
$query_part2 =~ s/issues AS issues2/old_issues AS issues2/; |
78 |
|
79 |
# query_part1 match issues of the day with current issues |
80 |
# query-part2 match issues of the day with old issues |
81 |
my $query = $query_part1."UNION ALL".$query_part2; |
82 |
|
83 |
my $sth = $dbh->prepare( $query ); |
84 |
$sth->execute($date, $date); |
85 |
|
86 |
my $row; |
87 |
my @reading_pairs; |
88 |
|
89 |
#for each reading pair, processes the id pairs |
90 |
while ( $row = $sth->fetchrow_hashref ){ |
91 |
#if necessary, normalize the ids |
92 |
$row->{isbn1} = C4::Koha::NormalizeISBN({ isbns => $row->{isbn1}, format => 'ISBN-13', strip_hyphens => 1 }) if $row->{isbn1}; |
93 |
$row->{isbn2} = C4::Koha::NormalizeISBN({ isbns => $row->{isbn2}, format => 'ISBN-13', strip_hyphens => 1 }) if $row->{isbn2}; |
94 |
|
95 |
# try to make pairs between every id possible |
96 |
foreach my $key1 ( qw ( isbn issn ismn isrn ean upc isrc ) ){ |
97 |
if ( $row->{ $key1."1" }){ |
98 |
foreach my $key2 ( qw ( isbn issn ismn isrn ean upc isrc ) ){ |
99 |
# if both selected ids exists |
100 |
if ( $row->{ $key2."2" } ){ |
101 |
# make pairs between every ids of the given type |
102 |
my $currentpair; |
103 |
$currentpair->{ idtype1 } = $key1; |
104 |
$currentpair->{ documentid1 } = $row->{$key1."1"}; |
105 |
$currentpair->{ idtype2 } = $key2; |
106 |
$currentpair->{ documentid2 } = $row->{$key2."2"}; |
107 |
push @reading_pairs, $currentpair; |
108 |
} |
109 |
} |
86 |
} |
110 |
} |
87 |
} |
111 |
} |
88 |
} |
112 |
} |
89 |
} |
|
|
90 |
|
113 |
|
91 |
my $content; |
114 |
my $content; |
92 |
|
115 |
|
93 |
#informations for mana |
116 |
#informations for mana |
94 |
$MANA_IP=C4::Context->config("mana_config"); |
117 |
my $MANA_IP=C4::Context->config("mana_config"); |
95 |
$content->{ securitytoken } = $securitytoken; |
|
|
96 |
$content->{ reading_pairs } = \@reading_pairs; |
97 |
|
118 |
|
98 |
my $url = "$MANA_IP/bulk/reading_pair.json"; |
119 |
my $securitytoken = C4::Context->preference('ManaToken'); |
99 |
my $request = HTTP::Request->new( POST => $url ); |
120 |
$content->{ securitytoken } = $securitytoken; |
|
|
121 |
$content->{ reading_pairs } = \@reading_pairs; |
100 |
|
122 |
|
101 |
my $json = to_json( $content, { utf8 => 1 } ); |
123 |
my $url = "$MANA_IP/bulk/reading_pair.json"; |
102 |
#print $json; |
124 |
my $request = HTTP::Request->new( POST => $url ); |
103 |
|
125 |
|
104 |
#send to mana |
126 |
my $json = to_json( $content, { utf8 => 1 } ); |
105 |
$request->content($json); |
127 |
#print $json; |
106 |
my $result = Koha::SharedContent::process_request( $request ); |
128 |
|
107 |
print Data::Dumper::Dumper( $result ); |
129 |
#send to mana |
|
|
130 |
$request->content($json); |
131 |
my $result = Koha::SharedContent::process_request( $request ); |
132 |
#print Data::Dumper::Dumper( $result ); |
133 |
} |