Another purely documentation commit. Just changing formatting to ease
[koha.git] / C4 / Search.pm
1 package C4::Search;
2
3 # Copyright 2000-2006 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
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
10 # version.
11 #
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.
15 #
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
19
20 use strict;
21 use ZOOM;
22 use Smart::Comments;
23 use C4::Context;
24 use MARC::Record;
25 use MARC::File::XML;
26 use C4::Biblio;
27
28 require Exporter;
29
30 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
31
32 # set the version for version checking
33 $VERSION = do { my @v = '$Revision$' =~ /\d+/g;
34     shift(@v) . "." . join( "_", map { sprintf "%03d", $_ } @v );
35 };
36
37 =head1 NAME
38
39 C4::Search - Functions for searching the Koha catalog and other databases
40
41 =head1 SYNOPSIS
42
43   use C4::Search;
44
45 =head1 DESCRIPTION
46
47 This module provides the searching facilities for the Koha catalog and
48 other databases.
49
50 =head1 FUNCTIONS
51
52 =over 2
53
54 =cut
55
56 @ISA    = qw(Exporter);
57 @EXPORT = qw(search get_record);
58
59 # make all your functions, whether exported or not;
60
61 sub search {
62     my ( $search, $type, $number ) = @_;
63     my $dbh = C4::Context->dbh();
64     my $q;
65     my $Zconn;
66     my $raw;
67     eval { $Zconn = new ZOOM::Connection( C4::Context->config("zebradb") ); };
68     if ($@) {
69         warn "Error ", $@->code(), ": ", $@->message(), "\n";
70     }
71
72     if ( $type eq 'CQL' ) {
73         my $string;
74         if ( $search->{'cql'} ) {
75             $string = $search->{'cql'};
76         }
77         else {
78             foreach my $var ( keys %$search ) {
79                 $string .= "$var=\"$search->{$var}\" ";
80             }
81         }
82         $Zconn->option( cqlfile => C4::Context->config("intranetdir")
83               . "/zebra/pqf.properties" );
84         $Zconn->option( preferredRecordSyntax => "xml" );
85         $q = new ZOOM::Query::CQL2RPN( $string, $Zconn );
86     }
87     my $rs;
88     my $n;
89     eval {
90         $rs = $Zconn->search($q);
91         $n  = $rs->size();
92     };
93     if ($@) {
94         print "Error ", $@->code(), ": ", $@->message(), "\n";
95     }
96     my $i = 0;
97     my @results;
98     while ( $i < $n && $i < $number ) {
99         $raw = $rs->record($i)->raw();
100         my $record = MARC::Record->new_from_xml($raw);
101         my $line = MARCmarc2koha( $dbh, $record );
102         push @results, $line;
103 #        push @results,$raw;
104         $i++;
105     }
106     return ( \@results );
107
108 }
109
110 sub get_record {
111
112     # pass in an id (biblionumber at this stage) and get back a MARC record
113     my ($id) = @_;
114     my $q;
115     my $Zconn;
116     my $raw;
117     eval { $Zconn = new ZOOM::Connection( C4::Context->config("zebradb") ); };
118     if ($@) {
119         warn "Error ", $@->code(), ": ", $@->message(), "\n";
120     }
121     $Zconn->option( cqlfile => C4::Context->config("intranetdir")
122           . "/zebra/pqf.properties" );
123     $Zconn->option( preferredRecordSyntax => "xml" );
124     my $string = "identifier=$id";
125     warn $string;
126
127         $q = new ZOOM::Query::CQL2RPN( $string, $Zconn);
128     eval {
129 #        my $rs = $Zconn->search_pqf("\@attr 1=12 $id");
130         my $rs = $Zconn->search($q);
131         my $n  = $rs->size();
132         if ( $n > 0 ) {
133             $raw = $rs->record(0)->raw();
134         }
135     };
136     if ($@) {
137
138         print "Error ", $@->code(), ": ", $@->message(), "\n";
139     }
140     ###$raw
141     my $record = MARC::Record->new_from_xml($raw);
142     ###$record
143     return ($record);
144 }
145
146 1;
147 __END__
148
149 =back
150
151 =head1 AUTHOR
152
153 Koha Developement team <info@koha.org>
154
155 =cut
156