From 5b0b720a45df8abf35b75240b229c6c99f6df6db Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Thu, 18 Aug 2022 20:48:38 +0100 Subject: [PATCH 1/7] Hide DAB html until selected. --- templates/article.html | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/templates/article.html b/templates/article.html index 7c9afbf..d894d60 100644 --- a/templates/article.html +++ b/templates/article.html @@ -70,7 +70,7 @@ a.new { color: red; } cancel selection -
{{ dab.html | safe }}
+
{{ dab.html | safe }}
{% endfor %} @@ -90,6 +90,14 @@ a.new { color: red; } function jump_to(dab_num) { var highlight_title = "text-bg-primary"; + var dab_articles = document.getElementsByClassName("dab-article"); + for(var i=0; i Date: Thu, 18 Aug 2022 20:49:22 +0100 Subject: [PATCH 2/7] Add a global timeout value. --- dab_mechanic/wikidata_oauth.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/dab_mechanic/wikidata_oauth.py b/dab_mechanic/wikidata_oauth.py index 5af0976..5b048b2 100644 --- a/dab_mechanic/wikidata_oauth.py +++ b/dab_mechanic/wikidata_oauth.py @@ -3,8 +3,10 @@ from urllib.parse import urlencode from flask import current_app, session from requests_oauthlib import OAuth1Session -wiki_hostname = "en.wikipedia.org" -api_url = f"https://{wiki_hostname}/w/api.php" +WIKI_HOSTNAME = "en.wikipedia.org" +API_URL = f"https://{WIKI_HOSTNAME}/w/api.php" + +TIMEOUT = 20 def get_edit_proxy() -> dict[str, str]: @@ -28,12 +30,12 @@ def api_post_request(params: dict[str, str | int]): resource_owner_secret=session["owner_secret"], ) proxies = get_edit_proxy() - return oauth.post(api_url, data=params, timeout=10, proxies=proxies) + return oauth.post(API_URL, data=params, timeout=TIMEOUT, proxies=proxies) def raw_request(params): app = current_app - url = api_url + "?" + urlencode(params) + url = API_URL + "?" + urlencode(params) client_key = app.config["CLIENT_KEY"] client_secret = app.config["CLIENT_SECRET"] oauth = OAuth1Session( @@ -43,7 +45,7 @@ def raw_request(params): resource_owner_secret=session["owner_secret"], ) proxies = get_edit_proxy() - return oauth.get(url, timeout=10, proxies=proxies) + return oauth.get(url, timeout=TIMEOUT, proxies=proxies) def api_request(params): From daf2a254584b8eb79730a82b665a341f41650450 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Thu, 18 Aug 2022 20:51:18 +0100 Subject: [PATCH 3/7] Include all occurances of dab links, not just the first. --- dab_mechanic/wikipedia.py | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/dab_mechanic/wikipedia.py b/dab_mechanic/wikipedia.py index 57c03c4..9bcb0fa 100644 --- a/dab_mechanic/wikipedia.py +++ b/dab_mechanic/wikipedia.py @@ -121,10 +121,9 @@ def delete_toc(root: lxml.html.HtmlElement) -> None: toc.getparent().remove(toc) -def get_dab_html(dab_num: int, title: str) -> str: +def get_dab_html(dab_num: int, html: str) -> str: """Parse dab page and rewrite links.""" - dab_html = get_article_html(title) - root = lxml.html.fromstring(dab_html) + root = lxml.html.fromstring(html) delete_toc(root) element_id_map = {e.get("id"): e for e in root.findall(".//*[@id]")} @@ -160,6 +159,7 @@ class Article: self.dab_lookup: dict[int, str] = {} self.dab_order: list[str] = [] self.parse: Optional[dict[str, Any]] = None + self.dab_html: dict[str, str] = {} def save_endpoint(self) -> str: """Endpoint for saving changes.""" @@ -173,28 +173,25 @@ class Article: def iter_links(self) -> Iterator[tuple[lxml.html.Element, str]]: """Disambiguation links that need fixing.""" - seen = set() for a in self.root.findall(".//a[@href]"): title = a.get("title") if title is None or title not in self.links: continue - a.set("class", "disambig") - - if title in seen: - continue - seen.add(title) - yield a, title def process_links(self) -> None: """Process links in parsed wikitext.""" for dab_num, (a, title) in enumerate(self.iter_links()): + a.set("class", "disambig") a.set("id", f"dab-{dab_num}") + if title not in self.dab_html: + self.dab_html[title] = get_article_html(title) + dab: DabItem = { "num": dab_num, "title": title, - "html": get_dab_html(dab_num, title), + "html": get_dab_html(dab_num, self.dab_html[title]), } self.dab_list.append(dab) self.dab_order.append(title) From 0cc4f3ed7fd08c56d52a09538e12327bfd38b3a3 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Fri, 19 Aug 2022 10:59:53 +0100 Subject: [PATCH 4/7] WIP --- dab_mechanic/wikipedia.py | 22 +++++++++++++++------- templates/article.html | 21 +++++++++++++++++++-- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/dab_mechanic/wikipedia.py b/dab_mechanic/wikipedia.py index 9bcb0fa..8ee58d1 100644 --- a/dab_mechanic/wikipedia.py +++ b/dab_mechanic/wikipedia.py @@ -68,7 +68,7 @@ def needs_disambig(link: dict[str, Any]) -> bool: ) -def get_article_links(enwiki: str) -> list[str]: +def get_article_links(enwiki: str) -> dict[str, str]: """Get links that appear in this article.""" params: dict[str, str | int] = link_params(enwiki) @@ -92,11 +92,13 @@ def get_article_links(enwiki: str) -> list[str]: params["gplcontinue"] = data["continue"]["gplcontinue"] sleep(0.1) + ret_links = {} for link in set(links): - if link in redirects: - links.update(redirects[link]) + ret_links[link] = link + for r in redirects.get(link, []): + ret_links[r] = link - return list(links) + return ret_links # return {link["title"] for link in r.json()["query"]["pages"][0]["links"]} @@ -175,13 +177,18 @@ class Article: """Disambiguation links that need fixing.""" for a in self.root.findall(".//a[@href]"): title = a.get("title") - if title is None or title not in self.links: + if title is not None and title in self.links: + yield a, title, self.links[title] + + href = a.get("href") + if not href.startswith("/wiki/"): continue - yield a, title + a.set("href", "https://en.wikipedia.org" + href) + a.set("target", "_blank") def process_links(self) -> None: """Process links in parsed wikitext.""" - for dab_num, (a, title) in enumerate(self.iter_links()): + for dab_num, (a, link_to, title) in enumerate(self.iter_links()): a.set("class", "disambig") a.set("id", f"dab-{dab_num}") @@ -191,6 +198,7 @@ class Article: dab: DabItem = { "num": dab_num, "title": title, + "link_to": link_to, "html": get_dab_html(dab_num, self.dab_html[title]), } self.dab_list.append(dab) diff --git a/templates/article.html b/templates/article.html index d894d60..3f8ec6a 100644 --- a/templates/article.html +++ b/templates/article.html @@ -63,6 +63,7 @@ a.new { color: red; } {% for dab in article.dab_list %}

