Difficult usage of snippet : "Generate Refactoring Job REST request XML"

My goal is to be able to rename any document from outside of xwiki.
I understood that this is possible through refactoring Job mechanism.

As I need to perform the operation from outside the xwiki, I started from this nice snippet that is simplifying the Xml code generation :

https://snippets.xwiki.org/xwiki/bin/view/Extension/Generate%20Refactoring%20Job%20REST%20request%20XML/

And then I save xml content generated in a file output.xml and call :

curl -i --user "myuser:mypassword" -X PUT -H "Content-Type: text/xml" "http://localhost:8081/xwiki/rest/jobs?jobType=refactoring/move&async=false" --upload-file  output.xml

answer from webservice looks fine (1), except that it performs no change on my xwiki instance. :sob:

I tried to identify problems in job info in my tomcat/work/Catalina/localhost/xwiki/jobs/status/refactoring/*/JOBID directory without success,

So I made some tries(2) but so far no document changed their name from external request.

My conclusion is that maybe I am not able to target my document and request to rename a document that does not exist.
Could you please tell me in that situation (I use extensions Index Tree Macro+Tree Macro) what identify correctly the document place ?
sample

a) var source = new SpaceReference(“xwiki”, “Dossier”, “renaming”);
b) var source = new SpaceReference(“xwiki”, “Main”, “renaming”);
c) var source = new DocumentReference(“xwiki”, “Main”, “renaming”);
d) var source = new DocumentReference(“xwiki”, “Dossier”, “renaming”);

When I renamed a page in the “Dossier” space from interface, I saw in job status log :


<string>destination</string>
        <org.xwiki.model.reference.DocumentReference>
          <name>newDocName</name>
          <parent class="org.xwiki.model.reference.SpaceReference">
            <name>Main</name>
            <parent class="org.xwiki.model.reference.WikiReference">
              <name>xwiki</name>
              <type>WIKI</type>
            </parent>
            <type>SPACE</type>
          </parent>
          <type>DOCUMENT</type>
        </org.xwiki.model.reference.DocumentReference>
      </entry>

but when I use the snippet it generates :


<key>destination</key>
        <value>
            <org.xwiki.model.reference.SpaceReference xmlns="" xmlns:ns2="http://www.xwiki.org">
                <name>newDocName</name>
                <parent class="org.xwiki.model.reference.SpaceReference">
                    <name>Dossier</name>
                    <parent class="org.xwiki.model.reference.WikiReference">
                        <name>xwiki</name>
                        <type>WIKI</type>
                    </parent>
                    <type>SPACE</type>
                </parent>
                <type>SPACE</type>
            </org.xwiki.model.reference.SpaceReference>
        </value>

Maybe Document and Space difference is an issue ?

Thanks a lot in advance!

(1) call to rest jobs endpoint :


HTTP/1.1 200 
Content-Script-Type: text/javascript
Set-Cookie: JSESSIONID=9E66762AAD06F7DF3B821D061FZZZZZZZ; Path=/xwiki; HttpOnly
Date: Wed, 12 Jul 2023 18:12:08 GMT
Accept-Ranges: bytes
Server: Restlet-Framework/2.3.12
XWiki-User: xwiki:XWiki.admin
XWiki-Version: 14.10.13
XWiki-Form-Token: BGiefMG6ciemIwScxxZZZZZ
Content-Type: application/xml;charset=UTF-8
Content-Language: en
Transfer-Encoding: chunked

followed by an xml content that include


<state>FINISHED</state>

(2) some tries :

  • to make use of PageReference or DocumentReference instead of SpaceReference in snippet XML generation
  • to make use of Main as SPACE instead of “Dossier” as the parent Directory
  • to make different call with Rename or Move as JobType parameter and MoveRequest/RenameRequest in snippet…

For those who will be struggling to make use of Rest API for renaming usage, we made some progress on the subject.

We modified snippet (1) as visible in code below.

main changes are :

  • create a renameRequest
  • create source and destination object based on plain vanilla EntityReferences.
  • some properties changes (probably not changing anything)

Impact is that it modifies the xwiki in a way that Document is then accessible from another location.

So far this only change the URL to access document.

Unfortunately the title is still the same so to modify URL and title of a document, I suppose that it will require 2 steps operations while on the Xwiki interface this is apparently made through a “one click operation”.

{{groovy}}
import org.xwiki.model.reference.*;
import org.xwiki.model.*;
import java.util.Collection;
// Create a request to move a page using the standard API
// That's generally the only part you need to customize for your own request need

EntityReference source = new EntityReference("oldName", EntityType.DOCUMENT, new EntityReference("MyParentPage", EntityType.SPACE, new EntityReference("xwiki", EntityType.WIKI)));
EntityReference destination = new EntityReference("newName", EntityType.DOCUMENT, new EntityReference("MyParentPage", EntityType.SPACE, new EntityReference("xwiki", EntityType.WIKI)));

var jobRequest = services.component.getInstance(org.xwiki.refactoring.script.RequestFactory.class).createMoveRequest(source, destination);
/////////// SERIALIZATION ////////////////////
// cleanup the request from properties which make sense for a script but are pretty much useless in the case of a REST request
jobRequest.setInteractive(false);
var restJobRequest = services.component.getInstance(org.xwiki.rest.internal.ModelFactory.class).toRestJobRequest(jobRequest);
var writer = new java.io.StringWriter();
var context = javax.xml.bind.JAXBContext.newInstance("org.xwiki.rest.model.jaxb");
var marshaller = context.createMarshaller();
// Format the XML to make the example more readable but the XML obviously does not need to be formatter in the request
marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(restJobRequest, writer);
println "{{code language='xml'}}"
println writer.toString()
println "{{/code}}"
{{/groovy}}

It could be interesting to have an “all-in-one” jobRequest for such need maybe (or the refactoring move is doing it but it is still difficult to make it work.).

I’ll post later about the way to change title of document when I know more about it.