From df1643cd1df9ff4f5098e2581335be1c671f7a55 Mon Sep 17 00:00:00 2001 From: Markus-Rost Date: Wed, 29 Jul 2020 20:16:41 +0200 Subject: [PATCH] encode link parts to prevent broken links --- src/formatters/rc.py | 60 ++++++++++++++++++++++---------------------- src/misc.py | 5 ++++ 2 files changed, 35 insertions(+), 30 deletions(-) diff --git a/src/formatters/rc.py b/src/formatters/rc.py index 06c882a..783b652 100644 --- a/src/formatters/rc.py +++ b/src/formatters/rc.py @@ -344,9 +344,9 @@ async def embed_formatter(action, change, parsed_comment, categories, recent_cha parsed_comment = _("No description provided") if action != "suppressed": if "anon" in change: - author_url = create_article_path("Special:Contributions/{user}".format(user=change["user"].replace(" ", "_")), WIKI_ARTICLE_PATH) + author_url = create_article_path("Special:Contributions/{user}".format(user=change["user"]), WIKI_ARTICLE_PATH) else: - author_url = create_article_path("User:{}".format(change["user"].replace(" ", "_")), WIKI_ARTICLE_PATH) + author_url = create_article_path("User:{}".format(change["user"]), WIKI_ARTICLE_PATH) embed.set_author(change["user"], author_url) if action in ("edit", "new"): # edit or new page editsize = change["newlen"] - change["oldlen"] @@ -366,7 +366,7 @@ async def embed_formatter(action, change, parsed_comment, categories, recent_cha recent_changes.init_info() link = "{wiki}index.php?title={article}&curid={pageid}&diff={diff}&oldid={oldrev}".format( wiki=WIKI_SCRIPT_PATH, pageid=change["pageid"], diff=change["revid"], oldrev=change["old_revid"], - article=change["title"].replace(" ", "_")) + article=change["title"].replace(" ", "_").replace("%", "%25").replace("\\", "%5C").replace("&", "%26")) embed["title"] = "{redirect}{article} ({new}{minor}{bot}{space}{editsize})".format(redirect="⤷ " if "redirect" in change else "", article=change["title"], editsize="+" + str( editsize) if editsize > 0 else editsize, new=_("(N!) ") if action == "new" else "", minor=_("m") if action == "edit" and "minor" in change else "", bot=_('b') if "bot" in change else "", space=" " if "bot" in change or (action == "edit" and "minor" in change) or action == "new" else "") @@ -406,7 +406,7 @@ async def embed_formatter(action, change, parsed_comment, categories, recent_cha urls = await recent_changes.safe_request( "{wiki}?action=query&format=json&prop=imageinfo&list=&meta=&titles={filename}&iiprop=timestamp%7Curl%7Carchivename&iilimit=5".format( wiki=WIKI_API_PATH, filename=change["title"]), "query", "pages") - link = create_article_path(change["title"].replace(" ", "_"), WIKI_ARTICLE_PATH) + link = create_article_path(change["title"], WIKI_ARTICLE_PATH) additional_info_retrieved = False if urls is not None: logger.debug(urls) @@ -424,7 +424,7 @@ async def embed_formatter(action, change, parsed_comment, categories, recent_cha logger.warning("Request for additional image information have failed. The preview will not be shown.") if action in ("upload/overwrite", "upload/revert"): if additional_info_retrieved: - article_encoded = change["title"].replace(" ", "_") + article_encoded = change["title"].replace(" ", "_").replace("%", "%25").replace("\\", "%5C").replace("&", "%26") try: revision = img_info[num+1] except IndexError: @@ -447,23 +447,23 @@ async def embed_formatter(action, change, parsed_comment, categories, recent_cha if target[0][1] > 1: embed["image"]["url"] = image_direct_url elif action == "delete/delete": - link = create_article_path(change["title"].replace(" ", "_"), WIKI_ARTICLE_PATH) + link = create_article_path(change["title"], WIKI_ARTICLE_PATH) embed["title"] = _("Deleted page {article}").format(article=change["title"]) elif action == "delete/delete_redir": - link = create_article_path(change["title"].replace(" ", "_"), WIKI_ARTICLE_PATH) + link = create_article_path(change["title"], WIKI_ARTICLE_PATH) embed["title"] = _("Deleted redirect {article} by overwriting").format(article=change["title"]) elif action == "move/move": - link = create_article_path(change["logparams"]['target_title'].replace(" ", "_"), WIKI_ARTICLE_PATH) + link = create_article_path(change["logparams"]['target_title'], WIKI_ARTICLE_PATH) parsed_comment = "{supress}. {desc}".format(desc=parsed_comment, supress=_("No redirect has been made") if "suppressredirect" in change["logparams"] else _( "A redirect has been made")) embed["title"] = _("Moved {redirect}{article} to {target}").format(redirect="⤷ " if "redirect" in change else "", article=change["title"], target=change["logparams"]['target_title']) elif action == "move/move_redir": - link = create_article_path(change["logparams"]["target_title"].replace(" ", "_"), WIKI_ARTICLE_PATH) + link = create_article_path(change["logparams"]["target_title"], WIKI_ARTICLE_PATH) embed["title"] = _("Moved {redirect}{article} to {title} over redirect").format(redirect="⤷ " if "redirect" in change else "", article=change["title"], title=change["logparams"]["target_title"]) elif action == "protect/move_prot": - link = create_article_path(change["logparams"]["oldtitle_title"].replace(" ", "_"), WIKI_ARTICLE_PATH) + link = create_article_path(change["logparams"]["oldtitle_title"], WIKI_ARTICLE_PATH) embed["title"] = _("Moved protection settings from {redirect}{article} to {title}").format(redirect="⤷ " if "redirect" in change else "", article=change["logparams"]["oldtitle_title"], title=change["title"]) elif action == "block/block": @@ -472,7 +472,7 @@ async def embed_formatter(action, change, parsed_comment, categories, recent_cha ipaddress.ip_address(user) link = create_article_path("Special:Contributions/{user}".format(user=user), WIKI_ARTICLE_PATH) except ValueError: - link = create_article_path(change["title"].replace(" ", "_"), WIKI_ARTICLE_PATH) + link = create_article_path(change["title"], WIKI_ARTICLE_PATH) if change["logparams"]["duration"] == "infinite": block_time = _("for infinity and beyond") else: @@ -512,11 +512,11 @@ async def embed_formatter(action, change, parsed_comment, categories, recent_cha embed.add_field(_("Partial block details"), restriction_description, inline=True) embed["title"] = _("Blocked {blocked_user} {time}").format(blocked_user=user, time=block_time) elif action == "block/reblock": - link = create_article_path(change["title"].replace(" ", "_"), WIKI_ARTICLE_PATH) + link = create_article_path(change["title"], WIKI_ARTICLE_PATH) user = change["title"].split(':', 1)[1] embed["title"] = _("Changed block settings for {blocked_user}").format(blocked_user=user) elif action == "block/unblock": - link = create_article_path(change["title"].replace(" ", "_"), WIKI_ARTICLE_PATH) + link = create_article_path(change["title"], WIKI_ARTICLE_PATH) user = change["title"].split(':', 1)[1] embed["title"] = _("Unblocked {blocked_user}").format(blocked_user=user) elif action == "curseprofile/comment-created": @@ -541,7 +541,7 @@ async def embed_formatter(action, change, parsed_comment, categories, recent_cha change["user"] else _( "Edited a comment on their own profile") elif action == "curseprofile/profile-edited": - link = create_article_path("UserProfile:{target}".format(target=change["title"].split(':')[1].replace(" ", "_")), WIKI_ARTICLE_PATH) + link = create_article_path("UserProfile:{target}".format(target=change["title"].split(':')[1]), WIKI_ARTICLE_PATH) embed["title"] = _("Edited {target}'s profile").format(target=change["title"].split(':')[1]) if change["user"] != change["title"].split(':')[1] else _("Edited their own profile") if not change["parsedcomment"]: # If the field is empty parsed_comment = _("Cleared the {field} field").format(field=profile_field_name(change["logparams"]['4:section'], True, _)) @@ -557,7 +557,7 @@ async def embed_formatter(action, change, parsed_comment, categories, recent_cha link = create_article_path(change["title"], WIKI_ARTICLE_PATH) embed["title"] = _("Deleted a comment on {target}'s profile").format(target=change["title"].split(':')[1]) elif action in ("rights/rights", "rights/autopromote"): - link = create_article_path("User:{}".format(change["title"].split(":")[1].replace(" ", "_")), WIKI_ARTICLE_PATH) + link = create_article_path("User:{}".format(change["title"].split(":")[1]), WIKI_ARTICLE_PATH) if action == "rights/rights": embed["title"] = _("Changed group membership for {target}").format(target=change["title"].split(":")[1]) else: @@ -581,33 +581,33 @@ async def embed_formatter(action, change, parsed_comment, categories, recent_cha parsed_comment = _("Groups changed from {old_groups} to {new_groups}{reason}").format( old_groups=", ".join(old_groups), new_groups=', '.join(new_groups), reason=reason) elif action == "protect/protect": - link = create_article_path(change["title"].replace(" ", "_"), WIKI_ARTICLE_PATH) + link = create_article_path(change["title"], WIKI_ARTICLE_PATH) embed["title"] = _("Protected {target}").format(target=change["title"]) parsed_comment = "{settings}{cascade} | {reason}".format(settings=change["logparams"]["description"], cascade=_(" [cascading]") if "cascade" in change["logparams"] else "", reason=parsed_comment) elif action == "protect/modify": - link = create_article_path(change["title"].replace(" ", "_"), WIKI_ARTICLE_PATH) + link = create_article_path(change["title"], WIKI_ARTICLE_PATH) embed["title"] = _("Changed protection level for {article}").format(article=change["title"]) parsed_comment = "{settings}{cascade} | {reason}".format(settings=change["logparams"]["description"], cascade=_(" [cascading]") if "cascade" in change["logparams"] else "", reason=parsed_comment) elif action == "protect/unprotect": - link = create_article_path(change["title"].replace(" ", "_"), WIKI_ARTICLE_PATH) + link = create_article_path(change["title"], WIKI_ARTICLE_PATH) embed["title"] = _("Removed protection from {article}").format(article=change["title"]) elif action == "delete/revision": amount = len(change["logparams"]["ids"]) - link = create_article_path(change["title"].replace(" ", "_"), WIKI_ARTICLE_PATH) + link = create_article_path(change["title"], WIKI_ARTICLE_PATH) embed["title"] = ngettext("Changed visibility of revision on page {article} ", "Changed visibility of {amount} revisions on page {article} ", amount).format( article=change["title"], amount=amount) elif action == "import/upload": - link = create_article_path(change["title"].replace(" ", "_"), WIKI_ARTICLE_PATH) + link = create_article_path(change["title"], WIKI_ARTICLE_PATH) embed["title"] = ngettext("Imported {article} with {count} revision", "Imported {article} with {count} revisions", change["logparams"]["count"]).format( article=change["title"], count=change["logparams"]["count"]) elif action == "delete/restore": - link = create_article_path(change["title"].replace(" ", "_"), WIKI_ARTICLE_PATH) + link = create_article_path(change["title"], WIKI_ARTICLE_PATH) embed["title"] = _("Restored {article}").format(article=change["title"]) elif action == "delete/event": link = create_article_path("Special:RecentChanges", WIKI_ARTICLE_PATH) @@ -622,20 +622,20 @@ async def embed_formatter(action, change, parsed_comment, categories, recent_cha link = create_article_path("Special:AbuseFilter/{number}".format(number=change["logparams"]['newId']), WIKI_ARTICLE_PATH) embed["title"] = _("Created abuse filter number {number}").format(number=change["logparams"]['newId']) elif action == "merge/merge": - link = create_article_path(change["title"].replace(" ", "_"), WIKI_ARTICLE_PATH) + link = create_article_path(change["title"], WIKI_ARTICLE_PATH) embed["title"] = _("Merged revision histories of {article} into {dest}").format(article=change["title"], dest=change["logparams"]["dest_title"]) elif action == "newusers/autocreate": - link = create_article_path(change["title"].replace(" ", "_"), WIKI_ARTICLE_PATH) + link = create_article_path(change["title"], WIKI_ARTICLE_PATH) embed["title"] = _("Created account automatically") elif action == "newusers/create": - link = create_article_path(change["title"].replace(" ", "_"), WIKI_ARTICLE_PATH) + link = create_article_path(change["title"], WIKI_ARTICLE_PATH) embed["title"] = _("Created account") elif action == "newusers/create2": - link = create_article_path(change["title"].replace(" ", "_"), WIKI_ARTICLE_PATH) + link = create_article_path(change["title"].replace(" ", "_ WIKI_ARTICLE_PATH) embed["title"] = _("Created account {article}").format(article=change["title"]) elif action == "newusers/byemail": - link = create_article_path(change["title"].replace(" ", "_"), WIKI_ARTICLE_PATH) + link = create_article_path(change["title"], WIKI_ARTICLE_PATH) embed["title"] = _("Created account {article} and password was sent by email").format(article=change["title"]) elif action == "newusers/newusers": link = author_url @@ -657,19 +657,19 @@ async def embed_formatter(action, change, parsed_comment, categories, recent_cha embed["title"] = _("Deleted an entry in interwiki table") parsed_comment = _("Prefix: {prefix} | {desc}").format(desc=parsed_comment, prefix=change["logparams"]['0']) elif action == "contentmodel/change": - link = create_article_path(change["title"].replace(" ", "_"), WIKI_ARTICLE_PATH) + link = create_article_path(change["title"], WIKI_ARTICLE_PATH) embed["title"] = _("Changed the content model of the page {article}").format(article=change["title"]) parsed_comment = _("Model changed from {old} to {new}: {reason}").format(old=change["logparams"]["oldmodel"], new=change["logparams"]["newmodel"], reason=parsed_comment) elif action == "sprite/sprite": - link = create_article_path(change["title"].replace(" ", "_"), WIKI_ARTICLE_PATH) + link = create_article_path(change["title"], WIKI_ARTICLE_PATH) embed["title"] = _("Edited the sprite for {article}").format(article=change["title"]) elif action == "sprite/sheet": - link = create_article_path(change["title"].replace(" ", "_"), WIKI_ARTICLE_PATH) + link = create_article_path(change["title"], WIKI_ARTICLE_PATH) embed["title"] = _("Created the sprite sheet for {article}").format(article=change["title"]) elif action == "sprite/slice": - link = create_article_path(change["title"].replace(" ", "_"), WIKI_ARTICLE_PATH) + link = create_article_path(change["title"], WIKI_ARTICLE_PATH) embed["title"] = _("Edited the slice for {article}").format(article=change["title"]) elif action == "cargo/createtable": table = re.search(r"\[(.*?)\]\(<(.*?)>\)", parse_link(paths[3], change["logparams"]["0"])) diff --git a/src/misc.py b/src/misc.py index 9ec52a8..244094d 100644 --- a/src/misc.py +++ b/src/misc.py @@ -76,6 +76,11 @@ def escape_formatting(data: str) -> str: def create_article_path(article: str, WIKI_ARTICLE_PATH: str) -> str: """Takes the string and creates an URL with it as the article name""" + article = article.replace(" ", "_").replace("%", "%25").replace("\\", "%5C") + if "?" in WIKI_ARTICLE_PATH: + article = article.replace("&", "%26") + else: + article = article.replace("?", "%3F") return WIKI_ARTICLE_PATH.replace("$1", article)