Skip to main content

wmlinksfromhell

CI PyPI Python License Latest Release

wmlinksfromhell is a Python package for working with links on Wikimedia and other MediaWiki sites. It is mainly focused on Wikimedia interwiki links, language and project prefixes, and Wikimedia URLs, with support for local links and external URLs too.

It can resolve links to their destination wiki and page, convert them between interwiki and URL forms, and give you the details you need to work with them in your code.

Developed by Max.

Full documentation is available on Read the Docs. Development takes place on GitHub, and bugs and feature requests can be reported in the issue tracker.

Project links

Install

python3 -m pip install wmlinksfromhell

Pywikibot support is optional:

python3 -m pip install 'wmlinksfromhell[pywikibot]'

Supports Python 3.9 and newer.

Quick start

Get information about a wiki:

import wmlinksfromhell

wiki = wmlinksfromhell.wiki("enwiki")

print(wiki.language)       # en
print(wiki.hostname)       # en.wikipedia.org
print(wiki.page_url("Apple"))
print(wiki.interwiki("Apple"))

Resolve a Wikimedia link:

result = wmlinksfromhell.resolve("w:en:Apple")

print(result.dbname)        # enwiki
print(result.title)         # Apple
print(result.url)           # https://en.wikipedia.org/wiki/Apple

Use the full project name for project/language interwiki output when needed:

result = wmlinksfromhell.resolve_url(
    "https://hi.wikipedia.org/wiki/Apple",
    long_project_prefix=True,
)

print(result.canonical_interwiki)  # wikipedia:hi:Apple

The short form remains the default, and wikipedia:hi:Apple is accepted as input.

Parse links from MediaWiki text:

import wmlinksfromhell

code = wmlinksfromhell.parse(
    "[[w:en:Apple]] https://de.wikipedia.org/wiki/Berlin https://example.org/",
    source="metawiki",
)

for link in code.filter_links():
    print(link.original)
    print(link.dbname, link.title, link.url)

The parser keeps the original way a link was written separate from the destination it resolves to. This lets you work with the same destination whether it came from an interwiki link, a wiki URL, or another supported form.

Wikimedia links

The resolver understands Wikimedia language and project prefixes, including prefix chains:

w:en:Apple
wikt:simple:uppercase
b:de:Main Page
:de:q:Hauptseite
:m:en:About
:w:it:b:Wiskunde

It also handles Wikimedia URL forms such as article paths and index.php, including fragments, revisions, diffs, actions, and page IDs.

For example:

result = wmlinksfromhell.resolve(
    "https://en.wikipedia.org/w/index.php?title=Apple&oldid=123456",
)

print(result.dbname)       # enwiki
print(result.revision)     # 123456
print(result.interwiki)    # w:en:Special:PermanentLink/123456

A leading colon changes MediaWiki's special handling for some links, but it does not necessarily change the destination. For example, [[en:Apple]] and [[:en:Apple]] both point to English Wikipedia's Apple page; the leading-colon form is used when the link should be shown as a normal link in the page.

Local and external links

Local targets are resolved when a source wiki is available:

result = wmlinksfromhell.resolve(
    "Apple",
    source="enwiki",
)

print(result.dbname)       # enwiki
print(result.title)        # Apple

Ordinary external URLs are kept as external destinations:

result = wmlinksfromhell.resolve("https://example.org/page")

print(result.status)             # ResolutionStatus.EXTERNAL
print(result.destination_type)   # DestinationType.EXTERNAL
print(result.url)

Finding links

filter_links() can select links by the things you care about:

code.filter_links(dbname="enwiki")
code.filter_links(family="wikipedia", language="en")
code.filter_links(namespace="MediaWiki")
code.filter_links(hostname="en.wikipedia.org")

code.filter_links(wiki_only=True)
code.filter_links(interwiki_only=True)
code.filter_links(local_only=True)
code.filter_links(external_only=True)
code.filter_links(dbname_in={"enwiki", "dewiki"})

code.destinations returns the resolved destinations for parsed links.

Links inside HTML comments are ignored by default. Include them when needed:

code.filter_links(comment_links=True)

Working with a link

Links expose both the original link and its resolved destination:

for link in code.filter_links():
    print(link.original)
    print(link.dbname)
    print(link.title)
    print(link.interwiki)
    print(link.url)
    print(link.destination)

Links can also be matched and converted:

link.matches(dbname="enwiki", title="Apple")

link.set_url()
link.set_interwiki()
link.set_local(source="enwiki")
link.set_label("New label")

Metadata and serialization

Metadata can be enumerated without reaching into private state:

metadata = wmlinksfromhell.MetadataStore()
wmlinksfromhell.wikis(metadata)
metadata.wikis_for_family("wikipedia")
metadata.languages()
metadata.canonical_namespaces("enwiki")
metadata.interwiki_map("enwiki").prefixes()

Destination objects can be reconstructed from as_dict() output, and ResolutionResult objects can be serialized directly with as_json().

Command line

Resolve a value from the command line:

python -m wmlinksfromhell resolve "w:en:Apple"
python -m wmlinksfromhell resolve "Apple" --source enwiki --json

Pywikibot

When Pywikibot is installed, a Page can be passed directly to parse_page():

import pywikibot
import wmlinksfromhell

site = pywikibot.Site("en", "wikipedia")
page = pywikibot.Page(site, "Apple")

code = wmlinksfromhell.parse_page(page)

for link in code.filter_links():
    print(link.original, link.url)

Metadata

Wikimedia SiteMatrix and interwiki metadata can be loaded and cached locally. The lower-level MetadataStore, Resolver, and Destination APIs are available when you need more control.

See the API Reference for the full API.

Download files

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

Source Distribution

wmlinksfromhell-0.1.2.tar.gz (69.1 kB view details)

Uploaded Source

Built Distribution

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

wmlinksfromhell-0.1.2-py3-none-any.whl (47.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for wmlinksfromhell-0.1.2.tar.gz
Algorithm Hash digest
SHA256 7f9ab3049703c2047bbe59466f2104bea70b632018931fde4904e043af895c42
MD5 e09e1b21628a0c0e4dbddb55ff8d11e4
BLAKE2b-256 dc62314908b102bd46e3d1272b3b33dbfde4df3673a08dba6207409bfdd4ad56

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for wmlinksfromhell-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 7d641274adf9ffc164621848a011f95d7e5bcdf3750fbab20cc7a40c87464b73
MD5 e73affd456ecfb10c86662add1ca580b
BLAKE2b-256 2a3b020707dfce355bc40cc9711ccba0580c77628723446dcdfaae35e5bd2ac9

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

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