A new Date.pm to use for all date calculations. Mysql date calculations removed from...
[koha-ffzg.git] / C4 / Biblio.pm
index 015e309..22ddb90 100644 (file)
@@ -1,5 +1,5 @@
 package C4::Biblio;
-# New subs added by tgarip@neu.edu.tr 05/11/05
+# New XML API added by tgarip@neu.edu.tr 25/08/06
 # Copyright 2000-2002 Katipo Communications
 #
 # This file is part of Koha.
 use strict;
 require Exporter;
 use C4::Context;
-use MARC::Record;
-use MARC::File::USMARC;
-use MARC::File::XML;
 use XML::Simple;
 use Encode;
 use utf8;
-use Data::Dumper;
+
 use vars qw($VERSION @ISA @EXPORT);
 
 # set the version for version checking
@@ -49,20 +46,16 @@ $VERSION = 2.01;
 &NEWdelitem
 &NEWmodbiblioframework
 
-&MARCgetallitems 
+
 &MARCfind_marc_from_kohafield
 &MARCfind_frameworkcode
 &MARCfind_itemtype
 &MARCgettagslib
 &MARCitemsgettagslib
-&MARCkoha2marc
-&MARCmarc2koha 
-&MARCkoha2marcOnefield 
+
 &MARCfind_attr_from_kohafield
 &MARChtml2xml 
 
-&MARCgetbiblio 
-&MARCgetitem 
 
 &XMLgetbiblio 
 &XMLgetbibliohash
@@ -76,16 +69,20 @@ $VERSION = 2.01;
 &XMLmarc2koha_onerecord
 &XML_readline
 &XML_readline_onerecord
+&XML_readline_asarray
 &XML_writeline
+&XML_writeline_id
 &XMLmoditemonefield
-
+&XMLkoha2marc
+&XML_separate
+&XML_record_header
+&ZEBRAdelbiblio
 &ZEBRAgetrecord   
-
 &ZEBRAop 
 &ZEBRAopserver 
 &ZEBRA_readyXML 
 &ZEBRA_readyXML_noheader
-
+&ZEBRAopcommit
 &newbiblio
 &modbiblio
 &DisplayISBN
@@ -123,7 +120,6 @@ my $leader=$xml->{'leader'};
             foreach my $control (@$controlfields){
                if ($control->{'tag'} eq $tag){
                return  $control->{'content'} if $control->{'content'};
-
                }
            }
        }
@@ -131,6 +127,45 @@ my $leader=$xml->{'leader'};
 }## if tag is mapped
 return "";
 }
