From f701922b425f18205f2c66cbfb84aeac917808da Mon Sep 17 00:00:00 2001 From: Frisk Date: Sat, 8 May 2021 02:10:44 +0200 Subject: [PATCH] Partial migration of Translate extension logs --- extensions/base/translate.py | 222 +++++++++++++++++++++++++++++++++++ src/rc_formatters.py | 95 +++------------ 2 files changed, 238 insertions(+), 79 deletions(-) create mode 100644 extensions/base/translate.py diff --git a/extensions/base/translate.py b/extensions/base/translate.py new file mode 100644 index 0000000..30070b9 --- /dev/null +++ b/extensions/base/translate.py @@ -0,0 +1,222 @@ +# This file is part of Recent changes Goat compatible Discord webhook (RcGcDw). +# +# RcGcDw is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# RcGcDw is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with RcGcDw. If not, see . + +import logging +from src.discord.message import DiscordMessage +from src.api import formatter +from src.i18n import rc_formatters +from src.api.context import Context +from src.api.util import embed_helper, compact_author, create_article_path, sanitize_to_markdown, sanitize_to_url, \ + clean_link + +_ = rc_formatters.gettext +ngettext = rc_formatters.ngettext + +# I cried when I realized I have to migrate Translate extension logs, but this way I atone for my countless sins +# Translate - https://www.mediawiki.org/wiki/Extension:Translate +# pagetranslation/mark - Marking a page for translation + + +@formatter.embed(event="pagetranslation/mark") +def embed_pagetranslation_mark(ctx: Context, change: dict): + embed = DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url) + embed_helper(ctx, embed, change) + link = create_article_path(sanitize_to_url(change["title"])) + if "?" in link: + embed["url"] = link + "&oldid={}".format(change["logparams"]["revision"]) + else: + embed["url"] = link + "?oldid={}".format(change["logparams"]["revision"]) + embed["title"] = _("Marked \"{article}\" for translation").format(article=sanitize_to_markdown(change["title"])) + return embed + + +@formatter.compact(event="pagetranslation/mark") +def compact_pagetranslation_mark(ctx: Context, change: dict): + author, author_url = compact_author(ctx, change) + link = create_article_path(sanitize_to_url(change["title"])) + if "?" in link: + link = link + "&oldid={}".format(change["logparams"]["revision"]) + else: + link = link + "?oldid={}".format(change["logparams"]["revision"]) + link = clean_link(link) + parsed_comment = "" if ctx.parsedcomment is None else " *(" + ctx.parsedcomment + ")*" + content = _("[{author}]({author_url}) marked [{article}]({article_url}) for translation{comment}").format( + author=author, author_url=author_url, + article=change["title"], article_url=link, + comment=parsed_comment + ) + return DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url, content=content) + +# pagetranslation/unmark - Removing a page from translation system + + +@formatter.embed(event="pagetranslation/unmark") +def embed_pagetranslation_unmark(ctx: Context, change: dict): + embed = DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url) + embed_helper(ctx, embed, change) + embed["url"] = create_article_path(sanitize_to_url(change["title"])) + embed["title"] = _("Removed \"{article}\" from the translation system").format(article=sanitize_to_markdown(change["title"])) + return embed + + +@formatter.compact(event="pagetranslation/unmark") +def compact_pagetranslation_unmark(ctx: Context, change: dict): + author, author_url = compact_author(ctx, change) + parsed_comment = "" if ctx.parsedcomment is None else " *(" + ctx.parsedcomment + ")*" + link = clean_link(create_article_path(sanitize_to_url(change["title"]))) + content = _( + "[{author}]({author_url}) removed [{article}]({article_url}) from the translation system{comment}").format( + author=author, author_url=author_url, + article=sanitize_to_markdown(change["title"]), article_url=link, + comment=parsed_comment + ) + return DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url, content=content) + +# pagetranslation/moveok - Completed moving translation page + + +@formatter.embed(event="pagetranslation/moveok") +def embed_pagetranslation_moveok(ctx: Context, change: dict): + embed = DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url) + embed_helper(ctx, embed, change) + embed["url"] = create_article_path(sanitize_to_url(change["logparams"]["target"])) + embed["title"] = _("Completed moving translation pages from \"{article}\" to \"{target}\"").format( + article=sanitize_to_markdown(change["title"]), target=sanitize_to_markdown(change["logparams"]["target"])) + return embed + + +@formatter.compact(event="pagetranslation/moveok") +def compact_pagetranslation_moveok(ctx: Context, change: dict): + author, author_url = compact_author(ctx, change) + parsed_comment = "" if ctx.parsedcomment is None else " *(" + ctx.parsedcomment + ")*" + link = clean_link(create_article_path(sanitize_to_url(change["logparams"]["target"]))) + content = _( + "[{author}]({author_url}) completed moving translation pages from *{article}* to [{target}]({target_url}){comment}").format( + author=author, author_url=author_url, + article=sanitize_to_markdown(change["title"]), target=sanitize_to_markdown(change["logparams"]["target"]), + target_url=link, comment=parsed_comment + ) + return DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url, content=content) + +# pagetranslation/movenok - Failed while moving translation page + + +@formatter.embed(event="pagetranslation/movenok") +def embed_pagetranslation_movenok(ctx: Context, change: dict): + embed = DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url) + embed_helper(ctx, embed, change) + embed["url"] = create_article_path(sanitize_to_url(change["title"])) + embed["title"] = _("Encountered a problem while moving \"{article}\" to \"{target}\"").format( + article=sanitize_to_markdown(change["title"]), target=sanitize_to_markdown(change["logparams"]["target"])) + return embed + + +@formatter.compact(event="pagetranslation/movenok") +def compact_pagetranslation_movenok(ctx: Context, change: dict): + author, author_url = compact_author(ctx, change) + parsed_comment = "" if ctx.parsedcomment is None else " *(" + ctx.parsedcomment + ")*" + link = clean_link(create_article_path(sanitize_to_url(change["title"]))) + target_url = clean_link(create_article_path(sanitize_to_url(change["logparams"]["target"]))) + content = _( + "[{author}]({author_url}) encountered a problem while moving [{article}]({article_url}) to [{target}]({target_url}){comment}").format( + author=author, author_url=author_url, + article=sanitize_to_markdown(change["title"]), article_url=link, + target=sanitize_to_markdown(change["logparams"]["target"]), target_url=target_url, + comment=parsed_comment + ) + return DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url, content=content) + +# pagetranslation/deletefnok - Failure in deletion of translatable page + + +@formatter.embed(event="pagetranslation/deletefnok") +def embed_pagetranslation_deletefnok(ctx: Context, change: dict): + embed = DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url) + embed_helper(ctx, embed, change) + embed["url"] = create_article_path(sanitize_to_url(change["title"])) + embed["title"] = _("Failed to delete \"{article}\" which belongs to translatable page \"{target}\"").format( + article=sanitize_to_markdown(change["title"]), target=sanitize_to_markdown(change["logparams"]["target"])) + return embed + + +@formatter.compact(event="pagetranslation/deletefnok") +def compact_pagetranslation_deletefnok(ctx: Context, change: dict): + author, author_url = compact_author(ctx, change) + parsed_comment = "" if ctx.parsedcomment is None else " *(" + ctx.parsedcomment + ")*" + link = clean_link(create_article_path(sanitize_to_url(change["title"]))) + target_url = clean_link(create_article_path(sanitize_to_url(change["logparams"]["target"]))) + content = _( + "[{author}]({author_url}) failed to delete [{article}]({article_url}) which belongs to translatable page [{target}]({target_url}){comment}").format( + author=author, author_url=author_url, + article=sanitize_to_markdown(change["title"]), article_url=link, + target=sanitize_to_markdown(change["logparams"]["target"]), target_url=target_url, + comment=parsed_comment + ) + return DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url, content=content) + +# pagetranslation/deletelok - Completion in deleting a page? + + +@formatter.embed(event="pagetranslation/deletelok") +def embed_pagetranslation_deletelok(ctx: Context, change: dict): + embed = DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url) + embed_helper(ctx, embed, change) + embed["url"] = create_article_path(sanitize_to_url(change["title"])) + embed["title"] = _("Completed deletion of translation page \"{article}\"").format( + article=sanitize_to_markdown(change["title"])) + return embed + + +@formatter.compact(event="pagetranslation/deletelok") +def compact_pagetranslation_deletelok(ctx: Context, change: dict): + author, author_url = compact_author(ctx, change) + parsed_comment = "" if ctx.parsedcomment is None else " *(" + ctx.parsedcomment + ")*" + link = clean_link(create_article_path(sanitize_to_url(change["title"]))) + content = _( + "[{author}]({author_url}) completed deletion of translation page [{article}]({article_url}){comment}").format( + author=author, author_url=author_url, + article=sanitize_to_markdown(change["title"]), article_url=link, + comment=parsed_comment + ) + return DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url, content=content) + +# pagetranslation/deletelnok - Failure in deletion of article belonging to a translation page + + +@formatter.embed(event="pagetranslation/deletelnok") +def embed_pagetranslation_deletelnok(ctx: Context, change: dict): + embed = DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url) + embed_helper(ctx, embed, change) + embed["url"] = create_article_path(sanitize_to_url(change["title"])) + embed["title"] = _("Failed to delete \"{article}\" which belongs to translation page \"{target}\"").format( + article=sanitize_to_markdown(change["title"]), target=sanitize_to_markdown(change["logparams"]["target"])) + return embed + + +@formatter.compact(event="pagetranslation/deletelnok") +def compact_pagetranslation_deletelnok(ctx: Context, change: dict): + author, author_url = compact_author(ctx, change) + parsed_comment = "" if ctx.parsedcomment is None else " *(" + ctx.parsedcomment + ")*" + link = clean_link(create_article_path(sanitize_to_url(change["title"]))) + target_url = clean_link(create_article_path(sanitize_to_url(change["logparams"]["target"]))) + content = _( + "[{author}]({author_url}) failed to delete [{article}]({article_url}) which belongs to translation page [{target}]({target_url}){comment}").format( + author=author, author_url=author_url, + article=sanitize_to_markdown(change["title"]), article_url=link, + target=sanitize_to_markdown(change["logparams"]["target"]), target_url=target_url, + comment=parsed_comment + ) + return DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url, content=content) + diff --git a/src/rc_formatters.py b/src/rc_formatters.py index 54615b7..1423dfa 100644 --- a/src/rc_formatters.py +++ b/src/rc_formatters.py @@ -261,72 +261,21 @@ def compact_formatter(action, change, parsed_comment, categories, recent_changes elif action == "datadump/delete": elif action == "pagetranslation/mark": - link = create_article_path(change["title"]) - if "?" in link: - link = link + "&oldid={}".format(change["logparams"]["revision"]) - else: - link = link + "?oldid={}".format(change["logparams"]["revision"]) - link = link_formatter(link) - content = _("[{author}]({author_url}) marked [{article}]({article_url}) for translation{comment}").format( - author=author, author_url=author_url, - article=change["title"], article_url=link, - comment=parsed_comment - ) + elif action == "pagetranslation/unmark": - link = link_formatter(create_article_path(change["title"])) - content = _("[{author}]({author_url}) removed [{article}]({article_url}) from the translation system{comment}").format( - author=author, author_url=author_url, - article=change["title"], article_url=link, - comment=parsed_comment - ) + elif action == "pagetranslation/moveok": - link = link_formatter(create_article_path(change["logparams"]["target"])) - content = _("[{author}]({author_url}) completed moving translation pages from *{article}* to [{target}]({target_url}){comment}").format( - author=author, author_url=author_url, - article=change["title"], target=change["logparams"]["target"], target_url=link, - comment=parsed_comment - ) + elif action == "pagetranslation/movenok": - link = link_formatter(create_article_path(change["title"])) - target_url = link_formatter(create_article_path(change["logparams"]["target"])) - content = _("[{author}]({author_url}) encountered a problem while moving [{article}]({article_url}) to [{target}]({target_url}){comment}").format( - author=author, author_url=author_url, - article=change["title"], article_url=link, - target=change["logparams"]["target"], target_url=target_url, - comment=parsed_comment - ) + elif action == "pagetranslation/deletefok": - link = link_formatter(create_article_path(change["title"])) - content = _("[{author}]({author_url}) completed deletion of translatable page [{article}]({article_url}){comment}").format( - author=author, author_url=author_url, - article=change["title"], article_url=link, - comment=parsed_comment - ) + elif action == "pagetranslation/deletefnok": - link = link_formatter(create_article_path(change["title"])) - target_url = link_formatter(create_article_path(change["logparams"]["target"])) - content = _("[{author}]({author_url}) failed to delete [{article}]({article_url}) which belongs to translatable page [{target}]({target_url}){comment}").format( - author=author, author_url=author_url, - article=change["title"], article_url=link, - target=change["logparams"]["target"], target_url=target_url, - comment=parsed_comment - ) + elif action == "pagetranslation/deletelok": - link = link_formatter(create_article_path(change["title"])) - content = _("[{author}]({author_url}) completed deletion of translation page [{article}]({article_url}){comment}").format( - author=author, author_url=author_url, - article=change["title"], article_url=link, - comment=parsed_comment - ) + elif action == "pagetranslation/deletelnok": - link = link_formatter(create_article_path(change["title"])) - target_url = link_formatter(create_article_path(change["logparams"]["target"])) - content = _("[{author}]({author_url}) failed to delete [{article}]({article_url}) which belongs to translation page [{target}]({target_url}){comment}").format( - author=author, author_url=author_url, - article=change["title"], article_url=link, - target=change["logparams"]["target"], target_url=target_url, - comment=parsed_comment - ) + elif action == "pagetranslation/encourage": link = link_formatter(create_article_path(change["title"])) content = _("[{author}]({author_url}) encouraged translation of [{article}]({article_url}){comment}").format( @@ -601,33 +550,21 @@ def embed_formatter(action, change, parsed_comment, categories, recent_changes): elif action == "datadump/delete": elif action == "pagetranslation/mark": - link = create_article_path(change["title"]) - if "?" in link: - link = link + "&oldid={}".format(change["logparams"]["revision"]) - else: - link = link + "?oldid={}".format(change["logparams"]["revision"]) - embed["title"] = _("Marked \"{article}\" for translation").format(article=change["title"]) + elif action == "pagetranslation/unmark": - link = create_article_path(change["title"]) - embed["title"] = _("Removed \"{article}\" from the translation system").format(article=change["title"]) + elif action == "pagetranslation/moveok": - link = create_article_path(change["logparams"]["target"]) - embed["title"] = _("Completed moving translation pages from \"{article}\" to \"{target}\"").format(article=change["title"], target=change["logparams"]["target"]) + elif action == "pagetranslation/movenok": - link = create_article_path(change["title"]) - embed["title"] = _("Encountered a problem while moving \"{article}\" to \"{target}\"").format(article=change["title"], target=change["logparams"]["target"]) + elif action == "pagetranslation/deletefok": - link = create_article_path(change["title"]) - embed["title"] = _("Completed deletion of translatable page \"{article}\"").format(article=change["title"]) + elif action == "pagetranslation/deletefnok": - link = create_article_path(change["title"]) - embed["title"] = _("Failed to delete \"{article}\" which belongs to translatable page \"{target}\"").format(article=change["title"], target=change["logparams"]["target"]) + elif action == "pagetranslation/deletelok": - link = create_article_path(change["title"]) - embed["title"] = _("Completed deletion of translation page \"{article}\"").format(article=change["title"]) + elif action == "pagetranslation/deletelnok": - link = create_article_path(change["title"]) - embed["title"] = _("Failed to delete \"{article}\" which belongs to translation page \"{target}\"").format(article=change["title"], target=change["logparams"]["target"]) + elif action == "pagetranslation/encourage": link = create_article_path(change["title"]) embed["title"] = _("Encouraged translation of \"{article}\"").format(article=change["title"])