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

(-)a/Koha/BackgroundJob.pm (+2 lines)
Lines 407-412 sub core_types_to_classes { Link Here
407
        update_elastic_index                => 'Koha::BackgroundJob::UpdateElasticIndex',
407
        update_elastic_index                => 'Koha::BackgroundJob::UpdateElasticIndex',
408
        update_holds_queue_for_biblios      => 'Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue',
408
        update_holds_queue_for_biblios      => 'Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue',
409
        stage_marc_for_import               => 'Koha::BackgroundJob::StageMARCForImport',
409
        stage_marc_for_import               => 'Koha::BackgroundJob::StageMARCForImport',
410
        marc_import_commit_batch            => 'Koha::BackgroundJob::MARCImportCommitBatch',
411
        marc_import_revert_batch            => 'Koha::BackgroundJob::MARCImportRevertBatch',
410
    };
412
    };
411
}
413
}
412
414
(-)a/Koha/BackgroundJob/MARCImportCommitBatch.pm (+110 lines)
Line 0 Link Here
1
package Koha::BackgroundJob::MARCImportCommitBatch;
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
use Try::Tiny;
20
21
use base 'Koha::BackgroundJob';
22
23
use C4::ImportBatch qw(
24
    BatchCommitRecords
25
);
26
27
=head1 NAME
28
29
Koha::BackgroundJob::MARCImportCommitBatch - Commit records
30
31
This is a subclass of Koha::BackgroundJob.
32
33
=head1 API
34
35
=head2 Class methods
36
37
=head3 job_type
38
39
Define the job type of this job: marc_import_commit_batch
40
41
=cut
42
43
sub job_type {
44
    return 'marc_import_commit_batch';
45
}
46
47
=head3 process
48
49
Commit the records
50
51
=cut
52
53
sub process {
54
    my ( $self, $args ) = @_;
55
56
    $self->start;
57
58
    my $import_batch_id = $args->{import_batch_id};
59
    my $frameworkcode = $args->{frameworkcode};
60
61
    my @messages;
62
    my $job_progress = 0;
63
64
    my ( $num_added, $num_updated, $num_items_added,
65
        $num_items_replaced, $num_items_errored, $num_ignored );
66
    try {
67
        (
68
            $num_added, $num_updated, $num_items_added,
69
            $num_items_replaced, $num_items_errored, $num_ignored
70
          )
71
          = BatchCommitRecords( $import_batch_id, $frameworkcode, 50,
72
            sub { my $job_progress = shift; $self->progress( $job_progress )->store } );
73
    }
74
    catch {
75
        warn $_;
76
        die "Something terrible has happened!"
77
          if ( $_ =~ /Rollback failed/ );    # Rollback failed
78
    };
79
80
    my $report = {
81
        num_added          => $num_added,
82
        num_updated        => $num_updated,
83
        num_items_added    => $num_items_added,
84
        num_items_replaced => $num_items_replaced,
85
        num_items_errored  => $num_items_errored,
86
        num_ignored        => $num_ignored
87
    };
88
    my $data = $self->decoded_data;
89
    $data->{messages} = \@messages;
90
    $data->{report}   = $report;
91
92
    $self->finish($data);
93
}
94
95
=head3 enqueue
96
97
Enqueue the new job
98
99
=cut
100
101
sub enqueue {
102
    my ( $self, $args) = @_;
103
104
    $self->SUPER::enqueue({
105
        job_size => 0, # unknown for now
106
        job_args => $args
107
    });
108
}
109
110
1;
(-)a/Koha/BackgroundJob/MARCImportRevertBatch.pm (+110 lines)
Line 0 Link Here
1
package Koha::BackgroundJob::MARCImportRevertBatch;
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
use Try::Tiny;
20
21
use base 'Koha::BackgroundJob';
22
23
use C4::ImportBatch qw(
24
    BatchRevertRecords
25
);
26
27
=head1 NAME
28
29
Koha::BackgroundJob::MARCImportRevertBatch - Revert a batch
30
31
This is a subclass of Koha::BackgroundJob.
32
33
=head1 API
34
35
=head2 Class methods
36
37
=head3 job_type
38
39
Define the job type of this job: marc_import_revert_batch
40
41
=cut
42
43
sub job_type {
44
    return 'marc_import_revert_batch';
45
}
46
47
=head3 process
48
49
Revert a batch
50
51
=cut
52
53
sub process {
54
    my ( $self, $args ) = @_;
55
56
    $self->start;
57
58
    my $import_batch_id = $args->{import_batch_id};
59
60
    my @messages;
61
    my $job_progress = 0;
62
    my (
63
        $num_deleted,       $num_errors, $num_reverted,
64
        $num_items_deleted, $num_ignored
65
    );
66
67
    try {
68
        (
69
            $num_deleted,       $num_errors, $num_reverted,
70
            $num_items_deleted, $num_ignored
71
        ) = BatchRevertRecords( $import_batch_id, 50,
72
            sub { my $job_progress = shift; $self->progress( $job_progress )->store } );
73
    }
74
    catch {
75
        warn $_;
76
        die "Something terrible has happened!"
77
          if ( $_ =~ /Rollback failed/ );    # Rollback failed
78
    };
79
80
    my $report = {
81
        num_deleted       => $num_deleted,
82
        num_items_deleted => $num_items_deleted,
83
        num_errors        => $num_errors,
84
        num_reverted      => $num_reverted,
85
        num_ignored       => $num_ignored,
86
    };
87
88
    my $data = $self->decoded_data;
89
    $data->{messages} = \@messages;
90
    $data->{report}   = $report;
91
92
    $self->finish($data);
93
}
94
95
=head3 enqueue
96
97
Enqueue the new job
98
99
=cut
100
101
sub enqueue {
102
    my ( $self, $args) = @_;
103
104
    $self->SUPER::enqueue({
105
        job_size => 0, # unknown for now
106
        job_args => $args
107
    });
108
}
109
110
1;
(-)a/debian/templates/apache-shared-intranet-plack.conf (-1 lines)
Lines 14-20 Link Here
14
        ProxyPass "/cgi-bin/koha/offline_circ/process_koc.pl" "!"
