Sidehistorik
...
| 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 processcreate_allxwki_childrenpage(confluence_page_id,title,parent_url,content): slug return= title.replace("" def create_xwki_page(wiki_space,title,parent,content): ","") url = config.xwikiparent_url + "/rest/wikis/xwiki/spaces/" + wiki_spaceslug + "/pages/WebHome" +#print title.replace(" ",""(title) print (url) xml = "<page xmlns=\"http://www.xwiki.org\"><title>" + title + "</title>>" xml = xml + "<parent><title>" + wiki_space + "." + parent.replace(" ","")title + "</parent>title>" xml = xml + "<syntax>xwiki/2.1</syntax>" xml = xml + "<content>" + escape(content) + "</content></page>" #print (xml) headers = { "Content-Type": "application/xml" } response = requests.put(url, data=xml, headers=headers, auth=HTTPBasicAuth(config.username,config.password)) # Check#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,titleslug,file_path,filename): url = config.xwiki_url + "/rest/wikis/xwiki/spaces/" + wiki_space + "/pages/" + title.replace(" ","")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) #Start on the XWiki def attach_labels_to_page(wiki_space = "Sandbox" #Start Space in Confluence space_key = "ATLASSIAN" space = confluence.get_space(space_key, expand='description.plain,homepage') homepageid=space['homepage']['id'] prop = confluence.get_page_by_id(homepageid,expand="body.storage") content = prop["body"]["storage"]["value"] home_title = prop['title'] (result, log) = convert.convert(content) xwiki_page = create_xwki_page(wiki_space,home_title,"webHome",result) parent = home_title children = confluence.get_page_child_by_type(homepageid, 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 #confluence.get_page_labels(page_id, prefix=None, start=None, limit=None) #Attachments directory = "attachments/" + page_id os.makedirs(directory, exist_ok=True) (result, log) = convert.convert(content) xwiki_page = create_xwki_page(wiki_space,title,parent,result) #exit(0) attachments = confluence.download_attachments_from_page(page_id, path=directory) with os.scandir(directory) as entries: ,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: for entry in entries:print("Status code:", response.status_code) if entry.is_file(): attach_file_to_page(wiki_space,title,entry.path,entry.name) parent = titleprint("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) |