View | Details | Raw Unified | Return to bug 18618
Collapse All | Expand All

(-)a/misc/cronjobs/mana_send_pairs.pl (-78 / +104 lines)
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
}
(-)a/opac/svc/mana/getSuggestion (-10 / +62 lines)
Lines 37-43 use DateTime; Link Here
37
use DateTime::Format::MySQL;
37
use DateTime::Format::MySQL;
38
use DateTime::Duration;
38
use DateTime::Duration;
39
39
40
use constant DURATION_BEFORE_REFRESH => DateTime::Duration->new( months => 3 );
40
use constant DURATION_BEFORE_REFRESH => DateTime::Duration->new( weeks => 1 );
41
use C4::Output qw( output_with_http_headers );
41
use C4::Output qw( output_with_http_headers );
42
my $input = CGI->new;
42
my $input = CGI->new;
43
binmode STDOUT, ":encoding(UTF-8)";
43
binmode STDOUT, ":encoding(UTF-8)";
Lines 60-76 my $local_suggestions; Link Here
60
60
61
my $now;
61
my $now;
62
my $timestamp;
62
my $timestamp;
63
my $temp;
63
my $duration;
64
#check it doesn't already have the informations localy stored
64
#check it doesn't already have the informations localy stored
65
eval {
65
eval {
66
    $local_suggestions = Koha::Reading_suggestions->find( $biblionumber )->unblessed();
66
    $local_suggestions = Koha::Reading_suggestions->find( $biblionumber )->unblessed();
67
    $now = DateTime->now();
67
    $now = DateTime->now();
68
    $timestamp = DateTime::Format::MySQL->parse_timestamp( $local_suggestions->{timestamp} );
68
    $timestamp = DateTime::Format::MySQL->parse_timestamp( $local_suggestions->{timestamp} );
69
    $temp = $now - $timestamp;
69
    $duration = $now - $timestamp;
70
};
70
};
71
my @biblios;
71
my @biblios;
72
my @biblios_blessed;
72
my @biblios_blessed;
73
if ( $local_suggestions and DateTime::Duration->compare( ($now - $timestamp), DURATION_BEFORE_REFRESH ) == -1 ){
73
if ( $local_suggestions and DateTime::Duration->compare( $duration, DURATION_BEFORE_REFRESH ) == -1 ){
74
        delete $local_suggestions->{timestamp};
74
        delete $local_suggestions->{timestamp};
75
        delete $local_suggestions->{biblionumber};
75
        delete $local_suggestions->{biblionumber};
76
        foreach my $key ( sort keys %{ $local_suggestions }){
76
        foreach my $key ( sort keys %{ $local_suggestions }){
Lines 84-103 else{ Link Here
84
    my $idtype;
84
    my $idtype;
85
    my $documentid;
85
    my $documentid;
86
    my $notnormalizeddocumentid;
86
    my $notnormalizeddocumentid;
87
    if ( $biblioitem->isbn ){
87
    my $found = 0;
88
89
    if ( $biblioitem && $biblioitem->isbn ){
88
        $idtype= "isbn";
90
        $idtype= "isbn";
89
        $documentid= C4::Koha::NormalizeISBN({ isbn => $biblioitem->isbn, format => 'ISBN-13', strip_hyphens => 1 });
91
        $documentid= C4::Koha::NormalizeISBN({ isbn => $biblioitem->isbn, format => 'ISBN-13', strip_hyphens => 1 });
92
        if ($documentids) { $found = 1; };
90
    }
93
    }
91
    elsif ( $biblioitem->issn ){
94
    elsif ( $biblioitem && $biblioitem->issn ){
92
        $idtype= "issn";
95
        $idtype= "issn";
93
        $documentid= $biblioitem->issn;
96
        $documentid= $biblioitem->issn;
97
        if ($documentids) { $found = 1; };
94
    }
98
    }
95
    elsif ( $biblioitem->ean ){
99
    elsif ( $biblioitem && $biblioitem->ean ){
96
        $idtype= "ean";
100
        $idtype= "ean";
97
        $documentid= $biblioitem->ean;
101
        $documentid= $biblioitem->ean;
102
        if ($documentids) { $found = 1; };
98
    }
103
    }
99
    else{
104
    if ($found == 0){
100
        die "error: no propper identifier";
105
        my $dbh = C4::Context->dbh;
106
        my $query = "
107
            SELECT
108
              ExtractValue(metadata, '//datafield[\@tag=\"010\"]/subfield[\@code=\"a\"]') as isbn,
109
              ExtractValue(metadata, '//datafield[\@tag=\"011\"]/subfield[\@code=\"a\"]') as issn,
110
              ExtractValue(metadata, '//datafield[\@tag=\"013\"]/subfield[\@code=\"a\"]') as ismn,
111
              ExtractValue(metadata, '//datafield[\@tag=\"015\"]/subfield[\@code=\"a\"]') as isrn,
112
              ExtractValue(metadata, '//datafield[\@tag=\"016\"]/subfield[\@code=\"a\"]') as isrc,
113
              ExtractValue(metadata, '//datafield[\@tag=\"072\"]/subfield[\@code=\"a\"]') as upc,
114
              ExtractValue(metadata, '//datafield[\@tag=\"073\"]/subfield[\@code=\"a\"]') as ean
115
            FROM biblio_metadata
116
            WHERE biblionumber = ?
117
              AND format=\"marcxml\"
118
        ";
119
        my $sth = $dbh->prepare( $query );
120
        $sth->execute($biblionumber);
121
        my $row = $sth->fetchrow_hashref;
122
123
        if ($row->{isbn}){
124
            $idtype = "isbn";
125
            $documentid = C4::Koha::NormalizeISBN({ isbn => $row->{isbn}, format => 'ISBN-13', strip_hyphens => 1 });
126
        }
127
        elsif ($row->{issn}){
128
            $idtype = "issn";
129
            $documentid = $row->{issn};
130
        }
131
        elsif ($row->{ismn}){
132
            $idtype = "ismn";
133
            $documentid = $row->{ismn};
134
        }
135
        elsif ($row->{isrn}){
136
            $idtype = "isrn";
137
            $documentid = $row->{isrn};
138
        }
139
        elsif ($row->{isrc}){
140
            $idtype = "isrc";
141
            $documentid = $row->{isrc};
142
        }
143
        elsif ($row->{upc}){
144
            $idtype = "upc";
145
            $documentid = $row->{upc};
146
        }
147
        elsif ($row->{ean}){
148
            $idtype = "ean";
149
            $documentid = $row->{ean};
150
        }
151
        else{
152
            die "error: no propper identifier";
153
        }
101
    }
154
    }
102
    $notnormalizeddocumentid = $documentid;
155
    $notnormalizeddocumentid = $documentid;
103
    my $offset = 1;
156
    my $offset = 1;
104
- 

Return to bug 18618