Versioner sammenlignet

Nøgle

  • Linjen blev tilføjet.
  • Denne linje blev fjernet.
  • Formatering blev ændret.

...

Kodeblok
1st page under sandbox:

https://funky.mos-eisley.dk/rest/wikis/xwiki/spaces/Sandbox/spaces/AtlassianHome/pages/sub1

Sub parge for Page 1:

https://funky.mos-eisley.dk/rest/wikis/xwiki/spaces/Sandbox/spaces/sub1/pages/sub2WebHome - page under Sandbox

Sub pargepage "Page2" for Page 2AtlassianHome:

https://funky.mos-eisley.dk/rest/wikis/xwiki/spaces/Sandbox/spaces/sub1AtlassianHome/spaces/sub2Page2/pages/sub3WebHome

etc etc


Migration script (WIP):

Kodeblok
from atlassian import Confluence
from atlassian import Confluence
import convert
import requests
from requests.auth import HTTPBasicAuth
from xml.sax.saxutils import escape
import os
import config

confluence = Confluence(
    url=config.conflunce_url,
    username=config.username,
    password=config.password)

def create_xwki_page(confluence_page_id,title,parent_url,content):

    slug = title.replace(" ","")
    url = parent_url + "/spaces/" + slug + "/pages/WebHome"

    #print (title)
    print (url)
    
    xml = "<page xmlns=\"http://www.xwiki.org\">"
    xml = xml + "<title>" + title + "</title>"
    xml = xml + "<syntax>xwiki/2.1</syntax>"
    xml = xml + "<content>" +  escape(content) + "</content></page>"

    headers = {
        "Content-Type": "application/xml"
    }

    response = requests.put(url, data=xml, headers=headers, auth=HTTPBasicAuth(config.username,config.password))

    #Check the response
    if response.status_code == 201:
        print("Page " + title + " created")
    else:
        if response.status_code == 202:
            print("Page " + title + " updated")
        else:
            print("Status code:", response.status_code)
            print("Response:", response.text)

    #exit(0)

    #Children
    children = confluence.get_page_child_by_type(confluence_page_id, type='page', start=None, limit=None, expand=None)
    for page in children:

        page_id = page["id"]

        page1 = confluence.get_page_by_id(page_id=page_id,expand="body.storage")
        content = page1["body"]["storage"]["value"]
        title = page['title']

        #Labels
        #labels = confluence.get_page_labels(page_id, prefix=None, start=None, limit=None)
        #if labels:
        #    attach_labels_to_page(wiki_space,slug,labels)

        #Attachments
        directory = "attachments/" + page_id
        os.makedirs(directory, exist_ok=True)
        (result, log) = convert.convert(content)

        xwiki_page = create_xwki_page(page_id,title,parent_url + "/spaces/" + slug,result)
        #exit(0)
        #attachments = confluence.download_attachments_from_page(confluence_page_id, path=directory)
    
        #with os.scandir(directory) as entries:
        #    for entry in entries:
        #        if entry.is_file():
        #            attach_file_to_page(wiki_space,slug,entry.path,entry.name)




def attach_file_to_page(wiki_space,slug,file_path,filename):

    url = config.xwiki_url + "/rest/wikis/xwiki/spaces/" + wiki_space + "/pages/" + slug + "/attachments/" + filename

    headers = {
        "Content-Type": "application/xml",
        "XWiki-AttachmentComment": "Uploaded by migrator"
    }
    
    with open(file_path, "rb") as f:
        #files = {"file": f}  # 
        response = requests.put(url, data=f, headers=headers, auth=HTTPBasicAuth(config.username,config.password))

    # Check the response
    if response.status_code == 201:
        print("Attachment " + filename + " created")
    else:
        if response.status_code == 202:
            print("Attachment " + filename + " updated")
        else:
            print("Status code:", response.status_code)
            print("Response:", response.text)

def attach_labels_to_page(wiki_space,slug,labels):

    url = config.xwiki_url + "/rest/wikis/xwiki/spaces/" + wiki_space + "/pages/" + slug + "/tags"

    headers = {
        "Content-Type": "application/xml",
    }

    xml = "<tags xmlns=\"http://www.xwiki.org\">"

    for label in labels:
        
        xml = xml + "<tag>" + label + "</tag>"

    xml = xml + "</tag>"

    response = requests.put(url, data=xml, headers=headers, auth=HTTPBasicAuth(config.username,config.password))

    # Check the response
    if response.status_code == 201:
        print("Page " + title + " created")
    else:
        if response.status_code == 202:
            print("Page " + title + " updated")
        else:
            print("Status code:", response.status_code)
            print("Response:", response.text)


#Start on the XWiki
wiki_space = "Sandbox" 

#Start Space in Confluence
space_key = "ATLASSIAN"

space = confluence.get_space(space_key, expand='description.plain,homepage')
homepage_id=space['homepage']['id']
prop = confluence.get_page_by_id(homepage_id,expand="body.storage")
content = prop["body"]["storage"]["value"]
title = prop['title']
slug = title.replace(" ","")

(result, log) = convert.convert(content)

xwiki_page = create_xwki_page(homepage_id,title, config.xwiki_url + "/rest/wikis/xwiki/spaces/Sandbox",result)

...