{{ dab.title }}

+ {% if dab.title != dab.link_to %}
redirect from {{ dab.link_to }}
{% endif %}
highlight link @@ -87,7 +88,25 @@ a.new { color: red; } var dab_lookup = {{ article.dab_lookup | tojson }}; var dab_order = {{ article.dab_order | tojson }}; + var dab_links = document.getElementsByClassName("disambig"); + for(var i=0; i { + event.preventDefault(); + var dab_num = event.target.id.substring(4); + open_dab(dab_num); + }); + } + function jump_to(dab_num) { + open_dab(dab_num); + + var link = document.getElementById("dab-" + dab_num); + link.scrollIntoView(); + link.classList.add("disambig-highlight") + return false; + } + + function open_dab(dab_num) { var highlight_title = "text-bg-primary"; var dab_articles = document.getElementsByClassName("dab-article"); @@ -112,9 +131,7 @@ a.new { color: red; } card_title.classList.add(highlight_title); var link = document.getElementById("dab-" + dab_num); - link.scrollIntoView(); link.classList.add("disambig-highlight") - return false; } function clear_dab_highlight(dab_num) { From 16e776c058fef09c082569ce4516505415ca0b40 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Sun, 21 Aug 2022 11:30:02 +0100 Subject: [PATCH 5/7] Add code for mediawiki compare API --- dab_mechanic/mediawiki_api.py | 15 +++++++++++++++ templates/{save.html => preview.html} | 0 2 files changed, 15 insertions(+) rename templates/{save.html => preview.html} (100%) diff --git a/dab_mechanic/mediawiki_api.py b/dab_mechanic/mediawiki_api.py index 26d7a20..f681af3 100644 --- a/dab_mechanic/mediawiki_api.py +++ b/dab_mechanic/mediawiki_api.py @@ -43,3 +43,18 @@ def get_content(title: str) -> str: data = call(params) rev: str = data["query"]["pages"][0]["revisions"][0]["content"] return rev + + +def compare(title: str, new_text: str) -> str: + """Generate a diff for the new article text.""" + params: dict[str, str | int] = { + "format": "json", + "formatversion": 2, + "action": "compare", + "fromtitle": title, + "toslots": "main", + "totext-main": new_text, + "prop": "diff", + } + diff: str = call(params)["compare"]["body"] + return diff diff --git a/templates/save.html b/templates/preview.html similarity index 100% rename from templates/save.html rename to templates/preview.html From 7813d1a7f04656c4c5330fc7ccd7c2a8ea96a7a4 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Sun, 21 Aug 2022 11:31:21 +0100 Subject: [PATCH 6/7] Rename save page to preview page --- dab_mechanic/wikipedia.py | 3 ++ templates/article.html | 17 +++++---- templates/preview.html | 28 ++++++++++++--- web_view.py | 74 ++++++++++++++++++++++++++++----------- 4 files changed, 90 insertions(+), 32 deletions(-) diff --git a/dab_mechanic/wikipedia.py b/dab_mechanic/wikipedia.py index 8ee58d1..96eb110 100644 --- a/dab_mechanic/wikipedia.py +++ b/dab_mechanic/wikipedia.py @@ -186,6 +186,9 @@ class Article: a.set("href", "https://en.wikipedia.org" + href) a.set("target", "_blank") + def dab_link_to(self): + return [dab["link_to"] for dab in self.dab_list] + def process_links(self) -> None: """Process links in parsed wikitext.""" for dab_num, (a, link_to, title) in enumerate(self.iter_links()): diff --git a/templates/article.html b/templates/article.html index 3f8ec6a..87680b0 100644 --- a/templates/article.html +++ b/templates/article.html @@ -54,7 +54,7 @@ a.new { color: red; }

{{ article.enwiki }}

- +
@@ -62,8 +62,9 @@ a.new { color: red; }
There are {{ article.dab_list | count }} links in the article that need disambiguating.
{% for dab in article.dab_list %}
-

{{ dab.title }}

- {% if dab.title != dab.link_to %}
redirect from {{ dab.link_to }}
{% endif %} +
+

{{ dab.title }}

+ {% if dab.title != dab.link_to %}
redirect from {{ dab.link_to }}
{% endif %}
highlight link @@ -73,6 +74,7 @@ a.new { color: red; }
{{ dab.html | safe }}
+
{% endfor %}
@@ -85,8 +87,8 @@ a.new { color: red; } var edit_set = new Set(); var edits = {}; - var dab_lookup = {{ article.dab_lookup | tojson }}; var dab_order = {{ article.dab_order | tojson }}; + var dab_link_to = {{ article.dab_link_to() | tojson }}; var dab_links = document.getElementsByClassName("disambig"); for(var i=0; i edits[t]).map(t => [t, edits[t]]); + var saves = dab_link_to.map((link_to, num) => ( + {"num": num, "link_to": link_to, "title": edits[num]})); var save_edits = document.getElementById("save-edits"); save_edits.value = JSON.stringify(saves); } @@ -166,7 +169,7 @@ a.new { color: red; } document.getElementById("cancel-" + dab_num).classList.remove("d-none"); var title = element.getAttribute("title"); - edits[dab_lookup[dab_num]] = title; + edits[dab_num] = title; edit_set.add(dab_num); update_edits(); @@ -188,7 +191,7 @@ a.new { color: red; } } function cancel_selection(dab_num) { - delete edits[dab_lookup[dab_num]]; + delete edits[dab_num]; document.getElementById("cancel-" + dab_num).classList.add("d-none"); clear_dab_highlight(dab_num); edit_set.delete(dab_num); diff --git a/templates/preview.html b/templates/preview.html index 7bd28a5..68fa693 100644 --- a/templates/preview.html +++ b/templates/preview.html @@ -4,15 +4,33 @@ {{ title }} – dab mechanic +
-

Save edits: {{ title }}

-

Edit summary: {{ edit_summary }}

-
-
-
{{ text }}
+

Preview of changes: {{ title }}

+
+
+
Edit summary
+

{{ edit_summary }}

+
+ {#
{{ text }}
#} + + + + + + + + + + {{ diff | safe }} + +
+ + + diff --git a/web_view.py b/web_view.py index 2730f23..cfdff71 100755 --- a/web_view.py +++ b/web_view.py @@ -3,7 +3,8 @@ import inspect import json import re -from typing import Optional +from typing import Optional, TypedDict +import mwparserfromhell import flask import lxml.html @@ -73,19 +74,22 @@ def index(): return flask.render_template("index.html", articles=articles) -def make_disamb_link(edit: tuple[str, str]) -> str: - """Given an edit return the appropriate link.""" - return f"[[{edit[1]}|{edit[0]}]]" +class Edit(TypedDict): + """Edit to an article.""" + + num: int + link_to: str + title: str -def apply_edits(article_text: str, edits: list[tuple[str, str]]) -> str: +def apply_edits(article_text: str, edits: list[Edit]) -> str: """Apply edits to article text.""" def escape(s: str) -> str: return re.escape(s).replace("_", "[ _]").replace(r"\ ", "[ _]") - for link_from, link_to in edits: - print(rf"\[\[{escape(link_from)}\]\]") + for edit in edits: + # print(rf"\[\[{escape(link_from)}\]\]") article_text = re.sub( rf"\[\[{escape(link_from)}\]\]", f"[[{link_to}|{link_from}]]", @@ -95,31 +99,61 @@ def apply_edits(article_text: str, edits: list[tuple[str, str]]) -> str: return article_text -@app.route("/save/", methods=["POST"]) -def save(enwiki: str) -> Response | str: - """Save edits to article.""" - edits = [ - (link_to, link_from) - for link_to, link_from in json.loads(flask.request.form["edits"]) - ] +def make_disamb_link(edit: Edit) -> str: + """Given an edit return the appropriate link.""" + return f"[[{edit['title']}|{edit['link_to']}]]" - enwiki = enwiki.replace("_", " ") + +def build_edit_summary(edits: list[Edit]) -> str: + """Given a list of edits return an edit summary.""" titles = ", ".join(make_disamb_link(edit) for edit in edits[:-1]) if len(titles) > 1: titles += " and " titles += make_disamb_link(edits[-1]) - edit_summary = f"Disambiguate {titles} using [[User:Edward/Dab mechanic]]" + return f"Disambiguate {titles} using [[User:Edward/Dab mechanic]]" - article_text = apply_edits(mediawiki_api.get_content(enwiki), edits) +def get_links(wikicode, edits): + dab_titles = {dab["link_to"] for dab in edits} + return [ + link for link in wikicode.filter_wikilinks() if str(link.title) in dab_titles + ] + + +@app.route("/preview/", methods=["POST"]) +def preview(enwiki: str) -> Response | str: + """Save edits to article.""" + enwiki = enwiki.replace("_", " ") + + dab_links = json.loads(flask.request.form["edits"]) + edits = [edit for edit in dab_links if edit.get("title")] + + edit_summary = build_edit_summary(edits) + # return flask.jsonify(edits=dab_links, edit_summary=edit_summary) + + text = mediawiki_api.get_content(enwiki) + wikicode = mwparserfromhell.parse(text) + links = get_links(wikicode, dab_links) + assert len(links) == len(dab_links) + + for wikilink, edit in zip(links, dab_links): + print(edit, wikilink) + if not edit.get("title"): + continue + if not wikilink.text: + wikilink.text = wikilink.title + wikilink.title = edit["title"] + + diff = mediawiki_api.compare(enwiki, str(wikicode)) return flask.render_template( - "save.html", + "peview.html", edit_summary=edit_summary, title=enwiki, - edits=edits, - text=article_text, + edits=dab_links, + # text=str(wikicode), + diff=diff, ) From b6953cf52f1186770b7b03eacf07eb9d780edd81 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Fri, 29 Sep 2023 14:17:56 +0100 Subject: [PATCH 7/7] WIP --- dab_mechanic/mediawiki_api.py | 39 ++++++++++++-- dab_mechanic/wikipedia.py | 4 +- templates/article.html | 2 +- templates/index.html | 14 +++++ templates/navbar.html | 8 +-- templates/preview.html | 3 ++ web_view.py | 99 ++++++++++++++++++++++++++++------- 7 files changed, 135 insertions(+), 34 deletions(-) diff --git a/dab_mechanic/mediawiki_api.py b/dab_mechanic/mediawiki_api.py index f681af3..265cb73 100644 --- a/dab_mechanic/mediawiki_api.py +++ b/dab_mechanic/mediawiki_api.py @@ -30,19 +30,32 @@ def call(params: dict[str, str | int]) -> dict[str, Any]: return data.json() -def get_content(title: str) -> str: +def article_exists(title: str) -> bool: + """Get article text.""" + params: dict[str, str | int] = { + "action": "query", + "format": "json", + "formatversion": 2, + "titles": title, + } + return not call(params)["query"]["pages"][0].get("missing") + + +def get_content(title: str) -> tuple[str, int]: """Get article text.""" params: dict[str, str | int] = { "action": "query", "format": "json", "formatversion": 2, "prop": "revisions|info", - "rvprop": "content|timestamp", + "rvprop": "content|timestamp|ids", "titles": title, } data = call(params) - rev: str = data["query"]["pages"][0]["revisions"][0]["content"] - return rev + rev = data["query"]["pages"][0]["revisions"][0] + content: str = rev["content"] + revid: int = int(rev["revid"]) + return content, revid def compare(title: str, new_text: str) -> str: @@ -58,3 +71,21 @@ def compare(title: str, new_text: str) -> str: } diff: str = call(params)["compare"]["body"] return diff + + +def edit_page( + title: str, text: str, summary: str, baserevid: str, token: str +) -> dict[str, str | int]: + """Edit a page on Wikipedia.""" + params: dict[str, str | int] = { + "format": "json", + "formatversion": 2, + "action": "edit", + "title": title, + "text": text, + "baserevid": baserevid, + "token": token, + "summary": summary, + } + edit: str = call(params)["edit"] + return edit diff --git a/dab_mechanic/wikipedia.py b/dab_mechanic/wikipedia.py index 96eb110..fecf7f2 100644 --- a/dab_mechanic/wikipedia.py +++ b/dab_mechanic/wikipedia.py @@ -163,9 +163,9 @@ class Article: self.parse: Optional[dict[str, Any]] = None self.dab_html: dict[str, str] = {} - def save_endpoint(self) -> str: + def preview_endpoint(self) -> str: """Endpoint for saving changes.""" - href: str = flask.url_for("save", enwiki=self.enwiki.replace(" ", "_")) + href: str = flask.url_for("preview", enwiki=self.enwiki.replace(" ", "_")) return href def load(self) -> None: diff --git a/templates/article.html b/templates/article.html index 87680b0..eb1e62d 100644 --- a/templates/article.html +++ b/templates/article.html @@ -53,7 +53,7 @@ a.new { color: red; }

{{ article.enwiki }}

-
+ diff --git a/templates/index.html b/templates/index.html index 1758d8e..d6d6ca3 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,7 +1,21 @@ {% extends "base.html" %} +{% block title %}DAB Mechanic{% endblock %} + {% block content %}
+ + + article title: + + + + + {% if title and not exists %} +

No article titled "{{ title }}" found in Wikipedia.

+ {% endif %} + +
    {% for enwiki, count in articles %}
  1. diff --git a/templates/navbar.html b/templates/navbar.html index 9ce6f07..0b52925 100644 --- a/templates/navbar.html +++ b/templates/navbar.html @@ -15,13 +15,7 @@ Dab Mechanic diff --git a/templates/preview.html b/templates/preview.html index 68fa693..cd4bb3a 100644 --- a/templates/preview.html +++ b/templates/preview.html @@ -30,7 +30,10 @@ +
    + +
    diff --git a/web_view.py b/web_view.py index cfdff71..aa962bc 100755 --- a/web_view.py +++ b/web_view.py @@ -5,6 +5,7 @@ import json import re from typing import Optional, TypedDict import mwparserfromhell +from pprint import pprint import flask import lxml.html @@ -65,13 +66,25 @@ def parse_articles_with_dab_links(root: lxml.html.Element) -> list[tuple[str, in @app.route("/") def index(): + title = flask.request.args.get("title") + exists = None + if title: + title = title.strip() + exists = mediawiki_api.article_exists(title) + if exists: + return flask.redirect( + flask.url_for("article_page", enwiki=title.replace(" ", "_")) + ) + r = requests.get(awdl_url, params={"limit": 100}) root = lxml.html.fromstring(r.content) articles = parse_articles_with_dab_links(root) # articles = [line[:-1] for line in open("article_list")] - return flask.render_template("index.html", articles=articles) + return flask.render_template( + "index.html", title=title, exists=exists, articles=articles, + ) class Edit(TypedDict): @@ -82,7 +95,7 @@ class Edit(TypedDict): title: str -def apply_edits(article_text: str, edits: list[Edit]) -> str: +def old_apply_edits(article_text: str, edits: list[Edit]) -> str: """Apply edits to article text.""" def escape(s: str) -> str: @@ -114,49 +127,92 @@ def build_edit_summary(edits: list[Edit]) -> str: return f"Disambiguate {titles} using [[User:Edward/Dab mechanic]]" -def get_links(wikicode, edits): + +def get_links(wikicode, dab_links): + edits = [edit for edit in dab_links if edit.get("title")] + dab_titles = {dab["link_to"] for dab in edits} return [ link for link in wikicode.filter_wikilinks() if str(link.title) in dab_titles ] -@app.route("/preview/", methods=["POST"]) -def preview(enwiki: str) -> Response | str: - """Save edits to article.""" - enwiki = enwiki.replace("_", " ") - - dab_links = json.loads(flask.request.form["edits"]) - edits = [edit for edit in dab_links if edit.get("title")] - - edit_summary = build_edit_summary(edits) - # return flask.jsonify(edits=dab_links, edit_summary=edit_summary) - - text = mediawiki_api.get_content(enwiki) +def apply_edits(text, dab_links): wikicode = mwparserfromhell.parse(text) links = get_links(wikicode, dab_links) + if len(links) != len(dab_links): + print("links:", len(links)) + print("dab_links:", len(dab_links)) + print("dab_links:", dab_links) assert len(links) == len(dab_links) for wikilink, edit in zip(links, dab_links): - print(edit, wikilink) if not edit.get("title"): continue if not wikilink.text: wikilink.text = wikilink.title wikilink.title = edit["title"] - diff = mediawiki_api.compare(enwiki, str(wikicode)) + return str(wikicode) + + +@app.route("/preview/", methods=["POST"]) +def preview(enwiki: str) -> Response | str: + """Preview article edits.""" + enwiki = enwiki.replace("_", " ") + + dab_links = json.loads(flask.request.form["edits"]) + dab_links = [link for link in dab_links if "title" in link] + cur_text, baserevid = mediawiki_api.get_content(enwiki) + + text = apply_edits(cur_text, dab_links) + diff = mediawiki_api.compare(enwiki, text) return flask.render_template( - "peview.html", - edit_summary=edit_summary, + "preview.html", + edit_summary=build_edit_summary(dab_links), title=enwiki, edits=dab_links, - # text=str(wikicode), diff=diff, ) +def do_save(enwiki: str): + """Update page on Wikipedia.""" + dab_links = json.loads(flask.request.form["edits"]) + dab_links = [link for link in dab_links if "title" in link] + + cur_text, baserevid = mediawiki_api.get_content(enwiki) + + new_text = apply_edits(cur_text, dab_links) + token = wikidata_oauth.get_token() + + summary = build_edit_summary(dab_links) + print(summary) + + edit = mediawiki_api.edit_page( + title=enwiki, + text=new_text, + summary=summary, + baserevid=baserevid, + token=token, + ) + + return edit + + +@app.route("/save/", methods=["GET", "POST"]) +def save(enwiki: str) -> Response | str: + """Save edits to article.""" + enwiki_norm = enwiki.replace("_", " ") + + if flask.request.method == "GET": + return flask.render_template("edit_saved.html", title=enwiki_norm) + + do_save(enwiki_norm) + return flask.redirect(flask.url_for(flask.request.endpoint, enwiki=enwiki)) + + def redirect_if_needed(enwiki: str) -> Optional[Response]: """Check if there are spaces in the article name and redirect.""" return ( @@ -175,6 +231,9 @@ def article_page(enwiki: str) -> Response: if redirect: return redirect + if "owner_key" not in flask.session: + return flask.render_template("login_needed.html") + article = wikipedia.Article(enwiki) article.load() article.process_links()