Bug 14504: Add delete_items.pl: a command line batch deletion tool
[koha.git] / misc / cronjobs / delete_items.pl
1 #! /usr/bin/perl
2
3 use warnings;
4 use strict;
5 use Getopt::Long;
6 use C4::Context;
7 use C4::Items;
8 use C4::Circulation;
9 use Modern::Perl;
10 use Pod::Usage;
11
12 my $VERSION='1.0';
13
14 my $dbh = C4::Context->dbh();
15
16 my $query = {
17     target_items => q|SELECT itemnumber from items|
18 };
19
20 my $GLOBAL = {
21       query => $query
22     , sth   => {}
23 };
24
25 my $OPTIONS = {
26       criteria => []
27     , flags    => {
28             verbose   => ''
29           , commit    => ''
30           , help      => ''
31           , manual    => ''
32           , version   => ''
33       }
34 };
35
36 GetOptions(
37       'criteria=s' => $OPTIONS->{criteria}
38     , 'v|verbose'  => sub { $OPTIONS->{flags}->{verbose}   = 1 }
39     , 'V|version'  => sub { $OPTIONS->{flags}->{version}   = 1 }
40     , 'h|help'     => sub { $OPTIONS->{flags}->{help}      = 1 }
41     , 'm|manual'   => sub { $OPTIONS->{flags}->{manual}    = 1 }
42     , 'c|commit'   => sub { $OPTIONS->{flags}->{commit}    = 1 } # aka DO-EET!
43     , 'dry-run'    => sub { $OPTIONS->{flags}->{commit}    = 0;
44                             $OPTIONS->{flags}->{verbose}   = 1 }
45 );
46
47 my @criteria = @{ $OPTIONS->{criteria} };
48
49 if( $OPTIONS->{flags}->{version} ) {
50     say "$0 version $VERSION";
51     exit;
52 }
53
54 pod2usage( -verbose => 2 ) if  $OPTIONS->{flags}->{manual};
55 pod2usage(1) if ( $OPTIONS->{flags}->{help} || scalar @criteria == 0 );
56
57
58
59
60 sub verbose {
61     say @_ if $OPTIONS->{flags}->{verbose};
62 }
63
64
65 my $where_clause = ' where ' . join ( " and ", @criteria );
66
67 verbose "Where statement: $where_clause";
68
69 $GLOBAL->{sth}->{target_tiems} = $dbh->prepare( $query->{target_items} . $where_clause  );
70 $GLOBAL->{sth}->{target_tiems}->execute();
71
72 DELITEM: while ( my $item = $GLOBAL->{sth}->{target_tiems}->fetchrow_hashref() ) {
73     my $issue = GetOpenIssue( $item->{itemnumber} );
74     if( defined $issue ) {
75         verbose "Cannot delete '$item->{itemnumber}' -- item is checked out."
76     } else {
77         verbose "Deleting '$item->{itemnumber}' ";
78         C4::Items::DelItem( { itemnumber => $item->{itemnumber} } ) if $OPTIONS->{flags}->{commit} ;
79     }
80 }
81
82 =head1 NAME
83
84  delete_items.pl - A batch item deletion tool, which generates a query against
85                    the items database and deletes the items matching the
86                    criteria specified in the command line arguments.
87
88 =head1 SYNOPSIS
89
90 delete_items.pl [--help|--manual|--version]
91
92 delete_items.pl [--verbose] [--dry-run] --criteria "I<SQL CONDITIONAL EXPRESSION>" ... [--commit]
93
94 =cut
95
96 =head1 OPTIONS
97
98 =over 8
99
100 =item B<--help>
101
102 Show the brief help information.
103
104 =item B<--manual>
105
106 Read the manual, with examples.
107
108 =item B<--version>
109
110 Show the version number and exit.
111
112 =item B<--verbose>
113
114 Send the "WHERE" clause generated by the collected C<--criteria>
115 arguments, as well as items affected to Standard Out.
116
117 =item B<--criteria>
118
119 The C<--criteria> option may called multiple times. The following argument
120 must be a syntactically valid SQL statement which is part of the C<WHERE>
121 clause querying the items table. These are joined by C<AND>.
122
123 =item B<--commit>
124
125 No items will be deleted unless the C<--commit> flag is present.
126
127 =item B<--dry-run>
128
129 Disables C<--commit> flag and enables C<--verbose> flag.
130
131 =back
132
133 =cut
134
135
136 =head1 EXAMPLES
137
138   The following is an example of this script:
139
140  delete_items.pl --criteria "items.withdrawn ! 0"  --criteria "items.withdrawn_on < $(date --date="13 month ago" --rfc-3339=date)" --commit
141
142  delete_items.pl --criteria "itemlost >= '1'" --criteria "itemlost <='4'" --criteria "itemlost_on < '2014-04-28'" --commit
143
144 =cut
145
146
147 =head1 DESCRIPTION
148
149  This is a lightweight batch deletion tool for items, suitable for running in a cron job.
150
151 =cut
152
153
154 =head1 AUTHOR
155
156  Barton Chittenden <barton@bywatersolutions.com>
157
158 =cut