0

so I have written a small python script to write the content of a .txt file to a XWiki page. However, currently it just replaces the current content of the page with the new content. Is there a way to, instead of replacing the content, join/merge the new and old content, so that the old page content stays and the new page content just gets added to it?

Here is my code for reference:

import requests

url = "http://server:port/xwiki/rest/wikis/xwiki/spaces/Sandbox/pages/TestPage1/"
file_path = r"example.txt"

username = "username"
password = "password"

headers = {
    "Content-Type": "text/plain"
}

with open(file_path, "rb") as file:
    response = requests.put(url, headers=headers, data=file, auth=(username, password))

if response.status_code == 201:
    print("The Page was successfully created.")
elif response.status_code == 202:
    print("The page was successfully updated.")
elif response.status_code == 304:
    print("The Page was not modified.")
elif response.status_code == 401:
    print("The user is not authorized.")
else:
    print("Error uploading the data:", response.status_code)

I looked through the official XWiki RESTful API docs 3 times now but didn't find anything regarding merging/joining old and new content but maybe I missed something in my 3 times?

I would really appreciate any tips!

IShafigh
  • 1
  • 1

1 Answers1

0

This sounds very similar to an existing XWiki feature called "merge on save" (https://www.xwiki.org/xwiki/bin/view/ReleaseNotes/Data/XWiki/11.5RC1/Change007/) which basically merges your currently saved content in the case where the document you had opened in the editor was modified by someone else in the meantime.

Unfortunately, this is not exposed as a REST API, but it's part of the SaveAction.java that is called by the editor itself when saving. You might want to have to poke around in the code or use the Network Inspector in the browser to understand how to replicate such a call to XWiki from your python script.

An alternative approach which might be simpler for you, in some regards, might be to do the work in your python script instead, before finally pushing the new XWiki page content via the REST API. What you are actually looking for is doing what is called a "3-way merge" between the XWiki page's previous. Here's a nice thread on the topic, for more context: Why is a 3-way merge advantageous over a 2-way merge?

To make life easier, you'd be looking for a python library that does the merging for you and on a quick search, we can find these 2 examples:

Now you can go ahead, pick a library, and feed it the 3 versions of the wiki page you want to update:

  1. base version: this depends on what you have used as a starting point for your new version's content. It can be either the current wiki page content or the content of an older version.
  2. current version: pretty clear
  3. new version: you new version that you want to push to the page

The library's output should be the merged output between the current version and your new one. If your new content was based on the current version, it will be the easiest case and you will not have any conflicts. If the base was further down the versions history, you could end up with conflicts you need to resolve or you could even decide to push the content with conflicts that would be resolved in the XWiki editor instead.

With this approach you would be redoing what XWiki already does, but you would be using the documented REST API to retrieve the page content of the current version (or an older one, if needed) and also to push the new (merged) content to the wiki page. So this could be considered to be "cleaner", as it currently stands.

Also remember that XWiki Rest API mentions:

In addition to retrieving content in XML format, you can also retrieve it in JSON format by adding the parameter ?media=json in the URL. For example: http://localhost:8080/xwiki/rest/wikis?media=json

...and that a GET retrieves the entire XWiki page (i.e. with all metadata/attributes, among which the "content" as well) and that, symmetrically, a PUT request requires you submit the entire XWiki page as well (i.e. with all attributes and the modified "content" attribute). It's up to you to use either XML or JSON to do this. Consult the documentation for more: https://www.xwiki.org/xwiki/bin/view/Documentation/UserGuide/Features/XWikiRESTfulAPI#HFormatsoffiles

Hope this helps.

Eduard Moraru
  • 770
  • 4
  • 9