10 my $dbh = C4::Context->dbh();
13 target_items => q|SELECT itemnumber, biblionumber from items|
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!
41 my @criteria = @{ $OPTIONS->{criteria} };
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;
48 say @_ if $OPTIONS->{flags}->{verbose};
51 my $where_clause = ' where ' . join ( " and ", @criteria );
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->{itemnumber}, $item->{biblionumber} );
61 if( $status eq '1' ) {
62 C4::Items::DelItemCheck( $item->{itemnumber}, $item->{biblionumber} );
63 verbose "Deleting '$item->{itemnumber}'";
65 verbose "Item '$item->{itemnumber}' not deletd: $status";
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.
75 delete_items.pl [--help|--manual]
77 delete_items.pl [--verbose] --criteria "I<SQL CONDITIONAL EXPRESSION>" ... [--commit]
87 Show the brief help information.
91 Read the manual, with examples.
95 Send the "WHERE" clause generated by the collected C<--criteria>
96 arguments, as well as items affected to Standard Out.
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>.
106 No items will be deleted unless the C<--commit> flag is present.
115 The following is an example of this script:
117 delete_items.pl --criteria "items.withdrawn ! 0" --criteria "items.withdrawn_on < $(date --date="13 month ago" --rfc-3339=date)" --commit
119 delete_items.pl --criteria "itemlost >= '1'" --criteria "itemlost <='4'" --criteria "itemlost_on < '2014-04-28'" --commit
126 This is a lightweight batch deletion tool for items, suitable for running in a cron job.
133 Barton Chittenden <barton@bywatersolutions.com>