Moving docs without backlink

Hi all,
I’m trying to use refactoring module to move a lot of documents form a space to another; it’s working with these 3 lines:
> #set ($source = $services.model.resolveDocument(‘MyoldDoc’))
> #set ($destination = $services.model.resolveSpace(‘MynewDoc’))
> $services.refactoring.move($source, $destination).join

But it leave a backlink of the old page, and I don’t want it.

There is a way like in the rename process to avoid backlink to the old pages?

Thanks

Hi,

You can use a Move Request to have more control on the result, see https://extensions.xwiki.org/xwiki/bin/view/Extension/Refactoring%20Module#HMoveapagewithouttheautomaticcorrectionofbacklinkstotargetthenewlocation

The option you want is, I believe, https://www.xwiki.org/xwiki/bin/view/ScriptingDocumentation/?url=http:%2F%2Fnexus.xwiki.org%2Fnexus%2Fservice%2Flocal%2Frepositories%2Fpublic%2Farchive%2Forg%2Fxwiki%2Fplatform%2Fxwiki-platform-refactoring-api%2F13.10.3%2Fxwiki-platform-refactoring-api-13.10.3-javadoc.jar%2F!%2Forg%2Fxwiki%2Frefactoring%2Fjob%2FMoveRequest.html%23setAutoRedirect-boolean-

Funny thing, the documentation about refactoring module was updated yesterday, the previous version doesn’t have this detail.

Anyway thanks, I’ll try these.

It’s not funny, it’s the way to answer questions properly :slight_smile: (even if it could have been mentioned that the doc was updated), see https://dev.xwiki.org/xwiki/bin/view/Community/Contributing#HStrategiesforansweringquestions

1 Like

Thanks @vmassol , I got it, well done.

Unfortunately the move option still not working wiht these options.
I found these:

## **Move a page without the automatic correction of backlinks to target the new location**
#set ($source = $services.model.resolveDocument(‘Path.To.Source.WebHome’)) 
#set ($destination = **$services.model.resolveDocument**(‘Path.To.Destination.WebHome’))
#set ($moveRequest = $services.refactoring.requestFactory.createMoveRequest($source, $destination))
#set ($discard = $renameRequest.setUpdateLinks(false))
$services.refactoring.move($moveRequest)).join()

With the highligted option in line 2 the move is not working,

The move work with:
$services.model.resolveSpace(‘Path.To.New.Parent’)
as in the regular move, but still, the backlink in the old position is maintened.

I’m using it inside a velocity script, with rel. 15.10.6, if can help.

Hi @fabiod99,

The highlighted option in line 2 is invalid velocity code and you should get an error org.apache.velocity.exception.ParseErrorException: Encountered "*" at xwiki:Space.Page[line 1, column 22].
Then, if you want backlinks updated, you need to use setUpdateLinks(true).
The example in documentation says Move a page WITHOUT the automatic correction of backlinks

Hope it helps,
Alex

HI @acotiuga,
I’ve added the two “**” before and after the function I’m using only to highlight it, I’m not using them inside the script.

What I’m trying to achieve is to move the document to another space and leave no link at the old document location, I’m hoping I won’t come out as rude, but It’s challenging to explain a circumstance this particular.

So, do you get any errors?
There’s nothing wrong with $services.model.resolveDocument(), it will obtain a DocumentReference from a String.
The quote however is unusual in the example. It should be '. Also the quotes in ‘Path.To.New.Parent’ look wrong.

One more thing, I just fixed the example in documentation.
The problem is that the following line creates the $moveRequest object

#set ($moveRequest = $services.refactoring.requestFactory.createMoveRequest($source, $destination))

While the following one tries to use the $renameRequest object that doesn’t exist.

#set ($discard = $renameRequest.setUpdateLinks(false))
$services.refactoring.move($moveRequest)).join()

See https://extensions.xwiki.org/xwiki/bin/view/Extension/Refactoring%20Module?viewer=changes&rev1=31.1&rev2=32.1

1 Like

Thanks @acotiuga,
I hadn’t noticed it before, and I was using the wrong options for link ($moveRequest.setUpdateLinks(false) instead of $moveRequest.setAutoRedirect(false) )
With your improvement I was able to achieve the result with this script:

{{velocity}}
## In query field you have to specify the document name or part of the name
#set ($query = "Documents to move")
## In $newspace remember to keep a dot at the end
#set ($newspace = "path.to.destination.")
## This is fixed to build the path
#set ($webhome = ".WebHome")
## I define this to specify a space where to pick the documents, otherwise documents in all spaces are moved
## If you dont want that, simply remove from the query this part: 'and doc.space like $oldspace'
#set ($oldspace = "'Path.Source%'")
#set ($hql = "select doc.fullName from XWikiDocument as doc where lower(doc.title) like lower(:title) and doc.space like $oldspace")
#set ($results = $services.query.hql($hql).bindValue("title", "%$query%").execute())
#set ($updatedCount = 0)


#if ($results.size() > 0)
  #foreach ($docFullName in $results)
    #set ($doc = $xwiki.getDocument($docFullName))
    #set ($docTitle = $doc.getTitle())
    #set ($source = $services.model.resolveDocument($doc))
    #set ($destination = $services.model.resolveDocument($newspace+$docTitle+$webhome))
    Moving $source -> Source to $destination -> Destination
    #set ($moveRequest = $services.refactoring.requestFactory.createMoveRequest($source, $destination))
	## With this option you move the document without setting up a HTTP redirect from the old location to the new location
    #set ($discard = $moveRequest.setAutoRedirect(false))
	## With this option you move the document without the automatic correction of backlinks to target the new location
    ## #set ($discard = $moveRequest.setUpdateLinks(false))
    $services.refactoring.move($moveRequest)).join()
    #set ($updatedCount = $updatedCount + 1)
  #end
  Moved documents count: $updatedCount
#else
  No pages found with $query
#end
{{/velocity}}

Thanks again for you support

1 Like