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

(-)a/misc/cronjobs/delete_items.pl (-56 / +35 lines)
Lines 18-24 Link Here
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Getopt::Long qw( GetOptions );
20
use Getopt::Long qw( GetOptions );
21
use Pod::Usage qw( pod2usage );
21
use Pod::Usage   qw( pod2usage );
22
22
23
use Koha::Script -cron;
23
use Koha::Script -cron;
24
use C4::Biblio qw( DelBiblio );
24
use C4::Biblio qw( DelBiblio );
Lines 27-86 use Koha::Items; Link Here
27
27
28
my $dbh = C4::Context->dbh();
28
my $dbh = C4::Context->dbh();
29
29
30
my $query = {
30
my @where          = ();
31
    target_items => q|SELECT itemnumber, biblionumber from items|
31
my $verbose        = 0;
32
};
32
my $help           = 0;
33
33
my $manual         = 0;
34
my $GLOBAL = {
34
my $commit         = 0;
35
      query => $query
35
my $delete_biblios = 0;
36
    , sth   => {}
37
};
38
39
my $OPTIONS = {
40
      where => []
41
    , flags    => {
42
            verbose   => ''
43
          , commit    => ''
44
          , help      => ''
45
          , manual    => ''
46
          , del_bibs  => ''
47
      }
48
};
49
36
50
GetOptions(
37
GetOptions(
51
      'where=s'    => $OPTIONS->{where}
38
    'where=s'  => \@where, 'v|verbose' => \$verbose,
52
    , 'v|verbose'  => sub { $OPTIONS->{flags}->{verbose}   = 1 }
39
    'h|help'   => \$help,
53
    , 'h|help'     => sub { $OPTIONS->{flags}->{help}      = 1 }
40
    'm|manual' => \$manual,
54
    , 'm|manual'   => sub { $OPTIONS->{flags}->{manual}    = 1 }
41
    'c|commit' => \$commit,
55
    , 'c|commit'   => sub { $OPTIONS->{flags}->{commit}    = 1 } # aka DO-EET!
42
    'del_bibs' => \$delete_biblios
56
    , 'del_bibs'   => sub { $OPTIONS->{flags}->{del_bibs}  = 1 }
57
);
43
);
58
44
59
my @where = @{ $OPTIONS->{where} };
45
pod2usage( -verbose => 2 )                                                            if $manual;
46
pod2usage( -verbose => 1 )                                                            if $help;
47
pod2usage( -verbose => 1, -message => 'You must supply at least one --where option' ) if scalar @where == 0;
60
48
61
pod2usage( -verbose => 2 ) if  $OPTIONS->{flags}->{manual};
49
my $where_clause = ' where ' . join( " and ", @where );
62
pod2usage( -verbose => 1 ) if  $OPTIONS->{flags}->{help};
63
pod2usage( -verbose => 1 -msg => 'You must supply at least one --where option' ) if scalar @where == 0;
64
50
65
sub verbose {
51
$verbose && say "Where statement: $where_clause";
66
    say @_ if $OPTIONS->{flags}->{verbose};
52
if ($delete_biblios) {
53
    $verbose && say "Deleting bibliographic records when all items are deleted!";
67
}
54
}
68
55
69
my $where_clause = ' where ' . join ( " and ", @where );
56
print "Test run only! No data will be deleted.\n" unless $commit;
70
57
my $deleted_string = $commit ? "Deleted" : "Would have deleted";
71
verbose "Where statement: $where_clause";
72
if( $OPTIONS->{flags}->{del_bibs} ) {
73
    verbose "Deleting empty bib records!"
74
}
75
76
print "Test run only! No data will be deleted.\n" unless $OPTIONS->{flags}->{commit};
77
my $deleted_string = $OPTIONS->{flags}->{commit} ? "Deleted" : "Would have deleted";
78
58
79
# FIXME Use Koha::Items instead
59
# FIXME Use Koha::Items instead
80
$GLOBAL->{sth}->{target_items} = $dbh->prepare( $query->{target_items} . $where_clause  );
60
my $query = "SELECT itemnumber, biblionumber from items ";
81
$GLOBAL->{sth}->{target_items}->execute();
61
my $sth   = $dbh->prepare( $query . $where_clause );
62
$sth->execute();
82
63
83
DELITEM: while ( my $item = $GLOBAL->{sth}->{target_items}->fetchrow_hashref() ) {
64
DELITEM: while ( my $item = $sth->fetchrow_hashref() ) {
84
65
85
    my $item_object    = Koha::Items->find( $item->{itemnumber} );
66
    my $item_object    = Koha::Items->find( $item->{itemnumber} );
86
    my $holdings_count = $item_object->biblio->items->count;
67
    my $holdings_count = $item_object->biblio->items->count;
Lines 89-111 DELITEM: while ( my $item = $GLOBAL->{sth}->{target_items}->fetchrow_hashref() ) Link Here
89
    my $safe_to_delete = $item_object->safe_to_delete;
70
    my $safe_to_delete = $item_object->safe_to_delete;
90
    if ($safe_to_delete) {
71
    if ($safe_to_delete) {
91
        $item_object->safe_delete
72
        $item_object->safe_delete
92
            if $OPTIONS->{flags}->{commit};
73
            if $commit;
93
        verbose "$deleted_string item '$item->{itemnumber}' (of $holdings_count)";
74
        $verbose && say "$deleted_string item '$item->{itemnumber}' (of $holdings_count)";
94
75
95
        if ( $OPTIONS->{flags}->{del_bibs} && $holdings_count == 1 ) {    # aka DO-EET for empty bibs!
76
        if ( $delete_biblios && $holdings_count == 1 ) {    # aka DO-EET for empty bibs!
96
            $error = &DelBiblio( $item->{biblionumber} ) if $OPTIONS->{flags}->{commit};
77
            $error = &DelBiblio( $item->{biblionumber} ) if $commit;
97
            if ($error) {
78
            if ($error) {
98
                verbose "Could not delete bib '$item->{biblionumber}': $error";
79
                $verbose && say "Could not delete bib '$item->{biblionumber}': $error";
99
            } else {
80
            } else {
100
                verbose "No items remaining. $deleted_string bibliographic record '$item->{biblionumber}'";
81
                $verbose && say "No items remaining. $deleted_string bibliographic record '$item->{biblionumber}'";
101
            }
82
            }
102
        }
83
        }
103
    } else {
84
    } else {
104
        verbose sprintf "Item '%s' (Barcode: '%s', Title: '%s') cannot deleted: %s",
85
        say sprintf(
86
            "Item '%s' (Barcode: '%s', Title: '%s') cannot deleted: %s",
105
            $item_object->itemnumber,
87
            $item_object->itemnumber,
106
            $item_object->barcode,
88
            $item_object->barcode,
107
            $item_object->biblio->title,
89
            $item_object->biblio->title,
108
            @{ $safe_to_delete->messages }[0]->message;
90
            @{ $safe_to_delete->messages }[0]->message
91
        ) if $verbose;
109
    }
92
    }
110
}
93
}
111
94
Lines 156-162 Deletes the bibliographic record if the last item is deleted. Link Here
156
139
157
=cut
140
=cut
158
141
159
160
=head1 EXAMPLES
142
=head1 EXAMPLES
161
143
162
  The following is an example of this script:
144
  The following is an example of this script:
Lines 167-180 Deletes the bibliographic record if the last item is deleted. Link Here
167
149
168
=cut
150
=cut
169
151
170
171
=head1 DESCRIPTION
152
=head1 DESCRIPTION
172
153
173
 This is a lightweight batch deletion tool for items, suitable for running in a cron job.
154
 This is a lightweight batch deletion tool for items, suitable for running in a cron job.
174
155
175
=cut
156
=cut
176
157
177
178
=head1 AUTHOR
158
=head1 AUTHOR
179
159
180
 Barton Chittenden <barton@bywatersolutions.com>
160
 Barton Chittenden <barton@bywatersolutions.com>
181
- 

Return to bug 35654