Exemplo n.º 1
0
def get_rendered_sig(site: str, wikitext: str) -> str:
    url = f"https://{site}/api/rest_v1/transform/wikitext/to/html"
    payload = {"wikitext": wikitext, "body_only": True}
    text = datasources.backoff_retry("post", url, json=payload)
    text = text.replace("./", f"https://{site}/wiki/")
    _, sep1, rest = text.partition(">")
    inside, sep2, _ = rest.rpartition("</")
    if not sep1 or not sep2:
        return text
    else:
        return inside
Exemplo n.º 2
0
def evaluate_subst(text: str, sitedata: SiteData) -> str:
    """Perform substitution by removing "subst:" and expanding the wikitext"""
    for subst in sitedata.subst:
        text = text.replace(subst, "")
    data = {
        "action": "expandtemplates",
        "format": "json",
        "text": text,
        "prop": "wikitext",
    }
    url = f"https://{sitedata.hostname}/w/api.php"
    res = datasources.backoff_retry("get", url, params=data, output="json")
    return res["expandtemplates"]["wikitext"]
Exemplo n.º 3
0
def get_default_sig(site: str, user: str = "$1", nickname: str = "$2") -> str:
    url = f"https://{site}/w/api.php"
    params = {
        "action": "query",
        "format": "json",
        "meta": "allmessages",
        "formatversion": "2",
        "ammessages": "signature",
        "amprop": "",
        "amenableparser": 1,
    }
    res = datasources.backoff_retry("get", url, output="json", params=params)
    text = res["query"]["allmessages"][0]["content"]
    return text.replace("$1", user).replace("$2", nickname)
Exemplo n.º 4
0
def filter_page(url: str) -> Set[str]:
    if not url:
        return set()
    if "?" in url:
        url += "&action=raw"
    else:
        url += "?action=raw"

    res = datasources.backoff_retry("get", url, output="text")
    users = set()
    for line in res.split("\n"):
        match = re.search(
            r"(?<=User[_ ]talk:)[^\#\<\>\[\]\|\{\}\/:\n]*(?=[\}@\]])", line)
        if match:
            users.add(match.group(0))
    return users
Exemplo n.º 5
0
def get_lint_errors(sig: str, hostname: str, checks: Checks) -> Set[SigError]:
    """Use the REST API to get lint errors from the signature"""
    url = f"https://{hostname}/api/rest_v1/transform/wikitext/to/lint"
    data = {"wikitext": sig}

    res_json = datasources.backoff_retry("post", url, json=data, output="json")

    errors: Set[Optional[SigError]] = set()
    for error in res_json:
        if error.get("type", "") == "obsolete-tag":
            if checks & Checks.OBSOLETE_TAG:
                if error.get("params", {}).get("name", "") == "font":
                    errors.add(SigError("obsolete-font-tag"))
                else:
                    errors.add(lint_to_error(error))
        else:
            errors.add(lint_to_error(error))
    return cast(Set[SigError], errors - {None})