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

(-)a/C4/AuthoritiesMarc.pm (-5 / +62 lines)
Lines 685-691 sub AddAuthority { Link Here
685
        )->store();
685
        )->store();
686
        $authority->discard_changes();
686
        $authority->discard_changes();
687
        $authid = $authority->authid;
687
        $authid = $authority->authid;
688
        logaction( "AUTHORITIES", "ADD", $authid, "authority" ) if C4::Context->preference("AuthoritiesLog");
689
    } else {
688
    } else {
690
        $action    = 'modify';
689
        $action    = 'modify';
691
        $authority = Koha::Authorities->find($authid);
690
        $authority = Koha::Authorities->find($authid);
Lines 715-720 sub AddAuthority { Link Here
715
    }
714
    }
716
715
717
    _after_authority_action_hooks( { action => $action, authority_id => $authid } );
716
    _after_authority_action_hooks( { action => $action, authority_id => $authid } );
717
718
    if ( $action eq 'create' && C4::Context->preference("AuthoritiesLog") ) {
719
        $authority->discard_changes;
720
        logaction(
721
            "AUTHORITIES", "ADD", $authid, "authority", undef,
722
            _authority_log_data( $authority, $record )
723
        );
724
    }
725
718
    return ($authid);
726
    return ($authid);
719
}
727
}
720
728
Lines 744-750 sub DelAuthority { Link Here
744
        Koha::Authority::MergeRequests->search($condition)->delete;
752
        Koha::Authority::MergeRequests->search($condition)->delete;
745
        merge( { mergefrom => $authid } ) if !$skip_merge;
753
        merge( { mergefrom => $authid } ) if !$skip_merge;
746
754
747
        my $authority = Koha::Authorities->find($authid);
755
        my $authority    = Koha::Authorities->find($authid);
756
        my $deleted_marc = eval { $authority->record } if C4::Context->preference("AuthoritiesLog");
757
        my $deleted_data =
758
            C4::Context->preference("AuthoritiesLog")
759
            ? _authority_log_data( $authority, $deleted_marc )
760
            : undef;
761
748
        $schema->txn_do(
762
        $schema->txn_do(
749
            sub {
763
            sub {
750
                $authority->move_to_deleted;    #FIXME We should define 'move' ..
764
                $authority->move_to_deleted;    #FIXME We should define 'move' ..
Lines 752-758 sub DelAuthority { Link Here
752
            }
766
            }
753
        );
767
        );
754
768
755
        logaction( "AUTHORITIES", "DELETE", $authid, "authority" ) if C4::Context->preference("AuthoritiesLog");
769
        logaction( "AUTHORITIES", "DELETE", $authid, "authority", undef, $deleted_data )
770
            if C4::Context->preference("AuthoritiesLog");
756
        unless ($skip_record_index) {
771
        unless ($skip_record_index) {
757
            my $indexer = Koha::SearchEngine::Indexer->new( { index => $Koha::SearchEngine::AUTHORITIES_INDEX } );
772
            my $indexer = Koha::SearchEngine::Indexer->new( { index => $Koha::SearchEngine::AUTHORITIES_INDEX } );
758
            $indexer->index_records( $authid, "recordDelete", "authorityserver", undef );
773
            $indexer->index_records( $authid, "recordDelete", "authorityserver", undef );
Lines 784-796 sub ModAuthority { Link Here
784
    my $skip_record_index = $params->{skip_record_index} || 0;
799
    my $skip_record_index = $params->{skip_record_index} || 0;
785
800
786
    my $oldrecord = GetAuthority($authid);
801
    my $oldrecord = GetAuthority($authid);
802
    my ( $original_data, $original_marc );
803
    if ( C4::Context->preference("AuthoritiesLog") ) {
804
        my $old_authority = Koha::Authorities->find($authid);
805
        $original_data = _authority_log_data( $old_authority, $oldrecord );
806
    }
787
807
788
    #Now rewrite the $record to table with an add
808
    #Now rewrite the $record to table with an add
789
    $authid = AddAuthority( $record, $authid, $authtypecode, { skip_record_index => $skip_record_index } );
809
    $authid = AddAuthority( $record, $authid, $authtypecode, { skip_record_index => $skip_record_index } );
790
    merge( { mergefrom => $authid, MARCfrom => $oldrecord, mergeto => $authid, MARCto => $record } )
810
    merge( { mergefrom => $authid, MARCfrom => $oldrecord, mergeto => $authid, MARCto => $record } )
791
        if !$params->{skip_merge};
811
        if !$params->{skip_merge};
792
    logaction( "AUTHORITIES", "MODIFY", $authid, "authority BEFORE=>" . $oldrecord->as_formatted )
812
793
        if C4::Context->preference("AuthoritiesLog");
813
    if ( C4::Context->preference("AuthoritiesLog") ) {
814
        my $new_authority = Koha::Authorities->find($authid);
815
        logaction(
816
            "AUTHORITIES", "MODIFY", $authid,
817
            _authority_log_data( $new_authority, $record ),
818
            undef, $original_data
819
        );
820
    }
821
794
    return $authid;
822
    return $authid;
795
}
823
}
796
824
Lines 1495-1500 sub _get_authid_subfield { Link Here
1495
    return $field->subfield('9') || $field->subfield('3');
1523
    return $field->subfield('9') || $field->subfield('3');
1496
}
1524
}
1497
1525
1526
sub _marc_record_to_diffable {
1527
    my ($record) = @_;
1528
    return {} unless defined $record;
1529
    my %marc;
1530
    for my $field ( $record->fields ) {
1531
        my $tag = $field->tag;
1532
        if ( $field->is_control_field ) {
1533
            $marc{$tag} = $field->data;
1534
        } else {
1535
            my $ind1      = $field->indicator(1);
1536
            my $ind2      = $field->indicator(2);
1537
            my $formatted = "$ind1$ind2";
1538
            for my $subfield ( $field->subfields ) {
1539
                $formatted .= " \$$subfield->[0] $subfield->[1]";
1540
            }
1541
            push @{ $marc{$tag} }, $formatted;
1542
        }
1543
    }
1544
    return \%marc;
1545
}
1546
1547
sub _authority_log_data {
1548
    my ( $authority, $marc_record ) = @_;
1549
    my %data = %{ $authority->unblessed };
1550
    delete $data{marcxml};
1551
    $data{_marc} = _marc_record_to_diffable($marc_record);
1552
    return \%data;
1553
}
1554
1498
=head2 GetHeaderAuthority
1555
=head2 GetHeaderAuthority
1499
1556
1500
  $ref= &GetHeaderAuthority( $authid)
1557
  $ref= &GetHeaderAuthority( $authid)
(-)a/t/db_dependent/AuthoritiesMarc.t (-2 / +48 lines)
Lines 6-12 Link Here
6
use Modern::Perl;
6
use Modern::Perl;
7
7
8
use Test::NoWarnings;
8
use Test::NoWarnings;
9
use Test::More tests => 14;
9
use Test::More tests => 15;
10
use Test::MockModule;
10
use Test::MockModule;
11
use Test::Warn;
11
use Test::Warn;
12
use MARC::Field;
12
use MARC::Field;
Lines 14-19 use MARC::Record; Link Here
14
14
15
use t::lib::Mocks;
15
use t::lib::Mocks;
16
use t::lib::TestBuilder;
16
use t::lib::TestBuilder;
17
use Koha::ActionLogs;
17
use Koha::Database;
18
use Koha::Database;
18
use Koha::Authority::Types;
19
use Koha::Authority::Types;
19
20
Lines 398-400 subtest 'DelAuthority() tests' => sub { Link Here
398
399
399
    $schema->storage->txn_rollback;
400
    $schema->storage->txn_rollback;
400
};
401
};
401
- 
402
403
subtest 'Authority action logs include MARC diff' => sub {
404
    plan tests => 12;
405
406
    $schema->storage->txn_begin;
407
408
    t::lib::Mocks::mock_preference( 'AuthoritiesLog', 1 );
409
410
    my $auth_type = 'GEOGR_NAME';
411
412
    # ADD
413
    my $add_record = MARC::Record->new;
414
    $add_record->add_fields( [ '151', ' ', ' ', a => 'France' ] );
415
    my $auth_id = AddAuthority( $add_record, undef, $auth_type );
416
417
    my $add_logs = Koha::ActionLogs->search( { object => $auth_id, module => 'AUTHORITIES', action => 'ADD' } );
418
    is( $add_logs->count, 1, 'ADD action logged' );
419
    my $add_log = $add_logs->next;
420
    ok( defined $add_log->diff, 'ADD: diff column populated' );
421
    like( $add_log->diff, qr/_marc/,  'ADD: diff contains MARC field data' );
422
    like( $add_log->diff, qr/France/, 'ADD: diff contains heading content' );
423
424
    # MODIFY - change the heading
425
    my $mod_record = MARC::Record->new;
426
    $mod_record->add_fields( [ '151', ' ', ' ', a => 'France (updated)' ] );
427
    ModAuthority( $auth_id, $mod_record, $auth_type, { skip_merge => 1 } );
428
429
    my $mod_logs = Koha::ActionLogs->search( { object => $auth_id, module => 'AUTHORITIES', action => 'MODIFY' } );
430
    is( $mod_logs->count, 1, 'MODIFY action logged' );
431
    my $mod_log = $mod_logs->next;
432
    ok( defined $mod_log->diff, 'MODIFY: diff column populated' );
433
    like( $mod_log->diff, qr/_marc/,            'MODIFY: diff contains MARC field data' );
434
    like( $mod_log->diff, qr/France/,           'MODIFY: diff captures before value' );
435
    like( $mod_log->diff, qr/France.*updated/s, 'MODIFY: diff captures after value' );
436
437
    # DELETE
438
    DelAuthority( { authid => $auth_id, skip_merge => 1 } );
439
440
    my $del_logs = Koha::ActionLogs->search( { object => $auth_id, module => 'AUTHORITIES', action => 'DELETE' } );
441
    is( $del_logs->count, 1, 'DELETE action logged' );
442
    my $del_log = $del_logs->next;
443
    ok( defined $del_log->diff, 'DELETE: diff column populated' );
444
    like( $del_log->diff, qr/_marc/, 'DELETE: diff contains MARC field data' );
445
446
    $schema->storage->txn_rollback;
447
};

Return to bug 42032