+sub XML_readline_asarray{
+my ($xml,$kohafield,$recordtype,$tag,$subf)=@_;
+#$xml represents one record of MARCXML as perlhashed 
+## returns an array of read fields--useful for readind repeated fields
+### $recordtype is needed for mapping the correct field if supplied
+### If only $tag is give reads the whole tag
+my @value;
+ ($tag,$subf)=MARCfind_marc_from_kohafield($kohafield,$recordtype) if $kohafield;
+if ($tag){
+my $biblio=$xml->{'datafield'};
+my $controlfields=$xml->{'controlfield'};
+my $leader=$xml->{'leader'};
+ if ($tag>9){
+       foreach my $data (@$biblio){
+           if ($data->{'tag'} eq $tag){
+               foreach my $subfield ( $data->{'subfield'}){
+                   foreach my $code ( @$subfield){
+                       if ($code->{'code'} eq $subf || !$subf){
+                       push @value, $code->{'content'};
+                       }
+                  }
+               }
+          }
+       }
+  }else{
+       if ($tag eq "000" || $tag eq "LDR"){
+               push @value,  $leader->[0] if $leader->[0];
+       }else{
+            foreach my $control (@$controlfields){
+               if ($control->{'tag'} eq $tag){
+               push @value,    $control->{'content'} if $control->{'content'};
+
+               }
+           }
+       }
+   }##tag
+}## if tag is mapped
+return @value;
+}
 
 sub XML_readline{
 my ($xml,$kohafield,$recordtype,$tag,$subf)=@_;
@@ -200,7 +235,7 @@ $newvalue= Encode::decode('utf8',$newvalue) if $newvalue;
 my $biblio=$xml->{'datafield'};
 my $controlfield=$xml->{'controlfield'};
  ($tag,$subf)=MARCfind_marc_from_kohafield($kohafield,$recordtype) if $kohafield;
-my $updated=0;
+my $updated;
     if ($tag>9){
        foreach my $data (@$biblio){
                        if ($data->{'tag'} eq $tag){
@@ -223,7 +258,7 @@ my $updated=0;
                }
                 }
        ## Tag did not exist
-                 if (!$updated){
+            if (!$updated){
                if ($subf){     
                        push @$biblio,
                                            {
@@ -245,9 +280,9 @@ my $updated=0;
                                              'tag' =>$tag
                                            } ;
                   }                                                            
-                 }## created now
-    }else{
-       foreach my $control(@$controlfield){
+          }## created now
+    }elsif ($tag>0){
+       foreach my $control (@$controlfield){
                if ($control->{'tag'} eq $tag){
                        $control->{'content'}=$newvalue;
                        $updated=1;
@@ -260,6 +295,37 @@ my $updated=0;
 return $xml;
 }
 
+sub XML_writeline_id {
+### This routine is similar to XML_writeline but replaces a given value and do not create a new field
+## Useful for repeating fields
+## Currently  usedin authorities
+my ($xml,$oldvalue,$newvalue,$tag,$subf)=@_;
+$newvalue= Encode::decode('utf8',$newvalue) if $newvalue;
+my $biblio=$xml->{'datafield'};
+my $controlfield=$xml->{'controlfield'};
+    if ($tag>9){
+       foreach my $data (@$biblio){
+                       if ($data->{'tag'} eq $tag){
+                       my @subfields=$data->{'subfield'};
+                       foreach my $subfield ( @subfields){
+                             foreach my $code ( @$subfield){
+                               if ($code->{'code'} eq $subf && $code->{'content'} eq $oldvalue){       
+                               $code->{'content'}=$newvalue;
+                               }
+                             }
+                       }       
+               }
+                }
+    }else{
+       foreach my $control(@$controlfield){
+               if ($control->{'tag'} eq $tag  && $control->{'content'} eq $oldvalue ){
+                       $control->{'content'}=$newvalue;
+               }
+            }
+   }
+return $xml;
+}
+
 sub XML_xml2hash{
 ##make a perl hash from xml file
 my ($xml)=@_;
@@ -282,6 +348,7 @@ return ($biblio,@items);
 sub XML_xml2hash_onerecord{
 ##make a perl hash from xml file
 my ($xml)=@_;
+return undef unless $xml;
   my $hashed = XMLin( $xml ,KeyAttr =>['leader','controlfield','datafield'],ForceArray => ['leader','controlfield','datafield','subfield'],KeepRoot=>0);
 return $hashed;
 }
@@ -331,7 +398,7 @@ $marcxml=Encode::decode('utf8',$marcxml);
 sub XMLgetitemhash{
 ## Utility to return s hashed MARCXML
  my ( $dbh, $itemnumber,$barcode ) = @_;
-my $xml=XMLgeitem( $dbh, $itemnumber,$barcode);
+my $xml=XMLgetitem( $dbh, $itemnumber,$barcode);
 my $xmlhash=XML_xml2hash_onerecord($xml);
 return $xmlhash;
 }
@@ -369,7 +436,7 @@ if ($related_record eq "biblios" || $related_record eq "" || !$related_record){
                        
                }
        }else{
-       my $sth2=$dbh->prepare("SELECT  marctokoha from koha_attr where  recordtype like 'biblios' and tagfield is not null" );
+       my $sth2=$dbh->prepare("SELECT  kohafield from koha_attr where  recordtype like 'biblios' and tagfield is not null" );
        $sth2->execute();
        my $field;
                while ($field=$sth2->fetchrow) {
@@ -412,7 +479,7 @@ my $itemresult;
            push @items, $itemresult;
           }
        }else{
-       my $sth2=$dbh->prepare("SELECT  marctokoha from koha_attr where recordtype like 'holdings' and tagfield is not null" );
+       my $sth2=$dbh->prepare("SELECT  kohafield from koha_attr where recordtype like 'holdings' and tagfield is not null" );
           foreach my $holding (@$holdings){    
           $sth2->execute();
            my $field;
@@ -443,7 +510,7 @@ sub XMLmarc2koha_onerecord {
                        $result->{$field}=$val if $val;                 
                }
        }else{
-       my $sth2=$dbh->prepare("SELECT  marctokoha from koha_attr where  recordtype like ? and tagfield is not null" );
+       my $sth2=$dbh->prepare("SELECT  kohafield from koha_attr where  recordtype like ? and tagfield is not null" );
        $sth2->execute($related_record);
        my $field;
                while ($field=$sth2->fetchrow) {
@@ -482,14 +549,43 @@ my ($record) = XMLgetitem($dbh,$itemnumber);
                $sth->finish;
        }else{
                NEWmoditem($dbh,$recordhash,$biblionumber,$itemnumber);
+  }
+
 }
 
+sub XMLkoha2marc {
+# warn "MARCkoha2marc";
+## This routine  is still used for acqui management
+##Returns a  XML recordhash from a kohahash
+       my ($dbh,$result,$recordtype) = @_;
+###create a basic MARCXML
+# find today's date
+my ($sec,$min,$hour,$mday,$mon,$year) = localtime();
+       $year += 1900;
+       $mon += 1;
+       my $timestamp = sprintf("%4d%02d%02d%02d%02d%02d.0",
+               $year,$mon,$mday,$hour,$min,$sec);
+$year=substr($year,2,2);
+       my $accdate=sprintf("%2d%02d%02d",$year,$mon,$mday);
+my ($titletag,$titlesubf)=MARCfind_marc_from_kohafield("title","biblios");
+##create a dummy record
+my $xml="<record><leader>     naa a22     7ar4500</leader><controlfield tag='xxx'></controlfield><datafield ind1='' ind2='' tag='$titletag'></datafield></record>";
+## Now build XML
+       my $record = XML_xml2hash($xml);
+       my $sth2=$dbh->prepare("SELECT  kohafield from koha_attr where tagfield is not null and recordtype=?");
+       $sth2->execute($recordtype);
+       my $field;
+       while (($field)=$sth2->fetchrow) {
+               $record=XML_writeline($record,$field,$result->{$field},$recordtype) if $result->{$field};
+       }
+return $record;
 }
+
 #
 #
 # MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC
 #
-## Script to deal with MARC read write operations
+## Script to deal with MARCXML related tables
 
 
 ##Sub to match kohafield to Z3950 -attributes
@@ -648,66 +744,6 @@ $recordtype="biblios" unless $recordtype;
 
 
 
-sub MARCgetbiblio {
-    # Returns MARC::Record of the biblio passed in parameter.
-    ### Takes a new parameter of $title_author =1 which parses the record obly on those fields and nothing else
-    ### Its useful when Koha requires only title&author for performance issues
-    my ( $dbh, $biblionumber, $title_author ) = @_;
-    my $sth =
-      $dbh->prepare("select marc from biblio where biblionumber=? "  );
-    $sth->execute( $biblionumber);
-   my ($marc)=$sth->fetchrow;
-my $record;
-       if ($title_author){
-       $record = MARC::File::USMARC::decode($marc,\&func_title_author);
-       }else{
-        $record = MARC::File::USMARC::decode($marc);
-       }
-$sth->finish;
- return $record;
-}
-
-
-
-
-
-sub MARCgetitem {
-# warn "MARCgetitem";
-    # Returns MARC::Record   of the item passed in parameter uses either itemnumber or barcode
-    my ( $dbh, $itemnumber,$barcode ) = @_;
-my $sth;
-if ($itemnumber){
-   $sth = $dbh->prepare("select i.marc from items i where i.itemnumber=?"  ); 
-    $sth->execute($itemnumber);
-}else{
- $sth = $dbh->prepare("select i.marc from  items i where i.barcode=?"  ); 
-    $sth->execute($barcode);
-}
- my ($marc)=$sth->fetchrow;
- my $record = MARC::File::USMARC::decode($marc);
-       
-    return ($record);
-}
-
-sub MARCgetallitems {
-# warn "MARCgetallitems";
-    # Returns an array of MARC::Record   of the items passed in parameter as biblionumber
-    my ( $dbh, $biblionumber ) = @_;
-my @results;
-my   $sth = $dbh->prepare("select marc from items where biblionumber =?"  ); 
-    $sth->execute($biblionumber);
-
- while(my ($marc)=$sth->fetchrow_array){
- my $record = MARC::File::USMARC::decode($marc);
-    push @results,$record;
-}
-return @results;
-}
-
-
-
-
-
 
 sub MARCfind_frameworkcode {
 # warn "MARCfind_frameworkcode";
@@ -732,9 +768,8 @@ sub MARCfind_itemtype {
 
 sub MARChtml2xml {
 # warn "MARChtml2xml ";
-       my ($tags,$subfields,$values,$indicator,$ind_tag) = @_;        
-#      use MARC::File::XML;
-       my $xml= marc_record_header('UTF-8'); #### we do not need a collection wrapper
+       my ($tags,$subfields,$values,$indicator,$ind_tag,$tagindex) = @_;        
+       my $xml= "<record>";
 
     my $prevvalue;
     my $prevtag=-1;
@@ -747,62 +782,62 @@ sub MARChtml2xml {
                @$values[$i] =~ s/"/&quot;/g;
                @$values[$i] =~ s/'/&apos;/g;
 
-               if ((@$tags[$i] ne $prevtag)){
-                       $j++ unless (@$tags[$i] eq "");
+               if ((@$tags[$i].@$tagindex[$i] ne $prevtag)){
+                       my $tag=@$tags[$i];
+                       $j++ unless ($tag eq "");
                        ## warn "IND:".substr(@$indicator[$j],0,1).substr(@$indicator[$j],1,1)." ".@$tags[$i];
                        if (!$first){
                        $xml.="</datafield>\n";
-                               if ((@$tags[$i] > 10) && (@$values[$i] ne "")){
+                               if (($tag> 10) && (@$values[$i] ne "")){
                                                my $ind1 = substr(@$indicator[$j],0,1);
                         my $ind2 = substr(@$indicator[$j],1,1);
-                        $xml.="<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
+                        $xml.="<datafield tag=\"$tag\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
                         $xml.="<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
                         $first=0;
                                } else {
                        $first=1;
                                }
-            } else {
+                               } else {
                        if (@$values[$i] ne "") {
                                # leader
-                               if (@$tags[$i] eq "000") {
+                               if ($tag eq "000") {
                                ##Force the leader to UTF8
                                substr(@$values[$i],9,1)="a";
                                                $xml.="<leader>@$values[$i]</leader>\n";
                                                $first=1;
                                        # rest of the fixed fields
-                               } elsif (@$tags[$i] < 10) {
-                                               $xml.="<controlfield tag=\"@$tags[$i]\">@$values[$i]</controlfield>\n";
+                               } elsif ($tag < 10) {
+                                               $xml.="<controlfield tag=\"$tag\">@$values[$i]</controlfield>\n";
                                                $first=1;
                                } else {
                                                my $ind1 = substr(@$indicator[$j],0,1);
                                                my $ind2 = substr(@$indicator[$j],1,1);
-                                               $xml.="<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
+                                               $xml.="<datafield tag=\"$tag\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
                                                $xml.="<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
                                                $first=0;                       
                                }
                        }
                        }
                } else { # @$tags[$i] eq $prevtag
-                if (@$values[$i] eq "") {
-                }
-                else {
+                                 unless (@$values[$i] eq "") {
+                       my $tag=@$tags[$i];
                                        if ($first){
                                                my $ind1 = substr(@$indicator[$j],0,1);                        
                                                my $ind2 = substr(@$indicator[$j],1,1);
-                                               $xml.="<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
+                                               $xml.="<datafield tag=\"$tag\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
                                                $first=0;
                                        }
                        $xml.="<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
                                }
                }
-               $prevtag = @$tags[$i];
+               $prevtag = @$tags[$i].@$tagindex[$i];
        }
        $xml.="</record>";
        # warn $xml;
        $xml=Encode::decode('utf8',$xml);
        return $xml;
 }
-sub marc_record_header {
+sub XML_record_header {
 ####  this one is for <record>
     my $format = shift;
     my $enc = shift || 'UTF-8';
@@ -828,128 +863,8 @@ KOHA_XML_HEADER
 
 
 
-sub MARCkoha2marc {
-# warn "MARCkoha2marc";
-## This routine most probably will be depreaceated -- it is still used for acqui management
-##Returns a  MARC record from a hash
-       my ($dbh,$result,$recordtype) = @_;
 
-       my $record = MARC::Record->new();
-       my $sth2=$dbh->prepare("SELECT  marctokoha from koha_attr where tagfield is not null and recordtype=?");
-       $sth2->execute($recordtype);
-       my $field;
-       while (($field)=$sth2->fetchrow) {
-               $record=&MARCkoha2marcOnefield($record,$field,$result->{$field},$recordtype) if $result->{$field};
-       }
-return $record;
-}
-sub MARCmarc2koha {
-# warn "MARCmarc2koha";
-##Returns a hash from MARC record
-       my ($dbh,$record,$related_record) = @_;
-       my $result;
-if (!$related_record){$related_record="biblios";}
-       my $sth2=$dbh->prepare("SELECT  marctokoha from koha_attr where  recordtype like ? and tagfield is not null" );
-       $sth2->execute($related_record);
-       my $field;
-       while ($field=$sth2->fetchrow) {
-               $result=&MARCmarc2kohaOneField($field,$record,$result,$related_record);
-       }
 
-## we only need the following for biblio data
-if ($related_record eq "biblios"){     
-# modify copyrightdate to keep only the 1st year found
-       my $temp = $result->{'copyrightdate'};
-       $temp =~ m/c(\d\d\d\d)/; # search cYYYY first
-       if ($1>0) {
-               $result->{'copyrightdate'} = $1;
-       } else { # if no cYYYY, get the 1st date.
-               $temp =~ m/(\d\d\d\d)/;
-               $result->{'copyrightdate'} = $1;
-       }
-# modify publicationyear to keep only the 1st year found
-       $temp = $result->{'publicationyear'};
-       $temp =~ m/c(\d\d\d\d)/; # search cYYYY first
-       if ($1>0) {
-               $result->{'publicationyear'} = $1;
-       } else { # if no cYYYY, get the 1st date.
-               $temp =~ m/(\d\d\d\d)/;
-               $result->{'publicationyear'} = $1;
-       }
-}
-       return $result;
-}
-
-sub MARCkoha2marcOnefield {
-##Updates or creates one field in MARC record
-    my ( $record, $kohafieldname, $value,$recordtype ) = @_;
-my ( $tagfield, $tagsubfield ) = MARCfind_marc_from_kohafield($kohafieldname,$recordtype);
-if ($tagfield){
-my $tag = $record->field($tagfield);
-    if  (  $tagfield>9) { 
-        if ($tag) {
-               if ($value){## We may be trying to delete a subfield value
-                        $tag->update( $tagsubfield=> $value );
-               }else{  
-               $tag->delete_subfield(code=>$tagsubfield);
-               }
-                $record->delete_field($tag);
-                $record->insert_fields_ordered($tag);         
-        }else {
-       my $newtag=MARC::Field->new( $tagfield, " ", " ", $tagsubfield => $value);
-            $record->insert_fields_ordered($newtag);   
-        }
-    }else {
-        if ($tag) {
-         if ($value){  
-                $tag->update( $value );
-                $record->delete_field($tag);
-                $record->insert_fields_ordered($tag);    
-         }else{
-         $record->delete_field($tag);  
-         }
-        }else {
-       my $newtag=MARC::Field->new( $tagfield => $value);
-            $record->insert_fields_ordered($newtag);   
-        }
-    }
-}## $tagfield defined
-    return $record;
-}
-
-sub MARCmarc2kohaOneField {
-    my (  $kohafield, $record, $result,$recordtype ) = @_;
-    #    # warn "kohatable / $kohafield / $result / ";
-    my $res = "";
-
-  my  ( $tagfield, $subfield ) = MARCfind_marc_from_kohafield($kohafield,$recordtype);
-if ($tagfield){
-    foreach my $field ( $record->field($tagfield) ) {
-               if ($field->tag()<10) {
-                       if ($result->{$kohafield}) {
-                               $result->{$kohafield} .= " | ".$field->data();
-                       } else {
-                               $result->{$kohafield} = $field->data();
-                       }
-               } else {
-                       if ( $field->subfields ) {
-                               my @subfields = $field->subfields();
-                               foreach my $subfieldcount ( 0 .. $#subfields ) {
-                                       if ($subfields[$subfieldcount][0] eq $subfield) {
-                                               if ( $result->{$kohafield} ) {
-                                                       $result->{$kohafield} .= " | " . $subfields[$subfieldcount][1];
-                                               }
-                                               else {
-                                                       $result->{$kohafield} = $subfields[$subfieldcount][1];
-                                               }
-                                       }
-                               }
-                       }
-               }
-    }
-}
-    return $result;
-}
 
 
 
@@ -981,20 +896,11 @@ sub NEWmodbiblioframework {
 
 
 sub NEWdelbiblio {
-
     my ( $dbh, $biblionumber ) = @_;
-my $sth=$dbh->prepare("SELECT itemnumber FROM items where biblionumber=?");
-
-$sth->execute($biblionumber);
-       while (my $itemnumber =$sth->fetchrow){
-       OLDdelitem($dbh,$itemnumber) ;
-       }
-
-       ZEBRAop($dbh,$biblionumber,"recordDelete","biblioserver");
-OLDdelbiblio($dbh,$biblionumber) ;
-
+ZEBRAop($dbh,$biblionumber,"recordDelete","biblioserver");
 }
 
+
 sub NEWnewitem {
     my ( $dbh, $xmlhash, $biblionumber ) = @_;
        my $itemtype= MARCfind_itemtype($dbh,$biblionumber);
@@ -1007,7 +913,6 @@ NEWmoditem ( $dbh, $xmlhash, $biblionumber, $itemnumber);
    
 ##Add biblionumber to $record
 $xmlhash=XML_writeline($xmlhash,"biblionumber",$biblionumber,"holdings");
-#    MARCkoha2marcOnefield($record,"biblionumber",$biblionumber,"holdings");
  my $sth=$dbh->prepare("select notforloan from itemtypes where itemtype='$itemtype'");
 $sth->execute();
 my $notforloan=$sth->fetchrow;
@@ -1080,7 +985,7 @@ my $sth=$dbh->prepare("SELECT biblionumber from items where itemnumber=?");
 $sth->execute($itemnumber);
 my $biblionumber=$sth->fetchrow;
 OLDdelitem( $dbh, $itemnumber ) ;
-ZEBRAop($dbh,$biblionumber,"recordDelete","biblioserver");
+ZEBRAop($dbh,$biblionumber,"specialUpdate","biblioserver");
 
 }
 
@@ -1144,7 +1049,7 @@ $xmlhash=XML_writeline($xmlhash,"indexedby",$cataloger,"biblios") if $cataloger;
 
 ## We must add the indexing fields for LC in MARC record--TG
 
-##   XMLmodLCindex($dbh,$xmlhash);
+  XMLmodLCindex($dbh,$xmlhash);
     OLDmodbiblio ($dbh,$xmlhash,$biblionumber,$frameworkcode);
     my $ok=ZEBRAop($dbh,$biblionumber,"specialUpdate","biblioserver");
     return ($biblionumber);
@@ -1225,7 +1130,6 @@ my $title=XML_readline_onerecord($xmlhash,"title","biblios");
 my $author=XML_readline_onerecord($xmlhash,"author","biblios");
 my $xml=XML_hash2xml($xmlhash);
 
-#my $marc=MARC::Record->new_from_xml($xml,'UTF-8');## this will be depreceated
 $isbn=~ s/(\.|\?|\;|\=|\-|\/|\\|\||\:|\*|\!|\,|\(|\)|\[|\]|\{|\}|\/)//g;
 $issn=~ s/(\.|\?|\;|\=|\-|\/|\\|\||\:|\*|\!|\,|\(|\)|\[|\]|\{|\}|\/)//g;
 $isbn=~s/^\s+|\s+$//g;
@@ -1269,49 +1173,50 @@ sub OLDdelbiblio {
 #
 #
 
-sub ZEBRAgetrecord {
-my $biblionumber=shift;
-my  @oConnection;
- $oConnection[0]=C4::Context->Zconn("biblioserver");
-my $field=MARCfind_attr_from_kohafield("biblionumber");
-my $query=$field." ".$biblionumber;
-my $oResult= $oConnection[0]->search_pqf($query);
-my $event;
-my $i;
-   while (($i = ZOOM::event(\@oConnection)) != 0) {
-       $event = $oConnection[$i-1]->last_event();
-       last if $event == ZOOM::Event::ZEND;
-   }# while
-if ($oResult->size()){
-my $xmlrecord=$oResult->record(0)->raw() ;
-$oConnection[0]->destroy;
-$xmlrecord=Encode::decode('utf8',$xmlrecord);
-my $hashed=XML_xml2hash($xmlrecord);
-my ( $xmlrecord, @itemsrecord) = XML_separate($hashed);
-return ($xmlrecord, @itemsrecord);
-}else{
-return (undef,undef);
-}
+sub ZEBRAdelbiblio {
+## Zebra calls this routine to delete after it deletes biblio from ZEBRAddb
+ my ( $dbh, $biblionumber ) = @_;
+my $sth=$dbh->prepare("SELECT itemnumber FROM items where biblionumber=?");
+
+$sth->execute($biblionumber);
+       while (my $itemnumber =$sth->fetchrow){
+       OLDdelitem($dbh,$itemnumber) ;
+       }       
+OLDdelbiblio($dbh,$biblionumber) ;
 }
 
+sub ZEBRAgetrecord{
+my $biblionumber=shift;
+my @kohafield="biblionumber";
+my @value=$biblionumber;
+my ($count,@result)=C4::Search::ZEBRAsearch_kohafields(\@kohafield,\@value);
+
+   if ($count>0){
+   my ( $xmlrecord, @itemsrecord) = XML_separate($result[0]);
+   return ($xmlrecord, @itemsrecord);
+   }else{
+   return (undef,undef);
+   }
+}
 
 sub ZEBRAop {
 ### Puts the zebra update in queue writes in zebraserver table
 my ($dbh,$biblionumber,$op,$server)=@_;
-my ($record);
-
+if (!$biblionumber){
+warn "Zebra received no biblionumber";
+}else{
 my $sth=$dbh->prepare("insert into zebraqueue  (biblio_auth_number ,server,operation) values(?,?,?)");
 $sth->execute($biblionumber,$server,$op);
 }
-
+}
 
 sub ZEBRAopserver{
 
 ###Accepts a $server variable thus we can use it to update  biblios, authorities or other zebra dbs
-my ($record,$op,$server)=@_;
-my @Zconnbiblio;
+my ($record,$op,$server,$biblionumber)=@_;
+
 my @port;
-my $Zpackage;
+
 my $tried=0;
 my $recon=0;
 my $reconnect=0;
@@ -1319,21 +1224,16 @@ $record=Encode::encode("UTF-8",$record);
 my $shadow=$server."shadow";
 reconnect:
 
-$Zconnbiblio[0]=C4::Context->Zconnauth($server);
+ my $Zconnbiblio=C4::Context->Zconnauth($server);
 if ($record){
-my $Zpackage = $Zconnbiblio[0]->package();
+my $Zpackage = $Zconnbiblio->package();
 $Zpackage->option(action => $op);
        $Zpackage->option(record => $record);
+       $Zpackage->option(recordIdOpaque => $biblionumber);
 retry:
                $Zpackage->send("update");
-my $i;
-my $event;
 
-while (($i = ZOOM::event(\@Zconnbiblio)) != 0) {
-    $event = $Zconnbiblio[0]->last_event();
-    last if $event == ZOOM::Event::ZEND;
-}
- my($error, $errmsg, $addinfo, $diagset) = $Zconnbiblio[0]->error_x();
+ my($error, $errmsg, $addinfo, $diagset) = $Zconnbiblio->error_x();
        if ($error==10007 && $tried<3) {## timeout --another 30 looonng seconds for this update
                sleep 1;        ##  wait a sec!
                $tried=$tried+1;
@@ -1346,39 +1246,41 @@ while (($i = ZOOM::event(\@Zconnbiblio)) != 0) {
                sleep 1;        ##  wait a sec!
                $recon=1;
                $Zpackage->destroy();
-               $Zconnbiblio[0]->destroy();
+               $Zconnbiblio->destroy();
                goto "reconnect";
        }elsif ($error){
        #       warn "Error-$server   $op  /errcode:, $error, /MSG:,$errmsg,$addinfo \n";       
                $Zpackage->destroy();
-               $Zconnbiblio[0]->destroy();
-       #       ZEBRAopfiles($dbh,$biblionumber,$record,$op,$server);
+               $Zconnbiblio->destroy();
                return 0;
        }
-       ## System preference batchMode=1 means wea are bulk importing
-       ## DO NOT COMMIT while in batchMode for faster operation
-       my $batchmode=C4::Context->preference('batchMode');
-        if (C4::Context->$shadow >0 && !$batchmode){
-        $Zpackage->send('commit');
-               while (($i = ZOOM::event(\@Zconnbiblio)) != 0) {
-                $event = $Zconnbiblio[0]->last_event();
-               last if $event == ZOOM::Event::ZEND;
-               }
-            my($error, $errmsg, $addinfo, $diagset) = $Zconnbiblio[0]->error_x();
-            if ($error) { ## This is serious ZEBRA server is not updating      
-            $Zpackage->destroy();
-            $Zconnbiblio[0]->destroy();
-            return 0;
-           }
-        }##commit
-#
+       
 $Zpackage->destroy();
-$Zconnbiblio[0]->destroy();
+$Zconnbiblio->destroy();
 return 1;
 }
 return 0;
 }
 
+
+sub ZEBRAopcommit {
+my $server=shift;
+
+my $Zconnbiblio=C4::Context->Zconnauth($server);
+
+my $Zpackage = $Zconnbiblio->package();
+ $Zpackage->send('commit');
+               
+                my($error, $errmsg, $addinfo, $diagset) = $Zconnbiblio->error_x();
+                if ($error) { ## This is serious ZEBRA server is not updating  
+            $Zpackage->destroy();
+            $Zconnbiblio->destroy();
+            return 0;
+           }
+$Zpackage->destroy();
+$Zconnbiblio->destroy();
+return 1;
+}
 sub ZEBRA_readyXML{
 my ($dbh,$biblionumber)=@_;
 my $biblioxml=XMLgetbiblio($dbh,$biblionumber);
@@ -1418,19 +1320,18 @@ return $zebraxml;
 #
 
 sub newbiblio {
-## Used in acqui management -- creates the biblio from hash rather than marc-record
+## Used in acqui management -- creates the biblio from koha hash 
     my ($biblio) = @_;
     my $dbh    = C4::Context->dbh;
-my $record=MARCkoha2marc($dbh,$biblio,"biblios");
-$record->encoding('UTF-8');
+my $record=XMLkoha2marc($dbh,$biblio,"biblios");
    my $biblionumber=NEWnewbiblio($dbh,$record);
     return ($biblionumber);
 }
 sub modbiblio {
-## Used in acqui management -- modifies the biblio from hash rather than marc-record
+## Used in acqui management -- modifies the biblio from koha hash rather than xml-hash
     my ($biblio) = @_;
     my $dbh    = C4::Context->dbh;
-my $record=MARCkoha2marc($dbh,$biblio,"biblios");
+my $record=XMLkoha2marc($dbh,$biblio,"biblios");
    my $biblionumber=NEWmodbiblio($dbh,$record,$biblio->{biblionumber});
     return ($biblionumber);
 }
@@ -1477,7 +1378,7 @@ sub getitemtypes {
 
 sub getkohafields{
 #returns MySQL like fieldnames to emulate searches on sql like fieldnames
-my $type=@_;
+my $type=shift;
 ## Either opac or intranet to select appropriate fields
 ## Assumes intranet
 $type="intra" unless $type;
@@ -1486,7 +1387,7 @@ my $dbh   = C4::Context->dbh;
   my $i=0;
 my @results;
 $type=$type."show";
-my $sth=$dbh->prepare("SELECT  * FROM koha_attr  where $type=1 order by liblibrarian");
+my $sth=$dbh->prepare("SELECT  * FROM koha_attr  where $type=1 order by label");
 $sth->execute();
 while (my $data=$sth->fetchrow_hashref){
        $results[$i]=$data;
@@ -1586,8 +1487,7 @@ return($lc1.$lc2);
 sub itemcalculator{
 ## Sublimentary function to obtain sorted LC for items. Not exported
 my ($dbh,$biblionumber,$callnumber)=@_;
-my $xml=XMLgetbiblio($dbh,$biblionumber);
-my $xmlhash=XML_xml2hash_onerecord($xml);
+my $xmlhash=XMLgetbibliohash($dbh,$biblionumber);
 my $lc=XML_readline_onerecord($xmlhash,"classification","biblios");
 my $cutter=XML_readline_onerecord($xmlhash,"subclass","biblios");
 my $all=$lc." ".$cutter;