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.0.tar.gz (12.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.0-py3-none-any.whl (15.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: mediawiki_abusefilter-0.1.0.tar.gz
  • Upload date:
  • Size: 12.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.0.tar.gz
Algorithm Hash digest
SHA256 0c8b6f9855476bf98edb35745e19e9cf4ce755892cc3718c12342a4ef2bc0811
MD5 e43e35aa9a677322af687a1034a70849
BLAKE2b-256 191786adc3afe9bcd5116301667a2076592801b04912a364c7b509311c023faa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mediawiki_abusefilter-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e7677b37250538b89f049ef61ad3897198449328b46c23b07ef602c0c6b06348
MD5 d772ef12a2804ab5ddeac4dcaf4e8819
BLAKE2b-256 402d96bc4d91ba14cca59d62df94669274e54b33adff0d58ec0c821907f5ef01

See more details on using hashes here.

Release history Release notifications | RSS feed

0.1.3

2 files

0.1.2

2 files

0.1.1

2 files

This release

0.1.0 This release

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