fixes for bug 662, securing prepare
[koha.git] / C4 / Maintainance.pm
1 package C4::Maintainance; #assumes C4/Maintainance
2
3 #package to deal with marking up output
4
5
6 # Copyright 2000-2002 Katipo Communications
7 #
8 # This file is part of Koha.
9 #
10 # Koha is free software; you can redistribute it and/or modify it under the
11 # terms of the GNU General Public License as published by the Free Software
12 # Foundation; either version 2 of the License, or (at your option) any later
13 # version.
14 #
15 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
16 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License along with
20 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
21 # Suite 330, Boston, MA  02111-1307 USA
22
23 use strict;
24 use C4::Context;
25
26 require Exporter;
27
28 use vars qw($VERSION @ISA @EXPORT);
29
30 # set the version for version checking
31 $VERSION = 0.01;
32
33 =head1 NAME
34
35 C4::Maintenance - Koha catalog maintenance functions
36
37 =head1 SYNOPSIS
38
39   use C4::Maintenance;
40
41 =head1 DESCRIPTION
42
43 The functions in this module perform various catalog-maintenance
44 functions, including deleting and undeleting books, fixing
45 miscategorized items, etc.
46
47 =head1 FUNCTIONS
48
49 =over 2
50
51 =cut
52
53 @ISA = qw(Exporter);
54 @EXPORT = qw(&listsubjects &updatesub &shiftgroup &deletedbib &undeletebib
55 &updatetype);
56
57 =item listsubjects
58
59   ($count, $results) = &listsubjects($subject, $n, $offset);
60
61 Finds the subjects that begin with C<$subject> in the bibliosubject
62 table of the Koha database.
63
64 C<&listsubjects> returns a two-element array. C<$results> is a
65 reference-to-array, in which each element is a reference-to-hash
66 giving information about the given subject. C<$count> is the number of
67 elements in C<@{$results}>.
68
69 Probably the only interesting field in C<$results->[$i]> is
70 C<subject>, the subject in question.
71
72 C<&listsubject> returns up to C<$n> items, starting at C<$offset>. If
73 C<$n> is 0, it will return all matching subjects.
74
75 =cut
76 #'
77 # FIXME - This API is bogus. The way it's currently used, it should
78 # just return a list of strings.
79 sub listsubjects {
80   my ($sub,$num,$offset)=@_;
81   my $dbh = C4::Context->dbh;
82   my $query="Select * from bibliosubject where subject like '?%' group by subject";
83   my @bind = ($sub);
84   # FIXME - Make $num and $offset optional.
85   # If $num was given, make sure $offset was, too.
86   if ($num != 0){
87     $query.=" limit ?,?";
88     push(@bind,$offset,$num);
89   }
90   my $sth=$dbh->prepare($query);
91 #  print $query;
92   $sth->execute(@bind);
93   my @results;
94   my $i=0;
95   while (my $data=$sth->fetchrow_hashref){
96     $results[$i]=$data;
97     $i++;
98   }
99   $sth->finish;
100   return($i,\@results);
101 }
102
103 =item updatesub
104
105   &updatesub($newsubject, $oldsubject);
106
107 Renames a subject from C<$oldsubject> to C<$newsubject> in the
108 bibliosubject table of the Koha database.
109
110 =cut
111 #'
112 sub updatesub{
113   my ($sub,$oldsub)=@_;
114   my $dbh = C4::Context->dbh;
115   $sub=$dbh->quote($sub);
116   $oldsub=$dbh->quote($oldsub);
117   my $sth=$dbh->prepare("update bibliosubject set subject=? where subject=?");
118   $sth->execute($sub,$oldsub);
119   $sth->finish;
120 }
121
122 =item shiftgroup
123
124   &shiftgroup($biblionumber, $biblioitemnumber);
125
126 Changes the biblionumber associated with a given biblioitem.
127 C<$biblioitemnumber> is the number of the biblioitem to change.
128 C<$biblionumber> is the biblionumber to associate it with.
129
130 =cut
131 #'
132 sub shiftgroup{
133   my ($bib,$bi)=@_;
134   my $dbh = C4::Context->dbh;
135   my $sth=$dbh->prepare("update biblioitems set biblionumber=? where biblioitemnumber=?");
136   $sth->execute($bib,$bi);
137   $sth->finish;
138   $query="";
139   $sth=$dbh->prepare("update items set biblionumber=? where biblioitemnumber=?");
140   $sth->execute($bib,$bi);
141   $sth->finish;
142 }
143
144 =item deletedbib
145
146   ($count, $results) = &deletedbib($title);
147
148 Looks up deleted books whose title begins with C<$title>.
149
150 C<&deletedbib> returns a two-element list. C<$results> is a
151 reference-to-array; each element is a reference-to-hash whose keys are
152 the fields of the deletedbiblio table in the Koha database. C<$count>
153 is the number of elements in C<$results>.
154
155 =cut
156 #'
157 sub deletedbib{
158   my ($title)=@_;
159   my $dbh = C4::Context->dbh;
160   my $sth=$dbh->prepare("Select * from deletedbiblio where title like '?%' order by title");
161   $sth->execute($title);
162   my @results;
163   my $i=0;
164   while (my $data=$sth->fetchrow_hashref){
165     $results[$i]=$data;
166     $i++;
167   }
168   $sth->finish;
169   return($i,\@results);
170 }
171
172 =item undeletebib
173
174   &undeletebib($biblionumber);
175
176 Undeletes a book. C<&undeletebib> looks up the book with the given
177 biblionumber in the deletedbiblio table of the Koha database, and
178 moves its entry to the biblio table.
179
180 =cut
181 #'
182 sub undeletebib{
183   my ($bib)=@_;
184   my $dbh = C4::Context->dbh;
185   my $sth=$dbh->prepare("select * from deletedbiblio where biblionumber=?");
186   $sth->execute($bib);
187   if (my @data=$sth->fetchrow_array){
188     $sth->finish;
189     # FIXME - Doesn't this keep the same biblionumber? Isn't this
190     # forbidden by the definition of 'biblio'? Or doesn't it matter?
191     my $query="Insert into biblio values (";
192     $query .= ("?," x $#data);
193     $query=~ s/\,$/\)/;
194     #   print $query;
195     $sth=$dbh->prepare($query);
196     $sth->execute(@data);
197     $sth->finish;
198   }
199   $sth=$dbh->prepare("Delete from deletedbiblio where biblionumber=?");
200   $sth->execute($bib);
201   $sth->finish;
202 }
203
204 =item updatetype
205
206   &updatetype($biblioitemnumber, $itemtype);
207
208 Changes the type of the item with the given biblioitemnumber to be
209 C<$itemtype>.
210
211 =cut
212 #'
213 sub updatetype{
214   my ($bi,$type)=@_;
215   my $dbh = C4::Context->dbh;
216   my $sth=$dbh->prepare("Update biblioitems set itemtype=? where biblioitemnumber=?");
217   $sth->execute($type,$bi);
218   $sth->finish;
219 }
220
221 END { }       # module clean-up code here (global destructor)
222
223 1;
224 __END__
225
226 =back
227
228 =head1 AUTHOR
229
230 Koha Developement team <info@koha.org>
231
232 =cut