View | Details | Raw Unified | Return to bug 14504
Collapse All | Expand All

(-)a/misc/cronjobs/delete_items.pl (-39 / +22 lines)
Lines 1-7 Link Here
1
#! /usr/bin/perl
1
#! /usr/bin/perl
2
2
3
use warnings;
4
use strict;
5
use Getopt::Long;
3
use Getopt::Long;
6
use C4::Context;
4
use C4::Context;
7
use C4::Items;
5
use C4::Items;
Lines 9-20 use C4::Circulation; Link Here
9
use Modern::Perl;
7
use Modern::Perl;
10
use Pod::Usage;
8
use Pod::Usage;
11
9
12
my $VERSION='1.0';
13
14
my $dbh = C4::Context->dbh();
10
my $dbh = C4::Context->dbh();
15
11
16
my $query = {
12
my $query = {
17
    target_items => q|SELECT itemnumber from items|
13
    target_items => q|SELECT itemnumber, biblionumber from items|
18
};
14
};
19
15
20
my $GLOBAL = {
16
my $GLOBAL = {
Lines 40-95 GetOptions( Link Here
40
    , 'h|help'     => sub { $OPTIONS->{flags}->{help}      = 1 }
36
    , 'h|help'     => sub { $OPTIONS->{flags}->{help}      = 1 }
41
    , 'm|manual'   => sub { $OPTIONS->{flags}->{manual}    = 1 }
37
    , 'm|manual'   => sub { $OPTIONS->{flags}->{manual}    = 1 }
42
    , 'c|commit'   => sub { $OPTIONS->{flags}->{commit}    = 1 } # aka DO-EET!
38
    , 'c|commit'   => sub { $OPTIONS->{flags}->{commit}    = 1 } # aka DO-EET!
43
    , 'dry-run'    => sub { $OPTIONS->{flags}->{commit}    = 0;
44
                            $OPTIONS->{flags}->{verbose}   = 1 }
45
);
39
);
46
40
47
my @criteria = @{ $OPTIONS->{criteria} };
41
my @criteria = @{ $OPTIONS->{criteria} };
48
42
49
if( $OPTIONS->{flags}->{version} ) {
50
    say "$0 version $VERSION";
51
    exit;
52
}
53
54
pod2usage( -verbose => 2 ) if  $OPTIONS->{flags}->{manual};
43
pod2usage( -verbose => 2 ) if  $OPTIONS->{flags}->{manual};
55
pod2usage(1) if ( $OPTIONS->{flags}->{help} || scalar @criteria == 0 );
44
pod2usage( -verbose => 1 ) if  $OPTIONS->{flags}->{help};
56
45
pod2usage( -verbose => 1 -msg => 'You must supply at least one --criteria option' ) if  scalar @criteria == 0;
57
58
59
46
60
sub verbose {
47
sub verbose {
61
    say @_ if $OPTIONS->{flags}->{verbose};
48
    say @_ if $OPTIONS->{flags}->{verbose};
62
}
49
}
63
50
64
65
my $where_clause = ' where ' . join ( " and ", @criteria );
51
my $where_clause = ' where ' . join ( " and ", @criteria );
66
52
67
verbose "Where statement: $where_clause";
53
verbose "Where statement: $where_clause";
68
54
69
$GLOBAL->{sth}->{target_tiems} = $dbh->prepare( $query->{target_items} . $where_clause  );
55
$GLOBAL->{sth}->{target_items} = $dbh->prepare( $query->{target_items} . $where_clause  );
70
$GLOBAL->{sth}->{target_tiems}->execute();
56
$GLOBAL->{sth}->{target_items}->execute();
71
57
72
DELITEM: while ( my $item = $GLOBAL->{sth}->{target_tiems}->fetchrow_hashref() ) {
58
DELITEM: while ( my $item = $GLOBAL->{sth}->{target_items}->fetchrow_hashref() ) {
73
    my $issue = GetOpenIssue( $item->{itemnumber} );
59
    my $issue = GetOpenIssue( $item->{itemnumber} );
60
    my $holds = GetItemHolds( $item->{biblionumber}, $item->{itemnumber} );
61
74
    if( defined $issue ) {
62
    if( defined $issue ) {
75
        verbose "Cannot delete '$item->{itemnumber}' -- item is checked out."
63
        verbose "Cannot delete '$item->{itemnumber}' -- item is checked out.";
76
    } else {
64
        next DELITEM;
77
        verbose "Deleting '$item->{itemnumber}' ";
65
    } 
78
        C4::Items::DelItem( { itemnumber => $item->{itemnumber} } ) if $OPTIONS->{flags}->{commit} ;
66
79
    }
67
    if( defined $holds ) {
68
        verbose "Cannot delete '$item->{itemnumber}' -- item has open holds.";
69
        next DELITEM;
70
    } 
71
72
    verbose "Deleting '$item->{itemnumber}' ";
73
    C4::Items::DelItem( { itemnumber => $item->{itemnumber} } ) if $OPTIONS->{flags}->{commit} ;
80
}
74
}
81
75
82
=head1 NAME
76
=head1 NAME
83
77
84
 delete_items.pl - A batch item deletion tool, which generates a query against
78
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.
85
                   the items database and deletes the items matching the
86
                   criteria specified in the command line arguments.
87
79
88
=head1 SYNOPSIS
80
=head1 SYNOPSIS
89
81
90
delete_items.pl [--help|--manual|--version]
82
delete_items.pl [--help|--manual]
91
83
92
delete_items.pl [--verbose] [--dry-run] --criteria "I<SQL CONDITIONAL EXPRESSION>" ... [--commit]
84
delete_items.pl [--verbose] --criteria "I<SQL CONDITIONAL EXPRESSION>" ... [--commit]
93
85
94
=cut
86
=cut
95
87
Lines 105-114 Show the brief help information. Link Here
105
97
106
Read the manual, with examples.
98
Read the manual, with examples.
107
99
108
=item B<--version>
109
110
Show the version number and exit.
111
112
=item B<--verbose>
100
=item B<--verbose>
113
101
114
Send the "WHERE" clause generated by the collected C<--criteria>
102
Send the "WHERE" clause generated by the collected C<--criteria>
Lines 124-133 clause querying the items table. These are joined by C<AND>. Link Here
124
112
125
No items will be deleted unless the C<--commit> flag is present.
113
No items will be deleted unless the C<--commit> flag is present.
126
114
127
=item B<--dry-run>
128
129
Disables C<--commit> flag and enables C<--verbose> flag.
130
131
=back
115
=back
132
116
133
=cut
117
=cut
134
- 

Return to bug 14504