Bug 26080: Use the task queue for batch delete biblios
[koha.git] / tools / batch_delete_records.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Copyright 2013 BibLibre
6 #
7 # Koha is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as
9 # published by the Free Software Foundation; either version 3
10 # of the License, or (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General
18 # Public License along with Koha; if not, see
19 # <http://www.gnu.org/licenses>
20
21 use Modern::Perl;
22
23 use CGI;
24 use List::MoreUtils qw( uniq );
25 use Try::Tiny;
26
27 use C4::Auth qw( get_template_and_user );
28 use C4::Output qw( output_html_with_http_headers );
29 use C4::Auth qw( get_template_and_user );
30 use C4::Biblio qw( GetMarcBiblio );
31 use C4::AuthoritiesMarc;
32 use Koha::Virtualshelves;
33
34 use Koha::Authorities;
35 use Koha::Biblios;
36 use Koha::Items;
37 use Koha::BackgroundJob::BatchDeleteBiblio;
38
39 my $input = CGI->new;
40 my $op = $input->param('op') // q|form|;
41 my $recordtype = $input->param('recordtype') // 'biblio';
42
43 my ($template, $loggedinuser, $cookie) = get_template_and_user({
44         template_name => 'tools/batch_delete_records.tt',
45         query => $input,
46         type => "intranet",
47         flagsrequired => { tools => 'records_batchdel' },
48 });
49
50 my @records;
51 my @messages;
52 if ( $op eq 'form' ) {
53     # Display the form
54     $template->param(
55         op => 'form',
56         lists => scalar Koha::Virtualshelves->search(
57             [
58                 { category => 1, owner => $loggedinuser },
59                 { category => 2 }
60             ]
61         )
62     );
63 } elsif ( $op eq 'list' ) {
64     # List all records to process
65     my @record_ids;
66     if ( my $bib_list = $input->param('bib_list') ) {
67         # Come from the basket
68         @record_ids = split /\//, $bib_list;
69         $recordtype = 'biblio';
70     } elsif ( my $uploadfile = $input->param('uploadfile') ) {
71         # A file of id is given
72         binmode $uploadfile, ':encoding(UTF-8)';
73         while ( my $content = <$uploadfile> ) {
74             next unless $content;
75             $content =~ s/[\r\n]*$//;
76             push @record_ids, $content if $content;
77         }
78     } elsif ( my $shelf_number = $input->param('shelf_number') ) {
79         my $shelf = Koha::Virtualshelves->find($shelf_number);
80         my $contents = $shelf->get_contents;
81         while ( my $content = $contents->next ) {
82             my $biblionumber = $content->biblionumber;
83             push @record_ids, $biblionumber;
84         }
85     } else {
86         # The user enters manually the list of id
87         push @record_ids, split( /\s\n/, $input->param('recordnumber_list') );
88     }
89
90     for my $record_id ( uniq @record_ids ) {
91         if ( $recordtype eq 'biblio' ) {
92             # Retrieve biblio information
93             my $biblio = Koha::Biblios->find( $record_id );
94             unless ( $biblio ) {
95                 push @messages, {
96                     type => 'warning',
97                     code => 'biblio_not_exists',
98                     biblionumber => $record_id,
99                 };
100                 next;
101             }
102             my $holds_count = $biblio->holds->count;
103             $biblio = $biblio->unblessed;
104             my $record = &GetMarcBiblio({ biblionumber => $record_id });
105             $biblio->{itemnumbers} = [Koha::Items->search({ biblionumber => $record_id })->get_column('itemnumber')];
106             $biblio->{holds_count} = $holds_count;
107             $biblio->{issues_count} = C4::Biblio::CountItemsIssued( $record_id );
108             push @records, $biblio;
109         } else {
110             # Retrieve authority information
111             my $authority = C4::AuthoritiesMarc::GetAuthority( $record_id );
112             unless ( $authority ) {
113                 push @messages, {
114                     type => 'warning',
115                     code => 'authority_not_exists',
116                     authid => $record_id,
117                 };
118                 next;
119             }
120
121             $authority = {
122                 authid => $record_id,
123                 summary => C4::AuthoritiesMarc::BuildSummary( $authority, $record_id ),
124                 count_usage => Koha::Authorities->get_usage_count({ authid => $record_id }),
125             };
126             push @records, $authority;
127         }
128     }
129     $template->param(
130         records => \@records,
131         op => 'list',
132     );
133 } elsif ( $op eq 'delete' ) {
134     # We want to delete selected records!
135     my @record_ids = $input->multi_param('record_id');
136
137     try {
138         my $params = {
139             record_ids  => \@record_ids,
140         };
141
142         my $job_id =
143           $recordtype eq 'biblio'
144           ? Koha::BackgroundJob::BatchDeleteBiblio->new->enqueue($params)
145           : Koha::BackgroundJob::BatchDeleteAuthority->new->enqueue($params);
146
147         $template->param(
148             op => 'enqueued',
149             job_id => $job_id,
150         );
151     } catch {
152         push @messages, {
153             type => 'error',
154             code => 'cannot_enqueue_job',
155             error => $_,
156         };
157         $template->param( view => 'errors' );
158     };
159 }
160
161 $template->param(
162     messages => \@messages,
163     recordtype => $recordtype,
164 );
165
166 output_html_with_http_headers $input, $cookie, $template->output;