14 my $dbh = C4::Context->dbh();
17 target_items => q|SELECT itemnumber from items|
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 }
47 my @criteria = @{ $OPTIONS->{criteria} };
49 if( $OPTIONS->{flags}->{version} ) {
50 say "$0 version $VERSION";
54 pod2usage( -verbose => 2 ) if $OPTIONS->{flags}->{manual};
55 pod2usage(1) if ( $OPTIONS->{flags}->{help} || scalar @criteria == 0 );
61 say @_ if $OPTIONS->{flags}->{verbose};
65 my $where_clause = ' where ' . join ( " and ", @criteria );
67 verbose "Where statement: $where_clause";
69 $GLOBAL->{sth}->{target_tiems} = $dbh->prepare( $query->{target_items} . $where_clause );
70 $GLOBAL->{sth}->{target_tiems}->execute();
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."
77 verbose "Deleting '$item->{itemnumber}' ";
78 C4::Items::DelItem( { itemnumber => $item->{itemnumber} } ) if $OPTIONS->{flags}->{commit} ;
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.
90 delete_items.pl [--help|--manual|--version]
92 delete_items.pl [--verbose] [--dry-run] --criteria "I<SQL CONDITIONAL EXPRESSION>" ... [--commit]
102 Show the brief help information.
106 Read the manual, with examples.
110 Show the version number and exit.
114 Send the "WHERE" clause generated by the collected C<--criteria>
115 arguments, as well as items affected to Standard Out.
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>.
125 No items will be deleted unless the C<--commit> flag is present.
129 Disables C<--commit> flag and enables C<--verbose> flag.
138 The following is an example of this script:
140 delete_items.pl --criteria "items.withdrawn ! 0" --criteria "items.withdrawn_on < $(date --date="13 month ago" --rfc-3339=date)" --commit
142 delete_items.pl --criteria "itemlost >= '1'" --criteria "itemlost <='4'" --criteria "itemlost_on < '2014-04-28'" --commit
149 This is a lightweight batch deletion tool for items, suitable for running in a cron job.
156 Barton Chittenden <barton@bywatersolutions.com>