diff --git a/wikibot.py b/wikibot.py index 13d67a07640a31288bb0969bc18085e935ea1017..5b66fd146c5a9d68bfbfb03d0edb44397291c6d6 100644 --- a/wikibot.py +++ b/wikibot.py @@ -4,45 +4,92 @@ from xmlrpc.client import ServerProxy as Proxy, Error as wikiError import re import datetime + def move_page(original_page, new_page, delete=False): - """Moves a page, updates link to new page and flags or deletes the old one""" - 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}) + """Moves a page, updates link to new page and flags or deletes the old one""" + 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}) + change_links(original_page, new_page) # update back links 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}) - # update back links to new page # - change_links(original_page, 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 +def move_pages(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 + 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 + delete is a boolean flag wether old pages are to be deleted or just marked + """ + with Proxy(config.WIKI_API_URL) as proxy: + # move sites # + pages = proxy.dokuwiki.getPagelist("") + update_pages = {page.get("id"): new_name_func(page.get("id")) for page in pages + if re.fullmatch(original_name_regex, page.get("id"))} + for old, new in update_pages.items(): + page_content = proxy.wiki.getPage(old) + proxy.wiki.putPage(new, page_content, {"sum": "Moved from " + old}) + # bulk update for links # + update_backlink_sites = {} + for page in update_pages.keys(): + for link in proxy.wiki.getBackLinks(page): + if update_backlink_sites.get(link): + update_backlink_sites[link].append(page) + else: + update_backlink_sites[link] = [page] + reg = rf"\[\[\s*{{}}\s*\|(.*?)\]\]" + def _replacer(new_name): return ( + lambda matched: "[[" + new_name + "|" + matched.group(1) + "]]") + for page_to_update, old_pages in update_backlink_sites.items(): + page_content = proxy.wiki.getPage(page_to_update) + for old_page in old_pages: + page_content = re.sub(reg.format(old_page), _replacer( + update_pages[old_page]), page_content) + proxy.wiki.putPage(page_to_update, page_content, { + "sum": "Update links due to moving sites"}) + # either flag or delete original pages # + if not delete: + for old_page, new_page in update_pages.items(): + proxy.dokuwiki.appendPage(old_page, "\n\n DELETEME This page was moved to " + new_page, + {"sum": "Moved to " + new_page + " and marked page for deletion"}) + else: + for old_page, new_page in update_pages.items(): + proxy.wiki.putPage( + old_page, "", {"sum": "Moved to " + new_page}) def change_links(old_page, new_page): - """Updates pages that link to old page, to link to new page""" - with Proxy(config.WIKI_API_URL) as proxy: - backLinks = proxy.wiki.getBackLinks(old_page) - reg = rf"\[\[\s*{old_page}\s*\|(.*?)\]\]" # regex for dokuwiki links we want to replace - _replacer = lambda matched : "[[" + new_page + "|" + matched.group(1) + "]]" - for page in backLinks: - content = proxy.wiki.getPage(page) - content = re.sub(reg, _replacer, content) - proxy.wiki.putPage(page, content, {"sum":"Update links from " +old_page+ " to " + new_page}) + """Updates pages that link to old page, to link to new page""" + with Proxy(config.WIKI_API_URL) as proxy: + backLinks = proxy.wiki.getBackLinks(old_page) + # regex for dokuwiki links we want to replace + reg = rf"\[\[\s*{old_page}\s*\|(.*?)\]\]" + def _replacer( + matched): return "[[" + new_page + "|" + matched.group(1) + "]]" + for page in backLinks: + content = proxy.wiki.getPage(page) + content = re.sub(reg, _replacer, content) + proxy.wiki.putPage( + page, content, {"sum": "Update links from " + old_page + " to " + new_page}) def find_old_pages(timedelta, namespace=""): - """Returns all pages whose rev is older than the given timedelta""" - with Proxy(config.WIKI_API_URL) as proxy: - pages = proxy.dokuwiki.getPagelist(namespace) - proxy_time = proxy.dokuwiki.getTime() - old_pages = [] - if type(timedelta) is datetime.timedelta: - old_pages = [page for page in pages if proxy_time - page.get('rev') > timedelta.total_seconds()] - elif type(timedelta) is datetime.datetime: - old_pages = [page for page in pages if page.get('rev') < timedelta.timestamp()] - return old_pages - + """Returns all pages whose rev is older than the given timedelta""" + with Proxy(config.WIKI_API_URL) as proxy: + pages = proxy.dokuwiki.getPagelist(namespace) + proxy_time = proxy.dokuwiki.getTime() + old_pages = [] + if type(timedelta) is datetime.timedelta: + old_pages = [page for page in pages if proxy_time - + page.get('rev') > timedelta.total_seconds()] + elif type(timedelta) is datetime.datetime: + old_pages = [page for page in pages if page.get( + 'rev') < timedelta.timestamp()] + return old_pages