3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along with
15 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
16 # Suite 330, Boston, MA 02111-1307 USA
21 use C4::Biblio; # GetMarcFromKohaField
22 use C4::Koha; # getFacets
26 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
28 # set the version for version checking
33 C4::Search - Functions for searching the Koha catalog.
37 see opac/opac-search.pl or catalogue/search.pl for example of usage
41 This module provides the searching facilities for the Koha into a zebra catalog.
59 # make all your functions, whether exported or not;
61 =head2 findseealso($dbh,$fields);
63 C<$dbh> is a link to the DB handler.
66 my $dbh =C4::Context->dbh;
68 C<$fields> is a reference to the fields array
70 This function modify the @$fields array and add related fields to search on.
75 my ( $dbh, $fields ) = @_;
76 my $tagslib = GetMarcStructure( 1 );
77 for ( my $i = 0 ; $i <= $#{$fields} ; $i++ ) {
78 my ($tag) = substr( @$fields[$i], 1, 3 );
79 my ($subfield) = substr( @$fields[$i], 4, 1 );
80 @$fields[$i] .= ',' . $tagslib->{$tag}->{$subfield}->{seealso}
81 if ( $tagslib->{$tag}->{$subfield}->{seealso} );
87 ($biblionumber,$biblionumber,$title) = FindDuplicate($record);
93 my $dbh = C4::Context->dbh;
94 my $result = TransformMarcToKoha( $dbh, $record, '' );
99 my ( $biblionumber, $title );
101 # search duplicate on ISBN, easy and fast..
102 # ... normalize first
103 if ( $result->{isbn} ) {
104 $result->{isbn} =~ s/\(.*$//;
105 $result->{isbn} =~ s/\s+$//;
107 #$search->{'avoidquerylog'}=1;
108 if ( $result->{isbn} ) {
109 $query = "isbn=$result->{isbn}";
112 $result->{title} =~ s /\\//g;
113 $result->{title} =~ s /\"//g;
114 $result->{title} =~ s /\(//g;
115 $result->{title} =~ s /\)//g;
116 $query = "ti,ext=$result->{title}";
117 $query .= " and mt=$result->{itemtype}" if ($result->{itemtype});
118 if ($result->{author}){
119 $result->{author} =~ s /\\//g;
120 $result->{author} =~ s /\"//g;
121 $result->{author} =~ s /\(//g;
122 $result->{author} =~ s /\)//g;
123 $query .= " and au,ext=$result->{author}";
126 my ($error,$searchresults) =
127 SimpleSearch($query); # FIXME :: hardcoded !
129 foreach my $possible_duplicate_record (@$searchresults) {
131 MARC::Record->new_from_usmarc($possible_duplicate_record);
132 my $result = TransformMarcToKoha( $dbh, $marcrecord, '' );
134 # FIXME :: why 2 $biblionumber ?
136 push @results, $result->{'biblionumber'};
137 push @results, $result->{'title'};
145 ($error,$results) = SimpleSearch($query,@servers);
147 this function performs a simple search on the catalog using zoom.
153 * $query could be a simple keyword or a complete CCL query wich is depending on your ccl file.
154 * @servers is optionnal. default one is read on koha.xml
157 * $error is a string which containt the description error if there is one. Else it's empty.
158 * \@results is an array of marc record.
160 =item C<usage in the script:>
164 my ($error, $marcresults) = SimpleSearch($query);
166 if (defined $error) {
167 $template->param(query_error => $error);
168 warn "error: ".$error;
169 output_html_with_http_headers $input, $cookie, $template->output;
173 my $hits = scalar @$marcresults;
176 for(my $i=0;$i<$hits;$i++) {
178 my $marcrecord = MARC::File::USMARC::decode($marcresults->[$i]);
179 my $biblio = TransformMarcToKoha(C4::Context->dbh,$marcrecord,'');
181 #build the hash for the template.
182 $resultsloop{highlight} = ($i % 2)?(1):(0);
183 $resultsloop{title} = $biblio->{'title'};
184 $resultsloop{subtitle} = $biblio->{'subtitle'};
185 $resultsloop{biblionumber} = $biblio->{'biblionumber'};
186 $resultsloop{author} = $biblio->{'author'};
187 $resultsloop{publishercode} = $biblio->{'publishercode'};
188 $resultsloop{publicationyear} = $biblio->{'publicationyear'};
190 push @results, \%resultsloop;
192 $template->param(result=>\@results);
198 if (C4::Context->preference('NoZebra')) {
199 my $result = NZorder(NZanalyse($query))->{'biblioserver'}->{'RECORDS'};
200 return (undef,$result);
206 return ( "No query entered", undef ) unless $query;
208 #@servers = (C4::Context->config("biblioserver")) unless @servers;
210 ("biblioserver") unless @servers
211 ; # FIXME hardcoded value. See catalog/search.pl & opac-search.pl too.
214 for ( my $i = 0 ; $i < @servers ; $i++ ) {
215 $zconns[$i] = C4::Context->Zconn( $servers[$i], 1 );
218 ->search( new ZOOM::Query::CCL2RPN( $query, $zconns[$i] ) );
220 # getting error message if one occured.
222 $zconns[$i]->errmsg() . " ("
223 . $zconns[$i]->errcode() . ") "
224 . $zconns[$i]->addinfo() . " "
225 . $zconns[$i]->diagset();
227 return ( $error, undef ) if $zconns[$i]->errcode();
231 while ( ( my $i = ZOOM::event( \@zconns ) ) != 0 ) {
232 $ev = $zconns[ $i - 1 ]->last_event();
233 if ( $ev == ZOOM::Event::ZEND ) {
234 $hits = $tmpresults[ $i - 1 ]->size();
237 for ( my $j = 0 ; $j < $hits ; $j++ ) {
238 my $record = $tmpresults[ $i - 1 ]->record($j)->raw();
239 push @results, $record;
243 return ( undef, \@results );
247 # performs the search
250 $koha_query, $federated_query, $sort_by_ref,
251 $servers_ref, $results_per_page, $offset,
252 $expanded_facet, $branches, $query_type,
255 # warn "Query : $koha_query";
256 my @servers = @$servers_ref;
257 my @sort_by = @$sort_by_ref;
259 # create the zoom connection and query object
263 my $results_hashref = ();
266 my $facets_counter = ();
267 my $facets_info = ();
268 my $facets = getFacets();
270 #### INITIALIZE SOME VARS USED CREATE THE FACETED RESULTS
271 my @facets_loop; # stores the ref to array of hashes for template
272 for ( my $i = 0 ; $i < @servers ; $i++ ) {
273 $zconns[$i] = C4::Context->Zconn( $servers[$i], 1 );
275 # perform the search, create the results objects
276 # if this is a local search, use the $koha-query, if it's a federated one, use the federated-query
278 if ( $servers[$i] =~ /biblioserver/ ) {
279 $query_to_use = $koha_query;
282 $query_to_use = $federated_query;
285 # check if we've got a query_type defined
289 if ( $query_type =~ /^ccl/ ) {
291 s/\:/\=/g; # change : to = last minute (FIXME)
293 # warn "CCL : $query_to_use";
296 new ZOOM::Query::CCL2RPN( $query_to_use, $zconns[$i] )
299 elsif ( $query_type =~ /^cql/ ) {
301 # warn "CQL : $query_to_use";
304 new ZOOM::Query::CQL( $query_to_use, $zconns[$i] ) );
306 elsif ( $query_type =~ /^pqf/ ) {
308 # warn "PQF : $query_to_use";
311 new ZOOM::Query::PQF( $query_to_use, $zconns[$i] ) );
317 # warn "preparing to scan";
320 new ZOOM::Query::CCL2RPN( $query_to_use, $zconns[$i] )
325 # warn "LAST : $query_to_use";
328 new ZOOM::Query::CCL2RPN( $query_to_use, $zconns[$i] )
334 warn "prob with query toto $query_to_use " . $@;
337 # concatenate the sort_by limits and pass them to the results object
339 foreach my $sort (@sort_by) {
340 $sort_by .= $sort . " "; # used to be $sort,
342 $results[$i]->sort( "yaz", $sort_by ) if $sort_by;
344 while ( ( my $i = ZOOM::event( \@zconns ) ) != 0 ) {
345 my $ev = $zconns[ $i - 1 ]->last_event();
346 if ( $ev == ZOOM::Event::ZEND ) {
347 my $size = $results[ $i - 1 ]->size();
350 #$results_hash->{'server'} = $servers[$i-1];
351 # loop through the results
352 $results_hash->{'hits'} = $size;
354 if ( $offset + $results_per_page <= $size ) {
355 $times = $offset + $results_per_page;
360 for ( my $j = $offset ; $j < $times ; $j++ )
361 { #(($offset+$count<=$size) ? ($offset+$count):$size) ; $j++){
365 ## This is just an index scan
367 my ( $term, $occ ) = $results[ $i - 1 ]->term($j);
369 # here we create a minimal MARC record and hand it off to the
370 # template just like a normal result ... perhaps not ideal, but
372 my $tmprecord = MARC::Record->new();
373 $tmprecord->encoding('UTF-8');
376 # srote the minimal record in author/title (depending on MARC flavour)
377 if ( C4::Context->preference("marcflavour") eq
380 $tmptitle = MARC::Field->new(
387 $tmptitle = MARC::Field->new(
393 $tmprecord->append_fields($tmptitle);
394 $results_hash->{'RECORDS'}[$j] =
395 $tmprecord->as_usmarc();
398 $record = $results[ $i - 1 ]->record($j)->raw();
400 #warn "RECORD $j:".$record;
401 $results_hash->{'RECORDS'}[$j] =
402 $record; # making a reference to a hash
403 # Fill the facets while we're looping
404 $facet_record = MARC::Record->new_from_usmarc($record);
406 #warn $servers[$i-1].$facet_record->title();
407 for ( my $k = 0 ; $k <= @$facets ; $k++ ) {
408 if ( $facets->[$k] ) {
410 for my $tag ( @{ $facets->[$k]->{'tags'} } ) {
411 push @fields, $facet_record->field($tag);
413 for my $field (@fields) {
414 my @subfields = $field->subfields();
415 for my $subfield (@subfields) {
416 my ( $code, $data ) = @$subfield;
418 $facets->[$k]->{'subfield'} )
420 $facets_counter->{ $facets->[$k]
421 ->{'link_value'} }->{$data}++;
425 $facets_info->{ $facets->[$k]->{'link_value'} }
427 $facets->[$k]->{'label_value'};
428 $facets_info->{ $facets->[$k]->{'link_value'} }
429 ->{'expanded'} = $facets->[$k]->{'expanded'};
434 $results_hashref->{ $servers[ $i - 1 ] } = $results_hash;
437 #print "connection ", $i-1, ": $size hits";
438 #print $results[$i-1]->record(0)->render() if $size > 0;
441 sort { $facets_counter->{$b} <=> $facets_counter->{$a} }
442 keys %$facets_counter
446 my $number_of_facets;
447 my @this_facets_array;
450 $facets_counter->{$link_value}
451 ->{$b} <=> $facets_counter->{$link_value}->{$a}
452 } keys %{ $facets_counter->{$link_value} }
456 if ( ( $number_of_facets < 6 )
457 || ( $expanded_facet eq $link_value )
458 || ( $facets_info->{$link_value}->{'expanded'} ) )
461 # sanitize the link value ), ( will cause errors with CCL
462 my $facet_link_value = $one_facet;
463 $facet_link_value =~ s/(\(|\))/ /g;
465 # fix the length that will display in the label
466 my $facet_label_value = $one_facet;
467 $facet_label_value = substr( $one_facet, 0, 20 ) . "..."
468 unless length($facet_label_value) <= 20;
470 # well, if it's a branch, label by the name, not the code
471 if ( $link_value =~ /branch/ ) {
473 $branches->{$one_facet}->{'branchname'};
476 # but we're down with the whole label being in the link's title
477 my $facet_title_value = $one_facet;
479 push @this_facets_array,
483 $facets_counter->{$link_value}->{$one_facet},
484 facet_label_value => $facet_label_value,
485 facet_title_value => $facet_title_value,
486 facet_link_value => $facet_link_value,
487 type_link_value => $link_value,
492 unless ( $facets_info->{$link_value}->{'expanded'} ) {
494 if ( ( $number_of_facets > 6 )
495 && ( $expanded_facet ne $link_value ) );
500 type_link_value => $link_value,
501 type_id => $link_value . "_id",
503 $facets_info->{$link_value}->{'label_value'},
504 facets => \@this_facets_array,
505 expandable => $expandable,
506 expand => $link_value,
512 return ( undef, $results_hashref, \@facets_loop );
515 # build the query itself
517 my ( $query, $operators, $operands, $indexes, $limits, $sort_by ) = @_;
519 my @operators = @$operators if $operators;
520 my @indexes = @$indexes if $indexes;
521 my @operands = @$operands if $operands;
522 my @limits = @$limits if $limits;
523 my @sort_by = @$sort_by if $sort_by;
525 my $human_search_desc; # a human-readable query
526 my $machine_search_desc; #a machine-readable query
527 # FIXME: the locale should be set based on the syspref
528 my $stemmer = Lingua::Stem->new( -locale => 'EN-US' );
530 # FIXME: these should be stored in the db so the librarian can modify the behavior
531 $stemmer->add_exceptions(
540 # STEP I: determine if this is a form-based / simple query or if it's complex (if complex,
541 # we can't handle field weighting, stemming until a formal query parser is written
542 # I'll work on this soon -- JF
543 #if (!$query) { # form-based
544 # check if this is a known query language query, if it is, return immediately:
545 if ( $query =~ /^ccl=/ ) {
546 return ( undef, $', $', $', 'ccl' );
548 if ( $query =~ /^cql=/ ) {
549 return ( undef, $', $', $', 'cql' );
551 if ( $query =~ /^pqf=/ ) {
552 return ( undef, $', $', $', 'pqf' );
554 if ( $query =~ /(\(|\))/ ) { # sorry, too complex
555 return ( undef, $query, $query, $query, 'ccl' );
558 # form-based queries are limited to non-nested a specific depth, so we can easily
559 # modify the incoming query operands and indexes to do stemming and field weighting
560 # Once we do so, we'll end up with a value in $query, just like if we had an
561 # incoming $query from the user
564 ; # clear it out so we can populate properly with field-weighted stemmed query
566 ; # a flag used to keep track if there was a previous query
567 # if there was, we can apply the current operator
568 for ( my $i = 0 ; $i <= @operands ; $i++ ) {
569 my $operand = $operands[$i];
570 # remove stopwords from operand : parse all stopwords & remove them (case insensitive)
571 # we use IsAlpha unicode definition, to deal correctly with diacritics.
572 # otherwise, a french word like "leçon" is splitted in "le" "çon", le is an empty word, we get "çon"
573 # and don't find anything...
574 foreach (keys %{C4::Context->stopwords}) {
575 $operand=~ s/\P{IsAlpha}$_\P{IsAlpha}/ /i;
576 $operand=~ s/^$_\P{IsAlpha}/ /i;
577 $operand=~ s/\P{IsAlpha}$_$/ /i;
579 my $index = $indexes[$i];
581 my $stemming = C4::Context->parameters("Stemming") || 0;
582 my $weight_fields = C4::Context->parameters("WeightFields") || 0;
584 if ( $operands[$i] ) {
585 $operand =~ s/^(and |or |not )//i;
587 # STEMMING FIXME: need to refine the field weighting so stemmed operands don't disrupt the query ranking
589 my @words = split( / /, $operands[$i] );
590 my $stems = $stemmer->stem(@words);
591 foreach my $stem (@$stems) {
592 $stemmed_operand .= "$stem";
593 $stemmed_operand .= "?"
594 unless ( $stem =~ /(and$|or$|not$)/ )
595 || ( length($stem) < 3 );
596 $stemmed_operand .= " ";
598 #warn "STEM: $stemmed_operand";
601 #$operand = $stemmed_operand;
604 # FIELD WEIGHTING - This is largely experimental stuff. What I'm committing works
605 # pretty well but will work much better when we have an actual query parser
607 if ($weight_fields) {
609 " rk=("; # Specifies that we're applying rank
610 # keyword has different weight properties
611 if ( ( $index =~ /kw/ ) || ( !$index ) )
612 { # FIXME: do I need to add right-truncation in the case of stemming?
613 # a simple way to find out if this query uses an index
614 if ( $operand =~ /(\=|\:)/ ) {
615 $weighted_query .= " $operand";
619 " Title-cover,ext,r1=\"$operand\""
620 ; # index label as exact
622 " or ti,ext,r2=$operand"; # index as exact
623 #$weighted_query .= " or ti,phr,r3=$operand"; # index as phrase
624 #$weighted_query .= " or any,ext,r4=$operand"; # index as exact
626 " or kw,wrdl,r5=$operand"; # index as exact
627 $weighted_query .= " or wrd,fuzzy,r9=$operand";
628 $weighted_query .= " or wrd=$stemmed_operand"
632 elsif ( $index =~ /au/ ) {
634 " $index,ext,r1=$operand"; # index label as exact
635 #$weighted_query .= " or (title-sort-az=0 or $index,startswithnt,st-word,r3=$operand #)";
637 " or $index,phr,r3=$operand"; # index as phrase
638 $weighted_query .= " or $index,rt,wrd,r3=$operand";
640 elsif ( $index =~ /ti/ ) {
642 " Title-cover,ext,r1=$operand"; # index label as exact
643 $weighted_query .= " or Title-series,ext,r2=$operand";
645 #$weighted_query .= " or ti,ext,r2=$operand";
646 #$weighted_query .= " or ti,phr,r3=$operand";
647 #$weighted_query .= " or ti,wrd,r3=$operand";
649 " or (title-sort-az=0 or Title-cover,startswithnt,st-word,r3=$operand #)";
651 " or (title-sort-az=0 or Title-cover,phr,r6=$operand)";
653 #$weighted_query .= " or Title-cover,wrd,r5=$operand";
654 #$weighted_query .= " or ti,ext,r6=$operand";
655 #$weighted_query .= " or ti,startswith,phr,r7=$operand";
656 #$weighted_query .= " or ti,phr,r8=$operand";
657 #$weighted_query .= " or ti,wrd,r9=$operand";
659 #$weighted_query .= " or ti,ext,r2=$operand"; # index as exact
660 #$weighted_query .= " or ti,phr,r3=$operand"; # index as phrase
661 #$weighted_query .= " or any,ext,r4=$operand"; # index as exact
662 #$weighted_query .= " or kw,wrd,r5=$operand"; # index as exact
666 " $index,ext,r1=$operand"; # index label as exact
667 #$weighted_query .= " or $index,ext,r2=$operand"; # index as exact
669 " or $index,phr,r3=$operand"; # index as phrase
670 $weighted_query .= " or $index,rt,wrd,r3=$operand";
672 " or $index,wrd,r5=$operand"
673 ; # index as word right-truncated
674 $weighted_query .= " or $index,wrd,fuzzy,r8=$operand";
676 $weighted_query .= ")"; # close rank specification
677 $operand = $weighted_query;
680 # only add an operator if there is a previous operand
681 if ($previous_operand) {
682 if ( $operators[ $i - 1 ] ) {
683 $query .= " $operators[$i-1] $index: $operand";
685 $human_search_desc .=
686 " $operators[$i-1] $operands[$i]";
689 $human_search_desc .=
690 " $operators[$i-1] $index: $operands[$i]";
694 # the default operator is and
696 $query .= " and $index: $operand";
697 $human_search_desc .= " and $index: $operands[$i]";
702 $query .= " $operand";
703 $human_search_desc .= " $operands[$i]";
706 $query .= " $index: $operand";
707 $human_search_desc .= " $index: $operands[$i]";
709 $previous_operand = 1;
717 my $limit_search_desc;
718 foreach my $limit (@limits) {
720 # FIXME: not quite right yet ... will work on this soon -- JF
721 my $type = $1 if $limit =~ m/([^:]+):([^:]*)/;
722 if ( $limit =~ /available/ ) {
724 " (($query and datedue=0000-00-00) or ($query and datedue=0000-00-00 not lost=1) or ($query and datedue=0000-00-00 not lost=2))";
726 #$limit_search_desc.=" and available";
728 elsif ( ($limit_query) && ( index( $limit_query, $type, 0 ) > 0 ) ) {
729 if ( $limit_query !~ /\(/ ) {
731 substr( $limit_query, 0, index( $limit_query, $type, 0 ) )
733 . substr( $limit_query, index( $limit_query, $type, 0 ) )
737 substr( $limit_search_desc, 0,
738 index( $limit_search_desc, $type, 0 ) )
740 . substr( $limit_search_desc,
741 index( $limit_search_desc, $type, 0 ) )
747 chop $limit_search_desc;
748 $limit_query .= " or $limit )" if $limit;
749 $limit_search_desc .= " or $limit )" if $limit;
752 elsif ( ($limit_query) && ( $limit =~ /mc/ ) ) {
753 $limit_query .= " or $limit" if $limit;
754 $limit_search_desc .= " or $limit" if $limit;
757 # these are treated as AND
758 elsif ($limit_query) {
759 if ($limit =~ /branch/){
760 $limit_query .= " ) and ( $limit" if $limit;
761 $limit_search_desc .= " ) and ( $limit" if $limit;
763 $limit_query .= " or $limit" if $limit;
764 $limit_search_desc .= " or $limit" if $limit;
768 # otherwise, there is nothing but the limit
770 $limit_query .= "$limit" if $limit;
771 $limit_search_desc .= "$limit" if $limit;
775 # if there's also a query, we need to AND the limits to it
776 if ( ($limit_query) && ($query) ) {
777 $limit_query = " and (" . $limit_query . ")";
778 $limit_search_desc = " and ($limit_search_desc)" if $limit_search_desc;
781 $query .= $limit_query;
782 $human_search_desc .= $limit_search_desc;
784 # now normalize the strings
785 $query =~ s/ / /g; # remove extra spaces
786 $query =~ s/^ //g; # remove any beginning spaces
787 $query =~ s/:/=/g; # causes probs for server
788 $query =~ s/==/=/g; # remove double == from query
790 my $federated_query = $human_search_desc;
791 $federated_query =~ s/ / /g;
792 $federated_query =~ s/^ //g;
793 $federated_query =~ s/:/=/g;
794 my $federated_query_opensearch = $federated_query;
796 # my $federated_query_RPN = new ZOOM::Query::CCL2RPN( $query , C4::Context->ZConn('biblioserver'));
798 $human_search_desc =~ s/ / /g;
799 $human_search_desc =~ s/^ //g;
800 my $koha_query = $query;
802 # warn "QUERY:".$koha_query;
803 # warn "SEARCHDESC:".$human_search_desc;
804 # warn "FEDERATED QUERY:".$federated_query;
805 return ( undef, $human_search_desc, $koha_query, $federated_query );
808 # IMO this subroutine is pretty messy still -- it's responsible for
809 # building the HTML output for the template
811 my ( $searchdesc, $hits, $results_per_page, $offset, @marcresults ) = @_;
813 my $dbh = C4::Context->dbh;
817 my $span_terms_hashref;
818 for my $span_term ( split( / /, $searchdesc ) ) {
819 $span_term =~ s/(.*=|\)|\(|\+|\.)//g;
820 $span_terms_hashref->{$span_term}++;
823 #Build brancnames hash
825 #get branch information.....
828 $dbh->prepare("SELECT branchcode,branchname FROM branches")
829 ; # FIXME : use C4::Koha::GetBranches
831 while ( my $bdata = $bsth->fetchrow_hashref ) {
832 $branches{ $bdata->{'branchcode'} } = $bdata->{'branchname'};
836 #find itemtype & itemtype image
839 $dbh->prepare("SELECT itemtype,description,imageurl,summary,notforloan FROM itemtypes");
841 while ( my $bdata = $bsth->fetchrow_hashref ) {
842 $itemtypes{ $bdata->{'itemtype'} }->{description} =
843 $bdata->{'description'};
844 $itemtypes{ $bdata->{'itemtype'} }->{imageurl} = $bdata->{'imageurl'};
845 $itemtypes{ $bdata->{'itemtype'} }->{summary} = $bdata->{'summary'};
846 $itemtypes{ $bdata->{'itemtype'} }->{notforloan} = $bdata->{'notforloan'};
849 #search item field code
852 "select tagfield from marc_subfield_structure where kohafield like 'items.itemnumber'"
855 my ($itemtag) = $sth->fetchrow;
857 ## find column names of items related to MARC
858 my $sth2 = $dbh->prepare("SHOW COLUMNS from items");
860 my %subfieldstosearch;
861 while ( ( my $column ) = $sth2->fetchrow ) {
862 my ( $tagfield, $tagsubfield ) =
863 &GetMarcFromKohaField( "items." . $column, "" );
864 $subfieldstosearch{$column} = $tagsubfield;
868 if ( $hits && $offset + $results_per_page <= $hits ) {
869 $times = $offset + $results_per_page;
875 for ( my $i = $offset ; $i <= $times - 1 ; $i++ ) {
877 $marcrecord = MARC::File::USMARC::decode( $marcresults[$i] );
878 my $oldbiblio = TransformMarcToKoha( $dbh, $marcrecord, '' );
879 # add image url if there is one
880 if ( $itemtypes{ $oldbiblio->{itemtype} }->{imageurl} =~ /^http:/ ) {
881 $oldbiblio->{imageurl} =
882 $itemtypes{ $oldbiblio->{itemtype} }->{imageurl};
883 $oldbiblio->{description} =
884 $itemtypes{ $oldbiblio->{itemtype} }->{description};
887 $oldbiblio->{imageurl} =
888 getitemtypeimagesrc() . "/"
889 . $itemtypes{ $oldbiblio->{itemtype} }->{imageurl}
890 if ( $itemtypes{ $oldbiblio->{itemtype} }->{imageurl} );
891 $oldbiblio->{description} =
892 $itemtypes{ $oldbiblio->{itemtype} }->{description};
895 # build summary if there is one (the summary is defined in itemtypes table
897 if ($itemtypes{ $oldbiblio->{itemtype} }->{summary}) {
898 my $summary = $itemtypes{ $oldbiblio->{itemtype} }->{summary};
899 my @fields = $marcrecord->fields();
900 foreach my $field (@fields) {
901 my $tag = $field->tag();
902 my $tagvalue = $field->as_string();
903 $summary =~ s/\[(.?.?.?.?)$tag\*(.*?)]/$1$tagvalue$2\[$1$tag$2]/g;
905 my @subf = $field->subfields;
906 for my $i (0..$#subf) {
907 my $subfieldcode = $subf[$i][0];
908 my $subfieldvalue = $subf[$i][1];
909 my $tagsubf = $tag.$subfieldcode;
910 $summary =~ s/\[(.?.?.?.?)$tagsubf(.*?)]/$1$subfieldvalue$2\[$1$tagsubf$2]/g;
914 $summary =~ s/\[(.*?)]//g;
915 $summary =~ s/\n/<br>/g;
916 $oldbiblio->{summary} = $summary;
918 # add spans to search term in results
919 foreach my $term ( keys %$span_terms_hashref ) {
922 my $old_term = $term;
923 if ( length($term) > 3 ) {
924 $term =~ s/(.*=|\)|\(|\+|\.|\?)//g;
926 #FIXME: is there a better way to do this?
927 $oldbiblio->{'title'} =~ s/$term/<span class=term>$&<\/span>/gi;
928 $oldbiblio->{'subtitle'} =~
929 s/$term/<span class=term>$&<\/span>/gi;
931 $oldbiblio->{'author'} =~ s/$term/<span class=term>$&<\/span>/gi;
932 $oldbiblio->{'publishercode'} =~ s/$term/<span class=term>$&<\/span>/gi;
933 $oldbiblio->{'place'} =~ s/$term/<span class=term>$&<\/span>/gi;
934 $oldbiblio->{'pages'} =~ s/$term/<span class=term>$&<\/span>/gi;
935 $oldbiblio->{'notes'} =~ s/$term/<span class=term>$&<\/span>/gi;
936 $oldbiblio->{'size'} =~ s/$term/<span class=term>$&<\/span>/gi;
946 $oldbiblio->{'toggle'} = $toggle;
947 my @fields = $marcrecord->field($itemtag);
950 my $ordered_count = 0;
951 my $onloan_count = 0;
952 my $wthdrawn_count = 0;
953 my $itemlost_count = 0;
957 # check the loan status of the item :
958 # it is not stored in the MARC record, for pref (zebra reindexing)
959 # reason. Thus, we have to get the status from a specific SQL query
961 my $sth_issue = $dbh->prepare("
962 SELECT date_due,returndate
964 WHERE itemnumber=? AND returndate IS NULL");
965 my $items_count=scalar(@fields);
966 foreach my $field (@fields) {
968 foreach my $code ( keys %subfieldstosearch ) {
969 $item->{$code} = $field->subfield( $subfieldstosearch{$code} );
971 $sth_issue->execute($item->{itemnumber});
972 $item->{due_date} = format_date($sth_issue->fetchrow);
973 $item->{onloan} = 1 if $item->{due_date};
974 # at least one item can be reserved : suppose no
976 if ( $item->{wthdrawn} ) {
978 $items->{ $item->{'homebranch'}.'--'.$item->{'itemcallnumber'} }->{unavailable}=1;
979 $items->{ $item->{'homebranch'}.'--'.$item->{'itemcallnumber'} }->{wthdrawn}=1;
981 elsif ( $item->{itemlost} ) {
983 $items->{ $item->{'homebranch'}.'--'.$item->{'itemcallnumber'} }->{unavailable}=1;
984 $items->{ $item->{'homebranch'}.'--'.$item->{'itemcallnumber'} }->{itemlost}=1;
986 unless ( $item->{notforloan}) {
987 # OK, this one can be issued, so at least one can be reserved
990 if ( ( $item->{onloan} ) && ( $item->{onloan} != '0000-00-00' ) )
992 $items->{ $item->{'homebranch'}.'--'.$item->{'itemcallnumber'} }->{unavailable}=1;
993 $items->{ $item->{'homebranch'}.'--'.$item->{'itemcallnumber'} }->{onloancount} = 1;
994 $items->{ $item->{'homebranch'}.'--'.$item->{'itemcallnumber'} }->{due_date} = $item->{due_date};
997 if ( $item->{'homebranch'} ) {
998 $items->{ $item->{'homebranch'}.'--'.$item->{'itemcallnumber'} }->{count}++;
1002 elsif ( $item->{'holdingbranch'} ) {
1003 $items->{ $item->{'holdingbranch'} }->{count}++;
1005 $items->{ $item->{'homebranch'}.'--'.$item->{'itemcallnumber'} }->{itemcallnumber} = $item->{itemcallnumber};
1006 $items->{ $item->{'homebranch'}.'--'.$item->{'itemcallnumber'} }->{location} = $item->{location};
1007 $items->{ $item->{'homebranch'}.'--'.$item->{'itemcallnumber'} }->{branchcode} = $item->{homebranch};
1008 } # notforloan, item level and biblioitem level
1010 # last check for norequest : if itemtype is notforloan, it can't be reserved either, whatever the items
1011 $norequests = 1 if $itemtypes{$oldbiblio->{itemtype}}->{notforloan};
1013 for my $key ( sort keys %$items ) {
1015 branchname => $branches{$items->{$key}->{branchcode}},
1016 branchcode => $items->{$key}->{branchcode},
1017 count => $items->{$key}->{count}==1 ?"":$items->{$key}->{count},
1018 itemcallnumber => $items->{$key}->{itemcallnumber},
1019 location => $items->{$key}->{location},
1020 onloancount => $items->{$key}->{onloancount},
1021 due_date => $items->{$key}->{due_date},
1022 wthdrawn => $items->{$key}->{wthdrawn},
1023 lost => $items->{$key}->{itemlost},
1025 push @items_loop, $this_item;
1027 $oldbiblio->{norequests} = $norequests;
1028 $oldbiblio->{items_count} = $items_count;
1029 $oldbiblio->{items_loop} = \@items_loop;
1030 $oldbiblio->{onloancount} = $onloan_count;
1031 $oldbiblio->{wthdrawncount} = $wthdrawn_count;
1032 $oldbiblio->{itemlostcount} = $itemlost_count;
1033 $oldbiblio->{orderedcount} = $ordered_count;
1034 $oldbiblio->{isbn} =~ s/-//g; # deleting - in isbn to enable amazon content
1035 push( @newresults, $oldbiblio );
1042 #----------------------------------------------------------------------
1044 # Non-Zebra GetRecords#
1045 #----------------------------------------------------------------------
1049 NZgetRecords has the same API as zera getRecords, even if some parameters are not managed
1055 $koha_query, $federated_query, $sort_by_ref,
1056 $servers_ref, $results_per_page, $offset,
1057 $expanded_facet, $branches, $query_type,
1060 my $result = NZanalyse($koha_query);
1061 return (undef,NZorder($result,@$sort_by_ref[0],$results_per_page,$offset),undef);
1066 NZanalyse : get a CQL string as parameter, and returns a list of biblionumber;title,biblionumber;title,...
1067 the list is builded from inverted index in nozebra SQL table
1068 note that title is here only for convenience : the sorting will be very fast when requested on title
1069 if the sorting is requested on something else, we will have to reread all results, and that may be longer.
1074 my ($string,$server) = @_;
1075 # $server contains biblioserver or authorities, depending on what we search on.
1076 #warn "querying : $string on $server";
1077 $server='biblioserver' unless $server;
1078 # if we have a ", replace the content to discard temporarily any and/or/not inside
1080 if ($string =~/"/) {
1081 $string =~ s/"(.*?)"/__X__/;
1083 # print "commacontent : $commacontent\n";
1085 # split the query string in 3 parts : X AND Y means : $left="X", $operand="AND" and $right="Y"
1086 # then, call again NZanalyse with $left and $right
1087 # (recursive until we find a leaf (=> something without and/or/not)
1088 $string =~ /(.*)( and | or | not | AND | OR | NOT )(.*)/;
1091 my $operand = lc($2);
1092 # it's not a leaf, we have a and/or/not
1094 # reintroduce comma content if needed
1095 $right =~ s/__X__/"$commacontent"/ if $commacontent;
1096 $left =~ s/__X__/"$commacontent"/ if $commacontent;
1097 # warn "node : $left / $operand / $right\n";
1098 my $leftresult = NZanalyse($left,$server);
1099 my $rightresult = NZanalyse($right,$server);
1100 # OK, we have the results for right and left part of the query
1101 # depending of operand, intersect, union or exclude both lists
1102 # to get a result list
1103 if ($operand eq ' and ') {
1104 my @leftresult = split /;/, $leftresult;
1105 # my @rightresult = split /;/,$leftresult;
1107 # parse the left results, and if the biblionumber exist in the right result, save it in finalresult
1108 # the result is stored twice, to have the same weight for AND than OR.
1109 # example : TWO : 61,61,64,121 (two is twice in the biblio #61) / TOWER : 61,64,130
1110 # result : 61,61,61,61,64,64 for two AND tower : 61 has more weight than 64
1111 foreach (@leftresult) {
1112 if ($rightresult =~ "$_;") {
1113 $finalresult .= "$_;$_;";
1116 return $finalresult;
1117 } elsif ($operand eq ' or ') {
1118 # just merge the 2 strings
1119 return $leftresult.$rightresult;
1120 } elsif ($operand eq ' not ') {
1121 my @leftresult = split /;/, $leftresult;
1122 # my @rightresult = split /;/,$leftresult;
1124 foreach (@leftresult) {
1125 unless ($rightresult =~ "$_;") {
1126 $finalresult .= "$_;";
1129 return $finalresult;
1131 # this error is impossible, because of the regexp that isolate the operand, but just in case...
1132 die "error : operand unknown : $operand for $string";
1134 # it's a leaf, do the real SQL query and return the result
1136 $string =~ s/__X__/"$commacontent"/ if $commacontent;
1137 $string =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\// /g;
1138 # warn "leaf : $string\n";
1139 # parse the string in in operator/operand/value again
1140 $string =~ /(.*)(=|>|>=|<|<=)(.*)/;
1145 # automatic replace for short operators
1146 $left='title' if $left eq 'ti';
1147 $left='author' if $left eq 'au';
1148 $left='publisher' if $left eq 'pb';
1149 $left='subject' if $left eq 'su';
1150 $left='koha-Auth-Number' if $left eq 'an';
1151 $left='keyword' if $left eq 'kw';
1153 #do a specific search
1154 my $dbh = C4::Context->dbh;
1155 $operator='LIKE' if $operator eq '=' and $right=~ /%/;
1156 my $sth = $dbh->prepare("SELECT biblionumbers FROM nozebra WHERE server=? AND indexname=? AND value $operator ?");
1157 # warn "$left / $operator / $right\n";
1158 # split each word, query the DB and build the biblionumbers result
1159 foreach (split / /,$right) {
1162 # warn "EXECUTE : $server, $left, $_";
1163 $sth->execute($server, $left, $_);
1164 while (my $line = $sth->fetchrow) {
1165 $biblionumbers .= $line;
1166 # warn "result : $line";
1168 # do a AND with existing list if there is one, otherwise, use the biblionumbers list as 1st result list
1170 my @leftresult = split /;/, $biblionumbers;
1172 foreach (@leftresult) {
1173 if ($results =~ "$_;") {
1179 $results = $biblionumbers;
1183 #do a complete search (all indexes)
1184 my $dbh = C4::Context->dbh;
1185 my $sth = $dbh->prepare("SELECT biblionumbers FROM nozebra WHERE server=? AND value LIKE ?");
1186 # split each word, query the DB and build the biblionumbers result
1187 foreach (split / /,$string) {
1188 next if C4::Context->stopwords->{uc($_)}; # skip if stopword
1189 #warn "search on all indexes on $_";
1192 $sth->execute($server, $_);
1193 while (my $line = $sth->fetchrow) {
1194 $biblionumbers .= $line;
1196 # do a AND with existing list if there is one, otherwise, use the biblionumbers list as 1st result list
1198 my @leftresult = split /;/, $biblionumbers;
1200 foreach (@leftresult) {
1201 if ($results =~ "$_;") {
1207 $results = $biblionumbers;
1211 # warn "return : $results for LEAF : $string";
1218 $finalresult = NZorder($biblionumbers, $ordering,$results_per_page,$offset);
1226 my ($biblionumbers, $ordering,$results_per_page,$offset) = @_;
1227 # order title asc by default
1228 # $ordering = '1=36 <i' unless $ordering;
1229 $results_per_page=20 unless $results_per_page;
1230 $offset = 0 unless $offset;
1231 my $dbh = C4::Context->dbh;
1233 # order by POPULARITY
1235 if ($ordering =~ /1=9523/) {
1238 # popularity is not in MARC record, it's builded from a specific query
1239 my $sth = $dbh->prepare("select sum(issues) from items where biblionumber=?");
1240 foreach (split /;/,$biblionumbers) {
1241 my ($biblionumber,$title) = split /,/,$_;
1242 $result{$biblionumber}=GetMarcBiblio($biblionumber);
1243 $sth->execute($biblionumber);
1244 my $popularity= $sth->fetchrow ||0;
1245 # hint : the key is popularity.title because we can have
1246 # many results with the same popularity. In this cas, sub-ordering is done by title
1247 # we also have biblionumber to avoid bug for 2 biblios with the same title & popularity
1248 # (un-frequent, I agree, but we won't forget anything that way ;-)
1249 $popularity{sprintf("%10d",$popularity).$title.$biblionumber} = $biblionumber;
1251 # sort the hash and return the same structure as GetRecords (Zebra querying)
1254 if ($ordering eq '1=9523 >i') { # sort popularity DESC
1255 foreach my $key (sort {$b cmp $a} (keys %popularity)) {
1256 $result_hash->{'RECORDS'}[$numbers++] = $result{$popularity{$key}}->as_usmarc();
1258 } else { # sort popularity ASC
1259 foreach my $key (sort (keys %popularity)) {
1260 $result_hash->{'RECORDS'}[$numbers++] = $result{$popularity{$key}}->as_usmarc();
1264 $result_hash->{'hits'} = $numbers;
1265 $finalresult->{'biblioserver'} = $result_hash;
1266 return $finalresult;
1270 } elsif ($ordering eq '1=1003 <i'){
1272 foreach (split /;/,$biblionumbers) {
1273 my ($biblionumber,$title) = split /,/,$_;
1274 my $record=GetMarcBiblio($biblionumber);
1276 if (C4::Context->preference('marcflavour') eq 'UNIMARC') {
1277 $author=$record->subfield('200','f');
1278 $author=$record->subfield('700','a') unless $author;
1280 $author=$record->subfield('100','a');
1282 # hint : the result is sorted by title.biblionumber because we can have X biblios with the same title
1283 # and we don't want to get only 1 result for each of them !!!
1284 $result{$author.$biblionumber}=$record;
1286 # sort the hash and return the same structure as GetRecords (Zebra querying)
1289 if ($ordering eq '1=1003 <i') { # sort by author desc
1290 foreach my $key (sort (keys %result)) {
1291 $result_hash->{'RECORDS'}[$numbers++] = $result{$key}->as_usmarc();
1293 } else { # sort by author ASC
1294 foreach my $key (sort { $a cmp $b } (keys %result)) {
1295 $result_hash->{'RECORDS'}[$numbers++] = $result{$key}->as_usmarc();
1299 $result_hash->{'hits'} = $numbers;
1300 $finalresult->{'biblioserver'} = $result_hash;
1301 return $finalresult;
1303 # ORDER BY callnumber
1305 } elsif ($ordering eq '1=20 <i'){
1307 foreach (split /;/,$biblionumbers) {
1308 my ($biblionumber,$title) = split /,/,$_;
1309 my $record=GetMarcBiblio($biblionumber);
1311 my ($callnumber_tag,$callnumber_subfield)=GetMarcFromKohaField($dbh,'items.itemcallnumber');
1312 ($callnumber_tag,$callnumber_subfield)= GetMarcFromKohaField('biblioitems.callnumber') unless $callnumber_tag;
1313 if (C4::Context->preference('marcflavour') eq 'UNIMARC') {
1314 $callnumber=$record->subfield('200','f');
1316 $callnumber=$record->subfield('100','a');
1318 # hint : the result is sorted by title.biblionumber because we can have X biblios with the same title
1319 # and we don't want to get only 1 result for each of them !!!
1320 $result{$callnumber.$biblionumber}=$record;
1322 # sort the hash and return the same structure as GetRecords (Zebra querying)
1325 if ($ordering eq '1=1003 <i') { # sort by title desc
1326 foreach my $key (sort (keys %result)) {
1327 $result_hash->{'RECORDS'}[$numbers++] = $result{$key}->as_usmarc();
1329 } else { # sort by title ASC
1330 foreach my $key (sort { $a cmp $b } (keys %result)) {
1331 $result_hash->{'RECORDS'}[$numbers++] = $result{$key}->as_usmarc();
1335 $result_hash->{'hits'} = $numbers;
1336 $finalresult->{'biblioserver'} = $result_hash;
1337 return $finalresult;
1338 } elsif ($ordering =~ /1=31/){ #pub year
1340 foreach (split /;/,$biblionumbers) {
1341 my ($biblionumber,$title) = split /,/,$_;
1342 my $record=GetMarcBiblio($biblionumber);
1343 my ($publicationyear_tag,$publicationyear_subfield)=GetMarcFromKohaField($dbh,'biblioitems.publicationyear');
1344 my $publicationyear=$record->subfield($publicationyear_tag,$publicationyear_subfield);
1345 # hint : the result is sorted by title.biblionumber because we can have X biblios with the same title
1346 # and we don't want to get only 1 result for each of them !!!
1347 $result{$publicationyear.$biblionumber}=$record;
1349 # sort the hash and return the same structure as GetRecords (Zebra querying)
1352 if ($ordering eq '1=31 <i') { # sort by pubyear desc
1353 foreach my $key (sort (keys %result)) {
1354 $result_hash->{'RECORDS'}[$numbers++] = $result{$key}->as_usmarc();
1356 } else { # sort by pub year ASC
1357 foreach my $key (sort { $b cmp $a } (keys %result)) {
1358 $result_hash->{'RECORDS'}[$numbers++] = $result{$key}->as_usmarc();
1362 $result_hash->{'hits'} = $numbers;
1363 $finalresult->{'biblioserver'} = $result_hash;
1364 return $finalresult;
1368 } elsif ($ordering =~ /1=4/) {
1369 # the title is in the biblionumbers string, so we just need to build a hash, sort it and return
1371 foreach (split /;/,$biblionumbers) {
1372 my ($biblionumber,$title) = split /,/,$_;
1373 # hint : the result is sorted by title.biblionumber because we can have X biblios with the same title
1374 # and we don't want to get only 1 result for each of them !!!
1375 # hint & speed improvement : we can order without reading the record
1376 # so order, and read records only for the requested page !
1377 $result{$title.$biblionumber}=$biblionumber;
1379 # sort the hash and return the same structure as GetRecords (Zebra querying)
1382 if ($ordering eq '1=4 <i') { # sort by title desc
1383 foreach my $key (sort (keys %result)) {
1384 $result_hash->{'RECORDS'}[$numbers++] = $result{$key};
1386 } else { # sort by title ASC
1387 foreach my $key (sort { $b cmp $a } (keys %result)) {
1388 $result_hash->{'RECORDS'}[$numbers++] = $result{$key};
1391 # limit the $results_per_page to result size if it's more
1392 $results_per_page = $numbers-1 if $numbers < $results_per_page;
1393 # for the requested page, replace biblionumber by the complete record
1394 # speed improvement : avoid reading too much things
1395 for (my $counter=$offset;$counter<=$offset+$results_per_page;$counter++) {
1396 $result_hash->{'RECORDS'}[$counter] = GetMarcBiblio($result_hash->{'RECORDS'}[$counter])->as_usmarc;
1399 $result_hash->{'hits'} = $numbers;
1400 $finalresult->{'biblioserver'} = $result_hash;
1401 return $finalresult;
1406 # we need 2 hashes to order by ranking : the 1st one to count the ranking, the 2nd to order by ranking
1409 foreach (split /;/,$biblionumbers) {
1410 my ($biblionumber,$title) = split /,/,$_;
1411 $title =~ /(.*)-(\d)/;
1414 # note that we + the ranking because ranking is calculated on weight of EACH term requested.
1415 # if we ask for "two towers", and "two" has weight 2 in biblio N, and "towers" has weight 4 in biblio N
1416 # biblio N has ranking = 6
1417 $count_ranking{$biblionumber} += $ranking;
1419 # build the result by "inverting" the count_ranking hash
1420 # hing : as usual, we don't order by ranking only, to avoid having only 1 result for each rank. We build an hash on concat(ranking,biblionumber) instead
1422 foreach (keys %count_ranking) {
1423 $result{sprintf("%10d",$count_ranking{$_}).'-'.$_} = $_;
1425 # sort the hash and return the same structure as GetRecords (Zebra querying)
1428 foreach my $key (sort {$b cmp $a} (keys %result)) {
1429 $result_hash->{'RECORDS'}[$numbers++] = $result{$key};
1431 # limit the $results_per_page to result size if it's more
1432 $results_per_page = $numbers-1 if $numbers < $results_per_page;
1433 # for the requested page, replace biblionumber by the complete record
1434 # speed improvement : avoid reading too much things
1435 for (my $counter=$offset;$counter<=$offset+$results_per_page;$counter++) {
1436 $result_hash->{'RECORDS'}[$counter] = GetMarcBiblio($result_hash->{'RECORDS'}[$counter])->as_usmarc;
1439 $result_hash->{'hits'} = $numbers;
1440 $finalresult->{'biblioserver'} = $result_hash;
1441 return $finalresult;
1446 ($countchanged,$listunchanged) = ModBiblios($listbiblios, $tagsubfield,$initvalue,$targetvalue,$test);
1448 this function changes all the values $initvalue in subfield $tag$subfield in any record in $listbiblios
1449 test parameter if set donot perform change to records in database.
1455 * $listbiblios is an array ref to marcrecords to be changed
1456 * $tagsubfield is the reference of the subfield to change.
1457 * $initvalue is the value to search the record for
1458 * $targetvalue is the value to set the subfield to
1459 * $test is to be set only not to perform changes in database.
1461 =item C<Output arg:>
1462 * $countchanged counts all the changes performed.
1463 * $listunchanged contains the list of all the biblionumbers of records unchanged.
1465 =item C<usage in the script:>
1469 my ($countchanged, $listunchanged) = EditBiblios($results->{RECORD}, $tagsubfield,$initvalue,$targetvalue);;
1470 #If one wants to display unchanged records, you should get biblios foreach @$listunchanged
1471 $template->param(countchanged => $countchanged, loopunchanged=>$listunchanged);
1476 my ($listbiblios,$tagsubfield,$initvalue,$targetvalue,$test)=@_;
1479 my ($tag,$subfield)=($1,$2) if ($tagsubfield=~/^(\d{1,3})([a-z0-9A-Z@])?$/);
1480 if ((length($tag)<3)&& $subfield=~/0-9/){
1481 $tag=$tag.$subfield;
1484 my ($bntag,$bnsubf) = GetMarcFromKohaField('biblio.biblionumber');
1485 my ($itemtag,$itemsubf) = GetMarcFromKohaField('items.itemnumber');
1486 foreach my $usmarc (@$listbiblios){
1488 $record=eval{MARC::Record->new_from_usmarc($usmarc)};
1491 # usmarc is not a valid usmarc May be a biblionumber
1492 if ($tag eq $itemtag){
1493 my $bib=GetBiblioFromItemNumber($usmarc);
1494 $record=GetMarcItem($bib->{'biblionumber'},$usmarc) ;
1495 $biblionumber=$bib->{'biblionumber'};
1497 $record=GetMarcBiblio($usmarc);
1498 $biblionumber=$usmarc;
1502 $biblionumber = $record->subfield($bntag,$bnsubf);
1504 $biblionumber=$record->field($bntag)->data;
1507 #GetBiblionumber is to be written.
1508 #Could be replaced by TransformMarcToKoha (But Would be longer)
1509 if ($record->field($tag)){
1511 foreach my $field ($record->field($tag)){
1513 if ($field->delete_subfield('code' =>$subfield,'match'=>qr($initvalue))){
1516 $field->update($subfield,$targetvalue) if ($targetvalue);
1520 if ($field->delete_field($field)){
1525 $field->data=$targetvalue if ($field->data=~qr($initvalue));
1529 # warn $record->as_formatted;
1531 ModBiblio($record,$biblionumber,GetFrameworkCode($biblionumber)) unless ($test);
1533 push @unmatched, $biblionumber;
1536 push @unmatched, $biblionumber;
1539 return ($countmatched,\@unmatched);
1542 END { } # module clean-up code here (global destructor)
1549 Koha Developement team <info@koha.org>