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