14
        ProxyPass "/cgi-bin/koha/offline_circ/process_koc.pl" "!"
15
        ProxyPass "/cgi-bin/koha/tools/background-job-progress.pl" "!"
15
        ProxyPass "/cgi-bin/koha/tools/background-job-progress.pl" "!"
16
        ProxyPass "/cgi-bin/koha/tools/export.pl" "!"
16
        ProxyPass "/cgi-bin/koha/tools/export.pl" "!"
17
        ProxyPass "/cgi-bin/koha/tools/manage-marc-import.pl" "!"
18
        ProxyPass "/cgi-bin/koha/tools/upload-cover-image.pl" "!"
17
        ProxyPass "/cgi-bin/koha/tools/upload-cover-image.pl" "!"
19
        ProxyPass "/cgi-bin/koha/svc/cataloguing/metasearch" "!"
18
        ProxyPass "/cgi-bin/koha/svc/cataloguing/metasearch" "!"
20
19
(-)a/koha-tmpl/intranet-tmpl/prog/en/includes/background_jobs/marc_import_commit_batch.inc (+42 lines)
Line 0 Link Here
1
[% USE Koha %]
2
3
[% BLOCK report %]
4
    [% SET report = job.report %]
5
    [% IF report %]
6
        <div class="dialog message">Completed import of records</div>
7
        <table>
8
            <tr>
9
                <td>Number of records added</td>
10
                <td>[% report.num_added | html %]</td>
11
            </tr>
12
            <tr>
13
                <td>Number of records updated</td>
14
                <td>[% report.num_updated | html %]</td>
15
            </tr>
16
            <tr>
17
                <td>Number of records ignored</td>
18
                <td>[% report.num_ignored | html %]</td>
19
            </tr>
20
            [% IF ( report.record_type == 'biblio' ) %]
21
                <tr>
22
                    <td>Number of items added</td>
23
                    <td>[% report.num_items_added | html %]</td>
24
                </tr>
25
                <tr>
26
                    <td>Number of items replaced</td>
27
                    <td>[% report.num_items_replaced | html %]</td>
28
                </tr>
29
                <tr>
30
                    <td>Number of items ignored because of duplicate barcode</td>
31
                    <td>[% report.num_items_errored | html %]</td>
32
                </tr>
33
            [% END %]
34
        </table>
35
    [% END %]
36
[% END %]
37
38
[% BLOCK detail %]
39
[% END %]
40
41
[% BLOCK js %]
42
[% END %]
(-)a/koha-tmpl/intranet-tmpl/prog/en/includes/background_jobs/marc_import_revert_batch.inc (+38 lines)
Line 0 Link Here
1
[% USE Koha %]
2
3
[% BLOCK report %]
4
    [% SET report = job.report %]
5
    [% IF report %]
6
        <div class="dialog message">Success: Import reversed</div>
7
        <table>
8
            <tr>
9
                <td>Number of records deleted</td>
10
                <td>[% report.num_deleted | html %]</td>
11
            </tr>
12
            [% IF ( report.record_type == 'biblio' ) %]
13
                <tr>
14
                    <td>Number of items deleted</td>
15
                    <td>[% report.num_items_deleted | html %]</td>
16
                </tr>
17
                <tr>
18
                    <td>Number of records not deleted due to items on loan</td>
19
                    <td>[% report.num_errors | html %]</td>
20
                </tr>
21
            [% END %]
22
            <tr>
23
                <td>Number of records changed back</td>
24
                <td>[% report.num_reverted | html %]</td>
25
            </tr>
26
            <tr>
27
                <td>Number of records ignored</td>
28
                <td>[% report.num_ignored | html %]</td>
29
            </tr>
30
        </table>
31
    [% END %]
