From 403c86e594bbc44f88f8d8b8ceaf4904c60c408d Mon Sep 17 00:00:00 2001 From: Magnus Giesbert <magnus@fsmpi.rwth-aachen.de> Date: Thu, 28 Jul 2022 21:18:40 +0200 Subject: [PATCH] Add new file --- wikibot.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 wikibot.py diff --git a/wikibot.py b/wikibot.py new file mode 100644 index 0000000..6a2814c --- /dev/null +++ b/wikibot.py @@ -0,0 +1,27 @@ +# documentation of dokuwiki xmlrpc commands: https://www.dokuwiki.org/devel:xmlrpc +import config +from xmlrpc.client import ServerProxy as Proxy, Error as wikiError +import re + +# rename/move a page +def move_page(original_page, new_page, delete=False): + with Proxy(config.WIKI_API_URL) as proxy: + # create new page with identical content # + page_content = proxy.wiki.getPage(original_page) + proxy.wiki.putPage(new_page, page_content, {"sum":"Moved from " + original_page}) + + # update back links to new page # + backLinks = proxy.wiki.getBackLinks(original_page) + reg = rf"\[\[\s*{original_page}\s*\|(.*?)\]\]" # regex for dokuwiki links we want to replace + _replacer = lambda matched : "[[" + new_page + "|" + matched.group(1) + "]]" # replace function + for page in backLinks: + content = proxy.wiki.getPage(page) + content = re.sub(reg, _replacer, content) + proxy.wiki.putPage(page, content, {"sum":"Update links from " +original_page+ " to " + new_page}) + + # either flag or delete original page # + if not delete: + proxy.dokuwiki.appendPage(original_page, "\n DELETEME This page was moved to "+ new_page, + {"sum":"Moved to " + new_page + " and marked page for deletion"}) + else: + proxy.wiki.putPage(original_page, "", {"sum":"Moved to " + new_page}) # deletes original page -- GitLab