Add support for formatversion=2 from RcGcDw and add update formatters

This commit is contained in:
Frisk 2025-02-13 13:52:33 +01:00
parent 2dc3ec12c3
commit 2cacf10ee3
10 changed files with 165 additions and 116 deletions

View file

@ -24,9 +24,9 @@ import extensions.base.discussions
import extensions.base.curseprofile
import extensions.base.interwiki
import extensions.base.renameuser
import extensions.base.migrators
import extensions.base.approvedrevs
import extensions.base.templateclassification
import extensions.base.comments
import extensions.base.rcgcdb
import extensions.base.globalblocking
import extensions.base.migrators

View file

@ -63,6 +63,8 @@ def embed_abuselog(ctx: Context, change: dict):
embed.add_field(ctx._("Title"), "[{target}]({target_url})".format(target=change.get("title", ctx._("Unknown")),
target_url=clean_link(ctx.client.create_article_path(sanitize_to_url(change.get("title", ctx._("Unknown")))))), inline=True)
embed.add_field(ctx._("Performed"), abusefilter_actions(change["action"], ctx._, change["action"]), inline=True)
if change.get("wiki", None):
embed.add_field(ctx._("Wiki"), change["wiki"], inline=True)
embed.add_field(ctx._("Action taken"), "\n".join([abusefilter_results(result, ctx._, result) for result in results]))
embed_helper(ctx, embed, change, is_anon=abuse_filter_is_ip(change), set_desc=False)
return embed
@ -73,8 +75,12 @@ def compact_abuselog(ctx: Context, change: dict):
results = change["result"].split(",")
action = abuselog_action(results)
author, author_url = compact_author(ctx, change, is_anon=abuse_filter_is_ip(change))
message = ctx._("[{author}]({author_url}) triggered *[{abuse_filter}]({details_url})*, performing the action \"{action}\" on *[{target}]({target_url})* - action taken: {result}.").format(
author=author, author_url=author_url, abuse_filter=sanitize_to_markdown(change["filter"]),
if change.get("wiki", None):
message = ctx._("[{author}]({author_url}) triggered *[{abuse_filter}]({details_url})*, performing the action \"{action}\" on wiki \"{wiki}\" on *[{target}]({target_url})* - action taken: {result}.")
else:
message = ctx._("[{author}]({author_url}) triggered *[{abuse_filter}]({details_url})*, performing the action \"{action}\" on *[{target}]({target_url})* - action taken: {result}.")
message = message.format(
author=author, author_url=author_url, abuse_filter=sanitize_to_markdown(change["filter"]), wiki=change.get("wiki", None),
details_url=clean_link(ctx.client.create_article_path("Special:AbuseLog/{entry}".format(entry=change["id"]))),
action=abusefilter_actions(change["action"], ctx._, change["action"]), target=change.get("title", ctx._("Unknown")),
target_url=clean_link(ctx.client.create_article_path(sanitize_to_url(change.get("title", ctx._("Unknown"))))),

View file

@ -22,14 +22,13 @@ from src.misc import LinkParser
# Approved Revs - https://mediawiki.org/wiki/Extension:Approved_Revs
# approval/approvefile
@formatter.embed(event="approval/approvefile")
def embed_approval_approvefile(ctx: Context, change: dict):
embed = DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url)
embed_helper(ctx, embed, change)
embed["url"] = ctx.client.create_article_path(sanitize_to_url(change["title"]))
embed["title"] = ctx._("Approved a file revision of {file}").format(file=sanitize_to_markdown(change["title"]))
link_parser_object = ctx.client.LinkParser(ctx.client.WIKI_JUST_DOMAIN)
link_parser_object = LinkParser(ctx.client.WIKI_JUST_DOMAIN)
link_parser_object.feed(change.get("logparams", {}).get("0", ""))
embed["description"] = ctx._("File version from {time} got approved").format(name=change["title"], time=link_parser_object.new_string)
# TODO Make timestamp more user friendly? Getting user who uploaded will be a pain though, same with approval/approve
@ -47,7 +46,6 @@ def compact_approval_approvefile(ctx: Context, change: dict):
file_url=link)
return DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url, content=content)
# approval/approve
@formatter.embed(event="approval/approve")
def embed_approval_approve(ctx: Context, change: dict):
@ -55,7 +53,7 @@ def embed_approval_approve(ctx: Context, change: dict):
embed_helper(ctx, embed, change)
embed["url"] = ctx.client.create_article_path(sanitize_to_url(change["title"]))
embed["title"] = ctx._("Approved a revision of {article}").format(article=sanitize_to_markdown(change["title"]))
link_parser_object = ctx.client.LinkParser(ctx.client.WIKI_JUST_DOMAIN)
link_parser_object = LinkParser(ctx.client.WIKI_JUST_DOMAIN)
link_parser_object.feed(change.get("logparams", {}).get("0", ""))
embed["description"] = ctx._("Revision number {revision_id} got approved").format(name=change["title"], time=link_parser_object.new_string)
return embed
@ -72,7 +70,6 @@ def compact_approval_approve(ctx: Context, change: dict):
article_url=link)
return DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url, content=content)
# approval/unapprove
@formatter.embed(event="approval/unapprove")
def embed_approval_approve(ctx: Context, change: dict):

View file