32
[% END %]
33
34
[% BLOCK detail %]
35
[% END %]
36
37
[% BLOCK js %]
38
[% END %]
(-)a/koha-tmpl/intranet-tmpl/prog/en/includes/background_jobs/stage_marc_for_import.inc (+3 lines)
Lines 31-36 Link Here
31
                <li>New label batch created: # [% report.label_batch | html %] </li>
31
                <li>New label batch created: # [% report.label_batch | html %] </li>
32
            [% END %]
32
            [% END %]
33
        </ul>
33
        </ul>
34
        <p>
35
            <a href="/cgi-bin/koha/tools/manage-marc-import.pl?import_batch_id=[% report.import_batch_id | uri %]">View batch</a>
36
        </p>
34
        [% IF report.basketno && report.booksellerid %]
37
        [% IF report.basketno && report.booksellerid %]
35
        <p>
38
        <p>
36
            <a id="addtobasket" class="btn btn-default" href="/cgi-bin/koha/acqui/addorderiso2709.pl?import_batch_id=[% report.import_batch_id | html %]&basketno=[% report.basketno | html %]&booksellerid=[% report.booksellerid | html %]">Add staged files to basket</a>
39
            <a id="addtobasket" class="btn btn-default" href="/cgi-bin/koha/acqui/addorderiso2709.pl?import_batch_id=[% report.import_batch_id | html %]&basketno=[% report.basketno | html %]&booksellerid=[% report.booksellerid | html %]">Add staged files to basket</a>
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/background_jobs.tt (+4 lines)
Lines 43-48 Link Here
43
        <span>Holds queue update</span>
43
        <span>Holds queue update</span>
44
    [% CASE 'stage_marc_for_import' %]
44
    [% CASE 'stage_marc_for_import' %]
45
        <span>Staged MARC records for import</span>
45
        <span>Staged MARC records for import</span>
46
    [% CASE 'marc_import_commit_batch' %]
47
        <span>Import MARC records</span>
48
    [% CASE 'marc_import_revert_batch' %]
49
        <span>Revert import MARC records</span>
46
    [% CASE %]<span>Unknown job type '[% job_type | html %]'</span>
50
    [% CASE %]<span>Unknown job type '[% job_type | html %]'</span>
47
    [% END %]
51
    [% END %]
48
52
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/manage-marc-import.tt (-81 / +42 lines)
Lines 31-37 Link Here
31
            </li>
31
            </li>
32
            [% IF ( import_batch_id ) %]
32
            [% IF ( import_batch_id ) %]
33
                <li>
33
                <li>
34
                    <a href="[% script_name | url %]">Manage staged MARC records</a>
34
                    <a href="/cgi-bin/koha/tools/manage-marc-import.pl">Manage staged MARC records</a>
35
                </li>
35
                </li>
36
                <li>
36
                <li>
37
                    <a href="#" aria-current="page">
37
                    <a href="#" aria-current="page">
Lines 57-64 Link Here
57
                            &rsaquo; Batch [% import_batch_id | html %]
57
                            &rsaquo; Batch [% import_batch_id | html %]
58
                        [% END %]
58
                        [% END %]
59
                    </h1>
59
                    </h1>
60
                    [% FOREACH message IN messages %]
61
                      [% IF message.type == 'success' %]
62
                        <div class="dialog message">
63
                      [% ELSIF message.type == 'warning' %]
64
                        <div class="dialog alert">
65
                      [% ELSIF message.type == 'error' %]
66
                        <div class="dialog alert" style="margin:auto;">
67
                      [% END %]
68
                      [% IF message.code == 'cannot_enqueue_job' %]
69
                          <span>Cannot enqueue this job.</span>
70
                      [% END %]
71
                      [% IF message.error %]
72
                        <span>(The error was: [% message.error | html %], see the Koha log file for more information).</span>
73
                      [% END %]
74
                      </div>
75
                    [% END %]
76
77
                    [% IF job_enqueued %]
78
                        <div class="dialog message">
79
                          <p>The job has been enqueued! It will be processed as soon as possible.</p>
80
                          <p><a href="/cgi-bin/koha/admin/background_jobs.pl?op=view&id=[% job_id | uri %]" title="View detail of the enqueued job">View detail of the enqueued job</a>
81
                        </div>
60
82
61
                    [% IF ( label_batch_msg ) %]
83
                    [% ELSIF ( label_batch_msg ) %]
62
                        [% IF ( alert ) %]
84
                        [% IF ( alert ) %]
63
                            <div class="alert">
85
                            <div class="alert">
64
                        [% ELSE %]
86
                        [% ELSE %]
Lines 82-99 Link Here
82
                        <div class="dialog message">Import batch deleted successfully</div>
104
                        <div class="dialog message">Import batch deleted successfully</div>
83
                    [% END %]
105
                    [% END %]
84
106
85
                    [% UNLESS ( batch_list ) %]
107
                    [% UNLESS batch_list || batch_info || job_enqueued %]
