Bug 10777: email HTML reports with the proper Content-Type
[koha_fer] / misc / cronjobs / build_browser_and_cloud.pl
index 263d886..d3ead43 100755 (executable)
@@ -2,6 +2,7 @@
 # small script that builds the tag cloud
 
 use strict;
+#use warnings; FIXME - Bug 2505
 BEGIN {
     # find Koha's Perl modules
     # test carefully before changing this
@@ -56,7 +57,7 @@ $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;
 
 my $dbh = C4::Context->dbh;
@@ -79,6 +80,7 @@ my %browser_result;
 
 # the result hash for the cloud table
 my %cloud_result;
+
 while ((my ($biblionumber)= $sth->fetchrow)) {
     $i++;
     print "." unless $batch;
@@ -92,7 +94,7 @@ while ((my ($biblionumber)= $sth->fetchrow)) {
            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;
@@ -103,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;
         }
     }
 
@@ -121,7 +127,7 @@ 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') =~ m/^fr/i && $browser_tag eq '676' & $browser_subfield eq 'a') {
@@ -140,11 +146,19 @@ if ($browser_tag) {
 }
 
 # 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");