@ -36,10 +36,10 @@ def embed_cargo_createtable(ctx: Context, change: dict):
@formatter.compact(event="cargo/createtable")
def compact_cargo_createtable(ctx: Context, change: dict):
author, author_url = compact_author(ctx, change)
table = re.search(r"\[(.*?)]\(<(.*?)>\)", ctx.client.parse_links(change["logparams"]["0"]))
content = ctx._("[{author}]({author_url}) created the Cargo table \"{table}\"").format(author=author,
author_url=author_url,
table=table)
table_link = ctx.client.parse_links(change["logparams"]["0"])
content = ctx._("[{author}]({author_url}) created the Cargo table {table_link}").format(author=author,
author_url=author_url,
table_link=table_link)
return DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url, content=content)
@ -59,10 +59,10 @@ def embed_cargo_recreatetable(ctx: Context, change: dict):
@formatter.compact(event="cargo/recreatetable")
def compact_cargo_recreatetable(ctx: Context, change: dict):
author, author_url = compact_author(ctx, change)
table = re.search(r"\[(.*?)]\(<(.*?)>\)", ctx.client.parse_links(change["logparams"]["0"]))
content = ctx._("[{author}]({author_url}) recreated the Cargo table \"{table}\"").format(author=author,
author_url=author_url,
table=table)
table_link = ctx.client.parse_links(change["logparams"]["0"])
content = ctx._("[{author}]({author_url}) recreated the Cargo table {table_link}").format(author=author,
author_url=author_url,
table_link=table_link)
return DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url, content=content)
@ -82,10 +82,10 @@ def embed_cargo_replacetable(ctx: Context, change: dict):
@formatter.compact(event="cargo/replacetable")
def compact_cargo_replacetable(ctx: Context, change: dict):
author, author_url = compact_author(ctx, change)
table = re.search(r"\[(.*?)]\(<(.*?)>\)", ctx.client.parse_links(change["logparams"]["0"]))
content = ctx._("[{author}]({author_url}) replaced the Cargo table \"{table}\"").format(author=author,
author_url=author_url,
table=table)
table_link = ctx.client.parse_links(change["logparams"]["0"])
content = ctx._("[{author}]({author_url}) replaced the Cargo table {table_link}").format(author=author,
author_url=author_url,
table_link=table_link)
return DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url, content=content)

View file

@ -14,7 +14,6 @@
# along with RcGcDw. If not, see <http://www.gnu.org/licenses/>.
import logging
from src.discord.message import DiscordMessage
from src.api import formatter
from src.api.context import Context
@ -23,8 +22,6 @@ from src.api.util import embed_helper, compact_author, sanitize_to_markdown, san
# DataDumps - https://www.mediawiki.org/wiki/Extension:DataDump
# datadump/generate - Generating a dump of wiki
@formatter.embed(event="datadump/generate")
def embed_datadump_generate(ctx: Context, change: dict) -> DiscordMessage:
embed = DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url)
@ -44,9 +41,8 @@ def compact_datadump_generate(ctx: Context, change: dict):
)
return DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url, content=content)
# datadump/delete - Deleting a dump of a wiki
@formatter.embed(event="datadump/delete")
def embed_datadump_delete(ctx: Context, change: dict) -> DiscordMessage:
embed = DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url)
@ -65,3 +61,37 @@ def compact_datadump_delete(ctx: Context, change: dict) -> DiscordMessage:
comment=parsed_comment
)
return DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url, content=content)
# datadump/generate-in-progress - Datadump generation is in progress
@formatter.embed(event="datadump/generate-in-progress")
def embed_datadump_generate_in_progress(ctx: Context, change: dict) -> DiscordMessage:
embed = DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url)
embed_helper(ctx, embed, change)
embed["title"] = ctx._("Generation of a backup file ”{file}” has been initiated").format(file=sanitize_to_markdown(change["logparams"]["filename"]))
embed["url"] = ctx.client.create_article_path(sanitize_to_url(change["title"]))
return embed
@formatter.compact(event="datadump/generate-in-progress")
def compact_datadump_generate_in_progress(ctx: Context, change: dict) -> DiscordMessage:
content = ctx._("Generation of backup file ”*{file}*” has been initiated").format(file=sanitize_to_markdown(change["logparams"]["filename"])
)
return DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url, content=content)
# datadump/generate-completed - Datadump generation is completed
@formatter.embed(event="datadump/generate-completed")
def embed_datadump_generate_completed(ctx: Context, change: dict) -> DiscordMessage:
embed = DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url)
embed_helper(ctx, embed, change)
embed["title"] = ctx._("Generation of backup file ”{file}” has been completed").format(file=sanitize_to_markdown(change["logparams"]["filename"]))
embed["url"] = ctx.client.create_article_path(sanitize_to_url(change["title"]))
return embed
@formatter.compact(event="datadump/generate-completed")
def compact_datadump_generate_completed(ctx: Context, change: dict) -> DiscordMessage:
content = ctx._("Generation of backup file ”*{file}*” has been completed").format(file=sanitize_to_markdown(change["logparams"]["filename"])
)
return DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url, content=content)

View file