86
                        [% UNLESS ( batch_info ) %]
108
                        <div class="dialog message">
87
                            <div class="dialog message">
109
                            <p>No records have been staged.</p>
88
                                <p>No records have been staged.</p>
110
                            <p><a href="/cgi-bin/koha/tools/stage-marc-import.pl">Stage MARC records for import</a>.</p>
89
                                <p><a href="/cgi-bin/koha/tools/stage-marc-import.pl">Stage MARC records for import</a>.</p>
111
                        </div>
90
                            </div>
91
                        [% END %]
92
                    [% END %]
112
                    [% END %]
93
113
94
                    [% IF ( batch_info ) %]
114
                    [% IF ( batch_info ) %]
95
                        [% IF ( can_commit ) %]
115
                        [% IF ( can_commit ) %]
96
                            <form action="[% script_name | html %]" method="post">
116
                            <form method="post">
97
                                <input type="hidden" name="op" value="redo-matching" />
117
                                <input type="hidden" name="op" value="redo-matching" />
98
                                <input type="hidden" name="import_batch_id" value="[% import_batch_id | html %]" />
118
                                <input type="hidden" name="import_batch_id" value="[% import_batch_id | html %]" />
99
                                <input type="hidden" name="current_matcher_id" value="[% current_matcher_id | html %]" />
119
                                <input type="hidden" name="current_matcher_id" value="[% current_matcher_id | html %]" />
Lines 121-126 Link Here
121
                            <div class="dialog message">Changed item processing option</div>
141
                            <div class="dialog message">Changed item processing option</div>
122
                        [% END %]
142
                        [% END %]
123
143
144
                        [% UNLESS job_enqueued %]
124
                        <fieldset class="rows" id="staged-record-matching-rules">
145
                        <fieldset class="rows" id="staged-record-matching-rules">
125
                            <ol>
146
                            <ol>
126
                                <li><span class="label">File name:</span> [% file_name | html %]</li>
147
                                <li><span class="label">File name:</span> [% file_name | html %]</li>
Lines 236-242 Link Here
236
257
237
                        <div>
258
                        <div>
238
                            [% IF ( can_commit ) %]
259
                            [% IF ( can_commit ) %]
239
                                <form action="[% script_name | html %]" method="post" id="import_batch_form">
260
                                <form method="post" id="import_batch_form">
240
                                    <input type="hidden" name="op" value="commit-batch" />
261
                                    <input type="hidden" name="op" value="commit-batch" />
241
                                    <input type="hidden" name="runinbackground" value="" />
262
                                    <input type="hidden" name="runinbackground" value="" />
242
                                    <input type="hidden" name="completedJobID" value="" />
263
                                    <input type="hidden" name="completedJobID" value="" />
Lines 261-267 Link Here
261
                                </div>
282
                                </div>
