Skip to content

Instantly share code, notes, and snippets.

@jesusbagpuss
Created May 16, 2019 11:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jesusbagpuss/ee27acd24a5d0e3fa3d29ef0075d921b to your computer and use it in GitHub Desktop.
Save jesusbagpuss/ee27acd24a5d0e3fa3d29ef0075d921b to your computer and use it in GitHub Desktop.
Symplectic merge items fix for mangled documents
################################################################################
#
# This fix was submitted to Symplectic April 2018
# As yet they haven't made a new release of the connector with it included :o\
#
#
# The changes below consist of:
# - new method in Symplectic::RepoProcess::MergeManager
# - replace two method calls with calls to the new method
#
# - new method in Symplectic::RepoProcess::EPrintManager
# - replace one method call with a call to the new method
#
#
################################################################################
####
# Line 386
####
- my $new_doc = $doc->clone($new_eprint);
+ my $new_doc = $self->clone_document( 'eprint' => $new_eprint, 'doc' => $doc, 'reset_pos' => 1 );
####
# Insert the following somewhere in Symplectic::RepoProcess::EPrintManager
####
################################################################################
# Based on EPrints::DataObj::Document::clone
# NB Code duplication with Symplectic::RepoProcess::MergeManager
#
# Cloning documents can result in:
# - two documents with the same 'pos' field - and therefore sharing the same folder
# - 'spaces' in the document structure (e.g. pos=1 and pos=3, but no pos=2)
# this isn't what is needed. The code below manages these scenarios.
# EPrints' default behaviour is to remove the 'pos' during a clone *only* when the doc is being cloned to the same parent.
sub clone_document
{
my ($self, %args ) = @_;
my $eprint = $args{'eprint'};
my $doc = $args{'doc'};
my $reset_pos = $args{'reset_pos'};
my $data = EPrints::Utils::clone( $doc->{data} );
# cloning within the same eprint, in which case get a new position!
#if( defined $doc->parent && $eprint->id eq $doc->parent->id )
if( ( defined $doc->parent && $eprint->id eq $doc->parent->id ) || $reset_pos )
{
$data->{pos} = undef;
}
$data->{eprintid} = $eprint->get_id;
$data->{_parent} = $eprint;
# First create a new doc object
my $new_doc = $doc->{dataset}->create_object( $doc->{session}, $data );
return undef if !defined $new_doc;
my $ok = 1;
# Copy files
foreach my $file (@{$doc->get_value( "files" )})
{
$file->clone( $new_doc ) or $ok = 0, last;
}
if( !$ok )
{
$new_doc->remove();
return undef;
}
return $new_doc;
}
################################################################################
# End of method to be added
################################################################################
####
# Line 836 and line 884
####
- my $new_doc = $doc->clone($target);
+ my $new_doc = $self->clone_document( 'eprint' => $target, 'doc' => $doc, 'reset_pos' => 1 );
####
# Insert the following somewhere in Symplectic::RepoProcess::MergeManager
####
################################################################################
# Based on EPrints::DataObj::Document::clone
# NB Code duplication with Symplectic::RepoProcess::EPrintManager
#
# Cloning documents can result in:
# - two documents with the same 'pos' field - and therefore sharing the same folder
# - 'spaces' in the document structure (e.g. pos=1 and pos=3, but no pos=2)
# this isn't what is needed. The code below manages these scenarios.
# EPrints' default behaviour is to remove the 'pos' during a clone *only* when the doc is being cloned to the same parent.
sub clone_document
{
my ($self, %args ) = @_;
my $eprint = $args{'eprint'};
my $doc = $args{'doc'};
my $reset_pos = $args{'reset_pos'};
my $data = EPrints::Utils::clone( $doc->{data} );
# cloning within the same eprint, in which case get a new position!
#if( defined $doc->parent && $eprint->id eq $doc->parent->id )
if( ( defined $doc->parent && $eprint->id eq $doc->parent->id ) || $reset_pos )
{
$data->{pos} = undef;
}
$data->{eprintid} = $eprint->get_id;
$data->{_parent} = $eprint;
# First create a new doc object
my $new_doc = $doc->{dataset}->create_object( $doc->{session}, $data );
return undef if !defined $new_doc;
my $ok = 1;
# Copy files
foreach my $file (@{$doc->get_value( "files" )})
{
$file->clone( $new_doc ) or $ok = 0, last;
}
if( !$ok )
{
$new_doc->remove();
return undef;
}
return $new_doc;
}
################################################################################
# End of method to be added
################################################################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment