RcGcDw/extensions/base/mediawiki.py

124 lines
5 KiB
Python
Raw Normal View History

2021-04-24 09:19:38 +00:00
# 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 <http://www.gnu.org/licenses/>.
import logging
import math
from src.discord.message import DiscordMessage
from src.api import formatter
from src.i18n import rc_formatters
from src.api.context import Context
2021-04-28 11:37:32 +00:00
from src.api.util import embed_helper, sanitize_to_url, parse_mediawiki_changes, clean_link, compact_author
2021-04-24 09:19:38 +00:00
from src.configloader import settings
2021-04-24 12:55:37 +00:00
from src.exceptions import *
2021-04-24 09:19:38 +00:00
_ = rc_formatters.gettext
logger = logging.getLogger("extensions.base")
2021-04-27 13:10:29 +00:00
# Page edit - event edit
@formatter.embed(event="edit", mode="embed")
def embed_edit(ctx: Context, change: dict) -> DiscordMessage:
embed = DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url)
2021-04-27 13:10:29 +00:00
embed_helper(ctx, embed, change)
action = ctx.event
editsize = change["newlen"] - change["oldlen"]
if editsize > 0:
2021-04-27 13:10:29 +00:00
embed["color"] = min(65280, 35840 + (math.floor(editsize / 52)) * 256) # Choose shade of green
elif editsize < 0:
2021-04-27 13:10:29 +00:00
embed["color"] = min(16711680, 9175040 + (math.floor(abs(editsize) / 52)) * 65536) # Choose shade of red
elif editsize == 0:
embed["color"] = 8750469
if change["title"].startswith("MediaWiki:Tag-"): # Refresh tag list when tag display name is edited
ctx.client.refresh_internal_data()
2021-04-28 11:37:32 +00:00
# Sparse is better than dense.
# Readability counts.
embed["url"] = "{wiki}index.php?title={article}&curid={pageid}&diff={diff}&oldid={oldrev}".format(
wiki=ctx.client.WIKI_SCRIPT_PATH,
pageid=change["pageid"],
diff=change["revid"],
oldrev=change["old_revid"],
article=sanitize_to_url(change["title"])
)
embed["title"] = "{redirect}{article} ({new}{minor}{bot}{space}{editsize})".format(
2021-04-28 11:37:32 +00:00
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 "")
if settings["appearance"]["embed"]["show_edit_changes"]:
try:
if action == "new":
changed_content = ctx.client.make_api_request(
2021-04-28 11:37:32 +00:00
"?action=compare&format=json&fromslots=main&torev={diff}&fromtext-main=&topst=1&prop=diff".format(
diff=change["revid"]), "compare", "*")
2021-04-24 09:19:38 +00:00
else:
changed_content = ctx.client.make_api_request(
2021-04-27 13:10:29 +00:00
"?action=compare&format=json&fromrev={oldrev}&torev={diff}&topst=1&prop=diff".format(
diff=change["revid"], oldrev=change["old_revid"]), "compare", "*")
except ServerError:
changed_content = None
if changed_content:
2021-04-28 11:37:32 +00:00
parse_mediawiki_changes(ctx, changed_content, embed)
else:
logger.warning("Unable to download data on the edit content!")
2021-04-28 11:37:32 +00:00
embed["description"] = ctx.parsedcomment
return embed
2021-04-24 09:19:38 +00:00
2021-04-27 13:10:29 +00:00
@formatter.compact(event="edit", mode="compact")
def compact_edit(ctx: Context, change: dict):
2021-04-28 11:37:32 +00:00
parsed_comment = "" if ctx.parsedcomment is None else " *(" + ctx.parsedcomment + ")*"
author, author_url = compact_author(ctx, change)
2021-04-27 13:10:29 +00:00
action = ctx.event
2021-04-28 11:37:32 +00:00
edit_link = clean_link("{wiki}index.php?title={article}&curid={pageid}&diff={diff}&oldid={oldrev}".format(
2021-04-27 13:10:29 +00:00
wiki=ctx.client.WIKI_SCRIPT_PATH, pageid=change["pageid"], diff=change["revid"], oldrev=change["old_revid"],
2021-04-28 11:37:32 +00:00
article=sanitize_to_url(change["title"])))
2021-04-27 13:10:29 +00:00
logger.debug(edit_link)
edit_size = change["newlen"] - change["oldlen"]
sign = ""
if edit_size > 0:
sign = "+"
bold = ""
if abs(edit_size) > 500:
bold = "**"
if action == "edit":
content = _(
"[{author}]({author_url}) edited [{article}]({edit_link}){comment} {bold}({sign}{edit_size}){bold}").format(
author=author, author_url=author_url, article=change["title"], edit_link=edit_link, comment=parsed_comment,
edit_size=edit_size, sign=sign, bold=bold)
else:
content = _(
"[{author}]({author_url}) created [{article}]({edit_link}){comment} {bold}({sign}{edit_size}){bold}").format(
author=author, author_url=author_url, article=change["title"], edit_link=edit_link, comment=parsed_comment,
edit_size=edit_size, sign=sign, bold=bold)
return DiscordMessage(ctx.message_type, ctx.event, ctx.webhook_url, content=content)
2021-04-28 11:37:32 +00:00
# Page creation - event new aliases to embed_edit since they share a lot of their code
2021-04-27 13:10:29 +00:00
@formatter.embed(event="new", mode="embed")
def embed_new(ctx, change):
return embed_edit(ctx, change)
@formatter.compact(event="new", mode="compact")
def compact_new(ctx, change):
return compact_edit(ctx, change)