Replaced expressions of the form "$x = $x <op> $y" with "$x <op>= $y".
[koha_gimpoz] / C4 / Biblio.pm
index dce6212..16bb804 100644 (file)
@@ -1,6 +1,60 @@
-package C4::Biblio; 
+package C4::Biblio;
 # $Id$
 # $Log$
+# Revision 1.21  2002/10/13 11:34:14  arensb
+# Replaced expressions of the form "$x = $x <op> $y" with "$x <op>= $y".
+# Thus, $x = $x+2 becomes $x += 2, and so forth.
+#
+# Revision 1.20  2002/10/13 08:28:32  arensb
+# Deleted unused variables.
+# Removed trailing whitespace.
+#
+# Revision 1.19  2002/10/13 05:56:10  arensb
+# Added some FIXME comments.
+#
+# Revision 1.18  2002/10/11 12:34:53  arensb
+# Replaced &requireDBI with C4::Context->dbh
+#
+# Revision 1.17  2002/10/10 14:48:25  tipaul
+# bugfixes
+#
+# Revision 1.16  2002/10/07 14:04:26  tipaul
+# road to 1.3.1 : viewing MARC biblio
+#
+# Revision 1.15  2002/10/05 09:49:25  arensb
+# Merged with arensb-context branch: use C4::Context->dbh instead of
+# &C4Connect, and generally prefer C4::Context over C4::Database.
+#
+# Revision 1.14  2002/10/03 11:28:18  tipaul
+# Extending Context.pm to add stopword management and using it in MARC-API.
+# First benchmarks show a medium speed improvement, which  is nice as this part is heavily called.
+#
+# Revision 1.13  2002/10/02 16:26:44  tipaul
+# road to 1.3.1
+#
+# Revision 1.12.2.4  2002/10/05 07:09:31  arensb
+# Merged in changes from main branch.
+#
+# Revision 1.12.2.3  2002/10/05 06:12:10  arensb
+# Added a whole mess of FIXME comments.
+#
+# Revision 1.12.2.2  2002/10/05 04:03:14  arensb
+# Added some missing semicolons.
+#
+# Revision 1.12.2.1  2002/10/04 02:24:01  arensb
+# Use C4::Connect instead of C4::Database, C4::Connect->dbh instead
+# C4Connect.
+#
+# Revision 1.12.2.3  2002/10/05 06:12:10  arensb
+# Added a whole mess of FIXME comments.
+#
+# Revision 1.12.2.2  2002/10/05 04:03:14  arensb
+# Added some missing semicolons.
+#
+# Revision 1.12.2.1  2002/10/04 02:24:01  arensb
+# Use C4::Connect instead of C4::Database, C4::Connect->dbh instead
+# C4Connect.
+#
 # Revision 1.12  2002/10/01 11:48:51  arensb
 # Added some FIXME comments, mostly marking duplicate functions.
 #
@@ -53,7 +107,7 @@ package C4::Biblio;
 #
 
 
-# move from 1.2 to 1.4 version : 
+# move from 1.2 to 1.4 version :
 # 1.2 and previous version uses a specific API to manage biblios. This API uses old-DB style parameters.
 # In the 1.4 version, we want to do 2 differents things :
 #  - keep populating the old-DB, that has a LOT less datas than MARC
@@ -99,10 +153,11 @@ package C4::Biblio;
 
 use strict;
 require Exporter;
+use C4::Context;
 use C4::Database;
 use MARC::Record;
 
-use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
+use vars qw($VERSION @ISA @EXPORT);
 
 # set the version for version checking
 $VERSION = 0.01;
@@ -113,69 +168,33 @@ $VERSION = 0.01;
 # as the old-style API and the ALL one are the only public functions.
 #
 @EXPORT = qw(
-            &updateBiblio &updateBiblioItem &updateItem 
-            &itemcount &newbiblio &newbiblioitem 
+            &updateBiblio &updateBiblioItem &updateItem
+            &itemcount &newbiblio &newbiblioitem
             &modnote &newsubject &newsubtitle
             &modbiblio &checkitems
             &newitems &modbibitem
-            &modsubtitle &modsubject &modaddauthor &moditem &countitems 
-            &delitem &deletebiblioitem &delbiblio  
+            &modsubtitle &modsubject &modaddauthor &moditem &countitems
+            &delitem &deletebiblioitem &delbiblio
             &getitemtypes &getbiblio
             &getbiblioitembybiblionumber
             &getbiblioitem &getitemsbybiblioitem &isbnsearch
             &skip
             &newcompletebiblioitem
 
+            &MARCfind_oldbiblionumber_from_MARCbibid
+            &MARCfind_MARCbibid_from_oldbiblionumber
+
             &ALLnewbiblio &ALLnewitem
 
             &MARCgettagslib
             &MARCaddbiblio &MARCadditem
-            &MARCmodsubfield &MARCaddsubfield 
+            &MARCmodsubfield &MARCaddsubfield
             &MARCmodbiblio &MARCmoditem
-            &MARCfindsubfield 
+            &MARCfindsubfield
             &MARCkoha2marcBiblio &MARCmarc2koha &MARCkoha2marcItem
             &MARCgetbiblio &MARCgetitem
             &MARCaddword &MARCdelword
  );
-%EXPORT_TAGS = ( );     # eg: TAG => [ qw!name1 name2! ],
-
-# your exported package globals go here,
-# as well as any optionally exported functions
-
-@EXPORT_OK   = qw($Var1 %Hashit);      # FIXME - These are never used
-
-
-# non-exported package globals go here
-use vars qw(@more $stuff);             # FIXME - These are never used
-
-# initalize package globals, first exported ones
-
-# FIXME - These are never used
-my $Var1   = '';
-my %Hashit = ();
-
-
-# then the others (which are still accessible as $Some::Module::stuff)
-# FIXME - These are never used
-my $stuff  = '';
-my @more   = ();
-
-# all file-scoped lexicals must be created before
-# the functions below that use them.
-
-# file-private lexicals go here
-# FIXME - These are never used
-my $priv_var    = '';
-my %secret_hash = ();
-
-# here's a file-private function as a closure,
-# callable as &$priv_func;  it cannot be prototyped.
-# FIXME - This is never used
-my $priv_func = sub {
-  # stuff goes here.
-  };
-  
-# make all your functions, whether exported or not;
 
 #
 #