@ -79,7 +79,7 @@ class DiscussionsFromHellParser:
self.markdown_text = "{old}{img_url}\n".format(old=self.markdown_text, img_url=
self.post["_embedded"]["contentImages"][int(item["attrs"]["id"])]["url"])
self.image_last = self.post["_embedded"]["contentImages"][int(item["attrs"]["id"])]["url"]
except (IndexError, ValueError, TypeError):
except (IndexError, ValueError):
logger.warning("Image {} not found.".format(item["attrs"]["id"]))
logger.debug(self.markdown_text)
elif item["type"] == "code_block":
@ -103,7 +103,7 @@ class DiscussionsFromHellParser:
for mark in marks:
if mark["type"] == "mention":
prefix += "["
suffix = "]({wiki}f/u/{userid}){suffix}".format(wiki=self.ctx.client.WIKI_SCRIPT_PATH,
suffix = "]({wiki}f/u/{userid}){suffix}".format(wiki=self.ctx.settings["fandom_discussions"]["wiki_url"],
userid=mark["attrs"]["userId"], suffix=suffix)
elif mark["type"] == "strong":
prefix += "**"
@ -136,16 +136,16 @@ def common_discussions(post: dict, embed: DiscordMessage, ctx: Context):
@formatter.embed(event="discussion/forum")
def embed_discussion_forum(ctx: Context, post: dict):
embed = DiscordMessage("embed", "discussion", ctx.webhook_url)
embed = DiscordMessage("embed", "discussion", ctx.settings["fandom_discussions"]["webhookURL"])
common_discussions(post, embed, ctx)
author = ctx._("unknown") # Fail safe
if post["createdBy"]["name"]:
author = post["createdBy"]["name"]
embed.set_author(author, "{url}f/u/{creatorId}".format(url=ctx.client.WIKI_SCRIPT_PATH,
embed.set_author(author, "{url}f/u/{creatorId}".format(url=ctx.settings["fandom_discussions"]["wiki_url"],
creatorId=post["creatorId"]),
icon_url=post["createdBy"]["avatarUrl"])
if not post["isReply"]:
embed["url"] = "{url}f/p/{threadId}".format(url=ctx.client.WIKI_SCRIPT_PATH,
embed["url"] = "{url}f/p/{threadId}".format(url=ctx.settings["fandom_discussions"]["wiki_url"],
threadId=post["threadId"])
embed["title"] = ctx._("Created \"{title}\"").format(title=post["title"])
thread_funnel = post.get("funnel")
@ -189,7 +189,7 @@ def embed_discussion_forum(ctx: Context, post: dict):
else:
embed.event_type = "discussion/forum/reply"
embed["title"] = ctx._("Replied to \"{title}\"").format(title=post["_embedded"]["thread"][0]["title"])
embed["url"] = "{url}f/p/{threadId}/r/{postId}".format(url=ctx.client.WIKI_SCRIPT_PATH,
embed["url"] = "{url}f/p/{threadId}/r/{postId}".format(url=ctx.settings["fandom_discussions"]["wiki_url"],
threadId=post["threadId"], postId=post["id"])
return embed
@ -200,7 +200,7 @@ def compact_discussion_forum(ctx: Context, post: dict):
author = ctx._("unknown") # Fail safe
if post["createdBy"]["name"]:
author = post["createdBy"]["name"]
author_url = "<{url}f/u/{creatorId}>".format(url=ctx.client.WIKI_SCRIPT_PATH,
author_url = "<{url}f/u/{creatorId}>".format(url=ctx.settings["fandom_discussions"]["wiki_url"],
creatorId=post["creatorId"])
if not post["isReply"]:
thread_funnel = post.get("funnel")
@ -219,13 +219,13 @@ def compact_discussion_forum(ctx: Context, post: dict):
thread_funnel))
event_type = "unknown"
message = msg_text.format(author=author, author_url=author_url, title=post["title"],
url=ctx.client.WIKI_SCRIPT_PATH, threadId=post["threadId"],
url=ctx.settings["fandom_discussions"]["wiki_url"], threadId=post["threadId"],
forumName=post["forumName"])
else:
event_type = "discussion/forum/reply"
message = ctx._(
"[{author}]({author_url}) created a [reply](<{url}f/p/{threadId}/r/{postId}>) to [{title}](<{url}f/p/{threadId}>) in {forumName}").format(
author=author, author_url=author_url, url=ctx.client.WIKI_SCRIPT_PATH,
author=author, author_url=author_url, url=ctx.settings["fandom_discussions"]["wiki_url"],
threadId=post["threadId"], postId=post["id"], title=post["_embedded"]["thread"][0]["title"],
forumName=post["forumName"])
return DiscordMessage("compact", event_type, ctx.webhook_url, content=message)
@ -238,14 +238,14 @@ def compact_author_discussions(post: dict, ctx: Context):
author = ctx._("unknown") # Fail safe
if post["creatorIp"]:
author = post["creatorIp"][1:] if ctx.settings.get("hide_ips", False) is False else ctx._("Unregistered user")
author_url = "<{url}wiki/Special:Contributions{creatorIp}>".format(url=ctx.client.WIKI_SCRIPT_PATH,
author_url = "<{url}wiki/Special:Contributions{creatorIp}>".format(url=ctx.settings["fandom_discussions"]["wiki_url"],
creatorIp=post["creatorIp"])
else:
if post["createdBy"]["name"]:
author = post["createdBy"]["name"]
author_url = clean_link(ctx.client.create_article_path("User:{user}".format(user=author)))
else:
author_url = "<{url}f/u/{creatorId}>".format(url=ctx.client.WIKI_SCRIPT_PATH,
author_url = "<{url}f/u/{creatorId}>".format(url=ctx.settings["fandom_discussions"]["wiki_url"],
creatorId=post["creatorId"])
return author, author_url
@ -256,22 +256,22 @@ def embed_author_discussions(post: dict, embed: DiscordMessage, ctx: Context):
author = post["creatorIp"][1:]
embed.set_author(author if ctx.settings.get("hide_ips", False) is False else ctx._("Unregistered user"),
"{url}wiki/Special:Contributions{creatorIp}".format(
url=ctx.client.WIKI_SCRIPT_PATH, creatorIp=post["creatorIp"]))
url=ctx.settings["fandom_discussions"]["wiki_url"], creatorIp=post["creatorIp"]))
else:
if post["createdBy"]["name"]:
author = post["createdBy"]["name"]
embed.set_author(author, "{url}wiki/User:{creator}".format(url=ctx.client.WIKI_SCRIPT_PATH,
embed.set_author(author, "{url}wiki/User:{creator}".format(url=ctx.settings["fandom_discussions"]["wiki_url"],
creator=author.replace(" ", "_")),
icon_url=post["createdBy"]["avatarUrl"])
else:
embed.set_author(author, "{url}f/u/{creatorId}".format(url=ctx.client.WIKI_SCRIPT_PATH,
embed.set_author(author, "{url}f/u/{creatorId}".format(url=ctx.settings["fandom_discussions"]["wiki_url"],
creatorId=post["creatorId"]),
icon_url=post["createdBy"]["avatarUrl"])
@formatter.embed(event="discussion/wall")
def embed_discussion_wall(ctx: Context, post: dict):
embed = DiscordMessage("embed", "discussion", ctx.webhook_url)
embed = DiscordMessage("embed", "discussion", ctx.settings["fandom_discussions"]["webhookURL"])
common_discussions(post, embed, ctx)
embed_author_discussions(post, embed, ctx)
user_wall = ctx._("unknown") # Fail safe
@ -280,13 +280,13 @@ def embed_discussion_wall(ctx: Context, post: dict):
if not post["isReply"]:
embed.event_type = "discussion/wall/post"
embed["url"] = "{url}wiki/Message_Wall:{user_wall}?threadId={threadId}".format(
url=ctx.client.WIKI_SCRIPT_PATH, user_wall=quote_plus(user_wall.replace(" ", "_")),
url=ctx.settings["fandom_discussions"]["wiki_url"], user_wall=quote_plus(user_wall.replace(" ", "_")),
threadId=post["threadId"])
embed["title"] = ctx._("Created \"{title}\" on {user}'s Message Wall").format(title=post["title"], user=user_wall)
else:
embed.event_type = "discussion/wall/reply"
embed["url"] = "{url}wiki/Message_Wall:{user_wall}?threadId={threadId}#{replyId}".format(
url=ctx.client.WIKI_SCRIPT_PATH, user_wall=quote_plus(user_wall.replace(" ", "_")),
url=ctx.settings["fandom_discussions"]["wiki_url"], user_wall=quote_plus(user_wall.replace(" ", "_")),
threadId=post["threadId"], replyId=post["id"])
embed["title"] = ctx._("Replied to \"{title}\" on {user}'s Message Wall").format(
title=post["_embedded"]["thread"][0]["title"], user=user_wall)
@ -303,13 +303,13 @@ def compact_discussion_wall(ctx: Context, post: dict):
event_type = "discussion/wall/post"
message = ctx._(
"[{author}]({author_url}) created [{title}](<{url}wiki/Message_Wall:{user_wall}?threadId={threadId}>) on [{user}'s Message Wall](<{url}wiki/Message_Wall:{user_wall}>)").format(
author=author, author_url=author_url, title=post["title"], url=ctx.client.WIKI_SCRIPT_PATH,
author=author, author_url=author_url, title=post["title"], url=ctx.settings["fandom_discussions"]["wiki_url"],
user=user_wall, user_wall=quote_plus(user_wall.replace(" ", "_")), threadId=post["threadId"])
else:
event_type = "discussion/wall/reply"
message = ctx._(
"[{author}]({author_url}) created a [reply](<{url}wiki/Message_Wall:{user_wall}?threadId={threadId}#{replyId}>) to [{title}](<{url}wiki/Message_Wall:{user_wall}?threadId={threadId}>) on [{user}'s Message Wall](<{url}wiki/Message_Wall:{user_wall}>)").format(
author=author, author_url=author_url, url=ctx.client.WIKI_SCRIPT_PATH,
author=author, author_url=author_url, url=ctx.settings["fandom_discussions"]["wiki_url"],
title=post["_embedded"]["thread"][0]["title"], user=user_wall,
user_wall=quote_plus(user_wall.replace(" ", "_")), threadId=post["threadId"], replyId=post["id"])
return DiscordMessage("compact", event_type, ctx.webhook_url, content=message)
@ -319,12 +319,12 @@ def compact_discussion_wall(ctx: Context, post: dict):
@formatter.embed(event="discussion/article_comment")
def embed_discussion_article_comment(ctx: Context, post: dict):
embed = DiscordMessage("embed", "discussion", ctx.webhook_url)
embed = DiscordMessage("embed", "discussion", ctx.settings["fandom_discussions"]["webhookURL"])
common_discussions(post, embed, ctx)
embed_author_discussions(post, embed, ctx)
article_paths = ctx.comment_page
if article_paths is None:
article_paths = {"title": ctx._("unknown"), "fullUrl": ctx.client.WIKI_SCRIPT_PATH} # No page known
article_paths = {"title": ctx._("unknown"), "fullUrl": ctx.settings["fandom_discussions"]["wiki_url"]} # No page known
if not post["isReply"]:
embed.event_type = "discussion/comment/post"
embed["url"] = "{url}?commentId={commentId}".format(url=article_paths["fullUrl"], commentId=post["threadId"])
@ -344,7 +344,7 @@ def compact_discussion_article_comment(ctx: Context, post: dict):
author, author_url = compact_author_discussions(post, ctx)
article_paths = ctx.comment_page
if article_paths is None:
article_paths = {"title": ctx._("unknown"), "fullUrl": ctx.client.WIKI_SCRIPT_PATH} # No page known
article_paths = {"title": ctx._("unknown"), "fullUrl": ctx.settings["fandom_discussions"]["wiki_url"]} # No page known
article_paths["fullUrl"] = article_paths["fullUrl"].replace(")", "\\)").replace("()", "\\(")
if not post["isReply"]:
event_type = "discussion/comment/post"

View file

@ -64,30 +64,6 @@ def compact_managewiki_delete(ctx: Context, change: dict):
comment=parsed_comment)
return DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url, content=content)
# managewiki/delete-group - Deleting a group
@formatter.embed(event="managewiki/delete-group")
def embed_managewiki_delete_group(ctx: Context, change: dict) -> DiscordMessage:
embed = DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url)
embed_helper(ctx, embed, change)
embed["url"] = ctx.client.create_article_path(sanitize_to_url(change["title"]))
group = change["title"].split("/")[-1]
embed["title"] = ctx._("Deleted a \"{group}\" user group").format(group=group)
return embed
@formatter.compact(event="managewiki/delete-group")
def compact_managewiki_delete_group(ctx: Context, change: dict) -> DiscordMessage:
author, author_url = compact_author(ctx, change)
parsed_comment = compact_summary(ctx)
group = change["title"].split("/")[-1]
content = ctx._("[{author}]({author_url}) deleted a usergroup *{group}*{comment}").format(author=author,
author_url=author_url,
group=group,
comment=parsed_comment)
return DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url, content=content)
# managewiki/lock - Locking a wiki
@ -180,6 +156,29 @@ def compact_managewiki_rights(ctx: Context, change: dict):
)
return DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url, content=content)
# managewiki/delete-group - Deleting user groups
@formatter.embed(event="managewiki/delete-group")
def embed_managewiki_delete_group(ctx: Context, change: dict):
embed = DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url)
embed_helper(ctx, embed, change)
embed["url"] = ctx.client.create_article_path(sanitize_to_url(change["title"]))
group_name = change["title"].split("/permissions/", 1)[1]
embed["title"] = ctx._("Deleted \"{usergroup_name}\" usergroup").format(usergroup_name=group_name)
return embed
@formatter.compact(event="managewiki/delete-group")
def compact_managewiki_delete_group(ctx: Context, change: dict):
author, author_url = compact_author(ctx, change)
parsed_comment = compact_summary(ctx)
group_name = change["title"].split("/permissions/", 1)[1]
content = ctx._("[{author}]({author_url}) deleted user group *{group_name}*{comment}").format(
author=author, author_url=author_url, group_name=group_name, comment=parsed_comment
)
return DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url, content=content)
# managewiki/undelete - Restoring a wiki

