Bug 14504: (QA followup) use TestBuilder, remove do_not_commit
[koha.git] / misc / cronjobs / delete_items.pl
1 #! /usr/bin/perl
2
3 use Getopt::Long;
4 use C4::Context;
5 use C4::Items;
6 use C4::Circulation;
7 use Modern::Perl;
8 use Pod::Usage;
9
10 my $dbh = C4::Context->dbh();
11
12 my $query = {
13     target_items => q|SELECT itemnumber, biblionumber from items|
14 };
15
16 my $GLOBAL = {
17       query => $query
18     , sth   => {}
19 };
20
21 my $OPTIONS = {
22       criteria => []
23     , flags    => {
24             verbose   => ''
25           , commit    => ''
26           , help      => ''
27           , manual    => ''
28           , version   => ''
29       }
30 };
31
32 GetOptions(
33       'criteria=s' => $OPTIONS->{criteria}
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!
39 );
40
41 my @criteria = @{ $OPTIONS->{criteria} };
42
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 --criteria option' ) if  scalar @criteria == 0;
46
47 sub verbose {
48     say @_ if $OPTIONS->{flags}->{verbose};
49 }
50
51 my $where_clause = ' where ' . join ( " and ", @criteria );
52
53 verbose "Where statement: $where_clause";
54
55 $GLOBAL->{sth}->{target_items} = $dbh->prepare( $query->{target_items} . $where_clause  );
56 $GLOBAL->{sth}->{target_items}->execute();
57
58 DELITEM: while ( my $item = $GLOBAL->{sth}->{target_items}->fetchrow_hashref() ) {
59
60     my $status = C4::Items::ItemSafeToDelete( $dbh, $item->{itemnumber}, $item->{biblionumber} );
61     if( $status == 1 )  {
62         C4::Items::DelItemCheck( $dbh, $item->{itemnumber}, $item->{biblionumber} );
63         verbose "Deleting '$item->{itemnumber}'";
64     } else {
65         verbose "Item '$item->{itemnumber}' not deletd: $status";
66     }
67 }
68
69 =head1 NAME
70
71 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.
72
73 =head1 SYNOPSIS
74
75 delete_items.pl [--help|--manual]
76
77 delete_items.pl [--verbose] --criteria "I<SQL CONDITIONAL EXPRESSION>" ... [--commit]
78
79 =cut
80
81 =head1 OPTIONS
82
83 =over 8
84
85 =item B<--help>
86
87 Show the brief help information.
88
89 =item B<--manual>
90
91 Read the manual, with examples.
92
93 =item B<--verbose>
94
95 Send the "WHERE" clause generated by the collected C<--criteria>
96 arguments, as well as items affected to Standard Out.
97
98 =item B<--criteria>
99
100 The C<--criteria> option may called multiple times. The following argument
101 must be a syntactically valid SQL statement which is part of the C<WHERE>
102 clause querying the items table. These are joined by C<AND>.
103
104 =item B<--commit>
105
106 No items will be deleted unless the C<--commit> flag is present.
107
108 =back
109
110 =cut
111
112
113 =head1 EXAMPLES
114
115   The following is an example of this script:
116
117  delete_items.pl --criteria "items.withdrawn ! 0"  --criteria "items.withdrawn_on < $(date --date="13 month ago" --rfc-3339=date)" --commit
118
119  delete_items.pl --criteria "itemlost >= '1'" --criteria "itemlost <='4'" --criteria "itemlost_on < '2014-04-28'" --commit
120
121 =cut
122
123
124 =head1 DESCRIPTION
125
126  This is a lightweight batch deletion tool for items, suitable for running in a cron job.
127
128 =cut
129
130
131 =head1 AUTHOR
132
133  Barton Chittenden <barton@bywatersolutions.com>
134
135 =cut