@@ -183,7 +202,7 @@ my $priv_func = sub {
 #
 #
 # all the following subs takes a MARC::Record as parameter and manage
-# the MARC-DB. They are called by the 1.0/1.2 xxx subs, and by the 
+# the MARC-DB. They are called by the 1.0/1.2 xxx subs, and by the
 # ALLxxx subs (xxx deals with old-DB parameters, the ALLxxx deals with MARC-DB parameter)
 
 =head1 SYNOPSIS
@@ -213,7 +232,7 @@ my $priv_func = sub {
 
 =head2 &MARCaddsubfield($dbh,$bibid,$tagid,$indicator,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalue);
       adds a subfield in a biblio (in the MARC tables only).
-     
+
 =head2 $MARCRecord = &MARCgetbiblio($dbh,$bibid);
       Returns a MARC::Record for the biblio $bibid.
 
@@ -269,17 +288,28 @@ sub MARCgettagslib {
     my ($dbh,$forlibrarian)= @_;
     my $sth;
     if ($forlibrarian eq 1) {
-       $sth=$dbh->prepare("select tagfield,tagsubfield,liblibrarian as lib from marc_subfield_structure");
+       $sth=$dbh->prepare("select tagfield,liblibrarian as lib from marc_tag_structure");
+    } else {
+       $sth=$dbh->prepare("select tagfield,libopac as lib from marc_tag_structure");
+    }
+    $sth->execute;
+    my ($lib,$tag,$res,$tab);
+    while ( ($tag,$lib,$tab) = $sth->fetchrow) {
+       $res->{$tag}->{lib}=$lib;
+       $res->{$tab}->{tab}="";
+    }
+
+    if ($forlibrarian eq 1) {
+       $sth=$dbh->prepare("select tagfield,tagsubfield,liblibrarian as lib,tab from marc_subfield_structure");
     } else {
-       $sth=$dbh->prepare("select tagfield,tagsubfield,libopac as lib from marc_subfield_structure");
+       $sth=$dbh->prepare("select tagfield,tagsubfield,libopac as lib,tab from marc_subfield_structure");
     }
     $sth->execute;
-    my $lib;
-    my $tag;
+
     my $subfield;
-    my $res;
-    while ( ($tag,$subfield,$lib) = $sth->fetchrow) {
-       $res->{$tag}->{$subfield}=$lib;
+    while ( ($tag,$subfield,$lib,$tab) = $sth->fetchrow) {
+       $res->{$tag}->{$subfield}->{lib}=$lib;
+       $res->{$tag}->{$subfield}->{tab}=$tab;
     }
     return $res;
 }
@@ -413,42 +443,38 @@ sub MARCgetbiblio {
     my ($dbh,$bibid)=@_;
     my $record = MARC::Record->new();
 #---- TODO : the leader is missing
-    my $sth=$dbh->prepare("select bibid,subfieldid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue,valuebloblink 
-                                from marc_subfield_table 
+    my $sth=$dbh->prepare("select bibid,subfieldid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue,valuebloblink
+                                from marc_subfield_table
                                 where bibid=? order by tagorder,subfieldorder
                         ");
     my $sth2=$dbh->prepare("select subfieldvalue from marc_blob_subfield where blobidlink=?");
     $sth->execute($bibid);
+    my $prevtagorder=1;
+    my $prevtag;
+    my %subfieldlist={};
     while (my $row=$sth->fetchrow_hashref) {
-       if ($row->{'valuebloblink'}) { #---- search blob if there is one
-           $sth2->execute($row->{'valuebloblink'});
-           my $row2=$sth2->fetchrow_hashref;
-           $sth2->finish;
-           $row->{'subfieldvalue'}=$row2->{'subfieldvalue'};
-       }
-       if ($record->field($row->{'tag'})) {
-           my $field;
-#--- this test must stay as this, because of strange behaviour of mySQL/Perl DBI with char var containing a number...
-#--- sometimes, eliminates 0 at beginning, sometimes no ;-\\\
-           if (length($row->{'tag'}) <3) {
-               $row->{'tag'} = "0".$row->{'tag'};
-           }
-           $field =$record->field($row->{'tag'});
-           if ($field) {
-               my $x = $field->add_subfields($row->{'subfieldcode'},$row->{'subfieldvalue'});
-               $record->delete_field($field);
-               $record->add_fields($field);
-           }
-       } else {
-           if (length($row->{'tag'}) < 3) {
-               $row->{'tag'} = "0".$row->{'tag'};
-           }
-           my $temp = MARC::Field->new($row->{'tag'}," "," ", $row->{'subfieldcode'} => $row->{'subfieldvalue'});
-           $record->add_fields($temp);
+               if ($row->{'valuebloblink'}) { #---- search blob if there is one
+                       $sth2->execute($row->{'valuebloblink'});
+                       my $row2=$sth2->fetchrow_hashref;
+                       $sth2->finish;
+                       $row->{'subfieldvalue'}=$row2->{'subfieldvalue'};
+               }
+               if ($row->{tagorder} ne $prevtagorder) {
+                       my $field = MARC::Field->new( $prevtag, "", "", %subfieldlist);
+                       $record->add_fields($field);
+                       $prevtagorder=$row->{tagorder};
+                       $prevtag = $row->{tag};
+                       %subfieldlist={};
+                       %subfieldlist->{$row->{'subfieldcode'}} = $row->{'subfieldvalue'};
+               } else {
+                       %subfieldlist->{$row->{'subfieldcode'}} = $row->{'subfieldvalue'};
+                       $prevtag= $row->{tag};
+               }
        }
-
-    }
-    return $record;
+       # the last has not been included inside the loop... do it now !
+       my $field = MARC::Field->new( $prevtag, "", "", %subfieldlist);
+       $record->add_fields($field);
+       return $record;
 }
 sub MARCgetitem {
 # Returns MARC::Record of the biblio passed in parameter.
@@ -460,10 +486,11 @@ sub MARCgetitem {
     $sth2->execute($bibid,$itemnumber);
     my ($tagorder) = $sth2->fetchrow_array();
 #---- TODO : the leader is missing
-    my $sth=$dbh->prepare("select bibid,subfieldid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue,valuebloblink 
-                                from marc_subfield_table 
+    my $sth=$dbh->prepare("select bibid,subfieldid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue,valuebloblink
+                                from marc_subfield_table
                                 where bibid=? and tagorder=? order by subfieldorder
                         ");
+    # FIXME - There's already a $sth2 in this scope.
     my $sth2=$dbh->prepare("select subfieldvalue from marc_blob_subfield where blobidlink=?");
     $sth->execute($bibid,$tagorder);
     while (my $row=$sth->fetchrow_hashref) {
@@ -539,7 +566,7 @@ sub MARCmoditem {
     }
 # otherwise, skip through each subfield...
     my @fields = $record->fields();
-# search old MARC item 
+# search old MARC item
     my $sth2 = $dbh->prepare("select tagorder from marc_subfield_table,marc_subfield_structure where marc_subfield_table.tag=marc_subfield_structure.tagfield and marc_subfield_table.subfieldcode=marc_subfield_structure.tagsubfield and bibid=? and kohafield='items.itemnumber' and subfieldvalue=?");
     $sth2->execute($bibid,$itemnumber);
     my ($tagorder) = $sth2->fetchrow_array();
@@ -641,7 +668,7 @@ sub MARCfindsubfield {
 sub MARCfindsubfieldid {
     my ($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
     my $sth=$dbh->prepare("select subfieldid from marc_subfield_table
-                       where bibid=? and tag=? and tagorder=? 
+                       where bibid=? and tag=? and tagorder=?
                                and subfieldcode=? and subfieldorder=?");
     $sth->execute($bibid,$tag,$tagorder,$subfield,$subfieldorder);
     my ($res) = $sth->fetchrow;
@@ -652,7 +679,7 @@ sub MARCdelsubfield {
 # delete a subfield for $bibid / tag / tagorder / subfield / subfieldorder
     my ($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
     $dbh->do("delete from marc_subfield_table where bibid='$bibid' and
-                       tag='$tag' and tagorder='$tagorder' 
+                       tag='$tag' and tagorder='$tagorder'
                        and subfieldcode='$subfield' and subfieldorder='$subfieldorder
                        ");
 }
@@ -671,8 +698,8 @@ sub MARCkoha2marcBiblio {
     my $record = MARC::Record->new();
 #--- if bibid, then retrieve old-style koha data
     if ($biblionumber>0) {
-       my $sth2=$dbh->prepare("select biblionumber,author,title,unititle,notes,abstract,serial,seriestitle,copyrightdate,timestamp 
-               from biblio where biblionumber=?");             
+       my $sth2=$dbh->prepare("select biblionumber,author,title,unititle,notes,abstract,serial,seriestitle,copyrightdate,timestamp
+               from biblio where biblionumber=?");
        $sth2->execute($biblionumber);
        my $row=$sth2->fetchrow_hashref;
        my $code;
@@ -686,10 +713,10 @@ sub MARCkoha2marcBiblio {
     if ($biblioitemnumber>0) {
        my $sth2=$dbh->prepare(" SELECT biblioitemnumber,biblionumber,volume,number,classification,
                                                itemtype,url,isbn,issn,dewey,subclass,publicationyear,publishercode,
-                                               volumedate,volumeddesc,timestamp,illus,pages,notes,size,place 
+                                               volumedate,volumeddesc,timestamp,illus,pages,notes,size,place
                                        FROM biblioitems
                                        WHERE biblionumber=? and biblioitemnumber=?
-                                       ");             
+                                       ");
        $sth2->execute($biblionumber,$biblioitemnumber);
        my $row=$sth2->fetchrow_hashref;
        my $code;
@@ -715,7 +742,7 @@ sub MARCkoha2marcItem {
        my $sth2=$dbh->prepare("SELECT itemnumber,biblionumber,multivolumepart,biblioitemnumber,barcode,dateaccessioned,
                                                booksellerid,homebranch,price,replacementprice,replacementpricedate,datelastborrowed,
                                                datelastseen,multivolume,stack,notforloan,itemlost,wthdrawn,bulk,issues,renewals,
-                                       reserves,restricted,binding,itemnotes,holdingbranch,timestamp 
+                                       reserves,restricted,binding,itemnotes,holdingbranch,timestamp
                                        FROM items
                                        WHERE itemnumber=?");
        $sth2->execute($itemnumber);
@@ -771,19 +798,23 @@ sub MARCmarc2koha {
     while (($field)=$sth2->fetchrow) {
        $result=&MARCmarc2kohaOneField($sth,"biblio",$field,$record,$result);
     }
+    # FIXME - There's already a $sth2 in this scope.
     my $sth2=$dbh->prepare("SHOW COLUMNS from biblioitems");
     $sth2->execute;
+    # FIXME - There's already a $field in this scope.
     my $field;
     while (($field)=$sth2->fetchrow) {
        $result=&MARCmarc2kohaOneField($sth,"biblioitems",$field,$record,$result);
     }
+    # FIXME - There's already a $sth2 in this scope.
     my $sth2=$dbh->prepare("SHOW COLUMNS from items");
     $sth2->execute;
+    # FIXME - There's already a $field in this scope.
     my $field;
     while (($field)=$sth2->fetchrow) {
        $result = &MARCmarc2kohaOneField($sth,"items",$field,$record,$result);
     }
-# additional authors : specific 
+# additional authors : specific
     $result = &MARCmarc2kohaOneField($sth,"additionalauthors","additionalauthors",$record,$result);
     return $result;
 }
@@ -815,13 +846,14 @@ sub MARCaddword {
     $sentence =~ s/(\.|\?|\:|\!|\'|,|\-)/ /g;
     my @words = split / /,$sentence;
 # build stopword list
-    my $sth2 =$dbh->prepare("select word from stopwords");
-    $sth2->execute;
-    my $stopwords;
-    my $stopword;
-    while(($stopword) = $sth2->fetchrow_array)  {
-       $stopwords->{$stopword} = $stopword;
-    }
+#    my $sth2 =$dbh->prepare("select word from stopwords");
+#    $sth2->execute;
+#    my $stopwords;
+#    my $stopword;
+#    while(($stopword) = $sth2->fetchrow_array)  {
+#      $stopwords->{$stopword} = $stopword;
+#    }
+    my $stopwords= C4::Context->stopwords;
     my $sth=$dbh->prepare("insert into marc_word (bibid, tag, tagorder, subfieldid, subfieldorder, word, sndx_word)
                        values (?,?,?,?,?,?,soundex(?))");
     foreach my $word (@words) {
@@ -844,7 +876,7 @@ sub MARCdelword {
 
 #
 #
-# ALL ALL ALL ALL ALL ALL ALL ALL ALL ALL ALL ALL ALL ALL ALL ALL ALL ALL 
+# ALL ALL ALL ALL ALL ALL ALL ALL ALL ALL ALL ALL ALL ALL ALL ALL ALL ALL
 #
 #
 # all the following subs are useful to manage MARC-DB with complete MARC records.
@@ -908,7 +940,7 @@ sub ALLnewbiblio {
        print "Error in ALLnewbiblio : biblio.biblionumber and biblioitems.biblioitemnumber MUST have the same field number";
        die;
     }
-    my $newfield = MARC::Field->new( $tagfield1,'','', 
+    my $newfield = MARC::Field->new( $tagfield1,'','',
                                     "$tagsubfield1" => $oldbibnum,
                                     "$tagsubfield2" => $oldbibitemnum);
 # drop old field and create new one...
@@ -924,7 +956,7 @@ sub ALLnewitem {
     my $itemnumber;
     my $error;
     ($itemnumber,$error) = &OLDnewitems($dbh,$item,$item->{'barcode'});
-# search MARC biblionumber 
+# search MARC biblionumber
     my $bibid=&MARCfind_MARCbibid_from_oldbiblionumber($dbh,$item->{'biblionumber'});
 # calculate tagorder
     my $sth = $dbh->prepare("select max(tagorder) from marc_subfield_table where bibid=?");
@@ -972,7 +1004,7 @@ sub ALLnewitem {
   They all are the exact copy of 1.0/1.2 version of the sub
   without the OLD. The OLDxxx is called by the original xxx sub.
   the 1.4 xxx sub also builds MARC::Record an calls the MARCxxx
+
   WARNING : there is 1 difference between initialxxx and OLDxxx :
   the db header $dbh is always passed as parameter
   to avoid over-DB connexion
@@ -1021,7 +1053,7 @@ sub ALLnewitem {
 =head2 OLDdeletebiblioitem($dbh,$biblioitemnumber);
   deletes a biblioitem
   NOTE : not standard sub name. Should be OLDdelbiblioitem()
+
 =head2 OLDdelbiblio($dbh,$biblio);
   delete a biblio
 
@@ -1073,7 +1105,7 @@ sub OLDmodbiblio {
 #  my $dbh   = C4Connect;
     my $query;
     my $sth;
-    
+
     $biblio->{'title'}         = $dbh->quote($biblio->{'title'});
     $biblio->{'author'}        = $dbh->quote($biblio->{'author'});
     $biblio->{'abstract'}      = $dbh->quote($biblio->{'abstract'});
@@ -1082,7 +1114,7 @@ sub OLDmodbiblio {
     $biblio->{'serial'}        = $dbh->quote($biblio->{'serial'});
     $biblio->{'unititle'}      = $dbh->quote($biblio->{'unititle'});
     $biblio->{'notes'}         = $dbh->quote($biblio->{'notes'});
-    
+
     $query = "Update biblio set
 title         = $biblio->{'title'},
 author        = $biblio->{'author'},
@@ -1094,11 +1126,10 @@ unititle      = $biblio->{'unititle'},
 notes         = $biblio->{'notes'}
 where biblionumber = $biblio->{'biblionumber'}";
     $sth   = $dbh->prepare($query);
-    
+
     $sth->execute;
-    
+
     $sth->finish;
-    $dbh->disconnect;
     return($biblio->{'biblionumber'});
 } # sub modbiblio
 
@@ -1135,8 +1166,6 @@ sub OLDmodaddauthor {
 
         $sth->finish;
     } # if
-
-  $dbh->disconnect;
 } # sub modaddauthor
 
 
@@ -1153,7 +1182,7 @@ sub OLDmodsubject {
                                and catalogueentry = '$subject[$i]'";
        my $sth   = $dbh->prepare($query);
        $sth->execute;
-       
+
        if (my $data = $sth->fetchrow_hashref) {
        } else {
            if ($force eq $subject[$i]) {
@@ -1177,7 +1206,7 @@ sub OLDmodsubject {
 
         $sth2->execute;
         while (my $data = $sth2->fetchrow_hashref) {
-          $error = $error."<br>$data->{'catalogueentry'}";
+          $error .= "<br>$data->{'catalogueentry'}";
         } # while
         $sth2->finish;
       } # else
@@ -1262,7 +1291,7 @@ sub OLDnewbiblioitem {
     my $sth   = $dbh->prepare($query);
     my $data;
     my $bibitemnum;
-    
+
     $biblioitem->{'volume'}          = $dbh->quote($biblioitem->{'volume'});
     $biblioitem->{'number'}       = $dbh->quote($biblioitem->{'number'});
     $biblioitem->{'classification'}  = $dbh->quote($biblioitem->{'classification'});
@@ -1283,7 +1312,7 @@ sub OLDnewbiblioitem {
     $biblioitem->{'place'}           = $dbh->quote($biblioitem->{'place'});
     $biblioitem->{'lccn'}            = $dbh->quote($biblioitem->{'lccn'});
     $biblioitem->{'marc'}            = $dbh->quote($biblioitem->{'marc'});
-  
+
     $sth->execute;
     $data       = $sth->fetchrow_arrayref;
     $bibitemnum = $$data[0] + 1;
@@ -1362,7 +1391,7 @@ sub OLDnewitems {
   $data       = $sth->fetchrow_hashref;
   $itemnumber = $data->{'max(itemnumber)'} + 1;
   $sth->finish;
-  
+
   $item->{'booksellerid'}     = $dbh->quote($item->{'booksellerid'});
   $item->{'homebranch'}       = $dbh->quote($item->{'homebranch'});
   $item->{'price'}            = $dbh->quote($item->{'price'});
@@ -1416,7 +1445,7 @@ sub OLDmoditem {
                              itemnotes='$item->{'notes'}',
                              homebranch='$item->{'homebranch'}',
                              itemlost='$item->{'lost'}',
-                             wthdrawn='$item->{'wthdrawn'}' 
+                             wthdrawn='$item->{'wthdrawn'}'
                           where itemnumber=$item->{'itemnum'}";
   }
   if ($item->{'replacement'} ne ''){
@@ -1441,7 +1470,7 @@ sub OLDdelitem{
   $sth->finish;
   $query="Insert into deleteditems values (";
   foreach my $temp (@data){
-    $query=$query."'$temp',";
+    $query .= "'$temp',";
   }
   $query=~ s/\,$/\)/;
 #  print $query;
@@ -1464,7 +1493,7 @@ where biblioitemnumber = $biblioitemnumber";
     my @results;
 
     $sth->execute;
-  
+
     if (@results = $sth->fetchrow_array) {
         $query = "Insert into deletedbiblioitems values (";
         foreach my $value (@results) {
@@ -1510,7 +1539,7 @@ sub OLDdelbiblio{
     $query="Insert into deletedbiblio values (";
     foreach my $temp (@data){
       $temp=~ s/\'/\\\'/g;
-      $query=$query."'$temp',";
+      $query .= "'$temp',";
     }
     $query=~ s/\,$/\)/;
 #   print $query;
@@ -1534,31 +1563,30 @@ sub OLDdelbiblio{
 
 # FIXME - This is the same as &C4::Acquisitions::itemcount, but not
 # the same as &C4::Search::itemcount
+# Since they're both exported, acqui/acquire.pl doesn't compile with -w.
 sub itemcount{
   my ($biblio)=@_;
-  my $dbh=C4Connect;
+  my $dbh = C4::Context->dbh;
   my $query="Select count(*) from items where biblionumber=$biblio";
 #  print $query;
   my $sth=$dbh->prepare($query);
   $sth->execute;
   my $data=$sth->fetchrow_hashref;
   $sth->finish;
-  $dbh->disconnect;
   return($data->{'count(*)'});
 }
 
 sub getorder{
   my ($bi,$bib)=@_;
-  my $dbh=C4Connect;
-  my $query="Select ordernumber 
-       from aqorders 
+  my $dbh = C4::Context->dbh;
+  my $query="Select ordernumber
+       from aqorders
        where biblionumber=? and biblioitemnumber=?";
   my $sth=$dbh->prepare($query);
   $sth->execute($bib,$bi);
   my $ordnum=$sth->fetchrow_hashref;
   $sth->finish;
   my $order=getsingleorder($ordnum->{'ordernumber'});
-  $dbh->disconnect;
 #  print $query;
   return ($order,$ordnum->{'ordernumber'});
 }
@@ -1567,9 +1595,9 @@ sub getorder{
 # &C4::Acquisitions::getsingleorder and &C4::Catalogue::getsingleorder
 sub getsingleorder {
   my ($ordnum)=@_;
-  my $dbh=C4Connect;
-  my $query="Select * from biblio,biblioitems,aqorders,aqorderbreakdown 
-  where aqorders.ordernumber=? 
+  my $dbh = C4::Context->dbh;
+  my $query="Select * from biblio,biblioitems,aqorders,aqorderbreakdown
+  where aqorders.ordernumber=?
   and biblio.biblionumber=aqorders.biblionumber and
   biblioitems.biblioitemnumber=aqorders.biblioitemnumber and
   aqorders.ordernumber=aqorderbreakdown.ordernumber";
@@ -1577,73 +1605,83 @@ sub getsingleorder {
   $sth->execute($ordnum);
   my $data=$sth->fetchrow_hashref;
   $sth->finish;
-  $dbh->disconnect;
   return($data);
 }
 
+# FIXME - This is in effect identical to &C4::Acquisitions::newbiblio.
+# Pick one and stick with it.
 sub newbiblio {
   my ($biblio) = @_;
-  my $dbh    = &C4Connect;
+  my $dbh    = C4::Context->dbh;
   my $bibnum=OLDnewbiblio($dbh,$biblio);
 # TODO : MARC add
-  $dbh->disconnect;
   return($bibnum);
 }
 
+# FIXME - This is in effect the same as &C4::Acquisitions::modbiblio.
+# Pick one and stick with it.
+# FIXME - Get the POD from C4::Acquisitions
 sub modbiblio {
   my ($biblio) = @_;
-  my $dbh  = C4Connect;
+  my $dbh  = C4::Context->dbh;
   my $biblionumber=OLDmodbiblio($dbh,$biblio);
-  $dbh->disconnect;
   return($biblionumber);
 } # sub modbiblio
 
+# FIXME - This is in effect identical to &C4::Acquisitions::modsubtitle.
+# Pick one and stick with it.
+# FIXME - Get the POD from C4::Acquisitions
 sub modsubtitle {
   my ($bibnum, $subtitle) = @_;
-  my $dbh   = C4Connect;
+  my $dbh   = C4::Context->dbh;
   &OLDmodsubtitle($dbh,$bibnum,$subtitle);
-  $dbh->disconnect;
 } # sub modsubtitle
 
 
 # FIXME - This is functionally identical to
 # &C4::Acquisitions::modaddauthor
+# Pick one and stick with it.
+# FIXME - Get the POD from C4::Acquisitions
 sub modaddauthor {
     my ($bibnum, $author) = @_;
-    my $dbh   = C4Connect;
+    my $dbh   = C4::Context->dbh;
     &OLDmodaddauthor($dbh,$bibnum,$author);
-    $dbh->disconnect;
 } # sub modaddauthor
 
 
+# FIXME - This is in effect identical to &C4::Acquisitions::modsubject.
+# Pick one and stick with it.
 sub modsubject {
   my ($bibnum, $force, @subject) = @_;
-  my $dbh   = C4Connect;
+  my $dbh   = C4::Context->dbh;
   my $error= &OLDmodsubject($dbh,$bibnum,$force, @subject);
   return($error);
 } # sub modsubject
 
+# FIXME - This is very similar to &C4::Acquisitions::modbibitem.
+# Pick one and stick with it.
 sub modbibitem {
     my ($biblioitem) = @_;
-    my $dbh   = C4Connect;
+    my $dbh   = C4::Context->dbh;
     &OLDmodbibitem($dbh,$biblioitem);
     my $MARCbibitem = MARCkoha2marcBiblio($dbh,$biblioitem);
     &MARCmodbiblio($dbh,$biblioitem->{biblionumber},0,$MARCbibitem);
-    $dbh->disconnect;
 } # sub modbibitem
 
+# FIXME - This is in effect identical to &C4::Acquisitions::modnote.
+# Pick one and stick with it.
 sub modnote {
   my ($bibitemnum,$note)=@_;
-  my $dbh=C4Connect;
+  my $dbh = C4::Context->dbh;
   &OLDmodnote($dbh,$bibitemnum,$note);
-  $dbh->disconnect;
 }
 
 # FIXME - This is quite similar in effect to &C4::newbiblioitem,
-# except for the MARC stuff.
+# except for the MARC stuff. There's also a &newbiblioitem in
+# acqui.simple/addbookslccn.pl
 sub newbiblioitem {
   my ($biblioitem) = @_;
-  my $dbh   = C4Connect;
+  my $dbh   = C4::Context->dbh;
   my $bibitemnum = &OLDnewbiblioitem($dbh,$biblioitem);
 #  print STDERR "bibitemnum : $bibitemnum\n";
   my $MARCbiblio= MARCkoha2marcBiblio($dbh,$biblioitem->{biblionumber},$bibitemnum);
@@ -1652,25 +1690,27 @@ sub newbiblioitem {
   return($bibitemnum);
 }
 
+# FIXME - This is in effect identical to &C4::Acquisitions::newsubject.
+# Pick one and stick with it.
 sub newsubject {
   my ($bibnum)=@_;
-  my $dbh=C4Connect;
+  my $dbh = C4::Context->dbh;
   &OLDnewsubject($dbh,$bibnum);
-  $dbh->disconnect;
 }
 
 # FIXME - This is just a wrapper around &OLDnewsubtitle
 # FIXME - This is in effect the same as &C4::Acquisitions::newsubtitle
 sub newsubtitle {
     my ($bibnum, $subtitle) = @_;
-    my $dbh   = C4Connect;
+    my $dbh   = C4::Context->dbh;
     &OLDnewsubtitle($dbh,$bibnum,$subtitle);
-  $dbh->disconnect;
 }
 
+# FIXME - This is different from &C4::Acquisitions::newitems, though
+# both are exported.
 sub newitems {
   my ($item, @barcodes) = @_;
-  my $dbh   = C4Connect;
+  my $dbh   = C4::Context->dbh;
   my $errors;
   my $itemnumber;
   my $error;
@@ -1683,14 +1723,16 @@ sub newitems {
       &MARCadditem($dbh,$MARCitem,$item->{biblionumber});
 #      print STDERR "MARCmodbiblio called\n";
   }
-  $dbh->disconnect;
   return($errors);
 }
 
+# FIXME - This appears to be functionally equivalent to
+# &C4::Acquisitions::moditem.
+# Pick one and stick with it.
 sub moditem {
     my ($item) = @_;
 #  my ($loan,$itemnum,$bibitemnum,$barcode,$notes,$homebranch,$lost,$wthdrawn,$replacement)=@_;
-    my $dbh=C4Connect;
+    my $dbh = C4::Context->dbh;
     &OLDmoditem($dbh,$item);
     warn "biblionumber : $item->{'biblionumber'} / $item->{'itemnum'}\n";
     my $MARCitem = &MARCkoha2marcItem($dbh,$item->{'biblionumber'},$item->{'itemnum'});
@@ -1699,12 +1741,13 @@ sub moditem {
 #      print STDERR "MARCitem ".$MARCitem->as_formatted()."\n";
     my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$item->{biblionumber});
     &MARCmoditem($dbh,$MARCitem,$bibid,$item->{itemnum},0);
-    $dbh->disconnect;
 }
 
+# FIXME - This is the same as &C4::Acquisitions::Checkitems.
+# Pick one and stick with it.
 sub checkitems{
   my ($count,@barcodes)=@_;
-  my $dbh=C4Connect;
+  my $dbh = C4::Context->dbh;
   my $error;
   for (my $i=0;$i<$count;$i++){
     $barcodes[$i]=uc $barcodes[$i];
@@ -1716,90 +1759,95 @@ sub checkitems{
     }
     $sth->finish;
   }
-  $dbh->disconnect;
   return($error);
 }
 
+# FIXME - This is identical to &C4::Acquisitions::countitems.
+# Pick one and stick with it.
 sub countitems{
   my ($bibitemnum)=@_;
-  my $dbh=C4Connect;
+  my $dbh = C4::Context->dbh;
   my $query="Select count(*) from items where biblioitemnumber='$bibitemnum'";
   my $sth=$dbh->prepare($query);
   $sth->execute;
   my $data=$sth->fetchrow_hashref;
   $sth->finish;
-  $dbh->disconnect;
   return($data->{'count(*)'});
 }
 
 # FIXME - This is just a wrapper around &OLDdelitem, and acts
 # identically to &C4::Acquisitions::delitem
+# Pick one and stick with it.
 sub delitem{
   my ($itemnum)=@_;
-  my $dbh=C4Connect;
+  my $dbh = C4::Context->dbh;
   &OLDdelitem($dbh,$itemnum);
-  $dbh->disconnect;
 }
 
+# FIXME - This is functionally identical to
+# &C4::Acquisitions::deletebiblioitem.
+# Pick one and stick with it.
 sub deletebiblioitem {
     my ($biblioitemnumber) = @_;
-    my $dbh   = C4Connect;
+    my $dbh   = C4::Context->dbh;
     &OLDdeletebiblioitem($dbh,$biblioitemnumber);
-    $dbh->disconnect;
 } # sub deletebiblioitem
 
 
+# FIXME - This is functionally identical to &C4::Acquisitions::delbiblio.
+# Pick one and stick with it.
 sub delbiblio {
   my ($biblio)=@_;
-  my $dbh=C4Connect;
+  my $dbh = C4::Context->dbh;
   &OLDdelbiblio($dbh,$biblio);
-  $dbh->disconnect;
 }
 
+# FIXME - This is identical to &C4::Acquisitions::getitemtypes.
+# Pick one and stick with it.
 sub getitemtypes {
-  my $dbh   = C4Connect;
+  my $dbh   = C4::Context->dbh;
   my $query = "select * from itemtypes";
   my $sth   = $dbh->prepare($query);
     # || die "Cannot prepare $query" . $dbh->errstr;
   my $count = 0;
   my @results;
-  
+
   $sth->execute;
     # || die "Cannot execute $query\n" . $sth->errstr;
   while (my $data = $sth->fetchrow_hashref) {
     $results[$count] = $data;
     $count++;
   } # while
-  
+
   $sth->finish;
-  $dbh->disconnect;
   return($count, @results);
 } # sub getitemtypes
 
 sub getbiblio {
     my ($biblionumber) = @_;
-    my $dbh   = C4Connect;
+    my $dbh   = C4::Context->dbh;
     my $query = "Select * from biblio where biblionumber = $biblionumber";
     my $sth   = $dbh->prepare($query);
       # || die "Cannot prepare $query\n" . $dbh->errstr;
     my $count = 0;
     my @results;
-    
+
     $sth->execute;
       # || die "Cannot execute $query\n" . $sth->errstr;
     while (my $data = $sth->fetchrow_hashref) {
       $results[$count] = $data;
       $count++;
     } # while
-    
+
     $sth->finish;
-    $dbh->disconnect;
     return($count, @results);
 } # sub getbiblio
 
+# FIXME - This is identical to &C4::Acquisitions::getbiblioitem.
+# Pick one and stick with it.
 sub getbiblioitem {
     my ($biblioitemnum) = @_;
-    my $dbh   = C4Connect;
+    my $dbh   = C4::Context->dbh;
     my $query = "Select * from biblioitems where
 biblioitemnumber = $biblioitemnum";
     my $sth   = $dbh->prepare($query);
@@ -1814,13 +1862,15 @@ biblioitemnumber = $biblioitemnum";
     } # while
 
     $sth->finish;
-    $dbh->disconnect;
     return($count, @results);
 } # sub getbiblioitem
 
+# FIXME - This is identical to
+# &C4::Acquisitions::getbiblioitembybiblionumber.
+# Pick one and stick with it.
 sub getbiblioitembybiblionumber {
     my ($biblionumber) = @_;
-    my $dbh   = C4Connect;
+    my $dbh   = C4::Context->dbh;
     my $query = "Select * from biblioitems where biblionumber =
 $biblionumber";
     my $sth   = $dbh->prepare($query);
@@ -1835,13 +1885,15 @@ $biblionumber";
     } # while
 
     $sth->finish;
-    $dbh->disconnect;
     return($count, @results);
 } # sub
 
+# FIXME - This is identical to
+# &C4::Acquisitions::getbiblioitembybiblionumber.
+# Pick one and stick with it.
 sub getitemsbybiblioitem {
     my ($biblioitemnum) = @_;
-    my $dbh   = C4Connect;
+    my $dbh   = C4::Context->dbh;
     my $query = "Select * from items, biblio where
 biblio.biblionumber = items.biblionumber and biblioitemnumber
 = $biblioitemnum";
@@ -1849,33 +1901,34 @@ biblio.biblionumber = items.biblionumber and biblioitemnumber
       # || die "Cannot prepare $query\n" . $dbh->errstr;
     my $count = 0;
     my @results;
-    
+
     $sth->execute;
       # || die "Cannot execute $query\n" . $sth->errstr;
     while (my $data = $sth->fetchrow_hashref) {
       $results[$count] = $data;
       $count++;
     } # while
-    
+
     $sth->finish;
-    $dbh->disconnect;
     return($count, @results);
 } # sub getitemsbybiblioitem
 
+# FIXME - This is identical to &C4::Acquisitions::isbnsearch.
+# Pick one and stick with it.
 sub isbnsearch {
     my ($isbn) = @_;
-    my $dbh   = C4Connect;
+    my $dbh   = C4::Context->dbh;
     my $count = 0;
     my $query;
     my $sth;
     my @results;
-    
+
     $isbn  = $dbh->quote($isbn);
     $query = "Select biblio.* from biblio, biblioitems where
 biblio.biblionumber = biblioitems.biblionumber
 and isbn = $isbn";
     $sth   = $dbh->prepare($query);
-    
+
     $sth->execute;
     while (my $data = $sth->fetchrow_hashref) {
         $results[$count] = $data;
@@ -1883,7 +1936,6 @@ and isbn = $isbn";
     } # while
 
     $sth->finish;
-    $dbh->disconnect;
     return($count, @results);
 } # sub isbnsearch
 
@@ -1891,7 +1943,7 @@ and isbn = $isbn";
 # At the moment this is just a straight copy of the subject code.  Needs heavy
 # modification to work for additional authors, obviously.
 # Check for additional author changes
-    
+
 #    my $newadditionalauthor='';
 #    my $additionalauthors;
 #    foreach $newadditionalauthor (@{$biblio->{'additionalauthor'}}) {
@@ -1962,6 +2014,7 @@ sub getoraddbiblio {
        # input params
        my (
          $dbh,         # db handle
+                       # FIXME - Unused argument
          $biblio,      # hash ref to fields
        )=@_;
 
@@ -1973,12 +2026,12 @@ sub getoraddbiblio {
        my $error;
 
        #-----
-       requireDBI($dbh,"getoraddbiblio");
+       $dbh = C4::Context->dbh;
 
        print "<PRE>Looking for biblio </PRE>\n" if $debug;
        $sth=$dbh->prepare("select biblionumber
                from biblio
-               where title=? and author=? 
+               where title=? and author=?
                  and copyrightdate=? and seriestitle=?");
        $sth->execute(
                $biblio->{title}, $biblio->{author},
@@ -2012,7 +2065,7 @@ sub getoraddbiblio {
 
 sub OLD_MAYBE_DELETED_newBiblioItem {
     my ($env, $biblioitem) = @_;
-    my $dbh=&C4Connect;  
+    my $dbh = C4::Context->dbh;
     my $biblionumber=$biblioitem->{'biblionumber'};
     my $biblioitemnumber=$biblioitem->{'biblioitemnumber'};
     my $volume=$biblioitem->{'volume'};
@@ -2266,13 +2319,12 @@ sub OLD_MAYBE_DELETED_newBiblioItem {
        addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
     }
     $sth->finish;
-    $dbh->disconnect;
     return ($env, $Record_ID);
 }
 
 sub OLD_MAYBE_DELETED_newItem {
     my ($env, $Record_ID, $item) = @_;
-    my $dbh=&C4Connect;  
+    my $dbh = C4::Context->dbh;
     my $barcode=$item->{'barcode'};
     my $q_barcode=$dbh->quote($barcode);
     my $biblionumber=$item->{'biblionumber'};
@@ -2375,7 +2427,7 @@ sub OLD_MAYBE_DELETED_updateBiblio {
     my ($env, $biblio) = @_;
     my $Record_ID;
     my $biblionumber=$biblio->{'biblionumber'};
-    my $dbh=&C4Connect;  
+    my $dbh = C4::Context->dbh;
     my $sth=$dbh->prepare("select * from biblio where biblionumber=$biblionumber");
     $sth->execute;
     my $origbiblio=$sth->fetchrow_hashref;
@@ -2398,7 +2450,7 @@ sub OLD_MAYBE_DELETED_updateBiblio {
        $origsubjects->{$subject}=1;
     }
 
-    
+
 # Obtain a list of MARC Record_ID's that are tied to this biblio
     $sth=$dbh->prepare("select bibid from marc_subfield_table where tag='090' and subfieldvalue=$biblionumber and subfieldcode='c'");
     $sth->execute;
@@ -2482,7 +2534,7 @@ sub OLD_MAYBE_DELETED_updateBiblio {
     }
 
 # Check for subject heading changes
-    
+
     my $newsubject='';
     my $subjects;
     foreach $newsubject (@{$biblio->{'subject'}}) {
@@ -2523,7 +2575,7 @@ sub OLD_MAYBE_DELETED_updateBiblioItem {
 # modified, and log all changes.
 
     my ($env, $biblioitem) = @_;
-    my $dbh=&C4Connect;  
+    my $dbh = C4::Context->dbh;
 
     my $biblioitemnumber=$biblioitem->{'biblioitemnumber'};
     my $sth=$dbh->prepare("select * from biblioitems where biblioitemnumber=$biblioitemnumber");
@@ -2643,8 +2695,6 @@ sub OLD_MAYBE_DELETED_updateBiblioItem {
        changeSubfield($Record_ID, '010', 'a', $obi->{'lccn'}, $biblioitem->{'lccn'});
     }
     $sth->finish;
-    $dbh->disconnect;
-
 }
 
 sub OLD_MAYBE_DELETED_updateItem {
@@ -2656,7 +2706,7 @@ sub OLD_MAYBE_DELETED_updateItem {
 # modified, and log all changes.
 
     my ($env, $item) = @_;
-    my $dbh=&C4Connect;  
+    my $dbh = C4::Context->dbh;
     my $itemnumber=$item->{'itemnumber'};
     my $biblionumber=$item->{'biblionumber'};
     my $biblioitemnumber=$item->{'biblioitemnumber'};
@@ -2691,7 +2741,7 @@ sub OLD_MAYBE_DELETED_updateItem {
     $sth=$dbh->prepare("select Subfield_ID from 8XX_Subfield_Table where Subfield_Mark=8 and Subfield_Value=$link and !(Subfield_ID=$Subfield876_ID)");
     $sth->execute;
     my ($Subfield852_ID) = $sth->fetchrow;
-    
+
     if ($item->{'barcode'} ne $olditem->{'barcode'}) {
        logchange('kohadb', 'change', 'items', 'barcode', $olditem->{'barcode'}, $item->{'barcode'});
        my $q_barcode=$dbh->quote($item->{'barcode'});
@@ -2797,7 +2847,6 @@ sub OLD_MAYBE_DELETED_updateItem {
        }
     }
     $sth->finish;
-    $dbh->disconnect;
 }
 
 # Add a biblioitem and related data to Koha database
@@ -2806,6 +2855,7 @@ sub OLD_MAY_BE_DELETED_newcompletebiblioitem {
 
        my (
          $dbh,                 # DBI handle
+                               # FIXME - Unused argument
          $biblio,              # hash ref to biblio record
          $biblioitem,          # hash ref to biblioitem record
          $subjects,            # list ref of subjects
@@ -2820,7 +2870,7 @@ sub OLD_MAY_BE_DELETED_newcompletebiblioitem {
        my $additionalauthor;
 
        #--------
-       requireDBI($dbh,"newcompletebiblioitem");
+       $dbh = C4::Context->dbh;
 
        print "<PRE>Trying to add biblio item Title=$biblio->{title} " .
                "ISBN=$biblioitem->{isbn} </PRE>\n" if $debug;