View file

@ -29,9 +29,13 @@ from src.exceptions import *
logger = logging.getLogger("extensions.base")
if 1 == 2: # additional translation strings in unreachable code
# noinspection PyUnreachableCode
_ = lambda a: a
# noinspection PyUnreachableCode
print(_("director"), _("bot"), _("editor"), _("directors"), _("sysop"), _("bureaucrat"), _("reviewer"),
_("autoreview"), _("autopatrol"), _("wiki_guardian"))
_("autoreview"), _("autopatrol"), _("wiki_guardian"), _.pgettext("Removal of content field in Revision Delete", "content"),
_.pgettext("Removal of comment field in Revision Delete", "comment"),
_.pgettext("Removal of user field in Revision Delete", "user"))
# Page edit - event edit, New - page creation
@ -60,26 +64,27 @@ def embed_edit(ctx: Context, change: dict) -> DiscordMessage:
article=sanitize_to_url(change["title"])
)
embed["title"] = "{redirect}{article} ({new}{minor}{bot}{space}{editsize})".format(
redirect="" if "redirect" in change else "",
redirect="" if change.get("redirect", False) else "",
article=sanitize_to_markdown(change["title"]),
editsize="+" + str(editsize) if editsize > 0 else editsize,
new=ctx._("(N!) ") if action == "new" else "",
minor=ctx._("m") if action == "edit" and "minor" in change else "",
bot=ctx._('b') if "bot" in change else "",
space=" " if "bot" in change or (action == "edit" and "minor" in change) or action == "new" else "")
minor=ctx._("m") if action == "edit" and change.get("minor", False) else "",
bot=ctx._('b') if change.get("bot", False) else "",
space=" " if "bot" in change or (action == "edit" and change.get("minor", False)) or action == "new" else "")
if ctx.settings["appearance"]["embed"]["show_edit_changes"]:
try:
if action == "new":
changed_content = ctx.client.make_api_request(
"?action=compare&format=json&fromslots=main&torev={diff}&fromtext-main=&topst=1&prop=diff".format(
diff=change["revid"]), "compare", "*")
"?action=compare&format=json&formatversion=2&fromslots=main&torev={diff}&fromtext-main=&topst=1&prop=diff".format(
diff=change["revid"]), "compare", "body")
else:
changed_content = ctx.client.make_api_request(
"?action=compare&format=json&fromrev={oldrev}&torev={diff}&topst=1&prop=diff".format(
diff=change["revid"], oldrev=change["old_revid"]), "compare", "*")
"?action=compare&format=json&formatversion=2&fromrev={oldrev}&torev={diff}&topst=1&prop=diff".format(
diff=change["revid"], oldrev=change["old_revid"]), "compare", "body")
except (ServerError, MediaWikiError):
changed_content = None
if changed_content:
ctx.changed_content = changed_content
parse_mediawiki_changes(ctx, changed_content, embed)
else:
logger.warning("Unable to download data on the edit content!")
@ -128,6 +133,7 @@ def embed_upload_upload(ctx: Context, change: dict) -> DiscordMessage:
params = OrderedDict()
params["action"] = "query"
params["format"] = "json"
params["formatversion"] = 2
if ctx.settings["license_detection"] and action == "upload/upload":
params["prop"] = "imageinfo|revisions"
params["rvprop"] = "content"
@ -149,8 +155,8 @@ def embed_upload_upload(ctx: Context, change: dict) -> DiscordMessage:
"Couldn't retrieve more information about the image {} because of unknown error".format(
change["title"]))
else:
if "-1" not in request_for_image_data: # Image still exists and not removed
image_data = next(iter(request_for_image_data.values()))
if not request_for_image_data[0].get("missing", False): # Image still exists and not removed
image_data = request_for_image_data[0]
else:
logger.warning("Request for additional image information have failed. The preview will not be shown.")
request_for_image_data = None
@ -164,7 +170,7 @@ def embed_upload_upload(ctx: Context, change: dict) -> DiscordMessage:
for num, revision in enumerate(urls):
if revision["timestamp"] == change["logparams"][
"img_timestamp"]: # find the correct revision corresponding for this log entry
image_direct_url = "{rev}{c}rcgcdb={cache}".format(rev=revision["url"],
image_direct_url = "{rev}{c}rcgcdw={cache}".format(rev=revision["url"],
c="&" if "?" in revision["url"] else "?",
cache=int(time.time() * 5)) # cachebusting
break
@ -176,15 +182,19 @@ def embed_upload_upload(ctx: Context, change: dict) -> DiscordMessage:
if action in ("upload/overwrite", "upload/revert"):
if image_direct_url:
try:
revision = image_data["imageinfo"][num + 1]
archive_name = image_data["imageinfo"][num + 1]["archivename"]
except IndexError:
logger.exception(
"Could not analize the information about the image (does it have only one version when expected more in overwrite?) which resulted in no Options field: {}".format(
image_data["imageinfo"]))
except KeyError:
logger.exception(
"Could not analize the information about the image, one of its revisions might have been deleted: {}".format(
image_data["imageinfo"]))
else:
undolink = "{wiki}index.php?title={filename}&action=revert&oldimage={archiveid}".format(
wiki=ctx.client.WIKI_SCRIPT_PATH, filename=sanitize_to_url(change["title"]),
archiveid=revision["archivename"])
archiveid=archive_name)
embed.add_field(ctx._("Options"), ctx._("([preview]({link}) | [undo]({undolink}))").format(
link=image_direct_url, undolink=undolink))
if ctx.settings["appearance"]["embed"]["embed_images"]:
@ -197,7 +207,7 @@ def embed_upload_upload(ctx: Context, change: dict) -> DiscordMessage:
embed["title"] = ctx._("Uploaded {name}").format(name=sanitize_to_markdown(change["title"]))
if ctx.settings["license_detection"] and image_direct_url:
try:
content = image_data['revisions'][0]["slots"]["main"]['*']
content = image_data['revisions'][0]["slots"]["main"]['content']
matches = re.search(re.compile(ctx.settings["license_regex"], re.IGNORECASE), content)
if matches is not None:
license = matches.group("license")
@ -368,6 +378,13 @@ def embed_delete_revision(ctx: Context, change: dict) -> DiscordMessage:
embed["title"] = ctx.ngettext("Changed visibility of revision on page {article} ",
"Changed visibility of {amount} revisions on page {article} ", amount).format(
article=sanitize_to_markdown(change["title"]), amount=amount)
embed["description"] += ctx.parsedcomment + ctx._("\nContents have been suppressed")
old_hidden_fields = {ctx._(key) for key, value in change["logparams"]["old"].items() if (key in ("content", "comment", "user") and value is True)}
new_hidden_fields = {ctx._(key) for key, value in change["logparams"]["new"].items() if (key in ("content", "comment", "user") and value is True)}
if new_hidden_fields - old_hidden_fields:
embed.add_field(ctx._("Hidden fields"), ctx._(", ").join(new_hidden_fields - old_hidden_fields))
if old_hidden_fields - new_hidden_fields:
embed.add_field(ctx._("Revealed fields"), ctx._(", ").join(old_hidden_fields - new_hidden_fields))
return embed
@ -394,9 +411,8 @@ def embed_move_move(ctx: Context, change: dict) -> DiscordMessage:
embed_helper(ctx, embed, change, set_desc=False)
embed["url"] = ctx.client.create_article_path(sanitize_to_url(change["logparams"]['target_title']))
embed["description"] = "{supress}. {desc}".format(desc=ctx.parsedcomment,
supress=ctx._("No redirect has been made") if "suppressredirect" in
change["logparams"] else ctx._("A redirect has been made"))
embed["title"] = ctx._("Moved {redirect}{article} to {target}").format(redirect="" if "redirect" in change else "",
supress=ctx._("No redirect has been made") if change["logparams"].get("suppressredirect", False) else ctx._("A redirect has been made"))
embed["title"] = ctx._("Moved {redirect}{article} to {target}").format(redirect="" if change.get("redirect", False) else "",
article=sanitize_to_markdown(change["title"]),
target=sanitize_to_markdown(
change["logparams"]['target_title']))
@ -407,12 +423,12 @@ def embed_move_move(ctx: Context, change: dict) -> DiscordMessage:
def compact_move_move(ctx: Context, change: dict) -> DiscordMessage:
author, author_url = compact_author(ctx, change)
link = clean_link(ctx.client.create_article_path(sanitize_to_url(change["logparams"]['target_title'])))
redirect_status = ctx._("without making a redirect") if "suppressredirect" in change["logparams"] else ctx._(
redirect_status = ctx._("without making a redirect") if change["logparams"].get("suppressredirect", False) else ctx._(
"with a redirect")
parsed_comment = compact_summary(ctx)
content = ctx._(
"[{author}]({author_url}) moved {redirect}*{article}* to [{target}]({target_url}) {made_a_redirect}{comment}").format(
author=author, author_url=author_url, redirect="" if "redirect" in change else "", article=sanitize_to_markdown(change["title"]),
author=author, author_url=author_url, redirect="" if change.get("redirect", False) else "", article=sanitize_to_markdown(change["title"]),
target=sanitize_to_markdown(change["logparams"]['target_title']), target_url=link, comment=parsed_comment,
made_a_redirect=redirect_status)
return DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url, content=content)
@ -427,10 +443,9 @@ def embed_move_move_redir(ctx: Context, change: dict) -> DiscordMessage:
embed_helper(ctx, embed, change, set_desc=False)
embed["url"] = ctx.client.create_article_path(sanitize_to_url(change["logparams"]['target_title']))
embed["description"] = "{supress}. {desc}".format(desc=ctx.parsedcomment,
supress=ctx._("No redirect has been made") if "suppressredirect" in
change["logparams"] else ctx._("A redirect has been made"))
supress=ctx._("No redirect has been made") if change["logparams"].get("suppressredirect", False) else ctx._("A redirect has been made"))
embed["title"] = ctx._("Moved {redirect}{article} to {title} over redirect").format(
redirect="" if "redirect" in change else "", article=sanitize_to_markdown(change["title"]),
redirect="" if change.get("redirect", False) else "", article=sanitize_to_markdown(change["title"]),
title=sanitize_to_markdown(change["logparams"]["target_title"]))
return embed
@ -439,12 +454,12 @@ def embed_move_move_redir(ctx: Context, change: dict) -> DiscordMessage:
def compact_move_move_redir(ctx: Context, change: dict) -> DiscordMessage:
author, author_url = compact_author(ctx, change)
link = clean_link(ctx.client.create_article_path(sanitize_to_url(change["logparams"]['target_title'])))
redirect_status = ctx._("without making a redirect") if "suppressredirect" in change["logparams"] else ctx._(
redirect_status = ctx._("without making a redirect") if change["logparams"].get("suppressredirect", False) else ctx._(
"with a redirect")
parsed_comment = compact_summary(ctx)
content = ctx._(
"[{author}]({author_url}) moved {redirect}*{article}* over redirect to [{target}]({target_url}) {made_a_redirect}{comment}").format(
author=author, author_url=author_url, redirect="" if "redirect" in change else "",
author=author, author_url=author_url, redirect="" if change.get("redirect", False) else "",
article=sanitize_to_markdown(change["title"]),
target=sanitize_to_markdown(change["logparams"]['target_title']), target_url=link, comment=parsed_comment,
made_a_redirect=redirect_status)
@ -460,7 +475,7 @@ def embed_protect_move_prot(ctx: Context, change: dict):
embed_helper(ctx, embed, change)
embed["url"] = ctx.client.create_article_path(sanitize_to_url(change["logparams"]["oldtitle_title"]))
embed["title"] = ctx._("Moved protection settings from {redirect}{article} to {title}").format(
redirect="" if "redirect" in change else "",
redirect="" if change.get("redirect", False) else "",
article=sanitize_to_markdown(change["logparams"]["oldtitle_title"]),
title=sanitize_to_markdown(change["title"]))
return embed
@ -473,7 +488,7 @@ def compact_protect_move_prot(ctx: Context, change: dict):
parsed_comment = compact_summary(ctx)
content = ctx._(
"[{author}]({author_url}) moved protection settings from {redirect}*{article}* to [{target}]({target_url}){comment}").format(
author=author, author_url=author_url, redirect="" if "redirect" in change else "",
author=author, author_url=author_url, redirect="" if change.get("redirect", False) else "",
article=sanitize_to_markdown(change["logparams"]["oldtitle_title"]),
target=sanitize_to_markdown(change["title"]), target_url=link, comment=parsed_comment)
return DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url, content=content)
@ -490,7 +505,7 @@ def embed_protect_protect(ctx: Context, change: dict):
embed["title"] = ctx._("Protected {target}").format(target=sanitize_to_markdown(change["title"]))
embed["description"] = "{settings}{cascade} | {reason}".format(
settings=sanitize_to_markdown(change["logparams"].get("description", "")),
cascade=ctx._(" [cascading]") if "cascade" in change["logparams"] else "",
cascade=ctx._(" [cascading]") if change["logparams"].get("cascade", False) else "",
reason=ctx.parsedcomment)
return embed
@ -505,7 +520,7 @@ def compact_protect_protect(ctx: Context, change: dict):
author=author, author_url=author_url,
article=sanitize_to_markdown(change["title"]), article_url=link,
settings=change["logparams"].get("description", "") + (
ctx._(" [cascading]") if "cascade" in change["logparams"] else ""),
ctx._(" [cascading]") if change["logparams"].get("cascade", False) else ""),
comment=parsed_comment)
return DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url, content=content)
@ -521,7 +536,7 @@ def embed_protect_modify(ctx: Context, change: dict):
embed["title"] = ctx._("Changed protection level for {article}").format(article=sanitize_to_markdown(change["title"]))
embed["description"] = "{settings}{cascade} | {reason}".format(
settings=sanitize_to_markdown(change["logparams"].get("description", "")),
cascade=ctx._(" [cascading]") if "cascade" in change["logparams"] else "",
cascade=ctx._(" [cascading]") if change["logparams"].get("cascade", False) else "",
reason=ctx.parsedcomment)
return embed
@ -536,7 +551,7 @@ def compact_protect_modify(ctx: Context, change: dict):
author=author, author_url=author_url,
article=sanitize_to_markdown(change["title"]), article_url=link,
settings=sanitize_to_markdown(change["logparams"].get("description", "")) + (
ctx._(" [cascading]") if "cascade" in change["logparams"] else ""),
ctx._(" [cascading]") if change["logparams"].get("cascade", False) else ""),
comment=parsed_comment)
return DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url, content=content)
@ -597,11 +612,12 @@ def embed_block_block(ctx: Context, change: dict):
embed = DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url)
user = change["title"].split(':', 1)[1]
try:
ipaddress.ip_address(user)
if "/" not in user:
ipaddress.ip_address(user)
embed["url"] = ctx.client.create_article_path("Special:Contributions/{user}".format(user=user))
except ValueError:
embed["url"] = ctx.client.create_article_path(sanitize_to_url(change["title"]))
if "sitewide" not in change["logparams"]:
if change["logparams"].get("sitewide", False) is False:
restriction_description = ""
if "restrictions" in change["logparams"]:
if "pages" in change["logparams"]["restrictions"] and change["logparams"]["restrictions"]["pages"]:
@ -619,7 +635,7 @@ def embed_block_block(ctx: Context, change: dict):
if str(namespace) == "0":
namespaces.append("*{ns}*".format(ns=ctx._("(Main)")))
elif str(namespace) in ctx.client.namespaces: # if we have cached namespace name for given namespace number, add its name to the list
namespaces.append("*{ns}*".format(ns=ctx.client.namespaces[str(namespace)]["*"]))
namespaces.append("*{ns}*".format(ns=ctx.client.namespaces[str(namespace)]["name"]))
else:
namespaces.append("*{ns}*".format(ns=namespace))
restriction_description = restriction_description + ", ".join(namespaces)
@ -632,7 +648,7 @@ def embed_block_block(ctx: Context, change: dict):
if block_flags:
embed.add_field(ctx._("Block flags"), ", ".join(
block_flags)) # TODO Translate flags into MW messages, this requires making additional request in init_request since we want to get all messages with prefix (amprefix) block-log-flags- and that parameter is exclusive with ammessages
embed["title"] = ctx._("Blocked {blocked_user} {time}").format(blocked_user=user, time=block_expiry(change, ctx))
embed["title"] = ctx._("Blocked {blocked_user} {time}").format(blocked_user=sanitize_to_markdown(user), time=block_expiry(change, ctx))
embed_helper(ctx, embed, change)
return embed
@ -644,7 +660,8 @@ def compact_block_block(ctx: Context, change: dict):
author, author_url = compact_author(ctx, change)
parsed_comment = compact_summary(ctx)
try:
ipaddress.ip_address(user)
if "/" not in user:
ipaddress.ip_address(user)
link = clean_link(ctx.client.create_article_path("Special:Contributions/{user}".format(user=user)))
except ValueError:
link = clean_link(ctx.client.create_article_path(sanitize_to_url(change["title"])))
@ -666,7 +683,7 @@ def compact_block_block(ctx: Context, change: dict):
if str(namespace) == "0":
namespaces.append("*{ns}*".format(ns=ctx._("(Main)")))
elif str(namespace) in ctx.client.namespaces: # if we have cached namespace name for given namespace number, add its name to the list
namespaces.append("*{ns}*".format(ns=ctx.client.namespaces[str(namespace)]["*"]))
namespaces.append("*{ns}*".format(ns=ctx.client.namespaces[str(namespace)]["name"]))
else:
namespaces.append("*{ns}*".format(ns=namespace))
restriction_description = restriction_description + ", ".join(namespaces)

