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

(-)a/Koha/Authority/MergeRequests.pm (+36 lines)
Lines 23-28 use MARC::Record; Link Here
23
23
24
use C4::Context;
24
use C4::Context;
25
use Koha::Authority::MergeRequest;
25
use Koha::Authority::MergeRequest;
26
use Koha::Database;
27
use Koha::DateUtils;
26
28
27
use parent qw(Koha::Objects);
29
use parent qw(Koha::Objects);
28
30
Lines 69-74 sub reporting_tag_xml { Link Here
69
    );
71
    );
70
}
72
}
71
73
74
=head3 cron_cleanup
75
76
    Koha::Authority::MergeRequests->cron_cleanup({
77
        reset_hours => 24, remove_days => 90,
78
    });
79
80
    Removes all entries with status "done" older than remove_days.
81
    Set all entries with status "in progress" back to 0 when the timestamp
82
    is older than reset_hours.
83
    Defaults: reset_hours = 1, remove_days = 30.
84
85
=cut
86
87
sub cron_cleanup {
88
    my ( $class_or_self, $params ) = @_;
89
    my $reset_hours = $params->{reset_hours} || 1;
90
    my $remove_days = $params->{remove_days} || 30;
91
    my $parser = Koha::Database->new->schema->storage->datetime_parser;
92
93
    my $dt = dt_from_string;
94
    $dt->subtract( hours => $reset_hours );
95
    $class_or_self->search({
96
        done => 2,
97
        timestamp => { '<' => $parser->format_datetime($dt) },
98
    })->update({ done => 0 });
99
100
    $dt = dt_from_string;
101
    $dt->subtract( days => $remove_days );
102
    $class_or_self->search({
103
        done => 1,
104
        timestamp => { '<' => $parser->format_datetime($dt) },
105
    })->delete;
106
}
107
72
=head3 _type
108
=head3 _type
73
109
74
Returns name of corresponding DBIC resultset
110
Returns name of corresponding DBIC resultset
(-)a/misc/cronjobs/merge_authorities.pl (+89 lines)
Line 0 Link Here
1
#!/usr/bin/perl
2
3
use Modern::Perl;
4
use Getopt::Long;
5
use Pod::Usage;
6
use Time::HiRes qw(gettimeofday);
7
8
use C4::AuthoritiesMarc;
9
use Koha::Authority::MergeRequests;
10
11
use constant RESET_HOURS => 24;
12
use constant REMOVE_DAYS => 30;
13
14
my ( $params );
15
GetOptions(
16
    'h' => \$params->{help},
17
    'v' => \$params->{verbose},
18
    'b' => \$params->{batch},
19
);
20
21
$|=1; # flushes output
22
if( $params->{batch} ) {
23
    handle_batch( $params );
24
} else {
25
    pod2usage(1);
26
}
27
28
sub handle_batch {
29
    my $params = shift;
30
    my $verbose = $params->{verbose};
31
32
    my $starttime = gettimeofday;
33
    print "Started merging\n" if $verbose;
34
35
    Koha::Authority::MergeRequests->cron_cleanup({ reset_hours => RESET_HOURS, remove_days => REMOVE_DAYS });
36
    my $rs = Koha::Authority::MergeRequests->search(
37
        { done => 0 },
38
        { order_by => { -asc => 'id' }}, # IMPORTANT
39
    );
40
    # For best results, postponed merges should be applied in right order.
41
    # Similarly, we do not only select the last one for a specific id.
42
43
    while( my $req = $rs->next ) {
44
        $req->done(2)->store;
45
        print "Merging auth " . $req->authid . " to " . ( $req->authid_new // 'NULL' ) . ".\n" if $verbose;
46
        my $newmarc = $req->authid_new
47
            ? GetAuthority( $req->authid_new )
48
            : undef;
49
        # Following merge call handles both modifications and deletes
50
        merge({
51
            mergefrom => $req->authid,
52
            MARCfrom => scalar $req->oldmarc,
53
            mergeto => $req->authid_new,
54
            MARCto => $newmarc,
55
            override_limit => 1,
56
        });
57
        $req->done(1)->store;
58
    }
59
    my $timeneeded = gettimeofday - $starttime;
60
    print "Done in $timeneeded seconds\n" if $verbose;
61
}
62
63
=head1 NAME
64
65
merge_authorities.pl
66
67
=head1 DESCRIPTION
68
69
Cron script to handle authority merge requests
70
71
=head1 SYNOPSIS
72
73
merge_authorities.pl -h
74
75
merge_authorities.pl -b -v
76
77
=head1 OPTIONS
78
79
-b : batch mode (You need to pass this parameter from crontab file)
80
81
-h : print usage statement
82
83
-v : verbose mode
84
85
=head1 AUTHOR
86
87
Koha Development Team
88
89
=cut
(-)a/misc/migration_tools/merge_authority.pl (-104 lines)
Lines 1-104 Link Here
1
#!/usr/bin/perl
2
# script that rebuild thesaurus from biblio table.
3
4
use strict;
5
#use warnings; FIXME - Bug 2505
6
BEGIN {
7
    # find Koha's Perl modules
8
    # test carefully before changing this
9
    use FindBin;
10
    eval { require "$FindBin::Bin/kohalib.pl" };
11
}
12
13
# Koha modules used
14
use C4::Context;
15
use C4::Search;
16
use C4::Biblio;
17
use C4::AuthoritiesMarc;
18
use Koha::Authorities;
19
use Koha::Authority::MergeRequests;
20
use Time::HiRes qw(gettimeofday);
21
22
use Getopt::Long;
23
my ($version, $verbose, $mergefrom,$mergeto,$noconfirm,$batch);
24
GetOptions(
25
    'h' => \$version,
26
    'f:s' => \$mergefrom,
27
    't:s' => \$mergeto,
28
    'v' => \$verbose,
29
    'n' => \$noconfirm,
30
    'b' => \$batch, 
31
);
32
33
if ($version || ($mergefrom eq '' && !$batch)) {
34
    print <<EOF
35
Script to merge an authority into another
36
parameters :
37
\th : this version/help screen
38
\tv : verbose mode (show many things on screen)
39
\tf : the authority number to merge (the one that can be deleted after the merge).
40
\tt : the authority number where to merge
41
\tn : don't ask for confirmation (useful for batch mergings, should not be used on command line)
42
\tb : batch Merging
43
44
All biblios with the authority in -t will be modified to be "connected" to authority -f
45
SAMPLE :
46
./merge_authority.pl -f 2457 -t 531
47
48
Before doing anything, the script will show both authorities and ask for confirmation. Of course, you can merge only 2 authorities of the same kind.
49
EOF
50
;#
51
die;
52
}#/'
53
54
my $dbh = C4::Context->dbh;
55
56
$|=1; # flushes output
57
my $authfrom = GetAuthority($mergefrom);
58
my $authto = GetAuthority($mergeto);
59
60
die "Authority $mergefrom does not exist" unless $authfrom;
61
die "Authority $mergeto does not exist"   unless $authto;
62
63
my $authtypecodefrom = Koha::Authorities->find($mergefrom)->authtypecode;
64
my $authtypecodeto = Koha::Authorities->find($mergeto)->authtypecode;
65
66
unless ($noconfirm || $batch) {
67
    print "************\n";
68
    print "You will merge authority : $mergefrom ($authtypecodefrom)\n".$authfrom->as_formatted;
69
    print "\n*************\n";
70
    print "Into authority : $mergeto ($authtypecodeto)\n".$authto->as_formatted;
71
    print "\n\nDo you confirm (enter YES)?";
72
    my $confirm = <STDIN>;
73
    chop $confirm;
74
    unless (uc($confirm) eq 'YES' and $authtypecodefrom eq $authtypecodeto) {
75
        print "IMPOSSIBLE : authorities are not of the same type ($authtypecodefrom vs $authtypecodeto) !!!\n" if $authtypecodefrom ne $authtypecodeto;
76
        print "Merge cancelled\n";
77
        exit;
78
    }
79
}
80
my $starttime = gettimeofday;
81
print "Merging\n" unless $noconfirm;
82
if ($batch) {
83
  my $authref;
84
  $dbh->do("update need_merge_authorities set done=2 where done=0"); #temporary status 2 means: selected for merge
85
  $authref=$dbh->selectall_arrayref("select id,authid,authid_new from need_merge_authorities where done=2");
86
  foreach my $row ( @$authref ) {
87
      my $req = Koha::Authority::MergeRequests->find( $row->[0] );
88
      my $marc = $req ? $req->oldmarc : undef;
89
      my $authid = $row->[1];
90
      my $authid_new = $row->[2];
91
      print "managing $authid\n" if $verbose;
92
      # Following merge call handles both modifications and deletes
93
      merge({ mergefrom => $authid, MARCfrom => $marc, mergeto => $authid_new, MARCto => GetAuthority($authid_new), override_limit => 1 });
94
  }
95
  $dbh->do("update need_merge_authorities set done=1 where done=2"); #DONE
96
} else {
97
  my $MARCfrom = GetAuthority($mergefrom);
98
  my $MARCto = GetAuthority($mergeto);
99
  &merge({ mergefrom => $mergefrom, MARCfrom => $MARCfrom, mergeto => $mergeto, MARCto => $MARCto });
100
  #Could add mergefrom authority to mergeto rejected forms before deletion 
101
  DelAuthority({ authid => $mergefrom }) if ($mergefrom != $mergeto);
102
}
103
my $timeneeded = gettimeofday - $starttime;
104
print "Done in $timeneeded seconds" unless $noconfirm;
(-)a/t/db_dependent/Authorities/MergeRequests.t (-1 / +44 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
use Modern::Perl;
4
use Test::More tests => 1;
5
6
use Koha::Authority::MergeRequest;
7
use Koha::Authority::MergeRequests;
8
use Koha::Database;
9
use Koha::DateUtils qw/dt_from_string/;
10
11
my $schema  = Koha::Database->new->schema;
12
$schema->storage->txn_begin;
13
14
subtest "Tests for cron_cleanup" => sub {
15
    plan tests => 3;
16
17
    my $dt = dt_from_string;
18
    $dt->subtract( hours => 2 );
19
    my $req1 = Koha::Authority::MergeRequest->new({
20
        authid => 1,
21
        done => 2,
22
        timestamp => $dt,
23
    })->store;
24
25
    $dt->subtract( days => 30 );
26
    my $req2 = Koha::Authority::MergeRequest->new({
27
        authid => 2,
28
        done => 1,
29
        timestamp => $dt,
30
    })->store;
31
32
    # Now test two cleanup calls
33
    # First call should only remove req2; second call should reset req1
34
    Koha::Authority::MergeRequests->cron_cleanup({ reset_hours => 3 });
35
    $req1->discard_changes; # requery
36
    is( $req1->done, 2, 'My request was not touched' );
37
    $req2->discard_changes; # requery
38
    is( $req2->in_storage, 0, 'Second request removed' );
39
    Koha::Authority::MergeRequests->cron_cleanup({ reset_hours => 1 });
40
    $req1->discard_changes; # requery
41
    is( $req1->done, 0, 'Yes, we got a reset' );
42
};
43
44
$schema->storage->txn_rollback;

Return to bug 9988