From 8c85670f9f5599850e56eb3d1b010f12397ba83f Mon Sep 17 00:00:00 2001
From: Magnus Giesbert <magnus@fsmpi.rwth-aachen.de>
Date: Fri, 29 Jul 2022 00:23:56 +0200
Subject: [PATCH] =?UTF-8?q?add=20=C2=B4change=5Flinks=C2=B4=20and=20update?=
 =?UTF-8?q?=20=C2=B4move=5Fpage=C2=B4?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 wikibot.py | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)

diff --git a/wikibot.py b/wikibot.py
index 9f6f9ee..6a38d67 100644
--- a/wikibot.py
+++ b/wikibot.py
@@ -12,13 +12,7 @@ def move_page(original_page, new_page, delete=False):
 		proxy.wiki.putPage(new_page, page_content, {"sum":"Moved from " + original_page})
 
 		# update back links to new page #
-		back_links = 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) + "]]"
-		for page in back_links:
-			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})
+		change_links(original_page, new_page)
 
 		# either flag or delete original page #
 		if not delete:
@@ -28,6 +22,18 @@ def move_page(original_page, new_page, delete=False):
 			proxy.wiki.putPage(original_page, "", {"sum":"Moved to " + new_page}) # deletes original 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})
+
+
 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:
-- 
GitLab