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; }