Skip to main content

mediawiki-abusefilter

CI Documentation status PyPI Python License Latest Release

Python client for working with MediaWiki AbuseFilters.

It provides a simple interface for listing, searching, creating, editing, and reviewing filters from Python.

Installation

pip install mediawiki-abusefilter

Quick start

import mediawiki_abusefilter as mwaf

filters = mwaf.filters(
    "https://example.org",
    username="MyBot@mybotpassword",
    password="BOT_PASSWORD"
)

filter = filters.get(123)

print(filter.description)
print(filter.rules)

The authentication object can also be created separately and reused:

auth = mwaf.auth(
    "https://example.org",
    username="MyBot@mybotpassword",
    password="BOT_PASSWORD"
)

filters = mwaf.filters("https://example.org", auth=auth)

Listing filters

filters.list()

Filters can be narrowed by status and visibility:

filters.list(enabled=True)
filters.list(public=True)
filters.list(private=True)

To search by text:

matches = filters.search("spam")

Large result sets can be limited with limit:

filters.list(limit=100)
filters.search("spam", limit=20)

Creating a filter

filter = filters.create(
    "Low edit count edits",
    "page_namespace == 0 & user_editcount < 10"
)

Filters can also be created with notes, actions, and other options:

filter = filters.create(
    "Example filter",
    "page_namespace == 0",
    notes="Created for testing",
    enabled=False,
    public=False,
    actions={
        "warn": "abusefilter-warning",
        "tag": ["review", "test"]
    }
)

Use dry_run=True to preview a new filter without saving it:

change = filters.create(
    "Example filter",
    "page_namespace == 0",
    dry_run=True
)

print(change)

Editing a filter

Get the filter and change only the values you need:

filter = filters.get(123)

filter.edit(description="Updated description")
filter.edit(enabled=False)
filter.edit(public=False)

Values that are not supplied are left unchanged.

Several changes can be made together:

filter.edit(
    description="Updated filter",
    rules="page_namespace == 0 & user_editcount < 20",
    enabled=True,
    public=True,
    actions={"warn": "abusefilter-warning"}
)

Notes

Notes are appended by default:

filter.edit(notes="Changed the edit-count limit")

To replace the existing notes:

filter.edit(
    notes="Updated notes",
    notes_mode="replace"
)

To clear the notes:

filter.edit(
    notes="",
    notes_mode="replace"
)

Notes can also include the current account and date:

filter.edit(
    notes="Changed the rule",
    sign_notes=True
)

Rule changes

Replace the complete rule:

filter.edit(
    rules="page_namespace == 0"
)

Or modify part of the existing rule:

filter.edit(replace=("old text", "new text"))
filter.edit(append=" & user_editcount < 20")
filter.edit(prepend="page_namespace == 0 & ")
filter.edit(remove=" & page_namespace == 1")
filter.edit(regex=(r"user_editcount\s*<\s*10", "user_editcount < 20"))

Replacement operations are strict by default. Use strict=False when a missing match should not raise an error:

filter.edit(
    replace=("old text", "new text"),
    strict=False
)

Actions

Actions can be configured when creating or editing a filter:

filter.edit(
    actions={
        "warn": "abusefilter-warning",
        "disallow": "abusefilter-disallowed",
        "blockautopromote": True,
        "block": {
            "talk": True,
            "anonymous": "1 day",
            "user": "1 day"
        },
        "tag": ["review", "bot"],
        "throttle": {
            "count": 5,
            "period": 120,
            "groups": "user"
        }
    }
)

Set an action to False to remove it:

filter.edit(
    actions={
        "warn": False,
        "tag": False
    }
)

History

Filter history is available through history():

for entry in filter.history(limit=20):
    print(
        entry["id"],
        entry["user"],
        entry["description"]
    )

Previewing changes

Use dry_run=True to inspect a change before applying it:

change = filter.edit(
    description="Preview only",
    rules="page_namespace == 0",
    dry_run=True
)

print(change)

Changes made through edit() and create() are verified by default.

Set verify=False when you specifically need to skip that verification:

filter.edit(
    rules="page_namespace == 0",
    verify=False
)

Account information

The current account can be inspected with whoami():

info = filters.whoami()

print(info["name"])
print(info["groups"])

Delete

filter.delete()

Requirements

Python 3.9 or newer.

Documentation

Full API documentation and usage examples are available on Read the Docs.

License

MIT

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mediawiki_abusefilter-0.1.2.tar.gz (16.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mediawiki_abusefilter-0.1.2-py3-none-any.whl (15.6 kB view details)

Uploaded Python 3

File details

Details for the file mediawiki_abusefilter-0.1.2.tar.gz.

File metadata

  • Download URL: mediawiki_abusefilter-0.1.2.tar.gz
  • Upload date:
  • Size: 16.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.3

File hashes

Hashes for mediawiki_abusefilter-0.1.2.tar.gz
Algorithm Hash digest
SHA256 5e61ef978094b5f6ba01c25929c91a6bf5e12883726546933cabeccc406054aa
MD5 b5c6389c6aa99b04ea2ac0ff71f8e3cf
BLAKE2b-256 e5e3c6493929cb9f986c7cb49a6d8c05e4c21724c6af1590f57d43cd5f1f1354

See more details on using hashes here.

File details

Details for the file mediawiki_abusefilter-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for mediawiki_abusefilter-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d2d7fe308378c30a2646fad97d47b9931b3b9d476f43e30ee9a605b552f0f94e
MD5 b808e9eeb8f3243267c77b4a61eac508
BLAKE2b-256 c4b3823b1bd65b09f9a65fa50d2aa59b1c2be0a47fdd6b2a9d77fa614b032f51

See more details on using hashes here.

Release history Release notifications | RSS feed

0.1.3

2 files

This release

0.1.2 This release

2 files

0.1.1

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page