X-Git-Url: http://koha-dev.rot13.org:8081/gitweb/?a=blobdiff_plain;f=misc%2Fcronjobs%2Fbuild_browser_and_cloud.pl;h=c8b12611fbef9818fa729c32b16d95a5a9ba0828;hb=88f63e01d29917e9323c57be97aaa91b9e00fd92;hp=ecce40e7e4121409c559cf8efe9f2ca4ae1fc7ee;hpb=6f7efca7e1e4070e402e53322fea823e564f5a00;p=srvgit diff --git a/misc/cronjobs/build_browser_and_cloud.pl b/misc/cronjobs/build_browser_and_cloud.pl index ecce40e7e4..c8b12611fb 100755 --- a/misc/cronjobs/build_browser_and_cloud.pl +++ b/misc/cronjobs/build_browser_and_cloud.pl @@ -2,17 +2,21 @@ # small script that builds the tag cloud use strict; +#use warnings; FIXME - Bug 2505 +use Koha::Script -cron; use C4::Koha; use C4::Context; -use C4::Biblio; use Date::Calc; use Time::HiRes qw(gettimeofday); use ZOOM; use MARC::File::USMARC; use Getopt::Long; +use C4::Log; +use Koha::Biblios; + my ( $input_marc_file, $number) = ('',0); -my ($version, $confirm,$test_parameter,$field,$batch,$max_digits,$cloud_tag); +my ($version, $confirm,$field,$batch,$max_digits,$cloud_tag); GetOptions( 'c' => \$confirm, 'h' => \$version, @@ -34,10 +38,10 @@ if ($version || (!$confirm)) { -t TTT to define the MARC fields/subfield to use to fill the tag cloud. If not defined, the cloud table won't be filled. example : - export PERL5LIB=/path/to/koha;export KOHA_CONF=/etc/koha.xml;./build_browser_and_cloud.pl -b -f 676a -t 606 -c + export PERL5LIB=/path/to/koha;export KOHA_CONF=/etc/koha/koha-conf.xml;./build_browser_and_cloud.pl -b -f 676a -t 606 -c EOF ; -die; +exit; } ################################## @@ -50,9 +54,11 @@ $max_digits=3 unless $max_digits; $field =~ /(\d\d\d)(.?)/; my $browser_tag = $1; my $browser_subfield = $2; -warn "browser : $browser_tag / $browser_subfield"; +warn "browser : $browser_tag / $browser_subfield" unless $batch; die "no cloud or browser field/subfield defined : nothing to do !" unless $browser_tag or $cloud_tag; +cronlogaction(); + my $dbh = C4::Context->dbh; my $i=0; @@ -73,20 +79,22 @@ my %browser_result; # the result hash for the cloud table my %cloud_result; + while ((my ($biblionumber)= $sth->fetchrow)) { $i++; print "." unless $batch; #now, parse the record, extract the item fields, and store them in somewhere else. my $Koharecord; + my $biblio = Koha::Biblios->find($biblionumber); eval{ - $Koharecord = GetMarcBiblio($biblionumber); + $Koharecord = $biblio->metadata->record }; if($@){ warn 'pb when getting biblio '.$i.' : '.$@; next; } # deal with BROWSER part - if ($browser_tag) { + if ($browser_tag && $Koharecord) { foreach my $browsed_field ($Koharecord->subfield($browser_tag,$browser_subfield)) { $browsed_field =~ s/\.//g; my $upto = length($browsed_field)<=$max_digits?length($browsed_field):$max_digits; @@ -97,15 +105,19 @@ while ((my ($biblionumber)= $sth->fetchrow)) { } } #deal with CLOUD part - if ($cloud_tag) { - foreach ($Koharecord->field($cloud_tag)) { - my $line; - foreach ($_->subfields()) { - next if $_->[0]=~ /\d/; - $line .= $_->[1].' '; + if ($cloud_tag && $Koharecord) { + if($Koharecord->field($cloud_tag)){ + foreach ($Koharecord->field($cloud_tag)) { + my $line; + foreach ($_->subfields()) { + next if $_->[0]=~ /\d/; + $line .= $_->[1].' '; + } + $line =~ s/ $//; + $cloud_result{$line}++; } - $line =~ s/ $//; - $cloud_result{$line}++; + }else{ + print "!" unless $batch; } } @@ -115,12 +127,13 @@ while ((my ($biblionumber)= $sth->fetchrow)) { # fills the browser table if ($browser_tag) { - print "inserting datas in browser table\n"; + print "inserting datas in browser table\n" unless $batch; # read existing classification table is possible my $classification; - if (C4::Context->preference('opaclanguages') eq 'fr' && $browser_tag eq '676' & $browser_subfield eq 'a') { + if (C4::Context->preference('OPACLanguages') =~ m/^fr/i && $browser_tag eq '676' & $browser_subfield eq 'a') { $classification = dewey_french(); } + foreach (keys %browser_result) { my $father = substr($_,0,-1); $browser_result{$father}->{notendnode}=1; @@ -128,16 +141,24 @@ if ($browser_tag) { $dbh->do("truncate browser"); my $sth = $dbh->prepare("insert into browser (level,classification,description,number,endnode) values (?,?,?,?,?)"); foreach (keys %browser_result) { - $sth->execute(length($_),$_,$classification->{$_}?$classification->{$_}:"classification $_",$browser_result{$_}->{value},$browser_result{$_}->{notendnode}?0:1); + $sth->execute(length($_),$_,$classification->{$_}?$classification->{$_}:"classification $_",$browser_result{$_}->{value},$browser_result{$_}->{notendnode}?0:1) if $browser_result{$_}->{value}; } } # fills the cloud (tags) table +my $sthver = $dbh->prepare("SELECT weight FROM tags WHERE entry = ? "); +my $sthins = $dbh->prepare("insert into tags (entry,weight) values (?,?)"); +my $sthup = $dbh->prepare("UPDATE tags SET weight = ? WHERE entry = ?"); if ($cloud_tag) { $dbh->do("truncate tags"); - my $sth = $dbh->prepare("insert into tags (entry,weight) values (?,?)"); - foreach (keys %cloud_result) { - $sth->execute($_,$cloud_result{$_}); + foreach my $key (keys %cloud_result) { + $sthver->execute($key); + if(my $row = $sthver->fetchrow_hashref){ + my $count = $row->{weight} + $cloud_result{$key}; + $sthup->execute($count, $key); + }else{ + $sthins->execute($key,$cloud_result{$key}); + } } } # $dbh->do("unlock tables");