262
                            [% END # /IF can_commit %]
283
                            [% END # /IF can_commit %]
263
                            [% IF ( can_revert ) %]
284
                            [% IF ( can_revert ) %]
264
                                <form action="[% script_name | html %]" method="post" id="revert_batch_form">
285
                                <form method="post" id="revert_batch_form">
265
                                    <input type="hidden" name="op" value="revert-batch" />
286
                                    <input type="hidden" name="op" value="revert-batch" />
266
                                    <input type="hidden" name="runinbackground" value="" />
287
                                    <input type="hidden" name="runinbackground" value="" />
267
                                    <input type="hidden" name="completedJobID" value="" />
288
                                    <input type="hidden" name="completedJobID" value="" />
Lines 274-339 Link Here
274
                                </div>
295
                                </div>
275
                            [% END # /IF can_revert %]
296
                            [% END # /IF can_revert %]
276
                        </div>
297
                        </div>
277
298
                        [% END %]
278
                        [% IF ( did_commit ) %]
279
                            <div class="dialog message">Completed import of records</div>
280
                            <table>
281
                                <tr>
282
                                    <td>Number of records added</td>
283
                                    <td>[% num_added | html %]</td>
284
                                </tr>
285
                                <tr>
286
                                    <td>Number of records updated</td>
287
                                    <td>[% num_updated | html %]</td>
288
                                </tr>
289
                                <tr>
290
                                    <td>Number of records ignored</td>
291
                                    <td>[% num_ignored | html %]</td>
292
                                </tr>
293
                                [% IF ( record_type == 'biblio' ) %]
294
                                    <tr>
295
                                        <td>Number of items added</td>
296
                                        <td>[% num_items_added | html %]</td>
297
                                    </tr>
298
                                    <tr>
299
                                        <td>Number of items replaced</td>
300
                                        <td>[% num_items_replaced | html %]</td>
301
                                    </tr>
302
                                    <tr>
303
                                        <td>Number of items ignored because of duplicate barcode</td>
304
                                        <td>[% num_items_errored | html %]</td>
305
                                    </tr>
306
                                [% END %]
307
                            </table>
308
                        [% END #/ IF did_commit %]
309
310
                        [% IF ( did_revert ) %]
311
                            <div class="dialog message">Success: Import reversed</div>
312
                            <table>
313
                                <tr>
314
                                    <td>Number of records deleted</td>
315
                                    <td>[% num_deleted | html %]</td>
316
                                </tr>
317
                                [% IF ( record_type == 'biblio' ) %]
318
                                    <tr>
319
                                        <td>Number of items deleted</td>
320
                                        <td>[% num_items_deleted | html %]</td>
321
                                    </tr>
322
                                    <tr>
323
                                        <td>Number of records not deleted due to items on loan</td>
324
                                        <td>[% num_errors | html %]</td>
325
                                    </tr>
326
                                [% END %]
327
                                <tr>
328
                                    <td>Number of records changed back</td>
329
                                    <td>[% num_reverted | html %]</td>
330
                                </tr>
331
                                <tr>
332
                                    <td>Number of records ignored</td>
333
                                    <td>[% num_ignored | html %]</td>
334
                                </tr>
335
                            </table>
336
                        [% END  # /IF did_revert%]
337
299
338
                    [% END # /IF batch_info %]
300
                    [% END # /IF batch_info %]
339
301
Lines 360-366 Link Here
360
                            [% FOREACH batch_lis IN batch_list %]
322
                            [% FOREACH batch_lis IN batch_list %]
361
                                <tr>
323
                                <tr>
362
                                    <td>[% batch_lis.import_batch_id | html %]</td>
324
                                    <td>[% batch_lis.import_batch_id | html %]</td>
363
                                    <td><a href="[% batch_lis.script_name | url %]?import_batch_id=[% batch_lis.import_batch_id | uri %]">[% batch_lis.file_name | html %]</a></td>
325
                                    <td><a href="?import_batch_id=[% batch_lis.import_batch_id | uri %]">[% batch_lis.file_name | html %]</a></td>
364
                                    <td>[% batch_lis.profile | html %]</td>
326
                                    <td>[% batch_lis.profile | html %]</td>
365
                                    <td>[% batch_lis.comments | html %]</td>
327
                                    <td>[% batch_lis.comments | html %]</td>
366
                                    <td>[% IF ( batch_lis.record_type == 'auth' ) %]Authority[% ELSE %]Bibliographic[% END %]</td>
328
                                    <td>[% IF ( batch_lis.record_type == 'auth' ) %]Authority[% ELSE %]Bibliographic[% END %]</td>
Lines 385-396 Link Here
385
                                    <td>[% batch_lis.num_records | html %]</td>
347
                                    <td>[% batch_lis.num_records | html %]</td>
386
                                    <td>[% batch_lis.num_items | html %]
348
                                    <td>[% batch_lis.num_items | html %]
387
                                        [% IF ( batch_lis.num_items && batch_lis.import_status == 'imported' ) %]
349
                                        [% IF ( batch_lis.num_items && batch_lis.import_status == 'imported' ) %]
388
                                            (<a href="[% batch_lis.script_name | url %]?import_batch_id=[% batch_lis.import_batch_id | uri %]&amp;op=create_labels">Create label batch</a>)
350
                                            (<a href="?import_batch_id=[% batch_lis.import_batch_id | uri %]&amp;op=create_labels">Create label batch</a>)
389
                                        [% END %]
351
                                        [% END %]
390
                                    </td>
352
                                    </td>
391
                                    <td class="actions">
353
                                    <td class="actions">
392
                                        [% IF ( batch_lis.can_clean ) %]
354
                                        [% IF ( batch_lis.can_clean ) %]
393
                                            <form method="post" action="[% batch_lis.script_name | html %]" name="clean_batch_[% batch_lis.import_batch_id | html %]" id="clean_batch_[% batch_lis.import_batch_id | html %]" class="batch_form batch_clean">
355
                                            <form method="post" name="clean_batch_[% batch_lis.import_batch_id | html %]" id="clean_batch_[% batch_lis.import_batch_id | html %]" class="batch_form batch_clean">
394
                                                <input type="hidden" name="import_batch_id" value="[% batch_lis.import_batch_id | html %]" />
356
                                                <input type="hidden" name="import_batch_id" value="[% batch_lis.import_batch_id | html %]" />
395
                                                <input type="hidden" name="op" value="clean-batch" />
357
                                                <input type="hidden" name="op" value="clean-batch" />
396
                                                <button type="submit" class="btn btn-default btn-xs"><i class="fa fa-eraser"></i> Clean</button>
358
                                                <button type="submit" class="btn btn-default btn-xs"><i class="fa fa-eraser"></i> Clean</button>
Lines 457-463 Link Here
457
[% MACRO jsinclude BLOCK %]
419
[% MACRO jsinclude BLOCK %]
458
    [% Asset.js("js/tools-menu.js") | $raw %]
420
    [% Asset.js("js/tools-menu.js") | $raw %]
459
    [% Asset.js("lib/jquery/plugins/humanmsg.js") | $raw %]
421
    [% Asset.js("lib/jquery/plugins/humanmsg.js") | $raw %]
460
    [% Asset.js("js/background-job-progressbar.js") | $raw %]
461
    [% INCLUDE 'datatables.inc' %]
422
    [% INCLUDE 'datatables.inc' %]
462
    <script>
423
    <script>
463
        $(document).ready(function() {
424
        $(document).ready(function() {
Lines 657-664 Link Here
657
        <ul class="pagination">
618
        <ul class="pagination">
658
            [% FOREACH page IN pages %]
619
            [% FOREACH page IN pages %]
659
                [% IF ( page.current_page && page.page_number > 1 ) %]
620
                [% IF ( page.current_page && page.page_number > 1 ) %]
660
                    <li><a href="[% page.script_name | url %]?offset=0" class="nav"><i class="fa fa-fw fa-angle-double-left"></i> First</a></li>
621
                    <li><a href="?offset=0" class="nav"><i class="fa fa-fw fa-angle-double-left"></i> First</a></li>
661
                    <li><a href="[% page.script_name | url %]?offset=[% offset - results_per_page | uri %]"><i class="fa fa-fw fa-angle-left"></i> Previous</a></li>
622
                    <li><a href="?offset=[% offset - results_per_page | uri %]"><i class="fa fa-fw fa-angle-left"></i> Previous</a></li>
662
                [% END %]
623
                [% END %]
663
            [% END %]
624
            [% END %]
664
            [% FOREACH page IN pages %]
625
            [% FOREACH page IN pages %]
Lines 666-680 Link Here
666
                    [% SET current_page = page.page_number %]
627
                    [% SET current_page = page.page_number %]
667
                    <li class="active"><span class="current">[% page.page_number | html %]</span></li>
628
                    <li class="active"><span class="current">[% page.page_number | html %]</span></li>
668
                [% ELSE %]
629
                [% ELSE %]
669
                    <li><a class="nav" href="[% page.script_name | url %]?offset=[% page.offset | uri %]">[% page.page_number | html %]</a></li>
630
                    <li><a class="nav" href="?offset=[% page.offset | uri %]">[% page.page_number | html %]</a></li>
670
                [% END %]
631
                [% END %]
671
            [% END %]
632
            [% END %]
672
            [% IF ( current_page < pages.size() ) %]
633
            [% IF ( current_page < pages.size() ) %]
673
                <li>
634
                <li>
674
                    <a href="[% page.script_name | url %]?offset=[% offset + results_per_page | uri %]" class="nav">Next <i class="fa fa-fw fa-angle-right"></i></a>
635
                    <a href="?offset=[% offset + results_per_page | uri %]" class="nav">Next <i class="fa fa-fw fa-angle-right"></i></a>
675
                </li>
636
                </li>
676
                <li>
637
                <li>
677
                    <a href="[% page.script_name | url %]?offset=[% ( results_per_page * ( pages.size - 1 ) ) | uri %]" class="nav">Last <i class="fa fa-fw fa-angle-double-right"></i></a>
638
                    <a href="?offset=[% ( results_per_page * ( pages.size - 1 ) ) | uri %]" class="nav">Last <i class="fa fa-fw fa-angle-double-right"></i></a>
678
                </li>
639
                </li>
679
            [% END %]
640
            [% END %]
680
        </ul>
641
        </ul>
(-)a/tools/manage-marc-import.pl (-155 / +47 lines)
Lines 23-28 use Modern::Perl; Link Here
23
use CGI qw ( -utf8 );
23
use CGI qw ( -utf8 );
24
use CGI::Cookie;
24
use CGI::Cookie;
25
use MARC::File::USMARC;
25
use MARC::File::USMARC;
26
use Try::Tiny;
26
27
27
# Koha modules used
28
# Koha modules used
28
use C4::Context;
29
use C4::Context;
Lines 31-49 use C4::Auth qw( get_template_and_user ); Link Here
31
use C4::Output qw( output_html_with_http_headers );
32
use C4::Output qw( output_html_with_http_headers );
32
use C4::ImportBatch qw( CleanBatch DeleteBatch GetImportBatch GetImportBatchOverlayAction GetImportBatchNoMatchAction GetImportBatchItemAction SetImportBatchOverlayAction SetImportBatchNoMatchAction SetImportBatchItemAction BatchFindDuplicates SetImportBatchMatcher GetItemNumbersFromImportBatch GetImportBatchRangeDesc GetNumberOfNonZ3950ImportBatches BatchCommitRecords BatchRevertRecords );
33
use C4::ImportBatch qw( CleanBatch DeleteBatch GetImportBatch GetImportBatchOverlayAction GetImportBatchNoMatchAction GetImportBatchItemAction SetImportBatchOverlayAction SetImportBatchNoMatchAction SetImportBatchItemAction BatchFindDuplicates SetImportBatchMatcher GetItemNumbersFromImportBatch GetImportBatchRangeDesc GetNumberOfNonZ3950ImportBatches BatchCommitRecords BatchRevertRecords );
33
use C4::Matcher;
34
use C4::Matcher;
34
use C4::BackgroundJob;
35
use C4::Labels::Batch;
35
use C4::Labels::Batch;
36
use Koha::BiblioFrameworks;
36
use Koha::BiblioFrameworks;
37
use Koha::BackgroundJob::MARCImportCommitBatch;
38
use Koha::BackgroundJob::MARCImportRevertBatch;
37
39
38
use Koha::Logger;
40
use Koha::Logger;
39
41
40
my $script_name = "/cgi-bin/koha/tools/manage-marc-import.pl";
41
42
42
my $input = CGI->new;
43
my $input = CGI->new;
43
my $op = $input->param('op') || '';
44
my $op = $input->param('op') || '';
44
my $completedJobID = $input->param('completedJobID');
45
our $runinbackground = $input->param('runinbackground');
46
my $import_batch_id = $input->param('import_batch_id') || '';
45
my $import_batch_id = $input->param('import_batch_id') || '';
46
my @messages;
47
47
48
# record list displays
48
# record list displays
49
my $offset = $input->param('offset') || 0;
49
my $offset = $input->param('offset') || 0;
Lines 56-65 my ($template, $loggedinuser, $cookie) Link Here
56
                 flagsrequired => {tools => 'manage_staged_marc'},
56
                 flagsrequired => {tools => 'manage_staged_marc'},
57
                 });
57
                 });
58
58
59
my %cookies = CGI::Cookie->fetch();
60
our $sessionID = $cookies{'CGISESSID'}->value;
61
our $dbh = C4::Context->dbh;
62
63
my $frameworks = Koha::BiblioFrameworks->search({ tagfield => { 'not' => undef } }, { join => 'marc_tag_structure', distinct => 'frameworkcode', order_by => ['frameworktext'] });
59
my $frameworks = Koha::BiblioFrameworks->search({ tagfield => { 'not' => undef } }, { join => 'marc_tag_structure', distinct => 'frameworkcode', order_by => ['frameworktext'] });
64
$template->param( frameworks => $frameworks );
60
$template->param( frameworks => $frameworks );
65
61
Lines 79-88 if ($op eq "create_labels") { Link Here
79
	$op='';
75
	$op='';
80
	$import_batch_id='';
76
	$import_batch_id='';
81
}
77
}
78
82
if ($op) {
79
if ($op) {
83
    $template->param(script_name => $script_name, $op => 1);
80
    $template->param($op => 1);
84
} else {
85
    $template->param(script_name => $script_name);
86
}
81
}
87
82
88
if ($op eq "") {
83
if ($op eq "") {
Lines 93-112 if ($op eq "") { Link Here
93
        import_records_list($template, $import_batch_id, $offset, $results_per_page);
88
        import_records_list($template, $import_batch_id, $offset, $results_per_page);
94
    }
89
    }
95
} elsif ($op eq "commit-batch") {
90
} elsif ($op eq "commit-batch") {
96
    if ($completedJobID) {
91
    my $frameworkcode = $input->param('framework');
97
        add_saved_job_results_to_template($template, $completedJobID);
92
    try {
98
    } else {
93
        my $job_id = Koha::BackgroundJob::MARCImportCommitBatch->new->enqueue(
99
        my $framework = $input->param('framework');
94
            {
100
        commit_batch($template, $import_batch_id, $framework);
95
                import_batch_id => $import_batch_id,
96
                frameworkcode   => $frameworkcode
97
            }
98
        );
99
        if ($job_id) {
100
            $template->param(
101
                job_enqueued => 1,
102
                job_id => $job_id,
103
            );
104
        }
101
    }
105
    }
102
    import_records_list($template, $import_batch_id, $offset, $results_per_page);
106
    catch {
107
        warn $_;
108
        push @messages,
109
          {
110
            type  => 'error',
111
            code  => 'cannot_enqueue_job',
112
            error => $_,
113
          };
114
    };
103
} elsif ($op eq "revert-batch") {
115
} elsif ($op eq "revert-batch") {
104
    if ($completedJobID) {
116
    try {
105
        add_saved_job_results_to_template($template, $completedJobID);
117
        my $job_id = Koha::BackgroundJob::MARCImportRevertBatch->new->enqueue(
106
    } else {
118
            { import_batch_id => $import_batch_id } );
107
        revert_batch($template, $import_batch_id);
119
        if ($job_id) {
120
            $template->param(
121
                job_enqueued => 1,
122
                job_id => $job_id,
123
            );
124
        }
108
    }
125
    }
109
    import_records_list($template, $import_batch_id, $offset, $results_per_page);
126
    catch {
127
        warn $_;
128
        push @messages,
129
          {
130
            type  => 'error',
131
            code  => 'cannot_enqueue_job',
132
            error => $_,
133
          };
134
    };
110
} elsif ($op eq "clean-batch") {
135
} elsif ($op eq "clean-batch") {
111
    CleanBatch($import_batch_id);
136
    CleanBatch($import_batch_id);
112
    import_batches_list($template, $offset, $results_per_page);
137
    import_batches_list($template, $offset, $results_per_page);
Lines 228-365 sub import_batches_list { Link Here
228
253
229
}
254
}
230
255
231
sub commit_batch {
232
    my ($template, $import_batch_id, $framework) = @_;
233
234
    my $job = undef;
235
    my ( $num_added, $num_updated, $num_items_added,
236
        $num_items_replaced, $num_items_errored, $num_ignored );
237
    my $callback = sub { };
238
    if ($runinbackground) {
239
        $job = put_in_background($import_batch_id);
240
        $callback = progress_callback( $job );
241
    }
242
    (
243
        $num_added, $num_updated, $num_items_added,
244
        $num_items_replaced, $num_items_errored, $num_ignored
245
      )
246
      = BatchCommitRecords( $import_batch_id, $framework, 50,
247
        $callback );
248
249
    my $results = {
250
        did_commit => 1,
251
        num_added => $num_added,
252
        num_updated => $num_updated,
253
        num_items_added => $num_items_added,
254
        num_items_replaced => $num_items_replaced,
255
        num_items_errored => $num_items_errored,
256
        num_ignored => $num_ignored
257
    };
258
    if ($runinbackground) {
259
        $job->finish($results);
260
    } else {
261
        add_results_to_template($template, $results);
262
    }
263
}
264
265
sub revert_batch {
266
    my ($template, $import_batch_id) = @_;
267
268
    my $job = undef;
269
            my (
270
                $num_deleted,       $num_errors, $num_reverted,
271
                $num_items_deleted, $num_ignored
272
            );
273
    my $schema = Koha::Database->new->schema;
274
    $schema->txn_do(
275
        sub {
276
            if ($runinbackground) {
277
                $job = put_in_background($import_batch_id);
278
            }
279
            (
280
                $num_deleted,       $num_errors, $num_reverted,
281
                $num_items_deleted, $num_ignored
282
            ) = BatchRevertRecords( $import_batch_id );
283
        }
284
    );
285
286
    my $results = {
287
        did_revert => 1,
288
        num_deleted => $num_deleted,
289
        num_items_deleted => $num_items_deleted,
290
        num_errors => $num_errors,
291
        num_reverted => $num_reverted,
292
        num_ignored => $num_ignored,
293
    };
294
    if ($runinbackground) {
295
        $job->finish($results);
296
    } else {
297
        add_results_to_template($template, $results);
298
    }
299
}
300
301
sub put_in_background {
302
    my $import_batch_id = shift;
303
304
    my $batch = GetImportBatch($import_batch_id);
305
    my $job = C4::BackgroundJob->new($sessionID, $batch->{'file_name'}, '/cgi-bin/koha/tools/manage-marc-import.pl', $batch->{'num_records'});
306
    my $jobID = $job->id();
307
308
    # fork off
309
    if (my $pid = fork) {
310
        # parent
311
        # return job ID as JSON
312
313
        # prevent parent exiting from
314
        # destroying the kid's database handle
315
        # FIXME: according to DBI doc, this may not work for Oracle
316
        $dbh->{InactiveDestroy}  = 1;
317
318
        my $reply = CGI->new("");
319
        print $reply->header(-type => 'text/html');
320
        print '{"jobID":"' . $jobID . '"}';
321
        exit 0;
322
    } elsif (defined $pid) {
323
        # child
324
        # close STDOUT to signal to Apache that
325
        # we're now running in the background
326
        close STDOUT;
327
        close STDERR;
328
        $SIG{__WARN__} = sub {
329
            my ($msg) = @_;
330
            my $logger = Koha::Logger->get;
331
            $logger->warn($msg);
332
        }
333
    } else {
334
        # fork failed, so exit immediately
335
        warn "fork failed while attempting to run tools/manage-marc-import.pl as a background job";
336
        exit 0;
337
    }
338
    return $job;
339
}
340
341
sub progress_callback {
342
    my $job = shift;
343
    return sub {
344
        my $progress = shift;
345
        $job->progress($progress);
346
    }
347
}
348
349
sub add_results_to_template {
350
    my $template = shift;
351
    my $results = shift;
352
    $template->param(map { $_ => $results->{$_} } keys %{ $results });
353
}
354
355
sub add_saved_job_results_to_template {
356
    my $template = shift;
357
    my $completedJobID = shift;
358
    my $job = C4::BackgroundJob->fetch($sessionID, $completedJobID);
359
    my $results = $job->results();
360
    add_results_to_template($template, $results);
361
}
362
363
sub import_records_list {
256
sub import_records_list {
364
    my ($template, $import_batch_id, $offset, $results_per_page) = @_;
257
    my ($template, $import_batch_id, $offset, $results_per_page) = @_;
365
258
366
- 

Return to bug 27421