Skip to content
Snippets Groups Projects
Commit 13dfc3ce authored by Magnus Giesbert's avatar Magnus Giesbert
Browse files

Use a global ServerProxy

Use a global regex for wiki-links
add function to get pages based on regex
rename move_pages
parent bf0b5686
No related branches found
No related tags found
No related merge requests found
...@@ -5,10 +5,13 @@ import datetime ...@@ -5,10 +5,13 @@ import datetime
import argparse import argparse
from xmlrpc.client import ServerProxy as Proxy, Error as wikiError from xmlrpc.client import ServerProxy as Proxy, Error as wikiError
# regex to change a links target while keeping its display name
WIKILINKREG = rf"\[\[\s*{{}}\s*\|(.*?)\]\]"
proxy = Proxy(config.WIKI_API_URL) # TODO
def get_time(): def get_time():
"""Return the dokwuikis current time as timestamp.""" """Return the dokwuikis current time as timestamp."""
with Proxy(config.WIKI_API_URL) as proxy:
return proxy.dokuwiki.getTime() return proxy.dokuwiki.getTime()
...@@ -25,7 +28,6 @@ def strip_namespace(full_name): ...@@ -25,7 +28,6 @@ def strip_namespace(full_name):
def move_page(old_pagename, new_pagename, delete=False): def move_page(old_pagename, new_pagename, delete=False):
"""Moves a page, updates links to new page and flags or deletes the old one.""" """Moves a page, updates links to new page and flags or deletes the old one."""
with Proxy(config.WIKI_API_URL) as proxy:
# create new page with identical content # # create new page with identical content #
page_content = proxy.wiki.getPage(old_pagename) page_content = proxy.wiki.getPage(old_pagename)
if not page_content: if not page_content:
...@@ -43,17 +45,24 @@ def move_page(old_pagename, new_pagename, delete=False): ...@@ -43,17 +45,24 @@ def move_page(old_pagename, new_pagename, delete=False):
"sum": "Moved to " + new_pagename}) "sum": "Moved to " + new_pagename})
def move_pages(original_name_regex, new_name_func, delete=False): def get_pages(name_regex):
""" Returns all pages which names completly match a given regex."""
pages = proxy.dokuwiki.getPagelist("")
update_pages = {page for page in pages
if re.fullmatch(name_regex, page.get("id"))}
return update_pages
def move_pages_regex(original_name_regex, new_name_func, delete=False):
""" Moves a bunch of pages where the complete name is matched by a given regex and new names are given via a function. """ Moves a bunch of pages where the complete name is matched by a given regex and new names are given via a function.
original_name_regex is a regular expression which the old names have to match completly. original_name_regex is a regular expression which the old names have to match completly.
new_name_func is a function which gets the matched old name as input and returns the new name. new_name_func is a function which gets the matched old name as input and returns the new name.
delete is a boolean flag wether old pages are to be deleted or just marked. delete is a boolean flag wether old pages are to be deleted or just marked.
""" """
with Proxy(config.WIKI_API_URL) as proxy: pages = get_pages(original_name_regex)
# move sites # # move pages #
pages = proxy.dokuwiki.getPagelist("") update_pages = {page.get("id"): new_name_func(
update_pages = {page.get("id"): new_name_func(page.get("id")) for page in pages page.get("id")) for page in pages}
if re.fullmatch(original_name_regex, page.get("id"))}
for old, new in update_pages.items(): for old, new in update_pages.items():
page_content = proxy.wiki.getPage(old) page_content = proxy.wiki.getPage(old)
proxy.wiki.putPage(new, page_content, {"sum": "Moved from " + old}) proxy.wiki.putPage(new, page_content, {"sum": "Moved from " + old})
...@@ -65,13 +74,12 @@ def move_pages(original_name_regex, new_name_func, delete=False): ...@@ -65,13 +74,12 @@ def move_pages(original_name_regex, new_name_func, delete=False):
update_backlink_sites[link].append(page) update_backlink_sites[link].append(page)
else: else:
update_backlink_sites[link] = [page] update_backlink_sites[link] = [page]
reg = rf"\[\[\s*{{}}\s*\|(.*?)\]\]"
def _replacer(new_name): return ( def _replacer(new_name): return (
lambda matched: "[[" + new_name + "|" + matched.group(1) + "]]") lambda matched: "[[" + new_name + "|" + matched.group(1) + "]]")
for page_to_update, old_pages in update_backlink_sites.items(): for page_to_update, old_pages in update_backlink_sites.items():
page_content = proxy.wiki.getPage(page_to_update) page_content = proxy.wiki.getPage(page_to_update)
for old_page in old_pages: for old_page in old_pages:
page_content = re.sub(reg.format(old_page), _replacer( page_content = re.sub(WIKILINKREG.format(old_page), _replacer(
update_pages[old_page]), page_content) update_pages[old_page]), page_content)
proxy.wiki.putPage(page_to_update, page_content, { proxy.wiki.putPage(page_to_update, page_content, {
"sum": "Update links due to moving sites"}) "sum": "Update links due to moving sites"})
...@@ -88,10 +96,9 @@ def move_pages(original_name_regex, new_name_func, delete=False): ...@@ -88,10 +96,9 @@ def move_pages(original_name_regex, new_name_func, delete=False):
def change_links(old_pagename, new_pagename): def change_links(old_pagename, new_pagename):
"""Updates pages that link to an old page, to link to new page instead.""" """Updates pages that link to an old page, to link to new page instead."""
with Proxy(config.WIKI_API_URL) as proxy:
backLinks = proxy.wiki.getBackLinks(old_pagename) backLinks = proxy.wiki.getBackLinks(old_pagename)
# regex for dokuwiki links we want to replace # regex for dokuwiki links we want to replace
reg = rf"\[\[\s*{old_pagename}\s*\|(.*?)\]\]" reg = WIKILINKREG.format(old_pagename)
def _replacer(matched): def _replacer(matched):
return "[[" + new_pagename + "|" + matched.group(1) + "]]" return "[[" + new_pagename + "|" + matched.group(1) + "]]"
...@@ -107,7 +114,6 @@ def find_old_pages(timedelta, namespace=""): ...@@ -107,7 +114,6 @@ def find_old_pages(timedelta, namespace=""):
Used timedelta can be given as datetime.datetime or datetime.timedelta object. Used timedelta can be given as datetime.datetime or datetime.timedelta object.
Optional a namespace can be given to only get old pages from that namespace. Optional a namespace can be given to only get old pages from that namespace.
""" """
with Proxy(config.WIKI_API_URL) as proxy:
pages = proxy.dokuwiki.getPagelist(namespace) pages = proxy.dokuwiki.getPagelist(namespace)
proxy_time = proxy.dokuwiki.getTime() proxy_time = proxy.dokuwiki.getTime()
old_pages = [] old_pages = []
...@@ -122,7 +128,6 @@ def find_old_pages(timedelta, namespace=""): ...@@ -122,7 +128,6 @@ def find_old_pages(timedelta, namespace=""):
def mark_page(pagename, message, summary="Marked page"): def mark_page(pagename, message, summary="Marked page"):
"""Puts a message at the beginning of a page""" """Puts a message at the beginning of a page"""
with Proxy(config.WIKI_API_URL) as proxy:
content = proxy.wiki.getPage(pagename) content = proxy.wiki.getPage(pagename)
proxy.wiki.putPage(pagename, message+"\n\n"+content, {"sum": summary}) proxy.wiki.putPage(pagename, message+"\n\n"+content, {"sum": summary})
...@@ -135,7 +140,6 @@ def mark_old_pages(timedelta, namespace=""): ...@@ -135,7 +140,6 @@ def mark_old_pages(timedelta, namespace=""):
old_pages = find_old_pages(timedelta, namespace) old_pages = find_old_pages(timedelta, namespace)
message = "FIXME This Page is out of date and should be reviewed\n\n" message = "FIXME This Page is out of date and should be reviewed\n\n"
summary = "Marked page due to its age" summary = "Marked page due to its age"
with Proxy(config.WIKI_API_URL) as proxy:
for page in old_pages: for page in old_pages:
if not proxy.wiki.getPage(page.get("id")).startswith("FIXME"): if not proxy.wiki.getPage(page.get("id")).startswith("FIXME"):
mark_page(page.get("id"), message, summary) mark_page(page.get("id"), message, summary)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment