Line 0
Link Here
|
|
|
1 |
package C4::SocialData; |
2 |
|
3 |
# This file is part of Koha. |
4 |
# |
5 |
# Koha is free software; you can redistribute it and/or modify it under the |
6 |
# terms of the GNU General Public License as published by the Free Software |
7 |
# Foundation; either version 2 of the License, or (at your option) any later |
8 |
# version. |
9 |
# |
10 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
11 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
12 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
13 |
# |
14 |
# You should have received a copy of the GNU General Public License along with |
15 |
# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place, |
16 |
# Suite 330, Boston, MA 02111-1307 USA |
17 |
|
18 |
use Modern::Perl; |
19 |
|
20 |
use C4::Context; |
21 |
use Business::ISBN; |
22 |
use C4::Koha; |
23 |
|
24 |
=head2 get_data |
25 |
|
26 |
Get social data from a biblio |
27 |
|
28 |
params: |
29 |
$isbn = isbn of the biblio (it must be the same in your database, isbn given to babelio) |
30 |
|
31 |
returns: |
32 |
this function returns an hashref with keys |
33 |
|
34 |
isbn = isbn |
35 |
num_critics = number of critics |
36 |
num_critics_pro = number of profesionnal critics |
37 |
num_quotations = number of quotations |
38 |
num_videos = number of videos |
39 |
score_avg = average score |
40 |
num_scores = number of score |
41 |
=cut |
42 |
sub get_data { |
43 |
my ( $isbn ) = @_; |
44 |
my $dbh = C4::Context->dbh; |
45 |
my $sth = $dbh->prepare( qq{SELECT * FROM social_data WHERE isbn = ? LIMIT 1} ); |
46 |
$sth->execute( $isbn ); |
47 |
my $results = $sth->fetchrow_hashref; |
48 |
|
49 |
return $results; |
50 |
} |
51 |
|
52 |
=head 2 |
53 |
|
54 |
Update Social data |
55 |
|
56 |
params: |
57 |
$url = url containing csv file with data |
58 |
|
59 |
data separator : ; (semicolon) |
60 |
data order : isbn ; active ; critics number , critics pro number ; quotations number ; videos number ; average score ; scores number |
61 |
|
62 |
=cut |
63 |
sub update_data { |
64 |
my ( $output_filepath ) = @_; |
65 |
|
66 |
my $dbh = C4::Context->dbh; |
67 |
my $sth = $dbh->prepare( qq{INSERT INTO social_data ( |
68 |
`isbn`, `num_critics`, `num_critics_pro`, `num_quotations`, `num_videos`, `score_avg`, `num_scores` |
69 |
) VALUES ( ?, ?, ?, ?, ?, ?, ? ) |
70 |
ON DUPLICATE KEY UPDATE `num_critics`=?, `num_critics_pro`=?, `num_quotations`=?, `num_videos`=?, `score_avg`=?, `num_scores`=? |
71 |
} ); |
72 |
|
73 |
open my $file, '<', $output_filepath or die "File $output_filepath can not be read"; |
74 |
my $sep = qq{;}; |
75 |
my $i = 0; |
76 |
my $unknown = 0; |
77 |
while ( my $line = <$file> ) { |
78 |
my ( $isbn, $active, $num_critics, $num_critics_pro, $num_quotations, $num_videos, $score_avg, $num_scores ) = split $sep, $line; |
79 |
next if not $active; |
80 |
eval { |
81 |
$sth->execute( $isbn, $num_critics, $num_critics_pro, $num_quotations, $num_videos, $score_avg, $num_scores, |
82 |
$num_critics, $num_critics_pro, $num_quotations, $num_videos, $score_avg, $num_scores |
83 |
); |
84 |
}; |
85 |
if ( $@ ) { |
86 |
warn "Can't insert $isbn ($@)"; |
87 |
} else { |
88 |
$i++; |
89 |
} |
90 |
} |
91 |
say "$i data insered or updated"; |
92 |
} |
93 |
|
94 |
=head 2 |
95 |
|
96 |
Get social data report |
97 |
|
98 |
=cut |
99 |
sub get_report { |
100 |
my $dbh = C4::Context->dbh; |
101 |
|
102 |
my $sth = $dbh->prepare( qq{ |
103 |
SELECT biblionumber, isbn FROM biblioitems |
104 |
} ); |
105 |
$sth->execute; |
106 |
my %results; |
107 |
while ( my ( $biblionumber, $isbn ) = $sth->fetchrow() ) { |
108 |
push @{ $results{no_isbn} }, { biblionumber => $biblionumber } and next if not $isbn; |
109 |
my $original_isbn = $isbn; |
110 |
$isbn =~ s/^\s*(\S*)\s*$/$1/; |
111 |
$isbn = GetNormalizedISBN( $isbn, undef, undef ); |
112 |
$isbn = Business::ISBN->new( $isbn ); |
113 |
next if not $isbn; |
114 |
eval{ |
115 |
$isbn = $isbn->as_isbn13->as_string; |
116 |
}; |
117 |
next if $@; |
118 |
$isbn =~ s/-//g; |
119 |
my $social_datas = C4::SocialData::get_data( $isbn ); |
120 |
if ( $social_datas ) { |
121 |
push @{ $results{with} }, { biblionumber => $biblionumber, isbn => $isbn, original => $original_isbn }; |
122 |
} else { |
123 |
push @{ $results{without} }, { biblionumber => $biblionumber, isbn => $isbn, original => $original_isbn }; |
124 |
} |
125 |
} |
126 |
return \%results; |
127 |
} |
128 |
|
129 |
1; |
130 |
|