View file

@ -161,7 +161,7 @@ class DomainManager:
wiki_json = wiki.json()
try:
wiki.statistics.update(Log(type=LogType.SCAN_REASON, title="Debug request for the wiki"))
params = OrderedDict({"action": "query", "format": "json", "uselang": "content", "list": "tags|recentchanges",
params = OrderedDict({"action": "query", "format": "json", "formatversion": "2", "uselang": "content", "list": "tags|recentchanges",
"meta": "siteinfo", "utf8": 1, "rcshow": "!bot",
"rcprop": "title|redirect|timestamp|ids|loginfo|parsedcomment|sizes|flags|tags|user|userid",
"rclimit": 500, "rctype": "edit|new|log|categorize", "siprop": "namespaces|general"})

View file

@ -324,7 +324,7 @@ class Wiki:
if warnings:
for warning in warnings:
logger.warning("MediaWiki returned the following warning: {code} - {text} on {url}.".format(
code=warning["code"], text=warning.get("text", warning.get("*", "")), url=url
code=warning["code"], text=warning.get("text", ""), url=url
))
return request_data
@ -445,7 +445,7 @@ class Wiki:
async def fetch_wiki(self, amount=10) -> dict:
if self.mw_messages is None or self.recache_requested:
params = OrderedDict({"action": "query", "format": "json", "uselang": "content", "list": "tags|recentchanges",
params = OrderedDict({"action": "query", "format": "json", "formatversion": "2", "uselang": "content", "list": "tags|recentchanges",
"meta": "allmessages|siteinfo",
"utf8": 1, "tglimit": "max", "tgprop": "displayname",
"rcprop": "title|redirect|timestamp|ids|loginfo|parsedcomment|sizes|flags|tags|user|userid",
@ -453,7 +453,7 @@ class Wiki:
"ammessages": "recentchanges-page-added-to-category|recentchanges-page-removed-from-category|recentchanges-page-added-to-category-bundled|recentchanges-page-removed-from-category-bundled",
"amenableparser": 1, "amincludelocal": 1, "siprop": "namespaces|general"})
else:
params = OrderedDict({"action": "query", "format": "json", "uselang": "content", "list": "recentchanges",
params = OrderedDict({"action": "query", "format": "json", "formatversion": "2", "uselang": "content", "list": "recentchanges",
"meta": "siteinfo", "utf8": 1, "rcshow": "!bot",
"rcprop": "title|redirect|timestamp|ids|loginfo|parsedcomment|sizes|flags|tags|user|userid",
"rclimit": amount, "rctype": "edit|new|log|categorize", "siprop": "namespaces|general"})
@ -613,15 +613,15 @@ def process_cachable(response: dict, wiki_object: Wiki) -> None:
mw_messages = response.get("query", {}).get("allmessages", [])
final_mw_messages = dict()
for msg in mw_messages:
if "missing" not in msg: # ignore missing strings
final_mw_messages[msg["name"]] = re.sub(r'\[\[.*?]]', '', msg["*"])
if not msg.get("missing", False): # ignore missing strings
final_mw_messages[msg["name"]] = re.sub(r'\[\[.*?]]', '', msg["content"])
else:
logger.warning("Could not fetch the MW message translation for: {}".format(msg["name"]))
wiki_object.mw_messages = MWMessages(final_mw_messages)
for tag in response["query"]["tags"]:
try:
if tag["displayname"]:
wiki_object.tags[tag["name"]] = (BeautifulSoup(tag["displayname"], "lxml")).get_text()
except KeyError:
else:
wiki_object.tags[tag["name"]] = None
wiki_object.namespaces = response["query"]["namespaces"]
wiki_object.recache_requested = False