Unable to manage pages created during migration from Confluence with refactoring service

Hi!

I’m having another non-typical use case. I want to restructure the navigation tree of a bunch of pages and since from the UI point of view, it is not possible, I am using $services.refactoring service, but it seems it works only for pages created via XWiki UI and, for some reason, skips all the pages that have been created during migration.

At first I’ve started with Ruby script to achieve my goal and it worked for locally crated branches, but completely skipped these created during migration. So I’ve tried with Velocity, but overall it behaves the same.

Do you have any clue why it may work this way and how to fix it? I would really appreciate not doing manual restructure through WEB UI of all the branches.

A piece of Ruby script to look at:

destination = $services.get('model').resolveSpace(destination_space_name)
source_doc.getChildren.each do |c|
  source = $services.get('model').resolveSpace(c.gsub(/.WebHome$/, ''))
  destination_document = [destination_space_name, source.getName(), "WebHome"].join(".")

  doc = $xwiki.getDocument(destination_document)
  if(!doc.isNew() || doc.isHidden())
    puts "Document '#{doc.getFullName()}' already exists, skipping..."
    next
  end
  puts "Copying '#{source}' to '#{destination}'..."
  # Copy is used intentionally in order to preserve old docs
  $services.get('refactoring').copy(source, destination).join()
end

A piece of Velocity script:

#set($destination = $services.get('model').resolveSpace($destination_space_name))
#foreach($c in $source_doc.getChildren())
  #set($source = $services.model.resolveSpace($c.replaceAll(".WebHome$", "")))
  #"Copying '${source}' to '${destination}'..."
  $services.refactoring.copy($source, $destination).join()
#end

FWIW, if I perform check with isNew against verifier_document (the destination document in this case), it always returns true…

  $services.get('refactoring').copy(source, destination).join()
  verifier_document = [destination_space_name, source.getName()].join(".")
  d = $xwiki.getDocument(verifier_document)
  p d.isNew()

So it seems $services.get('refactoring') API does not work in my case for some reason…
Ended up with a half-manual process with document creation and copying important attributes:

  ref = $services.get('model').createDocumentReference(
    $services.get('wiki').getCurrentWikiDescriptor.getId(),
    destination_space_name,
    source.getName()
  )
  d = $xwiki.getDocument(ref)
  d.setContent(src_doc.getContent())
  d.setTitle(src_doc.getTitle())
  src_doc.getAttachmentList.each do |attachment|
    d.addAttachment(attachment.getFilename(), attachment.getContentInputStream())
  end
  d.save()

If anyone has better idea, feel free to share.