Bug 16699: Move Swagger-related files to api/v1/swagger
[koha.git] / misc / cronjobs / delete_items.pl
blob78a9b35b8d85329404880a83ca0aee5d7c49f1b3
1 #! /usr/bin/perl
3 use Getopt::Long;
4 use C4::Context;
5 use C4::Items;
6 use C4::Circulation;
7 use Modern::Perl;
8 use Pod::Usage;
10 my $dbh = C4::Context->dbh();
12 my $query = {
13 target_items => q|SELECT itemnumber, biblionumber from items|
16 my $GLOBAL = {
17 query => $query
18 , sth => {}
21 my $OPTIONS = {
22 where => []
23 , flags => {
24 verbose => ''
25 , commit => ''
26 , help => ''
27 , manual => ''
28 , version => ''
32 GetOptions(
33 'where=s' => $OPTIONS->{where}
34 , 'v|verbose' => sub { $OPTIONS->{flags}->{verbose} = 1 }
35 , 'V|version' => sub { $OPTIONS->{flags}->{version} = 1 }
36 , 'h|help' => sub { $OPTIONS->{flags}->{help} = 1 }
37 , 'm|manual' => sub { $OPTIONS->{flags}->{manual} = 1 }
38 , 'c|commit' => sub { $OPTIONS->{flags}->{commit} = 1 } # aka DO-EET!
41 my @where = @{ $OPTIONS->{where} };
43 pod2usage( -verbose => 2 ) if $OPTIONS->{flags}->{manual};
44 pod2usage( -verbose => 1 ) if $OPTIONS->{flags}->{help};
45 pod2usage( -verbose => 1 -msg => 'You must supply at least one --where option' ) if scalar @where == 0;
47 sub verbose {
48 say @_ if $OPTIONS->{flags}->{verbose};
51 my $where_clause = ' where ' . join ( " and ", @where );
53 verbose "Where statement: $where_clause";
55 $GLOBAL->{sth}->{target_items} = $dbh->prepare( $query->{target_items} . $where_clause );
56 $GLOBAL->{sth}->{target_items}->execute();
58 DELITEM: while ( my $item = $GLOBAL->{sth}->{target_items}->fetchrow_hashref() ) {
60 my $status = C4::Items::ItemSafeToDelete( $item->{biblionumber}, $item->{itemnumber} );
61 if( $status eq '1' ) {
62 C4::Items::DelItemCheck( $item->{biblionumber}, $item->{itemnumber} )
63 if $OPTIONS->{flags}->{commit};
64 verbose "Deleting '$item->{itemnumber}'";
65 } else {
66 verbose "Item '$item->{itemnumber}' not deletd: $status";
70 =head1 NAME
72 delete_items.pl - A batch item deletion tool, which generates a query against the items database and deletes the items matching the criteria specified in the command line arguments.
74 =head1 SYNOPSIS
76 delete_items.pl [--help|--manual]
78 delete_items.pl [--verbose] --where "I<SQL CONDITIONAL EXPRESSION>" ... [--commit]
80 =cut
82 =head1 OPTIONS
84 =over 8
86 =item B<--help>
88 Show the brief help information.
90 =item B<--manual>
92 Read the manual, with examples.
94 =item B<--verbose>
96 Send the "WHERE" clause generated by the collected C<--where>
97 arguments, as well as items affected to Standard Out.
99 =item B<--where>
101 The C<--where> option may called multiple times. The following argument
102 must be a syntactically valid SQL statement which is part of the C<WHERE>
103 clause querying the items table. These are joined by C<AND>.
105 =item B<--commit>
107 No items will be deleted unless the C<--commit> flag is present.
109 =back
111 =cut
114 =head1 EXAMPLES
116 The following is an example of this script:
118 delete_items.pl --where "items.withdrawn ! 0" --where "items.withdrawn_on < $(date --date="13 month ago" --rfc-3339=date)" --commit
120 delete_items.pl --where "itemlost >= '1'" --where "itemlost <='4'" --where "itemlost_on < '2014-04-28'" --commit
122 =cut
125 =head1 DESCRIPTION
127 This is a lightweight batch deletion tool for items, suitable for running in a cron job.
129 =cut
132 =head1 AUTHOR
134 Barton Chittenden <barton@bywatersolutions.com>
136 =cut