3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
24 use MARC::File::USMARC;
27 use POSIX qw(strftime);
30 use C4::Dates qw/format_date/;
31 use C4::Log; # logaction
37 use vars qw($VERSION @ISA @EXPORT);
43 @ISA = qw( Exporter );
57 &GetBiblioItemByBiblioNumber
58 &GetBiblioFromItemNumber
72 &GetAuthorisedValueDesc
76 &GetPublisherNameFromIsbn
93 # To link headings in a bib record
94 # to authority records.
96 &LinkBibHeadingsToAuthorities
100 # those functions are exported but should not be used
101 # they are usefull is few circumstances, so are exported.
102 # but don't use them unless you're a core developer ;-)
109 &TransformHtmlToMarc2
112 &PrepareItemrecordDisplay
118 my $servers = C4::Context->config('memcached_servers');
120 require Memoize::Memcached;
121 import Memoize::Memcached qw(memoize_memcached);
124 servers => [ $servers ],
125 key_prefix => C4::Context->config('memcached_namespace') || 'koha',
127 memoize_memcached('GetMarcStructure', memcached => $memcached, expire_time => 600); #cache for 10 minutes
132 C4::Biblio - cataloging management functions
136 Biblio.pm contains functions for managing storage and editing of bibliographic data within Koha. Most of the functions in this module are used for cataloging records: adding, editing, or removing biblios, biblioitems, or items. Koha's stores bibliographic information in three places:
140 =item 1. in the biblio,biblioitems,items, etc tables, which are limited to a one-to-one mapping to underlying MARC data
142 =item 2. as raw MARC in the Zebra index and storage engine
144 =item 3. as raw MARC the biblioitems.marc and biblioitems.marcxml
148 In the 3.0 version of Koha, the authoritative record-level information is in biblioitems.marcxml
150 Because the data isn't completely normalized there's a chance for information to get out of sync. The design choice to go with a un-normalized schema was driven by performance and stability concerns. However, if this occur, it can be considered as a bug : The API is (or should be) complete & the only entry point for all biblio/items managements.
154 =item 1. Compared with MySQL, Zebra is slow to update an index for small data changes -- especially for proc-intensive operations like circulation
156 =item 2. Zebra's index has been known to crash and a backup of the data is necessary to rebuild it in such cases
160 Because of this design choice, the process of managing storage and editing is a bit convoluted. Historically, Biblio.pm's grown to an unmanagable size and as a result we have several types of functions currently:
164 =item 1. Add*/Mod*/Del*/ - high-level external functions suitable for being called from external scripts to manage the collection
166 =item 2. _koha_* - low-level internal functions for managing the koha tables
168 =item 3. Marc management function : as the MARC record is stored in biblioitems.marc(xml), some subs dedicated to it's management are in this package. They should be used only internally by Biblio.pm, the only official entry points being AddBiblio, AddItem, ModBiblio, ModItem.
170 =item 4. Zebra functions used to update the Zebra index
172 =item 5. internal helper functions such as char_decode, checkitems, etc. Some of these probably belong in Koha.pm
176 The MARC record (in biblioitems.marcxml) contains the complete marc record, including items. It also contains the biblionumber. That is the reason why it is not stored directly by AddBiblio, with all other fields . To save a biblio, we need to :
180 =item 1. save datas in biblio and biblioitems table, that gives us a biblionumber and a biblioitemnumber
182 =item 2. add the biblionumber and biblioitemnumber into the MARC records
184 =item 3. save the marc record
188 When dealing with items, we must :
192 =item 1. save the item in items table, that gives us an itemnumber
194 =item 2. add the itemnumber to the item MARC field
196 =item 3. overwrite the MARC record (with the added item) into biblioitems.marc(xml)
198 When modifying a biblio or an item, the behaviour is quite similar.
202 =head1 EXPORTED FUNCTIONS
208 ($biblionumber,$biblioitemnumber) = AddBiblio($record,$frameworkcode);
212 Exported function (core API) for adding a new biblio to koha.
214 The first argument is a C<MARC::Record> object containing the
215 bib to add, while the second argument is the desired MARC
218 This function also accepts a third, optional argument: a hashref
219 to additional options. The only defined option is C<defer_marc_save>,
220 which if present and mapped to a true value, causes C<AddBiblio>
221 to omit the call to save the MARC in C<bibilioitems.marc>
222 and C<biblioitems.marcxml> This option is provided B<only>
223 for the use of scripts such as C<bulkmarcimport.pl> that may need
224 to do some manipulation of the MARC record for item parsing before
225 saving it and which cannot afford the performance hit of saving
226 the MARC record twice. Consequently, do not use that option
227 unless you can guarantee that C<ModBiblioMarc> will be called.
233 my $frameworkcode = shift;
234 my $options = @_ ? shift : undef;
235 my $defer_marc_save = 0;
236 if (defined $options and exists $options->{'defer_marc_save'} and $options->{'defer_marc_save'}) {
237 $defer_marc_save = 1;
240 my ($biblionumber,$biblioitemnumber,$error);
241 my $dbh = C4::Context->dbh;
242 # transform the data into koha-table style data
243 my $olddata = TransformMarcToKoha( $dbh, $record, $frameworkcode );
244 ($biblionumber,$error) = _koha_add_biblio( $dbh, $olddata, $frameworkcode );
245 $olddata->{'biblionumber'} = $biblionumber;
246 ($biblioitemnumber,$error) = _koha_add_biblioitem( $dbh, $olddata );
248 _koha_marc_update_bib_ids($record, $frameworkcode, $biblionumber, $biblioitemnumber);
250 # update MARC subfield that stores biblioitems.cn_sort
251 _koha_marc_update_biblioitem_cn_sort($record, $olddata, $frameworkcode);
254 ModBiblioMarc( $record, $biblionumber, $frameworkcode ) unless $defer_marc_save;
256 logaction("CATALOGUING", "ADD", $biblionumber, "biblio") if C4::Context->preference("CataloguingLog");
257 return ( $biblionumber, $biblioitemnumber );
264 ModBiblio( $record,$biblionumber,$frameworkcode);
268 Replace an existing bib record identified by C<$biblionumber>
269 with one supplied by the MARC::Record object C<$record>. The embedded
270 item, biblioitem, and biblionumber fields from the previous
271 version of the bib record replace any such fields of those tags that
272 are present in C<$record>. Consequently, ModBiblio() is not
273 to be used to try to modify item records.
275 C<$frameworkcode> specifies the MARC framework to use
276 when storing the modified bib record; among other things,
277 this controls how MARC fields get mapped to display columns
278 in the C<biblio> and C<biblioitems> tables, as well as
279 which fields are used to store embedded item, biblioitem,
280 and biblionumber data for indexing.
285 my ( $record, $biblionumber, $frameworkcode ) = @_;
286 if (C4::Context->preference("CataloguingLog")) {
287 my $newrecord = GetMarcBiblio($biblionumber);
288 logaction("CATALOGUING", "MODIFY", $biblionumber, "BEFORE=>".$newrecord->as_formatted);
291 my $dbh = C4::Context->dbh;
293 $frameworkcode = "" unless $frameworkcode;
295 # get the items before and append them to the biblio before updating the record, atm we just have the biblio
296 my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField("items.itemnumber",$frameworkcode);
297 my $oldRecord = GetMarcBiblio( $biblionumber );
299 # delete any item fields from incoming record to avoid
300 # duplication or incorrect data - use AddItem() or ModItem()
302 foreach my $field ($record->field($itemtag)) {
303 $record->delete_field($field);
306 # parse each item, and, for an unknown reason, re-encode each subfield
307 # if you don't do that, the record will have encoding mixed
308 # and the biblio will be re-encoded.
309 # strange, I (Paul P.) searched more than 1 day to understand what happends
310 # but could only solve the problem this way...
311 my @fields = $oldRecord->field( $itemtag );
312 foreach my $fielditem ( @fields ){
314 foreach ($fielditem->subfields()) {
316 $field->add_subfields(Encode::encode('utf-8',$_->[0]) => Encode::encode('utf-8',$_->[1]));
318 $field = MARC::Field->new("$itemtag",'','',Encode::encode('utf-8',$_->[0]) => Encode::encode('utf-8',$_->[1]));
321 $record->append_fields($field);
324 # update biblionumber and biblioitemnumber in MARC
325 # FIXME - this is assuming a 1 to 1 relationship between
326 # biblios and biblioitems
327 my $sth = $dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
328 $sth->execute($biblionumber);
329 my ($biblioitemnumber) = $sth->fetchrow;
331 _koha_marc_update_bib_ids($record, $frameworkcode, $biblionumber, $biblioitemnumber);
333 # load the koha-table data object
334 my $oldbiblio = TransformMarcToKoha( $dbh, $record, $frameworkcode );
336 # update MARC subfield that stores biblioitems.cn_sort
337 _koha_marc_update_biblioitem_cn_sort($record, $oldbiblio, $frameworkcode);
339 # update the MARC record (that now contains biblio and items) with the new record data
340 &ModBiblioMarc( $record, $biblionumber, $frameworkcode );
342 # modify the other koha tables
343 _koha_modify_biblio( $dbh, $oldbiblio, $frameworkcode );
344 _koha_modify_biblioitem_nonmarc( $dbh, $oldbiblio );
348 =head2 ModBiblioframework
350 ModBiblioframework($biblionumber,$frameworkcode);
351 Exported function to modify a biblio framework
355 sub ModBiblioframework {
356 my ( $biblionumber, $frameworkcode ) = @_;
357 my $dbh = C4::Context->dbh;
358 my $sth = $dbh->prepare(
359 "UPDATE biblio SET frameworkcode=? WHERE biblionumber=?"
361 $sth->execute($frameworkcode, $biblionumber);
369 my $error = &DelBiblio($dbh,$biblionumber);
370 Exported function (core API) for deleting a biblio in koha.
371 Deletes biblio record from Zebra and Koha tables (biblio,biblioitems,items)
372 Also backs it up to deleted* tables
373 Checks to make sure there are not issues on any of the items
375 C<$error> : undef unless an error occurs
382 my ( $biblionumber ) = @_;
383 my $dbh = C4::Context->dbh;
384 my $error; # for error handling
386 # First make sure this biblio has no items attached
387 my $sth = $dbh->prepare("SELECT itemnumber FROM items WHERE biblionumber=?");
388 $sth->execute($biblionumber);
389 if (my $itemnumber = $sth->fetchrow){
390 # Fix this to use a status the template can understand
391 $error .= "This Biblio has items attached, please delete them first before deleting this biblio ";
394 return $error if $error;
396 # We delete attached subscriptions
397 my $subscriptions = &C4::Serials::GetFullSubscriptionsFromBiblionumber($biblionumber);
398 foreach my $subscription (@$subscriptions){
399 &C4::Serials::DelSubscription($subscription->{subscriptionid});
402 # Delete in Zebra. Be careful NOT to move this line after _koha_delete_biblio
403 # for at least 2 reasons :
404 # - we need to read the biblio if NoZebra is set (to remove it from the indexes
405 # - if something goes wrong, the biblio may be deleted from Koha but not from zebra
406 # and we would have no way to remove it (except manually in zebra, but I bet it would be very hard to handle the problem)
408 if (C4::Context->preference("NoZebra")) {
409 # only NoZebra indexing needs to have
410 # the previous version of the record
411 $oldRecord = GetMarcBiblio($biblionumber);
413 ModZebra($biblionumber, "recordDelete", "biblioserver", $oldRecord, undef);
415 # delete biblioitems and items from Koha tables and save in deletedbiblioitems,deleteditems
418 "SELECT biblioitemnumber FROM biblioitems WHERE biblionumber=?");
419 $sth->execute($biblionumber);
420 while ( my $biblioitemnumber = $sth->fetchrow ) {
422 # delete this biblioitem
423 $error = _koha_delete_biblioitems( $dbh, $biblioitemnumber );
424 return $error if $error;
427 # delete biblio from Koha tables and save in deletedbiblio
428 # must do this *after* _koha_delete_biblioitems, otherwise
429 # delete cascade will prevent deletedbiblioitems rows
430 # from being generated by _koha_delete_biblioitems
431 $error = _koha_delete_biblio( $dbh, $biblionumber );
433 logaction("CATALOGUING", "DELETE", $biblionumber, "") if C4::Context->preference("CataloguingLog");
438 =head2 LinkBibHeadingsToAuthorities
442 my $headings_linked = LinkBibHeadingsToAuthorities($marc);
446 Links bib headings to authority records by checking
447 each authority-controlled field in the C<MARC::Record>
448 object C<$marc>, looking for a matching authority record,
449 and setting the linking subfield $9 to the ID of that
452 If no matching authority exists, or if multiple
453 authorities match, no $9 will be added, and any
454 existing one inthe field will be deleted.
456 Returns the number of heading links changed in the
461 sub LinkBibHeadingsToAuthorities {
464 my $num_headings_changed = 0;
465 foreach my $field ($bib->fields()) {
466 my $heading = C4::Heading->new_from_bib_field($field);
467 next unless defined $heading;
470 my $current_link = $field->subfield('9');
472 # look for matching authorities
473 my $authorities = $heading->authorities();
475 # want only one exact match
476 if ($#{ $authorities } == 0) {
477 my $authority = MARC::Record->new_from_usmarc($authorities->[0]);
478 my $authid = $authority->field('001')->data();
479 next if defined $current_link and $current_link eq $authid;
481 $field->delete_subfield(code => '9') if defined $current_link;
482 $field->add_subfields('9', $authid);
483 $num_headings_changed++;
485 if (defined $current_link) {
486 $field->delete_subfield(code => '9');
487 $num_headings_changed++;
492 return $num_headings_changed;
499 $data = &GetBiblioData($biblionumber);
500 Returns information about the book with the given biblionumber.
501 C<&GetBiblioData> returns a reference-to-hash. The keys are the fields in
502 the C<biblio> and C<biblioitems> tables in the
504 In addition, C<$data-E<gt>{subject}> is the list of the book's
505 subjects, separated by C<" , "> (space, comma, space).
506 If there are multiple biblioitems with the given biblionumber, only
507 the first one is considered.
515 my $dbh = C4::Context->dbh;
517 # my $query = C4::Context->preference('item-level_itypes') ?
518 # " SELECT * , biblioitems.notes AS bnotes, biblio.notes
520 # LEFT JOIN biblioitems ON biblio.biblionumber = biblioitems.biblionumber
521 # WHERE biblio.biblionumber = ?
522 # AND biblioitems.biblionumber = biblio.biblionumber
525 my $query = " SELECT * , biblioitems.notes AS bnotes, itemtypes.notforloan as bi_notforloan, biblio.notes
527 LEFT JOIN biblioitems ON biblio.biblionumber = biblioitems.biblionumber
528 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
529 WHERE biblio.biblionumber = ?
530 AND biblioitems.biblionumber = biblio.biblionumber ";
532 my $sth = $dbh->prepare($query);
533 $sth->execute($bibnum);
535 $data = $sth->fetchrow_hashref;
539 } # sub GetBiblioData
541 =head2 &GetBiblioItemData
545 $itemdata = &GetBiblioItemData($biblioitemnumber);
547 Looks up the biblioitem with the given biblioitemnumber. Returns a
548 reference-to-hash. The keys are the fields from the C<biblio>,
549 C<biblioitems>, and C<itemtypes> tables in the Koha database, except
550 that C<biblioitems.notes> is given as C<$itemdata-E<gt>{bnotes}>.
557 sub GetBiblioItemData {
558 my ($biblioitemnumber) = @_;
559 my $dbh = C4::Context->dbh;
560 my $query = "SELECT *,biblioitems.notes AS bnotes
561 FROM biblio LEFT JOIN biblioitems on biblio.biblionumber=biblioitems.biblionumber ";
562 unless(C4::Context->preference('item-level_itypes')) {
563 $query .= "LEFT JOIN itemtypes on biblioitems.itemtype=itemtypes.itemtype ";
565 $query .= " WHERE biblioitemnumber = ? ";
566 my $sth = $dbh->prepare($query);
568 $sth->execute($biblioitemnumber);
569 $data = $sth->fetchrow_hashref;
572 } # sub &GetBiblioItemData
574 =head2 GetBiblioItemByBiblioNumber
578 NOTE : This function has been copy/paste from C4/Biblio.pm from head before zebra integration.
584 sub GetBiblioItemByBiblioNumber {
585 my ($biblionumber) = @_;
586 my $dbh = C4::Context->dbh;
587 my $sth = $dbh->prepare("Select * FROM biblioitems WHERE biblionumber = ?");
591 $sth->execute($biblionumber);
593 while ( my $data = $sth->fetchrow_hashref ) {
594 push @results, $data;
601 =head2 GetBiblioFromItemNumber
605 $item = &GetBiblioFromItemNumber($itemnumber,$barcode);
607 Looks up the item with the given itemnumber. if undef, try the barcode.
609 C<&itemnodata> returns a reference-to-hash whose keys are the fields
610 from the C<biblio>, C<biblioitems>, and C<items> tables in the Koha
618 sub GetBiblioFromItemNumber {
619 my ( $itemnumber, $barcode ) = @_;
620 my $dbh = C4::Context->dbh;
623 $sth=$dbh->prepare( "SELECT * FROM items
624 LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
625 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber
626 WHERE items.itemnumber = ?") ;
627 $sth->execute($itemnumber);
629 $sth=$dbh->prepare( "SELECT * FROM items
630 LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
631 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber
632 WHERE items.barcode = ?") ;
633 $sth->execute($barcode);
635 my $data = $sth->fetchrow_hashref;
644 $isbd = &GetISBDView($biblionumber);
646 Return the ISBD view which can be included in opac and intranet
653 my $biblionumber = shift;
654 my $record = GetMarcBiblio($biblionumber);
655 my $itemtype = &GetFrameworkCode($biblionumber);
656 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField("items.holdingbranch",$itemtype);
657 my $tagslib = &GetMarcStructure( 1, $itemtype );
659 my $ISBD = C4::Context->preference('ISBD');
664 foreach my $isbdfield ( split (/#/, $bloc) ) {
666 # $isbdfield= /(.?.?.?)/;
667 $isbdfield =~ /(\d\d\d)([^\|])?\|(.*)\|(.*)\|(.*)/;
668 my $fieldvalue = $1 || 0;
669 my $subfvalue = $2 || "";
671 my $analysestring = $4;
674 # warn "==> $1 / $2 / $3 / $4";
675 # my $fieldvalue=substr($isbdfield,0,3);
676 if ( $fieldvalue > 0 ) {
677 my $hasputtextbefore = 0;
678 my @fieldslist = $record->field($fieldvalue);
679 @fieldslist = sort {$a->subfield($holdingbrtagsubf) cmp $b->subfield($holdingbrtagsubf)} @fieldslist if ($fieldvalue eq $holdingbrtagf);
681 # warn "ERROR IN ISBD DEFINITION at : $isbdfield" unless $fieldvalue;
682 # warn "FV : $fieldvalue";
683 if ($subfvalue ne ""){
684 foreach my $field ( @fieldslist ) {
685 foreach my $subfield ($field->subfield($subfvalue)){
686 my $calculated = $analysestring;
687 my $tag = $field->tag();
692 GetAuthorisedValueDesc( $tag, $subfvalue,
693 $subfield, '', $tagslib );
694 my $tagsubf = $tag . $subfvalue;
696 s/\{(.?.?.?.?)$tagsubf(.*?)\}/$1$subfieldvalue$2\{$1$tagsubf$2\}/g;
697 $calculated =~s#/cgi-bin/koha/[^/]+/([^.]*.pl\?.*)$#opac-$1#g;
699 # field builded, store the result
700 if ( $calculated && !$hasputtextbefore )
701 { # put textbefore if not done
702 $blocres .= $textbefore;
703 $hasputtextbefore = 1;
706 # remove punctuation at start
707 $calculated =~ s/^( |;|:|\.|-)*//g;
708 $blocres .= $calculated;
713 $blocres .= $textafter if $hasputtextbefore;
715 foreach my $field ( @fieldslist ) {
716 my $calculated = $analysestring;
717 my $tag = $field->tag();
721 my @subf = $field->subfields;
722 for my $i ( 0 .. $#subf ) {
723 my $valuecode = $subf[$i][1];
724 my $subfieldcode = $subf[$i][0];
726 GetAuthorisedValueDesc( $tag, $subf[$i][0],
727 $subf[$i][1], '', $tagslib );
728 my $tagsubf = $tag . $subfieldcode;
730 $calculated =~ s/ # replace all {{}} codes by the value code.
731 \{\{$tagsubf\}\} # catch the {{actualcode}}
733 $valuecode # replace by the value code
737 s/\{(.?.?.?.?)$tagsubf(.*?)\}/$1$subfieldvalue$2\{$1$tagsubf$2\}/g;
738 $calculated =~s#/cgi-bin/koha/[^/]+/([^.]*.pl\?.*)$#opac-$1#g;
741 # field builded, store the result
742 if ( $calculated && !$hasputtextbefore )
743 { # put textbefore if not done
744 $blocres .= $textbefore;
745 $hasputtextbefore = 1;
748 # remove punctuation at start
749 $calculated =~ s/^( |;|:|\.|-)*//g;
750 $blocres .= $calculated;
753 $blocres .= $textafter if $hasputtextbefore;
757 $blocres .= $isbdfield;
762 $res =~ s/\{(.*?)\}//g;
764 $res =~ s/\n/<br\/>/g;
776 ( $count, @results ) = &GetBiblio($biblionumber);
783 my ($biblionumber) = @_;
784 my $dbh = C4::Context->dbh;
785 my $sth = $dbh->prepare("SELECT * FROM biblio WHERE biblionumber = ?");
788 $sth->execute($biblionumber);
789 while ( my $data = $sth->fetchrow_hashref ) {
790 $results[$count] = $data;
794 return ( $count, @results );
797 =head2 GetBiblioItemInfosOf
801 GetBiblioItemInfosOf(@biblioitemnumbers);
807 sub GetBiblioItemInfosOf {
808 my @biblioitemnumbers = @_;
811 SELECT biblioitemnumber,
815 WHERE biblioitemnumber IN (' . join( ',', @biblioitemnumbers ) . ')
817 return get_infos_of( $query, 'biblioitemnumber' );
820 =head1 FUNCTIONS FOR HANDLING MARC MANAGEMENT
822 =head2 GetMarcStructure
826 $res = GetMarcStructure($forlibrarian,$frameworkcode);
828 Returns a reference to a big hash of hash, with the Marc structure for the given frameworkcode
829 $forlibrarian :if set to 1, the MARC descriptions are the librarians ones, otherwise it's the public (OPAC) ones
830 $frameworkcode : the framework code to read
836 # cache for results of GetMarcStructure -- needed
838 our $marc_structure_cache;
840 sub GetMarcStructure {
841 my ( $forlibrarian, $frameworkcode ) = @_;
842 my $dbh=C4::Context->dbh;
843 $frameworkcode = "" unless $frameworkcode;
845 if (defined $marc_structure_cache and exists $marc_structure_cache->{$forlibrarian}->{$frameworkcode}) {
846 return $marc_structure_cache->{$forlibrarian}->{$frameworkcode};
849 my $sth = $dbh->prepare(
850 "SELECT COUNT(*) FROM marc_tag_structure WHERE frameworkcode=?");
851 $sth->execute($frameworkcode);
852 my ($total) = $sth->fetchrow;
853 $frameworkcode = "" unless ( $total > 0 );
854 $sth = $dbh->prepare(
855 "SELECT tagfield,liblibrarian,libopac,mandatory,repeatable
856 FROM marc_tag_structure
857 WHERE frameworkcode=?
860 $sth->execute($frameworkcode);
861 my ( $liblibrarian, $libopac, $tag, $res, $tab, $mandatory, $repeatable );
863 while ( ( $tag, $liblibrarian, $libopac, $mandatory, $repeatable ) =
866 $res->{$tag}->{lib} =
867 ( $forlibrarian or !$libopac ) ? $liblibrarian : $libopac;
868 $res->{$tag}->{tab} = "";
869 $res->{$tag}->{mandatory} = $mandatory;
870 $res->{$tag}->{repeatable} = $repeatable;
873 $sth = $dbh->prepare(
874 "SELECT tagfield,tagsubfield,liblibrarian,libopac,tab,mandatory,repeatable,authorised_value,authtypecode,value_builder,kohafield,seealso,hidden,isurl,link,defaultvalue
875 FROM marc_subfield_structure
876 WHERE frameworkcode=?
877 ORDER BY tagfield,tagsubfield
881 $sth->execute($frameworkcode);
884 my $authorised_value;
896 $tag, $subfield, $liblibrarian,
898 $mandatory, $repeatable, $authorised_value,
899 $authtypecode, $value_builder, $kohafield,
900 $seealso, $hidden, $isurl,
906 $res->{$tag}->{$subfield}->{lib} =
907 ( $forlibrarian or !$libopac ) ? $liblibrarian : $libopac;
908 $res->{$tag}->{$subfield}->{tab} = $tab;
909 $res->{$tag}->{$subfield}->{mandatory} = $mandatory;
910 $res->{$tag}->{$subfield}->{repeatable} = $repeatable;
911 $res->{$tag}->{$subfield}->{authorised_value} = $authorised_value;
912 $res->{$tag}->{$subfield}->{authtypecode} = $authtypecode;
913 $res->{$tag}->{$subfield}->{value_builder} = $value_builder;
914 $res->{$tag}->{$subfield}->{kohafield} = $kohafield;
915 $res->{$tag}->{$subfield}->{seealso} = $seealso;
916 $res->{$tag}->{$subfield}->{hidden} = $hidden;
917 $res->{$tag}->{$subfield}->{isurl} = $isurl;
918 $res->{$tag}->{$subfield}->{'link'} = $link;
919 $res->{$tag}->{$subfield}->{defaultvalue} = $defaultvalue;
922 $marc_structure_cache->{$forlibrarian}->{$frameworkcode} = $res;
927 =head2 GetUsedMarcStructure
929 the same function as GetMarcStructure except it just takes field
930 in tab 0-9. (used field)
932 my $results = GetUsedMarcStructure($frameworkcode);
934 L<$results> is a ref to an array which each case containts a ref
935 to a hash which each keys is the columns from marc_subfield_structure
937 L<$frameworkcode> is the framework code.
941 sub GetUsedMarcStructure($){
942 my $frameworkcode = shift || '';
945 FROM marc_subfield_structure
947 AND frameworkcode = ?
948 ORDER BY tagfield, tagsubfield
950 my $sth = C4::Context->dbh->prepare($query);
951 $sth->execute($frameworkcode);
952 return $sth->fetchall_arrayref({});
955 =head2 GetMarcFromKohaField
959 ($MARCfield,$MARCsubfield)=GetMarcFromKohaField($kohafield,$frameworkcode);
960 Returns the MARC fields & subfields mapped to the koha field
961 for the given frameworkcode
967 sub GetMarcFromKohaField {
968 my ( $kohafield, $frameworkcode ) = @_;
969 return 0, 0 unless $kohafield and defined $frameworkcode;
970 my $relations = C4::Context->marcfromkohafield;
972 $relations->{$frameworkcode}->{$kohafield}->[0],
973 $relations->{$frameworkcode}->{$kohafield}->[1]
981 my $record = GetMarcBiblio($biblionumber);
985 Returns MARC::Record representing bib identified by
986 C<$biblionumber>. If no bib exists, returns undef.
987 The MARC record contains both biblio & item data.
992 my $biblionumber = shift;
993 my $dbh = C4::Context->dbh;
995 $dbh->prepare("SELECT marcxml FROM biblioitems WHERE biblionumber=? ");
996 $sth->execute($biblionumber);
997 my $row = $sth->fetchrow_hashref;
998 my $marcxml = StripNonXmlChars($row->{'marcxml'});
999 MARC::File::XML->default_record_format(C4::Context->preference('marcflavour'));
1000 my $record = MARC::Record->new();
1002 $record = eval {MARC::Record::new_from_xml( $marcxml, "utf8", C4::Context->preference('marcflavour'))};
1003 if ($@) {warn " problem with :$biblionumber : $@ \n$marcxml";}
1004 # $record = MARC::Record::new_from_usmarc( $marc) if $marc;
1015 my $marcxml = GetXmlBiblio($biblionumber);
1017 Returns biblioitems.marcxml of the biblionumber passed in parameter.
1018 The XML contains both biblio & item datas
1025 my ( $biblionumber ) = @_;
1026 my $dbh = C4::Context->dbh;
1028 $dbh->prepare("SELECT marcxml FROM biblioitems WHERE biblionumber=? ");
1029 $sth->execute($biblionumber);
1030 my ($marcxml) = $sth->fetchrow;
1034 =head2 GetCOinSBiblio
1038 my $coins = GetCOinSBiblio($biblionumber);
1040 Returns the COinS(a span) which can be included in a biblio record
1046 sub GetCOinSBiblio {
1047 my ( $biblionumber ) = @_;
1048 my $record = GetMarcBiblio($biblionumber);
1050 # get the coin format
1051 my $pos7 = substr $record->leader(), 7,1;
1052 my $pos6 = substr $record->leader(), 6,1;
1055 my ($aulast, $aufirst) = ('','');
1064 if ( C4::Context->preference("marcflavour") eq "UNIMARC" ){
1069 'b' => 'manuscript',
1071 'd' => 'manuscript',
1075 'i' => 'audioRecording',
1076 'j' => 'audioRecording',
1079 'm' => 'computerProgram',
1084 'a' => 'journalArticle',
1088 $genre = $fmts6->{$pos6} ? $fmts6->{$pos6} : 'book' ;
1090 if( $genre eq 'book' ){
1091 $genre = $fmts7->{$pos7} if $fmts7->{$pos7};
1094 ##### We must transform mtx to a valable mtx and document type ####
1095 if( $genre eq 'book' ){
1097 }elsif( $genre eq 'journal' ){
1099 }elsif( $genre eq 'journalArticle' ){
1106 $genre = ($mtx eq 'dc') ? "&rft.type=$genre" : "&rft.genre=$genre";
1109 $aulast = $record->subfield('700','a');
1110 $aufirst = $record->subfield('700','b');
1111 $oauthors = "&rft.au=$aufirst $aulast";
1113 if($record->field('200')){
1114 for my $au ($record->field('200')->subfield('g')){
1115 $oauthors .= "&rft.au=$au";
1118 $title = ( $mtx eq 'dc' ) ? "&rft.title=".$record->subfield('200','a') :
1119 "&rft.title=".$record->subfield('200','a')."&rft.btitle=".$record->subfield('200','a');
1120 $pubyear = $record->subfield('210','d');
1121 $publisher = $record->subfield('210','c');
1122 $isbn = $record->subfield('010','a');
1123 $issn = $record->subfield('011','a');
1125 # MARC21 need some improve
1128 $genre = "&rft.genre=book";
1131 if ($record->field('100')) {
1132 $oauthors .= "&rft.au=".$record->subfield('100','a');
1135 if($record->field('700')){
1136 for my $au ($record->field('700')->subfield('a')){
1137 $oauthors .= "&rft.au=$au";
1140 $title = "&rft.btitle=".$record->subfield('245','a');
1141 $subtitle = $record->subfield('245', 'b') || '';
1142 $title .= $subtitle;
1143 $pubyear = $record->subfield('260', 'c') || '';
1144 $publisher = $record->subfield('260', 'b') || '';
1145 $isbn = $record->subfield('020', 'a') || '';
1146 $issn = $record->subfield('022', 'a') || '';
1149 my $coins_value = "ctx_ver=Z39.88-2004&rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3A$mtx$genre$title&rft.isbn=$isbn&rft.issn=$issn&rft.aulast=$aulast&rft.aufirst=$aufirst$oauthors&rft.pub=$publisher&rft.date=$pubyear";
1150 $coins_value =~ s/(\ |&[^a])/\+/g;
1151 #<!-- TMPL_VAR NAME="ocoins_format" -->&rft.au=<!-- TMPL_VAR NAME="author" -->&rft.btitle=<!-- TMPL_VAR NAME="title" -->&rft.date=<!-- TMPL_VAR NAME="publicationyear" -->&rft.pages=<!-- TMPL_VAR NAME="pages" -->&rft.isbn=<!-- TMPL_VAR NAME=amazonisbn -->&rft.aucorp=&rft.place=<!-- TMPL_VAR NAME="place" -->&rft.pub=<!-- TMPL_VAR NAME="publishercode" -->&rft.edition=<!-- TMPL_VAR NAME="edition" -->&rft.series=<!-- TMPL_VAR NAME="series" -->&rft.genre="
1153 return $coins_value;
1156 =head2 GetAuthorisedValueDesc
1160 my $subfieldvalue =get_authorised_value_desc(
1161 $tag, $subf[$i][0],$subf[$i][1], '', $taglib, $category);
1162 Retrieve the complete description for a given authorised value.
1164 Now takes $category and $value pair too.
1165 my $auth_value_desc =GetAuthorisedValueDesc(
1166 '','', 'DVD' ,'','','CCODE');
1172 sub GetAuthorisedValueDesc {
1173 my ( $tag, $subfield, $value, $framework, $tagslib, $category ) = @_;
1174 my $dbh = C4::Context->dbh;
1178 return $value unless defined $tagslib->{$tag}->{$subfield}->{'authorised_value'};
1181 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
1182 return C4::Branch::GetBranchName($value);
1186 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "itemtypes" ) {
1187 return getitemtypeinfo($value)->{description};
1190 #---- "true" authorized value
1191 $category = $tagslib->{$tag}->{$subfield}->{'authorised_value'}
1194 if ( $category ne "" ) {
1197 "SELECT lib FROM authorised_values WHERE category = ? AND authorised_value = ?"
1199 $sth->execute( $category, $value );
1200 my $data = $sth->fetchrow_hashref;
1201 return $data->{'lib'};
1204 return $value; # if nothing is found return the original value
1212 $marcnotesarray = GetMarcNotes( $record, $marcflavour );
1213 Get all notes from the MARC record and returns them in an array.
1214 The note are stored in differents places depending on MARC flavour
1221 my ( $record, $marcflavour ) = @_;
1223 if ( $marcflavour eq "MARC21" ) {
1226 else { # assume unimarc if not marc21
1233 foreach my $field ( $record->field($scope) ) {
1234 my $value = $field->as_string();
1235 if ( $note ne "" ) {
1236 $marcnote = { marcnote => $note, };
1237 push @marcnotes, $marcnote;
1240 if ( $note ne $value ) {
1241 $note = $note . " " . $value;
1246 $marcnote = { marcnote => $note };
1247 push @marcnotes, $marcnote; #load last tag into array
1250 } # end GetMarcNotes
1252 =head2 GetMarcSubjects
1256 $marcsubjcts = GetMarcSubjects($record,$marcflavour);
1257 Get all subjects from the MARC record and returns them in an array.
1258 The subjects are stored in differents places depending on MARC flavour
1264 sub GetMarcSubjects {
1265 my ( $record, $marcflavour ) = @_;
1266 my ( $mintag, $maxtag );
1267 if ( $marcflavour eq "MARC21" ) {
1271 else { # assume unimarc if not marc21
1281 foreach my $field ( $record->field('6..' )) {
1282 next unless $field->tag() >= $mintag && $field->tag() <= $maxtag;
1284 my @subfields = $field->subfields();
1287 # if there is an authority link, build the link with an= subfield9
1288 my $subfield9 = $field->subfield('9');
1289 for my $subject_subfield (@subfields ) {
1290 # don't load unimarc subfields 3,4,5
1291 next if (($marcflavour eq "UNIMARC") and ($subject_subfield->[0] =~ /3|4|5/ ) );
1292 # don't load MARC21 subfields 2 (FIXME: any more subfields??)
1293 next if (($marcflavour eq "MARC21") and ($subject_subfield->[0] =~ /2/ ) );
1294 my $code = $subject_subfield->[0];
1295 my $value = $subject_subfield->[1];
1296 my $linkvalue = $value;
1297 $linkvalue =~ s/(\(|\))//g;
1298 my $operator = " and " unless $counter==0;
1300 @link_loop = ({'limit' => 'an' ,link => "$subfield9" });
1302 push @link_loop, {'limit' => 'su', link => $linkvalue, operator => $operator };
1304 my $separator = C4::Context->preference("authoritysep") unless $counter==0;
1306 my @this_link_loop = @link_loop;
1307 push @subfields_loop, {code => $code, value => $value, link_loop => \@this_link_loop, separator => $separator} unless ($subject_subfield->[0] eq 9 );
1311 push @marcsubjects, { MARCSUBJECT_SUBFIELDS_LOOP => \@subfields_loop };
1314 return \@marcsubjects;
1315 } #end getMARCsubjects
1317 =head2 GetMarcAuthors
1321 authors = GetMarcAuthors($record,$marcflavour);
1322 Get all authors from the MARC record and returns them in an array.
1323 The authors are stored in differents places depending on MARC flavour
1329 sub GetMarcAuthors {
1330 my ( $record, $marcflavour ) = @_;
1331 my ( $mintag, $maxtag );
1332 # tagslib useful for UNIMARC author reponsabilities
1333 my $tagslib = &GetMarcStructure( 1, '' ); # FIXME : we don't have the framework available, we take the default framework. May be buggy on some setups, will be usually correct.
1334 if ( $marcflavour eq "MARC21" ) {
1338 elsif ( $marcflavour eq "UNIMARC" ) { # assume unimarc if not marc21
1347 foreach my $field ( $record->fields ) {
1348 next unless $field->tag() >= $mintag && $field->tag() <= $maxtag;
1351 my @subfields = $field->subfields();
1353 # if there is an authority link, build the link with Koha-Auth-Number: subfield9
1354 my $subfield9 = $field->subfield('9');
1355 for my $authors_subfield (@subfields) {
1356 # don't load unimarc subfields 3, 5
1357 next if ($marcflavour eq 'UNIMARC' and ($authors_subfield->[0] =~ /3|5/ ) );
1358 my $subfieldcode = $authors_subfield->[0];
1359 my $value = $authors_subfield->[1];
1360 my $linkvalue = $value;
1361 $linkvalue =~ s/(\(|\))//g;
1362 my $operator = " and " unless $count_auth==0;
1363 # if we have an authority link, use that as the link, otherwise use standard searching
1365 @link_loop = ({'limit' => 'an' ,link => "$subfield9" });
1368 # reset $linkvalue if UNIMARC author responsibility
1369 if ( $marcflavour eq 'UNIMARC' and ($authors_subfield->[0] eq "4")) {
1370 $linkvalue = "(".GetAuthorisedValueDesc( $field->tag(), $authors_subfield->[0], $authors_subfield->[1], '', $tagslib ).")";
1372 push @link_loop, {'limit' => 'au', link => $linkvalue, operator => $operator };
1374 $value = GetAuthorisedValueDesc( $field->tag(), $authors_subfield->[0], $authors_subfield->[1], '', $tagslib ) if ( $marcflavour eq 'UNIMARC' and ($authors_subfield->[0] =~/4/));
1375 my @this_link_loop = @link_loop;
1376 my $separator = C4::Context->preference("authoritysep") unless $count_auth==0;
1377 push @subfields_loop, {code => $subfieldcode, value => $value, link_loop => \@this_link_loop, separator => $separator} unless ($authors_subfield->[0] eq '9' );
1380 push @marcauthors, { MARCAUTHOR_SUBFIELDS_LOOP => \@subfields_loop };
1382 return \@marcauthors;
1389 $marcurls = GetMarcUrls($record,$marcflavour);
1390 Returns arrayref of URLs from MARC data, suitable to pass to tmpl loop.
1391 Assumes web resources (not uncommon in MARC21 to omit resource type ind)
1398 my ( $record, $marcflavour ) = @_;
1401 for my $field ( $record->field('856') ) {
1404 for my $note ( $field->subfield('z') ) {
1405 push @notes, { note => $note };
1407 my @urls = $field->subfield('u');
1408 foreach my $url (@urls) {
1409 if ( $marcflavour eq 'MARC21' ) {
1410 my $s3 = $field->subfield('3');
1411 my $link = $field->subfield('y');
1412 unless ( $url =~ /^\w+:/ ) {
1413 if ( $field->indicator(1) eq '7' ) {
1414 $url = $field->subfield('2') . "://" . $url;
1415 } elsif ( $field->indicator(1) eq '1' ) {
1416 $url = 'ftp://' . $url;
1418 # properly, this should be if ind1=4,
1419 # however we will assume http protocol since we're building a link.
1420 $url = 'http://' . $url;
1423 # TODO handle ind 2 (relationship)
1428 $marcurl->{'linktext'} = $link || $s3 || C4::Context->preference('URLLinkText') || $url;
1429 $marcurl->{'part'} = $s3 if ($link);
1430 $marcurl->{'toc'} = 1 if ( defined($s3) && $s3 =~ /^[Tt]able/ );
1432 $marcurl->{'linktext'} = $field->subfield('2') || C4::Context->preference('URLLinkText') || $url;
1433 $marcurl->{'MARCURL'} = $url;
1435 push @marcurls, $marcurl;
1441 =head2 GetMarcSeries
1445 $marcseriesarray = GetMarcSeries($record,$marcflavour);
1446 Get all series from the MARC record and returns them in an array.
1447 The series are stored in differents places depending on MARC flavour
1454 my ($record, $marcflavour) = @_;
1455 my ($mintag, $maxtag);
1456 if ($marcflavour eq "MARC21") {
1459 } else { # assume unimarc if not marc21
1469 foreach my $field ($record->field('440'), $record->field('490')) {
1471 #my $value = $field->subfield('a');
1472 #$marcsubjct = {MARCSUBJCT => $value,};
1473 my @subfields = $field->subfields();
1474 #warn "subfields:".join " ", @$subfields;
1477 for my $series_subfield (@subfields) {
1479 undef $volume_number;
1480 # see if this is an instance of a volume
1481 if ($series_subfield->[0] eq 'v') {
1485 my $code = $series_subfield->[0];
1486 my $value = $series_subfield->[1];
1487 my $linkvalue = $value;
1488 $linkvalue =~ s/(\(|\))//g;
1489 my $operator = " and " unless $counter==0;
1490 push @link_loop, {link => $linkvalue, operator => $operator };
1491 my $separator = C4::Context->preference("authoritysep") unless $counter==0;
1492 if ($volume_number) {
1493 push @subfields_loop, {volumenum => $value};
1496 push @subfields_loop, {code => $code, value => $value, link_loop => \@link_loop, separator => $separator, volumenum => $volume_number};
1500 push @marcseries, { MARCSERIES_SUBFIELDS_LOOP => \@subfields_loop };
1501 #$marcsubjct = {MARCSUBJCT => $field->as_string(),};
1502 #push @marcsubjcts, $marcsubjct;
1506 my $marcseriessarray=\@marcseries;
1507 return $marcseriessarray;
1508 } #end getMARCseriess
1510 =head2 GetFrameworkCode
1514 $frameworkcode = GetFrameworkCode( $biblionumber )
1520 sub GetFrameworkCode {
1521 my ( $biblionumber ) = @_;
1522 my $dbh = C4::Context->dbh;
1523 my $sth = $dbh->prepare("SELECT frameworkcode FROM biblio WHERE biblionumber=?");
1524 $sth->execute($biblionumber);
1525 my ($frameworkcode) = $sth->fetchrow;
1526 return $frameworkcode;
1529 =head2 GetPublisherNameFromIsbn
1531 $name = GetPublishercodeFromIsbn($isbn);
1538 sub GetPublisherNameFromIsbn($){
1540 $isbn =~ s/[- _]//g;
1542 my @codes = (split '-', DisplayISBN($isbn));
1543 my $code = $codes[0].$codes[1].$codes[2];
1544 my $dbh = C4::Context->dbh;
1546 SELECT distinct publishercode
1549 AND publishercode IS NOT NULL
1552 my $sth = $dbh->prepare($query);
1553 $sth->execute("$code%");
1554 my $name = $sth->fetchrow;
1555 return $name if length $name;
1559 =head2 TransformKohaToMarc
1563 $record = TransformKohaToMarc( $hash )
1564 This function builds partial MARC::Record from a hash
1565 Hash entries can be from biblio or biblioitems.
1566 This function is called in acquisition module, to create a basic catalogue entry from user entry
1572 sub TransformKohaToMarc {
1574 my $sth = C4::Context->dbh->prepare(
1575 "SELECT tagfield,tagsubfield FROM marc_subfield_structure WHERE frameworkcode=? AND kohafield=?"
1577 my $record = MARC::Record->new();
1578 SetMarcUnicodeFlag($record, C4::Context->preference("marcflavour"));
1579 foreach (keys %{$hash}) {
1580 &TransformKohaToMarcOneField( $sth, $record, $_, $hash->{$_}, '' );
1585 =head2 TransformKohaToMarcOneField
1589 $record = TransformKohaToMarcOneField( $sth, $record, $kohafieldname, $value, $frameworkcode );
1595 sub TransformKohaToMarcOneField {
1596 my ( $sth, $record, $kohafieldname, $value, $frameworkcode ) = @_;
1597 $frameworkcode='' unless $frameworkcode;
1601 if ( !defined $sth ) {
1602 my $dbh = C4::Context->dbh;
1603 $sth = $dbh->prepare(
1604 "SELECT tagfield,tagsubfield FROM marc_subfield_structure WHERE frameworkcode=? AND kohafield=?"
1607 $sth->execute( $frameworkcode, $kohafieldname );
1608 if ( ( $tagfield, $tagsubfield ) = $sth->fetchrow ) {
1609 my $tag = $record->field($tagfield);
1611 $tag->update( $tagsubfield => $value );
1612 $record->delete_field($tag);
1613 $record->insert_fields_ordered($tag);
1616 $record->add_fields( $tagfield, " ", " ", $tagsubfield => $value );
1622 =head2 TransformHtmlToXml
1626 $xml = TransformHtmlToXml( $tags, $subfields, $values, $indicator, $ind_tag, $auth_type )
1628 $auth_type contains :
1629 - nothing : rebuild a biblio, un UNIMARC the encoding is in 100$a pos 26/27
1630 - UNIMARCAUTH : rebuild an authority. In UNIMARC, the encoding is in 100$a pos 13/14
1631 - ITEM : rebuild an item : in UNIMARC, 100$a, it's in the biblio ! (otherwise, we would get 2 100 fields !)
1637 sub TransformHtmlToXml {
1638 my ( $tags, $subfields, $values, $indicator, $ind_tag, $auth_type ) = @_;
1639 my $xml = MARC::File::XML::header('UTF-8');
1640 $xml .= "<record>\n";
1641 $auth_type = C4::Context->preference('marcflavour') unless $auth_type;
1642 MARC::File::XML->default_record_format($auth_type);
1643 # in UNIMARC, field 100 contains the encoding
1644 # check that there is one, otherwise the
1645 # MARC::Record->new_from_xml will fail (and Koha will die)
1646 my $unimarc_and_100_exist=0;
1647 $unimarc_and_100_exist=1 if $auth_type eq 'ITEM'; # if we rebuild an item, no need of a 100 field
1652 for ( my $i = 0 ; $i < @$tags ; $i++ ) {
1653 if (C4::Context->preference('marcflavour') eq 'UNIMARC' and @$tags[$i] eq "100" and @$subfields[$i] eq "a") {
1654 # if we have a 100 field and it's values are not correct, skip them.
1655 # if we don't have any valid 100 field, we will create a default one at the end
1656 my $enc = substr( @$values[$i], 26, 2 );
1657 if ($enc eq '01' or $enc eq '50' or $enc eq '03') {
1658 $unimarc_and_100_exist=1;
1663 @$values[$i] =~ s/&/&/g;
1664 @$values[$i] =~ s/</</g;
1665 @$values[$i] =~ s/>/>/g;
1666 @$values[$i] =~ s/"/"/g;
1667 @$values[$i] =~ s/'/'/g;
1668 # if ( !utf8::is_utf8( @$values[$i] ) ) {
1669 # utf8::decode( @$values[$i] );
1671 if ( ( @$tags[$i] ne $prevtag ) ) {
1672 $j++ unless ( @$tags[$i] eq "" );
1674 $xml .= "</datafield>\n";
1675 if ( ( @$tags[$i] && @$tags[$i] > 10 )
1676 && ( @$values[$i] ne "" ) )
1678 my $ind1 = substr( @$indicator[$j], 0, 1 );
1680 if ( @$indicator[$j] ) {
1681 $ind2 = substr( @$indicator[$j], 1, 1 );
1684 warn "Indicator in @$tags[$i] is empty";
1687 $xml .= "<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
1688 $xml .= "<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
1696 if ( @$values[$i] ne "" ) {
1699 if ( @$tags[$i] eq "000" ) {
1700 $xml .= "<leader>@$values[$i]</leader>\n";
1703 # rest of the fixed fields
1705 elsif ( @$tags[$i] < 10 ) {
1706 $xml .= "<controlfield tag=\"@$tags[$i]\">@$values[$i]</controlfield>\n";
1710 my $ind1 = default_ind_to_space( substr( @$indicator[$j], 0, 1 ) );
1711 my $ind2 = default_ind_to_space( substr( @$indicator[$j], 1, 1 ) );
1712 $xml .= "<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
1713 $xml .= "<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
1719 else { # @$tags[$i] eq $prevtag
1720 if ( @$values[$i] eq "" ) {
1724 my $ind1 = default_ind_to_space( substr( @$indicator[$j], 0, 1 ) );
1725 my $ind2 = default_ind_to_space( substr( @$indicator[$j], 1, 1 ) );
1726 $xml .= "<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
1729 $xml .= "<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
1732 $prevtag = @$tags[$i];
1734 $xml .= "</datafield>\n" if @$tags > 0;
1735 if (C4::Context->preference('marcflavour') eq 'UNIMARC' and !$unimarc_and_100_exist) {
1736 # warn "SETTING 100 for $auth_type";
1737 my $string = strftime( "%Y%m%d", localtime(time) );
1738 # set 50 to position 26 is biblios, 13 if authorities
1740 $pos=13 if $auth_type eq 'UNIMARCAUTH';
1741 $string = sprintf( "%-*s", 35, $string );
1742 substr( $string, $pos , 6, "50" );
1743 $xml .= "<datafield tag=\"100\" ind1=\"\" ind2=\"\">\n";
1744 $xml .= "<subfield code=\"a\">$string</subfield>\n";
1745 $xml .= "</datafield>\n";
1747 $xml .= "</record>\n";
1748 $xml .= MARC::File::XML::footer();
1752 =head2 default_ind_to_space
1754 Passed what should be an indicator returns a space
1755 if its undefined or zero length
1759 sub default_ind_to_space {
1761 if (!defined $s || $s eq q{} ) {
1767 =head2 TransformHtmlToMarc
1769 L<$record> = TransformHtmlToMarc(L<$params>,L<$cgi>)
1770 L<$params> is a ref to an array as below:
1772 'tag_010_indicator1_531951' ,
1773 'tag_010_indicator2_531951' ,
1774 'tag_010_code_a_531951_145735' ,
1775 'tag_010_subfield_a_531951_145735' ,
1776 'tag_200_indicator1_873510' ,
1777 'tag_200_indicator2_873510' ,
1778 'tag_200_code_a_873510_673465' ,
1779 'tag_200_subfield_a_873510_673465' ,
1780 'tag_200_code_b_873510_704318' ,
1781 'tag_200_subfield_b_873510_704318' ,
1782 'tag_200_code_e_873510_280822' ,
1783 'tag_200_subfield_e_873510_280822' ,
1784 'tag_200_code_f_873510_110730' ,
1785 'tag_200_subfield_f_873510_110730' ,
1787 L<$cgi> is the CGI object which containts the value.
1788 L<$record> is the MARC::Record object.
1792 sub TransformHtmlToMarc {
1796 # explicitly turn on the UTF-8 flag for all
1797 # 'tag_' parameters to avoid incorrect character
1798 # conversion later on
1799 my $cgi_params = $cgi->Vars;
1800 foreach my $param_name (keys %$cgi_params) {
1801 if ($param_name =~ /^tag_/) {
1802 my $param_value = $cgi_params->{$param_name};
1803 if (utf8::decode($param_value)) {
1804 $cgi_params->{$param_name} = $param_value;
1806 # FIXME - need to do something if string is not valid UTF-8
1810 # creating a new record
1811 my $record = MARC::Record->new();
1814 while ($params->[$i]){ # browse all CGI params
1815 my $param = $params->[$i];
1817 # if we are on biblionumber, store it in the MARC::Record (it may not be in the edited fields)
1818 if ($param eq 'biblionumber') {
1819 my ( $biblionumbertagfield, $biblionumbertagsubfield ) =
1820 &GetMarcFromKohaField( "biblio.biblionumber", '' );
1821 if ($biblionumbertagfield < 10) {
1822 $newfield = MARC::Field->new(
1823 $biblionumbertagfield,
1824 $cgi->param($param),
1827 $newfield = MARC::Field->new(
1828 $biblionumbertagfield,
1831 "$biblionumbertagsubfield" => $cgi->param($param),
1834 push @fields,$newfield if($newfield);
1836 elsif ($param =~ /^tag_(\d*)_indicator1_/){ # new field start when having 'input name="..._indicator1_..."
1839 my $ind1 = substr($cgi->param($param),0,1);
1840 my $ind2 = substr($cgi->param($params->[$i+1]),0,1);
1844 if($tag < 10){ # no code for theses fields
1845 # in MARC editor, 000 contains the leader.
1846 if ($tag eq '000' ) {
1847 $record->leader($cgi->param($params->[$j+1])) if length($cgi->param($params->[$j+1]))==24;
1848 # between 001 and 009 (included)
1849 } elsif ($cgi->param($params->[$j+1]) ne '') {
1850 $newfield = MARC::Field->new(
1852 $cgi->param($params->[$j+1]),
1855 # > 009, deal with subfields
1857 while(defined $params->[$j] && $params->[$j] =~ /_code_/){ # browse all it's subfield
1858 my $inner_param = $params->[$j];
1860 if($cgi->param($params->[$j+1]) ne ''){ # only if there is a value (code => value)
1861 $newfield->add_subfields(
1862 $cgi->param($inner_param) => $cgi->param($params->[$j+1])
1866 if ( $cgi->param($params->[$j+1]) ne '' ) { # creating only if there is a value (code => value)
1867 $newfield = MARC::Field->new(
1871 $cgi->param($inner_param) => $cgi->param($params->[$j+1]),
1878 push @fields,$newfield if($newfield);
1883 $record->append_fields(@fields);
1887 # cache inverted MARC field map
1888 our $inverted_field_map;
1890 =head2 TransformMarcToKoha
1894 $result = TransformMarcToKoha( $dbh, $record, $frameworkcode )
1898 Extract data from a MARC bib record into a hashref representing
1899 Koha biblio, biblioitems, and items fields.
1902 sub TransformMarcToKoha {
1903 my ( $dbh, $record, $frameworkcode, $limit_table ) = @_;
1906 $limit_table=$limit_table||0;
1907 $frameworkcode = '' unless defined $frameworkcode;
1909 unless (defined $inverted_field_map) {
1910 $inverted_field_map = _get_inverted_marc_field_map();
1914 if ( defined $limit_table && $limit_table eq 'items') {
1915 $tables{'items'} = 1;
1917 $tables{'items'} = 1;
1918 $tables{'biblio'} = 1;
1919 $tables{'biblioitems'} = 1;
1922 # traverse through record
1923 MARCFIELD: foreach my $field ($record->fields()) {
1924 my $tag = $field->tag();
1925 next MARCFIELD unless exists $inverted_field_map->{$frameworkcode}->{$tag};
1926 if ($field->is_control_field()) {
1927 my $kohafields = $inverted_field_map->{$frameworkcode}->{$tag}->{list};
1928 ENTRY: foreach my $entry (@{ $kohafields }) {
1929 my ($subfield, $table, $column) = @{ $entry };
1930 next ENTRY unless exists $tables{$table};
1931 my $key = _disambiguate($table, $column);
1932 if ($result->{$key}) {
1933 unless (($key eq "biblionumber" or $key eq "biblioitemnumber") and ($field->data() eq "")) {
1934 $result->{$key} .= " | " . $field->data();
1937 $result->{$key} = $field->data();
1941 # deal with subfields
1942 MARCSUBFIELD: foreach my $sf ($field->subfields()) {
1943 my $code = $sf->[0];
1944 next MARCSUBFIELD unless exists $inverted_field_map->{$frameworkcode}->{$tag}->{sfs}->{$code};
1945 my $value = $sf->[1];
1946 SFENTRY: foreach my $entry (@{ $inverted_field_map->{$frameworkcode}->{$tag}->{sfs}->{$code} }) {
1947 my ($table, $column) = @{ $entry };
1948 next SFENTRY unless exists $tables{$table};
1949 my $key = _disambiguate($table, $column);
1950 if ($result->{$key}) {
1951 unless (($key eq "biblionumber" or $key eq "biblioitemnumber") and ($value eq "")) {
1952 $result->{$key} .= " | " . $value;
1955 $result->{$key} = $value;
1962 # modify copyrightdate to keep only the 1st year found
1963 if (exists $result->{'copyrightdate'}) {
1964 my $temp = $result->{'copyrightdate'};
1965 $temp =~ m/c(\d\d\d\d)/;
1966 if ( $temp =~ m/c(\d\d\d\d)/ and $1 > 0 ) { # search cYYYY first
1967 $result->{'copyrightdate'} = $1;
1969 else { # if no cYYYY, get the 1st date.
1970 $temp =~ m/(\d\d\d\d)/;
1971 $result->{'copyrightdate'} = $1;
1975 # modify publicationyear to keep only the 1st year found
1976 if (exists $result->{'publicationyear'}) {
1977 my $temp = $result->{'publicationyear'};
1978 if ( $temp =~ m/c(\d\d\d\d)/ and $1 > 0 ) { # search cYYYY first
1979 $result->{'publicationyear'} = $1;
1981 else { # if no cYYYY, get the 1st date.
1982 $temp =~ m/(\d\d\d\d)/;
1983 $result->{'publicationyear'} = $1;
1990 sub _get_inverted_marc_field_map {
1992 my $relations = C4::Context->marcfromkohafield;
1994 foreach my $frameworkcode (keys %{ $relations }) {
1995 foreach my $kohafield (keys %{ $relations->{$frameworkcode} }) {
1996 next unless @{ $relations->{$frameworkcode}->{$kohafield} }; # not all columns are mapped to MARC tag & subfield
1997 my $tag = $relations->{$frameworkcode}->{$kohafield}->[0];
1998 my $subfield = $relations->{$frameworkcode}->{$kohafield}->[1];
1999 my ($table, $column) = split /[.]/, $kohafield, 2;
2000 push @{ $field_map->{$frameworkcode}->{$tag}->{list} }, [ $subfield, $table, $column ];
2001 push @{ $field_map->{$frameworkcode}->{$tag}->{sfs}->{$subfield} }, [ $table, $column ];
2007 =head2 _disambiguate
2011 $newkey = _disambiguate($table, $field);
2013 This is a temporary hack to distinguish between the
2014 following sets of columns when using TransformMarcToKoha.
2016 items.cn_source & biblioitems.cn_source
2017 items.cn_sort & biblioitems.cn_sort
2019 Columns that are currently NOT distinguished (FIXME
2020 due to lack of time to fully test) are:
2022 biblio.notes and biblioitems.notes
2027 FIXME - this is necessary because prefixing each column
2028 name with the table name would require changing lots
2029 of code and templates, and exposing more of the DB
2030 structure than is good to the UI templates, particularly
2031 since biblio and bibloitems may well merge in a future
2032 version. In the future, it would also be good to
2033 separate DB access and UI presentation field names
2040 sub CountItemsIssued {
2041 my ( $biblionumber ) = @_;
2042 my $dbh = C4::Context->dbh;
2043 my $sth = $dbh->prepare('SELECT COUNT(*) as issuedCount FROM items, issues WHERE items.itemnumber = issues.itemnumber AND items.biblionumber = ?');
2044 $sth->execute( $biblionumber );
2045 my $row = $sth->fetchrow_hashref();
2046 return $row->{'issuedCount'};
2050 my ($table, $column) = @_;
2051 if ($column eq "cn_sort" or $column eq "cn_source") {
2052 return $table . '.' . $column;
2059 =head2 get_koha_field_from_marc
2063 $result->{_disambiguate($table, $field)} = get_koha_field_from_marc($table,$field,$record,$frameworkcode);
2065 Internal function to map data from the MARC record to a specific non-MARC field.
2066 FIXME: this is meant to replace TransformMarcToKohaOneField after more testing.
2072 sub get_koha_field_from_marc {
2073 my ($koha_table,$koha_column,$record,$frameworkcode) = @_;
2074 my ( $tagfield, $subfield ) = GetMarcFromKohaField( $koha_table.'.'.$koha_column, $frameworkcode );
2076 foreach my $field ( $record->field($tagfield) ) {
2077 if ( $field->tag() < 10 ) {
2079 $kohafield .= " | " . $field->data();
2082 $kohafield = $field->data();
2086 if ( $field->subfields ) {
2087 my @subfields = $field->subfields();
2088 foreach my $subfieldcount ( 0 .. $#subfields ) {
2089 if ( $subfields[$subfieldcount][0] eq $subfield ) {
2092 " | " . $subfields[$subfieldcount][1];
2096 $subfields[$subfieldcount][1];
2107 =head2 TransformMarcToKohaOneField
2111 $result = TransformMarcToKohaOneField( $kohatable, $kohafield, $record, $result, $frameworkcode )
2117 sub TransformMarcToKohaOneField {
2119 # FIXME ? if a field has a repeatable subfield that is used in old-db,
2120 # only the 1st will be retrieved...
2121 my ( $kohatable, $kohafield, $record, $result, $frameworkcode ) = @_;
2123 my ( $tagfield, $subfield ) =
2124 GetMarcFromKohaField( $kohatable . "." . $kohafield,
2126 foreach my $field ( $record->field($tagfield) ) {
2127 if ( $field->tag() < 10 ) {
2128 if ( $result->{$kohafield} ) {
2129 $result->{$kohafield} .= " | " . $field->data();
2132 $result->{$kohafield} = $field->data();
2136 if ( $field->subfields ) {
2137 my @subfields = $field->subfields();
2138 foreach my $subfieldcount ( 0 .. $#subfields ) {
2139 if ( $subfields[$subfieldcount][0] eq $subfield ) {
2140 if ( $result->{$kohafield} ) {
2141 $result->{$kohafield} .=
2142 " | " . $subfields[$subfieldcount][1];
2145 $result->{$kohafield} =
2146 $subfields[$subfieldcount][1];
2156 =head1 OTHER FUNCTIONS
2159 =head2 PrepareItemrecordDisplay
2163 PrepareItemrecordDisplay($itemrecord,$bibnum,$itemumber);
2165 Returns a hash with all the fields for Display a given item data in a template
2171 sub PrepareItemrecordDisplay {
2173 my ( $bibnum, $itemnum, $defaultvalues ) = @_;
2175 my $dbh = C4::Context->dbh;
2176 my $frameworkcode = &GetFrameworkCode( $bibnum );
2177 my ( $itemtagfield, $itemtagsubfield ) =
2178 &GetMarcFromKohaField( "items.itemnumber", $frameworkcode );
2179 my $tagslib = &GetMarcStructure( 1, $frameworkcode );
2180 my $itemrecord = C4::Items::GetMarcItem( $bibnum, $itemnum) if ($itemnum);
2182 my $authorised_values_sth =
2184 "SELECT authorised_value,lib FROM authorised_values WHERE category=? ORDER BY lib"
2186 foreach my $tag ( sort keys %{$tagslib} ) {
2187 my $previous_tag = '';
2189 # loop through each subfield
2191 foreach my $subfield ( sort keys %{ $tagslib->{$tag} } ) {
2192 next if ( subfield_is_koha_internal_p($subfield) );
2193 next if ( $tagslib->{$tag}->{$subfield}->{'tab'} ne "10" );
2195 $subfield_data{tag} = $tag;
2196 $subfield_data{subfield} = $subfield;
2197 $subfield_data{countsubfield} = $cntsubf++;
2198 $subfield_data{kohafield} =
2199 $tagslib->{$tag}->{$subfield}->{'kohafield'};
2201 # $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
2202 $subfield_data{marc_lib} = $tagslib->{$tag}->{$subfield}->{lib};
2203 $subfield_data{mandatory} =
2204 $tagslib->{$tag}->{$subfield}->{mandatory};
2205 $subfield_data{repeatable} =
2206 $tagslib->{$tag}->{$subfield}->{repeatable};
2207 $subfield_data{hidden} = "display:none"
2208 if $tagslib->{$tag}->{$subfield}->{hidden};
2211 ( $x, $value ) = _find_value( $tag, $subfield, $itemrecord );
2213 if (!defined $value) {
2216 $value =~ s/"/"/g;
2218 # search for itemcallnumber if applicable
2219 if ( $tagslib->{$tag}->{$subfield}->{kohafield} eq
2220 'items.itemcallnumber'
2221 && C4::Context->preference('itemcallnumber') )
2224 substr( C4::Context->preference('itemcallnumber'), 0, 3 );
2226 substr( C4::Context->preference('itemcallnumber'), 3, 1 );
2227 my $temp = $itemrecord->field($CNtag) if ($itemrecord);
2229 $value = $temp->subfield($CNsubfield);
2232 if ( $tagslib->{$tag}->{$subfield}->{kohafield} eq
2233 'items.itemcallnumber'
2234 && $defaultvalues->{'callnumber'} )
2236 my $temp = $itemrecord->field($subfield) if ($itemrecord);
2238 $value = $defaultvalues->{'callnumber'};
2241 if ( ($tagslib->{$tag}->{$subfield}->{kohafield} eq
2242 'items.holdingbranch' ||
2243 $tagslib->{$tag}->{$subfield}->{kohafield} eq
2245 && $defaultvalues->{'branchcode'} )
2247 my $temp = $itemrecord->field($subfield) if ($itemrecord);
2249 $value = $defaultvalues->{branchcode};
2252 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
2253 my @authorised_values;
2256 # builds list, depending on authorised value...
2258 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq
2261 if ( ( C4::Context->preference("IndependantBranches") )
2262 && ( C4::Context->userenv->{flags} % 2 != 1 ) )
2266 "SELECT branchcode,branchname FROM branches WHERE branchcode = ? ORDER BY branchname"
2268 $sth->execute( C4::Context->userenv->{branch} );
2269 push @authorised_values, ""
2271 $tagslib->{$tag}->{$subfield}->{mandatory} );
2272 while ( my ( $branchcode, $branchname ) =
2273 $sth->fetchrow_array )
2275 push @authorised_values, $branchcode;
2276 $authorised_lib{$branchcode} = $branchname;
2282 "SELECT branchcode,branchname FROM branches ORDER BY branchname"
2285 push @authorised_values, ""
2287 $tagslib->{$tag}->{$subfield}->{mandatory} );
2288 while ( my ( $branchcode, $branchname ) =
2289 $sth->fetchrow_array )
2291 push @authorised_values, $branchcode;
2292 $authorised_lib{$branchcode} = $branchname;
2298 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq
2303 "SELECT itemtype,description FROM itemtypes ORDER BY description"
2306 push @authorised_values, ""
2307 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
2308 while ( my ( $itemtype, $description ) =
2309 $sth->fetchrow_array )
2311 push @authorised_values, $itemtype;
2312 $authorised_lib{$itemtype} = $description;
2315 #---- "true" authorised value
2318 $authorised_values_sth->execute(
2319 $tagslib->{$tag}->{$subfield}->{authorised_value} );
2320 push @authorised_values, ""
2321 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
2322 while ( my ( $value, $lib ) =
2323 $authorised_values_sth->fetchrow_array )
2325 push @authorised_values, $value;
2326 $authorised_lib{$value} = $lib;
2329 $subfield_data{marc_value} = CGI::scrolling_list(
2330 -name => 'field_value',
2331 -values => \@authorised_values,
2332 -default => "$value",
2333 -labels => \%authorised_lib,
2340 $subfield_data{marc_value} =
2341 "<input type=\"text\" name=\"field_value\" value=\"$value\" size=\"50\" maxlength=\"255\" />";
2343 push( @loop_data, \%subfield_data );
2347 my $itemnumber = $itemrecord->subfield( $itemtagfield, $itemtagsubfield )
2348 if ( $itemrecord && $itemrecord->field($itemtagfield) );
2350 'itemtagfield' => $itemtagfield,
2351 'itemtagsubfield' => $itemtagsubfield,
2352 'itemnumber' => $itemnumber,
2353 'iteminformation' => \@loop_data
2359 # true ModZebra commented until indexdata fixes zebraDB crashes (it seems they occur on multiple updates
2361 # replaced by a zebraqueue table, that is filled with ModZebra to run.
2362 # the table is emptied by misc/cronjobs/zebraqueue_start.pl script
2363 # =head2 ModZebrafiles
2365 # &ModZebrafiles( $dbh, $biblionumber, $record, $folder, $server );
2369 # sub ModZebrafiles {
2371 # my ( $dbh, $biblionumber, $record, $folder, $server ) = @_;
2375 # C4::Context->zebraconfig($server)->{directory} . "/" . $folder . "/";
2376 # unless ( opendir( DIR, "$zebradir" ) ) {
2377 # warn "$zebradir not found";
2381 # my $filename = $zebradir . $biblionumber;
2384 # open( OUTPUT, ">", $filename . ".xml" );
2385 # print OUTPUT $record;
2394 ModZebra( $biblionumber, $op, $server, $oldRecord, $newRecord );
2396 $biblionumber is the biblionumber we want to index
2397 $op is specialUpdate or delete, and is used to know what we want to do
2398 $server is the server that we want to update
2399 $oldRecord is the MARC::Record containing the previous version of the record. This is used only when
2400 NoZebra=1, as NoZebra indexing needs to know the previous version of a record in order to
2402 $newRecord is the MARC::Record containing the new record. It is usefull only when NoZebra=1, and is used to know what to add to the nozebra database. (the record in mySQL being, if it exist, the previous record, the one just before the modif. We need both : the previous and the new one.
2409 ###Accepts a $server variable thus we can use it for biblios authorities or other zebra dbs
2410 my ( $biblionumber, $op, $server, $oldRecord, $newRecord ) = @_;
2411 my $dbh=C4::Context->dbh;
2413 # true ModZebra commented until indexdata fixes zebraDB crashes (it seems they occur on multiple updates
2415 # replaced by a zebraqueue table, that is filled with ModZebra to run.
2416 # the table is emptied by misc/cronjobs/zebraqueue_start.pl script
2418 if (C4::Context->preference("NoZebra")) {
2419 # lock the nozebra table : we will read index lines, update them in Perl process
2420 # and write everything in 1 transaction.
2421 # lock the table to avoid someone else overwriting what we are doing
2422 $dbh->do('LOCK TABLES nozebra WRITE,biblio WRITE,biblioitems WRITE, systempreferences WRITE, auth_types WRITE, auth_header WRITE, auth_subfield_structure READ');
2423 my %result; # the result hash that will be built by deletion / add, and written on mySQL at the end, to improve speed
2424 if ($op eq 'specialUpdate') {
2425 # OK, we have to add or update the record
2426 # 1st delete (virtually, in indexes), if record actually exists
2428 %result = _DelBiblioNoZebra($biblionumber,$oldRecord,$server);
2430 # ... add the record
2431 %result=_AddBiblioNoZebra($biblionumber,$newRecord, $server, %result);
2433 # it's a deletion, delete the record...
2434 # warn "DELETE the record $biblionumber on $server".$record->as_formatted;
2435 %result=_DelBiblioNoZebra($biblionumber,$oldRecord,$server);
2437 # ok, now update the database...
2438 my $sth = $dbh->prepare("UPDATE nozebra SET biblionumbers=? WHERE server=? AND indexname=? AND value=?");
2439 foreach my $key (keys %result) {
2440 foreach my $index (keys %{$result{$key}}) {
2441 $sth->execute($result{$key}->{$index}, $server, $key, $index);
2444 $dbh->do('UNLOCK TABLES');
2447 # we use zebra, just fill zebraqueue table
2449 my $check_sql = "SELECT COUNT(*) FROM zebraqueue
2451 AND biblio_auth_number = ?
2454 my $check_sth = $dbh->prepare_cached($check_sql);
2455 $check_sth->execute($server, $biblionumber, $op);
2456 my ($count) = $check_sth->fetchrow_array;
2457 $check_sth->finish();
2459 my $sth=$dbh->prepare("INSERT INTO zebraqueue (biblio_auth_number,server,operation) VALUES(?,?,?)");
2460 $sth->execute($biblionumber,$server,$op);
2466 =head2 GetNoZebraIndexes
2468 %indexes = GetNoZebraIndexes;
2470 return the data from NoZebraIndexes syspref.
2474 sub GetNoZebraIndexes {
2475 my $no_zebra_indexes = C4::Context->preference('NoZebraIndexes');
2477 INDEX: foreach my $line (split /['"],[\n\r]*/,$no_zebra_indexes) {
2478 $line =~ /(.*)=>(.*)/;
2479 my $index = $1; # initial ' or " is removed afterwards
2481 $index =~ s/'|"|\s//g;
2482 $fields =~ s/'|"|\s//g;
2483 $indexes{$index}=$fields;
2488 =head1 INTERNAL FUNCTIONS
2490 =head2 _DelBiblioNoZebra($biblionumber,$record,$server);
2492 function to delete a biblio in NoZebra indexes
2493 This function does NOT delete anything in database : it reads all the indexes entries
2494 that have to be deleted & delete them in the hash
2495 The SQL part is done either :
2496 - after the Add if we are modifying a biblio (delete + add again)
2497 - immediatly after this sub if we are doing a true deletion.
2498 $server can be 'biblioserver' or 'authorityserver' : it indexes biblios or authorities (in the same table, $server being part of the table itself
2503 sub _DelBiblioNoZebra {
2504 my ($biblionumber, $record, $server)=@_;
2507 my $dbh = C4::Context->dbh;
2511 if ($server eq 'biblioserver') {
2512 %index=GetNoZebraIndexes;
2513 # get title of the record (to store the 10 first letters with the index)
2514 my ($titletag,$titlesubfield) = GetMarcFromKohaField('biblio.title', ''); # FIXME: should be GetFrameworkCode($biblionumber) ??
2515 $title = lc($record->subfield($titletag,$titlesubfield));
2517 # for authorities, the "title" is the $a mainentry
2518 my ($auth_type_tag, $auth_type_sf) = C4::AuthoritiesMarc::get_auth_type_location();
2519 my $authref = C4::AuthoritiesMarc::GetAuthType($record->subfield($auth_type_tag, $auth_type_sf));
2520 warn "ERROR : authtype undefined for ".$record->as_formatted unless $authref;
2521 $title = $record->subfield($authref->{auth_tag_to_report},'a');
2522 $index{'mainmainentry'}= $authref->{'auth_tag_to_report'}.'a';
2523 $index{'mainentry'} = $authref->{'auth_tag_to_report'}.'*';
2524 $index{'auth_type'} = "${auth_type_tag}${auth_type_sf}";
2528 # remove blancks comma (that could cause problem when decoding the string for CQL retrieval) and regexp specific values
2529 $title =~ s/ |,|;|\[|\]|\(|\)|\*|-|'|=//g;
2530 # limit to 10 char, should be enough, and limit the DB size
2531 $title = substr($title,0,10);
2533 my $sth2=$dbh->prepare('SELECT biblionumbers FROM nozebra WHERE server=? AND indexname=? AND value=?');
2534 foreach my $field ($record->fields()) {
2535 #parse each subfield
2536 next if $field->tag <10;
2537 foreach my $subfield ($field->subfields()) {
2538 my $tag = $field->tag();
2539 my $subfieldcode = $subfield->[0];
2541 # check each index to see if the subfield is stored somewhere
2542 # otherwise, store it in __RAW__ index
2543 foreach my $key (keys %index) {
2544 # warn "examining $key index : ".$index{$key}." for $tag $subfieldcode";
2545 if ($index{$key} =~ /$tag\*/ or $index{$key} =~ /$tag$subfieldcode/) {
2547 my $line= lc $subfield->[1];
2548 # remove meaningless value in the field...
2549 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:/ /g;
2550 # ... and split in words
2551 foreach (split / /,$line) {
2552 next unless $_; # skip empty values (multiple spaces)
2553 # if the entry is already here, do nothing, the biblionumber has already be removed
2554 unless ( defined( $result{$key}->{$_} ) && ( $result{$key}->{$_} =~ /$biblionumber,$title\-(\d);/) ) {
2555 # get the index value if it exist in the nozebra table and remove the entry, otherwise, do nothing
2556 $sth2->execute($server,$key,$_);
2557 my $existing_biblionumbers = $sth2->fetchrow;
2559 if ($existing_biblionumbers) {
2560 # warn " existing for $key $_: $existing_biblionumbers";
2561 $result{$key}->{$_} =$existing_biblionumbers;
2562 $result{$key}->{$_} =~ s/$biblionumber,$title\-(\d);//;
2568 # the subfield is not indexed, store it in __RAW__ index anyway
2570 my $line= lc $subfield->[1];
2571 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:/ /g;
2572 # ... and split in words
2573 foreach (split / /,$line) {
2574 next unless $_; # skip empty values (multiple spaces)
2575 # if the entry is already here, do nothing, the biblionumber has already be removed
2576 unless ($result{'__RAW__'}->{$_} =~ /$biblionumber,$title\-(\d);/) {
2577 # get the index value if it exist in the nozebra table and remove the entry, otherwise, do nothing
2578 $sth2->execute($server,'__RAW__',$_);
2579 my $existing_biblionumbers = $sth2->fetchrow;
2581 if ($existing_biblionumbers) {
2582 $result{'__RAW__'}->{$_} =$existing_biblionumbers;
2583 $result{'__RAW__'}->{$_} =~ s/$biblionumber,$title\-(\d);//;
2593 =head2 _AddBiblioNoZebra($biblionumber, $record, $server, %result);
2595 function to add a biblio in NoZebra indexes
2599 sub _AddBiblioNoZebra {
2600 my ($biblionumber, $record, $server, %result)=@_;
2601 my $dbh = C4::Context->dbh;
2605 if ($server eq 'biblioserver') {
2606 %index=GetNoZebraIndexes;
2607 # get title of the record (to store the 10 first letters with the index)
2608 my ($titletag,$titlesubfield) = GetMarcFromKohaField('biblio.title', ''); # FIXME: should be GetFrameworkCode($biblionumber) ??
2609 $title = lc($record->subfield($titletag,$titlesubfield));
2611 # warn "server : $server";
2612 # for authorities, the "title" is the $a mainentry
2613 my ($auth_type_tag, $auth_type_sf) = C4::AuthoritiesMarc::get_auth_type_location();
2614 my $authref = C4::AuthoritiesMarc::GetAuthType($record->subfield($auth_type_tag, $auth_type_sf));
2615 warn "ERROR : authtype undefined for ".$record->as_formatted unless $authref;
2616 $title = $record->subfield($authref->{auth_tag_to_report},'a');
2617 $index{'mainmainentry'} = $authref->{auth_tag_to_report}.'a';
2618 $index{'mainentry'} = $authref->{auth_tag_to_report}.'*';
2619 $index{'auth_type'} = "${auth_type_tag}${auth_type_sf}";
2622 # remove blancks comma (that could cause problem when decoding the string for CQL retrieval) and regexp specific values
2623 $title =~ s/ |\.|,|;|\[|\]|\(|\)|\*|-|'|:|=|\r|\n//g;
2624 # limit to 10 char, should be enough, and limit the DB size
2625 $title = substr($title,0,10);
2627 my $sth2=$dbh->prepare('SELECT biblionumbers FROM nozebra WHERE server=? AND indexname=? AND value=?');
2628 foreach my $field ($record->fields()) {
2629 #parse each subfield
2630 ###FIXME: impossible to index a 001-009 value with NoZebra
2631 next if $field->tag <10;
2632 foreach my $subfield ($field->subfields()) {
2633 my $tag = $field->tag();
2634 my $subfieldcode = $subfield->[0];
2636 # warn "INDEXING :".$subfield->[1];
2637 # check each index to see if the subfield is stored somewhere
2638 # otherwise, store it in __RAW__ index
2639 foreach my $key (keys %index) {
2640 # warn "examining $key index : ".$index{$key}." for $tag $subfieldcode";
2641 if ($index{$key} =~ /$tag\*/ or $index{$key} =~ /$tag$subfieldcode/) {
2643 my $line= lc $subfield->[1];
2644 # remove meaningless value in the field...
2645 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:|\r|\n/ /g;
2646 # ... and split in words
2647 foreach (split / /,$line) {
2648 next unless $_; # skip empty values (multiple spaces)
2649 # if the entry is already here, improve weight
2650 # warn "managing $_";
2651 if ( exists $result{$key}->{$_} && $result{$key}->{"$_"} =~ /$biblionumber,\Q$title\E\-(\d+);/) {
2652 my $weight = $1 + 1;
2653 $result{$key}->{"$_"} =~ s/$biblionumber,\Q$title\E\-(\d+);//g;
2654 $result{$key}->{"$_"} .= "$biblionumber,$title-$weight;";
2656 # get the value if it exist in the nozebra table, otherwise, create it
2657 $sth2->execute($server,$key,$_);
2658 my $existing_biblionumbers = $sth2->fetchrow;
2660 if ($existing_biblionumbers) {
2661 $result{$key}->{"$_"} =$existing_biblionumbers;
2662 my $weight = defined $1 ? $1 + 1 : 1;
2663 $result{$key}->{"$_"} =~ s/$biblionumber,\Q$title\E\-(\d+);//g;
2664 $result{$key}->{"$_"} .= "$biblionumber,$title-$weight;";
2665 # create a new ligne for this entry
2667 # warn "INSERT : $server / $key / $_";
2668 $dbh->do('INSERT INTO nozebra SET server='.$dbh->quote($server).', indexname='.$dbh->quote($key).',value='.$dbh->quote($_));
2669 $result{$key}->{"$_"}.="$biblionumber,$title-1;";
2675 # the subfield is not indexed, store it in __RAW__ index anyway
2677 my $line= lc $subfield->[1];
2678 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:|\r|\n/ /g;
2679 # ... and split in words
2680 foreach (split / /,$line) {
2681 next unless $_; # skip empty values (multiple spaces)
2682 # if the entry is already here, improve weight
2683 my $tmpstr = $result{'__RAW__'}->{"$_"} || "";
2684 if ($tmpstr =~ /$biblionumber,\Q$title\E\-(\d+);/) {
2686 $result{'__RAW__'}->{"$_"} =~ s/$biblionumber,\Q$title\E\-(\d+);//;
2687 $result{'__RAW__'}->{"$_"} .= "$biblionumber,$title-$weight;";
2689 # get the value if it exist in the nozebra table, otherwise, create it
2690 $sth2->execute($server,'__RAW__',$_);
2691 my $existing_biblionumbers = $sth2->fetchrow;
2693 if ($existing_biblionumbers) {
2694 $result{'__RAW__'}->{"$_"} =$existing_biblionumbers;
2695 my $weight = ($1 ? $1 : 0) + 1;
2696 $result{'__RAW__'}->{"$_"} =~ s/$biblionumber,\Q$title\E\-(\d+);//;
2697 $result{'__RAW__'}->{"$_"} .= "$biblionumber,$title-$weight;";
2698 # create a new ligne for this entry
2700 $dbh->do('INSERT INTO nozebra SET server='.$dbh->quote($server).', indexname="__RAW__",value='.$dbh->quote($_));
2701 $result{'__RAW__'}->{"$_"}.="$biblionumber,$title-1;";
2716 ($indicators, $value) = _find_value($tag, $subfield, $record,$encoding);
2718 Find the given $subfield in the given $tag in the given
2719 MARC::Record $record. If the subfield is found, returns
2720 the (indicators, value) pair; otherwise, (undef, undef) is
2724 Such a function is used in addbiblio AND additem and serial-edit and maybe could be used in Authorities.
2725 I suggest we export it from this module.
2732 my ( $tagfield, $insubfield, $record, $encoding ) = @_;
2735 if ( $tagfield < 10 ) {
2736 if ( $record->field($tagfield) ) {
2737 push @result, $record->field($tagfield)->data();
2744 foreach my $field ( $record->field($tagfield) ) {
2745 my @subfields = $field->subfields();
2746 foreach my $subfield (@subfields) {
2747 if ( @$subfield[0] eq $insubfield ) {
2748 push @result, @$subfield[1];
2749 $indicator = $field->indicator(1) . $field->indicator(2);
2754 return ( $indicator, @result );
2757 =head2 _koha_marc_update_bib_ids
2761 _koha_marc_update_bib_ids($record, $frameworkcode, $biblionumber, $biblioitemnumber);
2763 Internal function to add or update biblionumber and biblioitemnumber to
2770 sub _koha_marc_update_bib_ids {
2771 my ($record, $frameworkcode, $biblionumber, $biblioitemnumber) = @_;
2773 # we must add bibnum and bibitemnum in MARC::Record...
2774 # we build the new field with biblionumber and biblioitemnumber
2775 # we drop the original field
2776 # we add the new builded field.
2777 my ($biblio_tag, $biblio_subfield ) = GetMarcFromKohaField("biblio.biblionumber",$frameworkcode);
2778 my ($biblioitem_tag, $biblioitem_subfield ) = GetMarcFromKohaField("biblioitems.biblioitemnumber",$frameworkcode);
2780 if ($biblio_tag != $biblioitem_tag) {
2781 # biblionumber & biblioitemnumber are in different fields
2783 # deal with biblionumber
2784 my ($new_field, $old_field);
2785 if ($biblio_tag < 10) {
2786 $new_field = MARC::Field->new( $biblio_tag, $biblionumber );
2789 MARC::Field->new( $biblio_tag, '', '',
2790 "$biblio_subfield" => $biblionumber );
2793 # drop old field and create new one...
2794 $old_field = $record->field($biblio_tag);
2795 $record->delete_field($old_field) if $old_field;
2796 $record->append_fields($new_field);
2798 # deal with biblioitemnumber
2799 if ($biblioitem_tag < 10) {
2800 $new_field = MARC::Field->new( $biblioitem_tag, $biblioitemnumber, );
2803 MARC::Field->new( $biblioitem_tag, '', '',
2804 "$biblioitem_subfield" => $biblioitemnumber, );
2806 # drop old field and create new one...
2807 $old_field = $record->field($biblioitem_tag);
2808 $record->delete_field($old_field) if $old_field;
2809 $record->insert_fields_ordered($new_field);
2812 # biblionumber & biblioitemnumber are in the same field (can't be <10 as fields <10 have only 1 value)
2813 my $new_field = MARC::Field->new(
2814 $biblio_tag, '', '',
2815 "$biblio_subfield" => $biblionumber,
2816 "$biblioitem_subfield" => $biblioitemnumber
2819 # drop old field and create new one...
2820 my $old_field = $record->field($biblio_tag);
2821 $record->delete_field($old_field) if $old_field;
2822 $record->insert_fields_ordered($new_field);
2826 =head2 _koha_marc_update_biblioitem_cn_sort
2830 _koha_marc_update_biblioitem_cn_sort($marc, $biblioitem, $frameworkcode);
2834 Given a MARC bib record and the biblioitem hash, update the
2835 subfield that contains a copy of the value of biblioitems.cn_sort.
2839 sub _koha_marc_update_biblioitem_cn_sort {
2841 my $biblioitem = shift;
2842 my $frameworkcode= shift;
2844 my ($biblioitem_tag, $biblioitem_subfield ) = GetMarcFromKohaField("biblioitems.cn_sort",$frameworkcode);
2845 return unless $biblioitem_tag;
2847 my ($cn_sort) = GetClassSort($biblioitem->{'biblioitems.cn_source'}, $biblioitem->{'cn_class'}, $biblioitem->{'cn_item'} );
2849 if (my $field = $marc->field($biblioitem_tag)) {
2850 $field->delete_subfield(code => $biblioitem_subfield);
2851 if ($cn_sort ne '') {
2852 $field->add_subfields($biblioitem_subfield => $cn_sort);
2855 # if we get here, no biblioitem tag is present in the MARC record, so
2856 # we'll create it if $cn_sort is not empty -- this would be
2857 # an odd combination of events, however
2859 $marc->insert_grouped_field(MARC::Field->new($biblioitem_tag, ' ', ' ', $biblioitem_subfield => $cn_sort));
2864 =head2 _koha_add_biblio
2868 my ($biblionumber,$error) = _koha_add_biblio($dbh,$biblioitem);
2870 Internal function to add a biblio ($biblio is a hash with the values)
2876 sub _koha_add_biblio {
2877 my ( $dbh, $biblio, $frameworkcode ) = @_;
2881 # set the series flag
2883 if ( $biblio->{'seriestitle'} ) { $serial = 1 };
2887 SET frameworkcode = ?,
2898 my $sth = $dbh->prepare($query);
2901 $biblio->{'author'},
2903 $biblio->{'unititle'},
2906 $biblio->{'seriestitle'},
2907 $biblio->{'copyrightdate'},
2908 $biblio->{'abstract'}
2911 my $biblionumber = $dbh->{'mysql_insertid'};
2912 if ( $dbh->errstr ) {
2913 $error.="ERROR in _koha_add_biblio $query".$dbh->errstr;
2918 #warn "LEAVING _koha_add_biblio: ".$biblionumber."\n";
2919 return ($biblionumber,$error);
2922 =head2 _koha_modify_biblio
2926 my ($biblionumber,$error) == _koha_modify_biblio($dbh,$biblio,$frameworkcode);
2928 Internal function for updating the biblio table
2934 sub _koha_modify_biblio {
2935 my ( $dbh, $biblio, $frameworkcode ) = @_;
2940 SET frameworkcode = ?,
2949 WHERE biblionumber = ?
2952 my $sth = $dbh->prepare($query);
2956 $biblio->{'author'},
2958 $biblio->{'unititle'},
2960 $biblio->{'serial'},
2961 $biblio->{'seriestitle'},
2962 $biblio->{'copyrightdate'},
2963 $biblio->{'abstract'},
2964 $biblio->{'biblionumber'}
2965 ) if $biblio->{'biblionumber'};
2967 if ( $dbh->errstr || !$biblio->{'biblionumber'} ) {
2968 $error.="ERROR in _koha_modify_biblio $query".$dbh->errstr;
2971 return ( $biblio->{'biblionumber'},$error );
2974 =head2 _koha_modify_biblioitem_nonmarc
2978 my ($biblioitemnumber,$error) = _koha_modify_biblioitem_nonmarc( $dbh, $biblioitem );
2980 Updates biblioitems row except for marc and marcxml, which should be changed
2987 sub _koha_modify_biblioitem_nonmarc {
2988 my ( $dbh, $biblioitem ) = @_;
2991 # re-calculate the cn_sort, it may have changed
2992 my ($cn_sort) = GetClassSort($biblioitem->{'biblioitems.cn_source'}, $biblioitem->{'cn_class'}, $biblioitem->{'cn_item'} );
2996 SET biblionumber = ?,
3002 publicationyear = ?,
3006 collectiontitle = ?,
3008 collectionvolume= ?,
3009 editionstatement= ?,
3010 editionresponsibility = ?,
3024 where biblioitemnumber = ?
3026 my $sth = $dbh->prepare($query);
3028 $biblioitem->{'biblionumber'},
3029 $biblioitem->{'volume'},
3030 $biblioitem->{'number'},
3031 $biblioitem->{'itemtype'},
3032 $biblioitem->{'isbn'},
3033 $biblioitem->{'issn'},
3034 $biblioitem->{'publicationyear'},
3035 $biblioitem->{'publishercode'},
3036 $biblioitem->{'volumedate'},
3037 $biblioitem->{'volumedesc'},
3038 $biblioitem->{'collectiontitle'},
3039 $biblioitem->{'collectionissn'},
3040 $biblioitem->{'collectionvolume'},
3041 $biblioitem->{'editionstatement'},
3042 $biblioitem->{'editionresponsibility'},
3043 $biblioitem->{'illus'},
3044 $biblioitem->{'pages'},
3045 $biblioitem->{'bnotes'},
3046 $biblioitem->{'size'},
3047 $biblioitem->{'place'},
3048 $biblioitem->{'lccn'},
3049 $biblioitem->{'url'},
3050 $biblioitem->{'biblioitems.cn_source'},
3051 $biblioitem->{'cn_class'},
3052 $biblioitem->{'cn_item'},
3053 $biblioitem->{'cn_suffix'},
3055 $biblioitem->{'totalissues'},
3056 $biblioitem->{'biblioitemnumber'}
3058 if ( $dbh->errstr ) {
3059 $error.="ERROR in _koha_modify_biblioitem_nonmarc $query".$dbh->errstr;
3062 return ($biblioitem->{'biblioitemnumber'},$error);
3065 =head2 _koha_add_biblioitem
3069 my ($biblioitemnumber,$error) = _koha_add_biblioitem( $dbh, $biblioitem );
3071 Internal function to add a biblioitem
3077 sub _koha_add_biblioitem {
3078 my ( $dbh, $biblioitem ) = @_;
3081 my ($cn_sort) = GetClassSort($biblioitem->{'biblioitems.cn_source'}, $biblioitem->{'cn_class'}, $biblioitem->{'cn_item'} );
3083 "INSERT INTO biblioitems SET
3090 publicationyear = ?,
3094 collectiontitle = ?,
3096 collectionvolume= ?,
3097 editionstatement= ?,
3098 editionresponsibility = ?,
3114 my $sth = $dbh->prepare($query);
3116 $biblioitem->{'biblionumber'},
3117 $biblioitem->{'volume'},
3118 $biblioitem->{'number'},
3119 $biblioitem->{'itemtype'},
3120 $biblioitem->{'isbn'},
3121 $biblioitem->{'issn'},
3122 $biblioitem->{'publicationyear'},
3123 $biblioitem->{'publishercode'},
3124 $biblioitem->{'volumedate'},
3125 $biblioitem->{'volumedesc'},
3126 $biblioitem->{'collectiontitle'},
3127 $biblioitem->{'collectionissn'},
3128 $biblioitem->{'collectionvolume'},
3129 $biblioitem->{'editionstatement'},
3130 $biblioitem->{'editionresponsibility'},
3131 $biblioitem->{'illus'},
3132 $biblioitem->{'pages'},
3133 $biblioitem->{'bnotes'},
3134 $biblioitem->{'size'},
3135 $biblioitem->{'place'},
3136 $biblioitem->{'lccn'},
3137 $biblioitem->{'marc'},
3138 $biblioitem->{'url'},
3139 $biblioitem->{'biblioitems.cn_source'},
3140 $biblioitem->{'cn_class'},
3141 $biblioitem->{'cn_item'},
3142 $biblioitem->{'cn_suffix'},
3144 $biblioitem->{'totalissues'}
3146 my $bibitemnum = $dbh->{'mysql_insertid'};
3147 if ( $dbh->errstr ) {
3148 $error.="ERROR in _koha_add_biblioitem $query".$dbh->errstr;
3152 return ($bibitemnum,$error);
3155 =head2 _koha_delete_biblio
3159 $error = _koha_delete_biblio($dbh,$biblionumber);
3161 Internal sub for deleting from biblio table -- also saves to deletedbiblio
3163 C<$dbh> - the database handle
3164 C<$biblionumber> - the biblionumber of the biblio to be deleted
3170 # FIXME: add error handling
3172 sub _koha_delete_biblio {
3173 my ( $dbh, $biblionumber ) = @_;
3175 # get all the data for this biblio
3176 my $sth = $dbh->prepare("SELECT * FROM biblio WHERE biblionumber=?");
3177 $sth->execute($biblionumber);
3179 if ( my $data = $sth->fetchrow_hashref ) {
3181 # save the record in deletedbiblio
3182 # find the fields to save
3183 my $query = "INSERT INTO deletedbiblio SET ";
3185 foreach my $temp ( keys %$data ) {
3186 $query .= "$temp = ?,";
3187 push( @bind, $data->{$temp} );
3190 # replace the last , by ",?)"
3192 my $bkup_sth = $dbh->prepare($query);
3193 $bkup_sth->execute(@bind);
3197 my $del_sth = $dbh->prepare("DELETE FROM biblio WHERE biblionumber=?");
3198 $del_sth->execute($biblionumber);
3205 =head2 _koha_delete_biblioitems
3209 $error = _koha_delete_biblioitems($dbh,$biblioitemnumber);
3211 Internal sub for deleting from biblioitems table -- also saves to deletedbiblioitems
3213 C<$dbh> - the database handle
3214 C<$biblionumber> - the biblioitemnumber of the biblioitem to be deleted
3220 # FIXME: add error handling
3222 sub _koha_delete_biblioitems {
3223 my ( $dbh, $biblioitemnumber ) = @_;
3225 # get all the data for this biblioitem
3227 $dbh->prepare("SELECT * FROM biblioitems WHERE biblioitemnumber=?");
3228 $sth->execute($biblioitemnumber);
3230 if ( my $data = $sth->fetchrow_hashref ) {
3232 # save the record in deletedbiblioitems
3233 # find the fields to save
3234 my $query = "INSERT INTO deletedbiblioitems SET ";
3236 foreach my $temp ( keys %$data ) {
3237 $query .= "$temp = ?,";
3238 push( @bind, $data->{$temp} );
3241 # replace the last , by ",?)"
3243 my $bkup_sth = $dbh->prepare($query);
3244 $bkup_sth->execute(@bind);
3247 # delete the biblioitem
3249 $dbh->prepare("DELETE FROM biblioitems WHERE biblioitemnumber=?");
3250 $del_sth->execute($biblioitemnumber);
3257 =head1 UNEXPORTED FUNCTIONS
3259 =head2 ModBiblioMarc
3261 &ModBiblioMarc($newrec,$biblionumber,$frameworkcode);
3263 Add MARC data for a biblio to koha
3265 Function exported, but should NOT be used, unless you really know what you're doing
3271 # pass the MARC::Record to this function, and it will create the records in the marc field
3272 my ( $record, $biblionumber, $frameworkcode ) = @_;
3273 my $dbh = C4::Context->dbh;
3274 my @fields = $record->fields();
3275 if ( !$frameworkcode ) {
3276 $frameworkcode = "";
3279 $dbh->prepare("UPDATE biblio SET frameworkcode=? WHERE biblionumber=?");
3280 $sth->execute( $frameworkcode, $biblionumber );
3282 my $encoding = C4::Context->preference("marcflavour");
3284 # deal with UNIMARC field 100 (encoding) : create it if needed & set encoding to unicode
3285 if ( $encoding eq "UNIMARC" ) {
3286 my $string = $record->subfield( 100, "a" );
3287 if ( ($string) && ( length($record->subfield( 100, "a" )) == 35 ) ) {
3288 my $f100 = $record->field(100);
3289 $record->delete_field($f100);
3292 $string = POSIX::strftime( "%Y%m%d", localtime );
3294 $string = sprintf( "%-*s", 35, $string );
3296 substr( $string, 22, 6, "frey50" );
3297 unless ( $record->subfield( 100, "a" ) ) {
3298 $record->insert_grouped_field(
3299 MARC::Field->new( 100, "", "", "a" => $string ) );
3303 if (C4::Context->preference("NoZebra")) {
3304 # only NoZebra indexing needs to have
3305 # the previous version of the record
3306 $oldRecord = GetMarcBiblio($biblionumber);
3310 "UPDATE biblioitems SET marc=?,marcxml=? WHERE biblionumber=?");
3311 $sth->execute( $record->as_usmarc(), $record->as_xml_record($encoding),
3314 ModZebra($biblionumber,"specialUpdate","biblioserver",$oldRecord,$record);
3315 return $biblionumber;
3318 =head2 z3950_extended_services
3320 z3950_extended_services($serviceType,$serviceOptions,$record);
3322 z3950_extended_services is used to handle all interactions with Zebra's extended serices package, which is employed to perform all management of the MARC data stored in Zebra.
3324 C<$serviceType> one of: itemorder,create,drop,commit,update,xmlupdate
3326 C<$serviceOptions> a has of key/value pairs. For instance, if service_type is 'update', $service_options should contain:
3328 action => update action, one of specialUpdate, recordInsert, recordReplace, recordDelete, elementUpdate.
3332 recordidOpaque => Opaque Record ID (user supplied) or recordidNumber => Record ID number (system number).
3333 syntax => the record syntax (transfer syntax)
3334 databaseName = Database from connection object
3336 To set serviceOptions, call set_service_options($serviceType)
3338 C<$record> the record, if one is needed for the service type
3340 A record should be in XML. You can convert it to XML from MARC by running it through marc2xml().
3344 sub z3950_extended_services {
3345 my ( $server, $serviceType, $action, $serviceOptions ) = @_;
3347 # get our connection object
3348 my $Zconn = C4::Context->Zconn( $server, 0, 1 );
3350 # create a new package object
3351 my $Zpackage = $Zconn->package();
3354 $Zpackage->option( action => $action );
3356 if ( $serviceOptions->{'databaseName'} ) {
3357 $Zpackage->option( databaseName => $serviceOptions->{'databaseName'} );
3359 if ( $serviceOptions->{'recordIdNumber'} ) {
3361 recordIdNumber => $serviceOptions->{'recordIdNumber'} );
3363 if ( $serviceOptions->{'recordIdOpaque'} ) {
3365 recordIdOpaque => $serviceOptions->{'recordIdOpaque'} );
3368 # this is an ILL request (Zebra doesn't support it, but Koha could eventually)
3369 #if ($serviceType eq 'itemorder') {
3370 # $Zpackage->option('contact-name' => $serviceOptions->{'contact-name'});
3371 # $Zpackage->option('contact-phone' => $serviceOptions->{'contact-phone'});
3372 # $Zpackage->option('contact-email' => $serviceOptions->{'contact-email'});
3373 # $Zpackage->option('itemorder-item' => $serviceOptions->{'itemorder-item'});
3376 if ( $serviceOptions->{record} ) {
3377 $Zpackage->option( record => $serviceOptions->{record} );
3379 # can be xml or marc
3380 if ( $serviceOptions->{'syntax'} ) {
3381 $Zpackage->option( syntax => $serviceOptions->{'syntax'} );
3385 # send the request, handle any exception encountered
3386 eval { $Zpackage->send($serviceType) };
3387 if ( $@ && $@->isa("ZOOM::Exception") ) {
3388 return "error: " . $@->code() . " " . $@->message() . "\n";
3391 # free up package resources
3392 $Zpackage->destroy();
3395 =head2 set_service_options
3397 my $serviceOptions = set_service_options($serviceType);
3399 C<$serviceType> itemorder,create,drop,commit,update,xmlupdate
3401 Currently, we only support 'create', 'commit', and 'update'. 'drop' support will be added as soon as Zebra supports it.
3405 sub set_service_options {
3406 my ($serviceType) = @_;
3409 # FIXME: This needs to be an OID ... if we ever need 'syntax' this sub will need to change
3410 # $serviceOptions->{ 'syntax' } = ''; #zebra doesn't support syntaxes other than xml
3412 if ( $serviceType eq 'commit' ) {
3416 if ( $serviceType eq 'create' ) {
3420 if ( $serviceType eq 'drop' ) {
3421 die "ERROR: 'drop' not currently supported (by Zebra)";
3423 return $serviceOptions;
3426 =head3 get_biblio_authorised_values
3428 find the types and values for all authorised values assigned to this biblio.
3432 MARC::Record of the bib
3434 returns: a hashref mapping the authorised value to the value set for this biblionumber
3436 $authorised_values = {
3437 'Scent' => 'flowery',
3438 'Audience' => 'Young Adult',
3439 'itemtypes' => 'SER',
3442 Notes: forlibrarian should probably be passed in, and called something different.
3447 sub get_biblio_authorised_values {
3448 my $biblionumber = shift;
3451 my $forlibrarian = 1; # are we in staff or opac?
3452 my $frameworkcode = GetFrameworkCode( $biblionumber );
3454 my $authorised_values;
3456 my $tagslib = GetMarcStructure( $forlibrarian, $frameworkcode )
3457 or return $authorised_values;
3459 # assume that these entries in the authorised_value table are bibliolevel.
3460 # ones that start with 'item%' are item level.
3461 my $query = q(SELECT distinct authorised_value, kohafield
3462 FROM marc_subfield_structure
3463 WHERE authorised_value !=''
3464 AND (kohafield like 'biblio%'
3465 OR kohafield like '') );
3466 my $bibliolevel_authorised_values = C4::Context->dbh->selectall_hashref( $query, 'authorised_value' );
3468 foreach my $tag ( keys( %$tagslib ) ) {
3469 foreach my $subfield ( keys( %{$tagslib->{ $tag }} ) ) {
3470 # warn "checking $subfield. type is: " . ref $tagslib->{ $tag }{ $subfield };
3471 if ( 'HASH' eq ref $tagslib->{ $tag }{ $subfield } ) {
3472 if ( defined $tagslib->{ $tag }{ $subfield }{'authorised_value'} && exists $bibliolevel_authorised_values->{ $tagslib->{ $tag }{ $subfield }{'authorised_value'} } ) {
3473 if ( defined $record->field( $tag ) ) {
3474 my $this_subfield_value = $record->field( $tag )->subfield( $subfield );
3475 if ( defined $this_subfield_value ) {
3476 $authorised_values->{ $tagslib->{ $tag }{ $subfield }{'authorised_value'} } = $this_subfield_value;
3483 # warn ( Data::Dumper->Dump( [ $authorised_values ], [ 'authorised_values' ] ) );
3484 return $authorised_values;
3494 Koha Developement team <info@koha.org>
3496 Paul POULAIN paul.poulain@free.fr
3498 Joshua Ferraro jmf@liblime.com