5 use Koha
::Script
-cron
;
12 my $dbh = C4
::Context
->dbh();
15 target_items
=> q
|SELECT itemnumber
, biblionumber from items
|
35 'where=s' => $OPTIONS->{where
}
36 , 'v|verbose' => sub { $OPTIONS->{flags
}->{verbose
} = 1 }
37 , 'V|version' => sub { $OPTIONS->{flags
}->{version
} = 1 }
38 , 'h|help' => sub { $OPTIONS->{flags
}->{help
} = 1 }
39 , 'm|manual' => sub { $OPTIONS->{flags
}->{manual
} = 1 }
40 , 'c|commit' => sub { $OPTIONS->{flags
}->{commit
} = 1 } # aka DO-EET!
43 my @where = @
{ $OPTIONS->{where
} };
45 pod2usage
( -verbose
=> 2 ) if $OPTIONS->{flags
}->{manual
};
46 pod2usage
( -verbose
=> 1 ) if $OPTIONS->{flags
}->{help
};
47 pod2usage
( -verbose
=> 1 -msg
=> 'You must supply at least one --where option' ) if scalar @where == 0;
50 say @_ if $OPTIONS->{flags
}->{verbose
};
53 my $where_clause = ' where ' . join ( " and ", @where );
55 verbose
"Where statement: $where_clause";
57 $GLOBAL->{sth
}->{target_items
} = $dbh->prepare( $query->{target_items
} . $where_clause );
58 $GLOBAL->{sth
}->{target_items
}->execute();
60 DELITEM
: while ( my $item = $GLOBAL->{sth
}->{target_items
}->fetchrow_hashref() ) {
62 my $status = C4
::Items
::ItemSafeToDelete
( $item->{biblionumber
}, $item->{itemnumber
} );
63 if( $status eq '1' ) {
64 C4
::Items
::DelItemCheck
( $item->{biblionumber
}, $item->{itemnumber
} )
65 if $OPTIONS->{flags
}->{commit
};
66 verbose
"Deleting '$item->{itemnumber}'";
68 verbose
"Item '$item->{itemnumber}' not deletd: $status";
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.
78 delete_items.pl [--help|--manual]
80 delete_items.pl [--verbose] --where "I<SQL CONDITIONAL EXPRESSION>" ... [--commit]
90 Show the brief help information.
94 Read the manual, with examples.
98 Send the "WHERE" clause generated by the collected C<--where>
99 arguments, as well as items affected to Standard Out.
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>.
109 No items will be deleted unless the C<--commit> flag is present.
118 The following is an example of this script:
120 delete_items.pl --where "items.withdrawn ! 0" --where "items.withdrawn_on < $(date --date="13 month ago" --rfc-3339=date)" --commit
122 delete_items.pl --where "itemlost >= '1'" --where "itemlost <='4'" --where "itemlost_on < '2014-04-28'" --commit
129 This is a lightweight batch deletion tool for items, suitable for running in a cron job.
136 Barton Chittenden <barton@bywatersolutions.com>