wmlinksfromhell 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- wmlinksfromhell-0.1.0/.gitignore +17 -0
- wmlinksfromhell-0.1.0/.readthedocs.yaml +13 -0
- wmlinksfromhell-0.1.0/CHANGELOG.md +13 -0
- wmlinksfromhell-0.1.0/LICENSE +21 -0
- wmlinksfromhell-0.1.0/MANIFEST.in +13 -0
- wmlinksfromhell-0.1.0/PKG-INFO +232 -0
- wmlinksfromhell-0.1.0/README.md +201 -0
- wmlinksfromhell-0.1.0/examples/comments.py +20 -0
- wmlinksfromhell-0.1.0/examples/convert_links.py +12 -0
- wmlinksfromhell-0.1.0/examples/pywikibot_example.py +13 -0
- wmlinksfromhell-0.1.0/examples/select_links.py +19 -0
- wmlinksfromhell-0.1.0/examples/simple_api.py +19 -0
- wmlinksfromhell-0.1.0/examples/sitematrix.py +25 -0
- wmlinksfromhell-0.1.0/mkdocs.yml +25 -0
- wmlinksfromhell-0.1.0/pyproject.toml +55 -0
- wmlinksfromhell-0.1.0/requirements-dev.txt +3 -0
- wmlinksfromhell-0.1.0/tests/conftest.py +31 -0
- wmlinksfromhell-0.1.0/tests/fixtures/interwiki-config.txt +28 -0
- wmlinksfromhell-0.1.0/tests/fixtures/meta_update_a_common_gadget.txt +14 -0
- wmlinksfromhell-0.1.0/tests/fixtures/noc_specials.txt +16 -0
- wmlinksfromhell-0.1.0/tests/test_cache.py +53 -0
- wmlinksfromhell-0.1.0/tests/test_comments_and_simple_api.py +112 -0
- wmlinksfromhell-0.1.0/tests/test_conversion.py +275 -0
- wmlinksfromhell-0.1.0/tests/test_destination.py +52 -0
- wmlinksfromhell-0.1.0/tests/test_interwiki.py +487 -0
- wmlinksfromhell-0.1.0/tests/test_matching.py +76 -0
- wmlinksfromhell-0.1.0/tests/test_mediawiki_link_docs.py +442 -0
- wmlinksfromhell-0.1.0/tests/test_mediawiki_url_cases.py +335 -0
- wmlinksfromhell-0.1.0/tests/test_metadata.py +732 -0
- wmlinksfromhell-0.1.0/tests/test_parser.py +64 -0
- wmlinksfromhell-0.1.0/tests/test_public_and_operations.py +207 -0
- wmlinksfromhell-0.1.0/tests/test_real_world.py +50 -0
- wmlinksfromhell-0.1.0/tests/test_resolution.py +71 -0
- wmlinksfromhell-0.1.0/tests/test_simple_api.py +155 -0
- wmlinksfromhell-0.1.0/tests/test_special_fallbacks.py +37 -0
- wmlinksfromhell-0.1.0/tests/test_transformation.py +102 -0
- wmlinksfromhell-0.1.0/tests/test_urls.py +205 -0
- wmlinksfromhell-0.1.0/wmlinksfromhell/__init__.py +73 -0
- wmlinksfromhell-0.1.0/wmlinksfromhell/cache.py +92 -0
- wmlinksfromhell-0.1.0/wmlinksfromhell/constants.py +307 -0
- wmlinksfromhell-0.1.0/wmlinksfromhell/destination.py +125 -0
- wmlinksfromhell-0.1.0/wmlinksfromhell/exceptions.py +29 -0
- wmlinksfromhell-0.1.0/wmlinksfromhell/interwiki.py +815 -0
- wmlinksfromhell-0.1.0/wmlinksfromhell/metadata.py +1175 -0
- wmlinksfromhell-0.1.0/wmlinksfromhell/models.py +165 -0
- wmlinksfromhell-0.1.0/wmlinksfromhell/nodes.py +447 -0
- wmlinksfromhell-0.1.0/wmlinksfromhell/operations.py +83 -0
- wmlinksfromhell-0.1.0/wmlinksfromhell/parser.py +186 -0
- wmlinksfromhell-0.1.0/wmlinksfromhell/pywikibot_support.py +13 -0
- wmlinksfromhell-0.1.0/wmlinksfromhell/resolver.py +266 -0
- wmlinksfromhell-0.1.0/wmlinksfromhell/url.py +402 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.1.0 — 2026-09-10
|
|
4
|
+
|
|
5
|
+
First public release.
|
|
6
|
+
|
|
7
|
+
- Wikimedia interwiki and language/project link resolution.
|
|
8
|
+
- Wikimedia wiki URL and `index.php` URL resolution.
|
|
9
|
+
- Support for local and external links.
|
|
10
|
+
- Prefix chains, namespaces, fragments, revisions, diffs, actions, and page IDs.
|
|
11
|
+
- Link matching and conversion between URL, interwiki, and local forms.
|
|
12
|
+
- SiteMatrix and interwiki metadata with local caching.
|
|
13
|
+
- Pywikibot `Site` and `Page` support.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Max
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
include README.md
|
|
2
|
+
include LICENSE
|
|
3
|
+
include CHANGELOG.md
|
|
4
|
+
include MANIFEST.in
|
|
5
|
+
include requirements-dev.txt
|
|
6
|
+
recursive-include examples *.py
|
|
7
|
+
recursive-include tests *.py *.txt
|
|
8
|
+
prune build
|
|
9
|
+
prune dist
|
|
10
|
+
prune dist_sdist
|
|
11
|
+
prune wmlinksfromhell.egg-info
|
|
12
|
+
prune .venv
|
|
13
|
+
prune docs
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: wmlinksfromhell
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A library for resolving and working with Wikimedia and MediaWiki links.
|
|
5
|
+
Author: Max
|
|
6
|
+
Author-email: madmax.wp@proton.me
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
Requires-Python: >=3.9
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
Keywords: mediawiki,wikimedia,wikipedia,interwiki,wikitext,links,parser,resolver
|
|
11
|
+
Project-URL: Documentation, https://wmlinksfromhell.readthedocs.io/en/latest/
|
|
12
|
+
Project-URL: Repository, https://github.com/MadMaxWP/wmlinksfromhell
|
|
13
|
+
Project-URL: Issues, https://github.com/MadMaxWP/wmlinksfromhell/issues
|
|
14
|
+
Project-URL: Changelog, https://github.com/MadMaxWP/wmlinksfromhell/blob/main/CHANGELOG.md
|
|
15
|
+
Classifier: Development Status :: 4 - Beta
|
|
16
|
+
Classifier: Intended Audience :: Developers
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
23
|
+
Classifier: Topic :: Text Processing :: Markup
|
|
24
|
+
Classifier: Topic :: Internet
|
|
25
|
+
Requires-Dist: mwparserfromhell>=0.6
|
|
26
|
+
Provides-Extra: pywikibot
|
|
27
|
+
Requires-Dist: pywikibot>=8.0; extra == "pywikibot"
|
|
28
|
+
Provides-Extra: dev
|
|
29
|
+
Requires-Dist: pytest>=7.0; extra == "dev"
|
|
30
|
+
Requires-Dist: pytest-cov; extra == "dev"
|
|
31
|
+
|
|
32
|
+
# wmlinksfromhell
|
|
33
|
+
|
|
34
|
+
[](https://github.com/MadMaxWP/wmlinksfromhell/actions/workflows/ci.yml)
|
|
35
|
+
[](https://pypi.org/project/wmlinksfromhell/)
|
|
36
|
+
[](https://pypi.org/project/wmlinksfromhell/)
|
|
37
|
+
[](https://pypi.org/project/wmlinksfromhell/)
|
|
38
|
+
[](https://github.com/MadMaxWP/wmlinksfromhell/blob/main/LICENSE)
|
|
39
|
+
[](https://github.com/MadMaxWP/wmlinksfromhell)
|
|
40
|
+
|
|
41
|
+
**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.
|
|
42
|
+
|
|
43
|
+
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.
|
|
44
|
+
|
|
45
|
+
Developed by [Max](https://github.com/MadMaxWP).
|
|
46
|
+
|
|
47
|
+
Full documentation is available on [Read the Docs](https://wmlinksfromhell.readthedocs.io/en/latest/). Development takes place on [GitHub](https://github.com/MadMaxWP/wmlinksfromhell), and bugs and feature requests can be reported in the [issue tracker](https://github.com/MadMaxWP/wmlinksfromhell/issues).
|
|
48
|
+
|
|
49
|
+
## Project links
|
|
50
|
+
|
|
51
|
+
- [Documentation](https://wmlinksfromhell.readthedocs.io/en/latest/)
|
|
52
|
+
- [API Reference](https://wmlinksfromhell.readthedocs.io/en/latest/api-reference/)
|
|
53
|
+
- [Changelog](https://github.com/MadMaxWP/wmlinksfromhell/blob/main/CHANGELOG.md)
|
|
54
|
+
- [Issues](https://github.com/MadMaxWP/wmlinksfromhell/issues)
|
|
55
|
+
- [Repository](https://github.com/MadMaxWP/wmlinksfromhell)
|
|
56
|
+
|
|
57
|
+
## Install
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
python3 -m pip install wmlinksfromhell
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Pywikibot support is optional:
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
python3 -m pip install 'wmlinksfromhell[pywikibot]'
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Supports Python 3.9 and newer.
|
|
70
|
+
|
|
71
|
+
## Quick start
|
|
72
|
+
|
|
73
|
+
Get information about a wiki:
|
|
74
|
+
|
|
75
|
+
```python
|
|
76
|
+
import wmlinksfromhell
|
|
77
|
+
|
|
78
|
+
wiki = wmlinksfromhell.wiki("enwiki")
|
|
79
|
+
|
|
80
|
+
print(wiki.language) # en
|
|
81
|
+
print(wiki.hostname) # en.wikipedia.org
|
|
82
|
+
print(wiki.page_url("Apple"))
|
|
83
|
+
print(wiki.interwiki("Apple"))
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
Resolve a Wikimedia link:
|
|
87
|
+
|
|
88
|
+
```python
|
|
89
|
+
result = wmlinksfromhell.resolve("w:en:Apple")
|
|
90
|
+
|
|
91
|
+
print(result.dbname) # enwiki
|
|
92
|
+
print(result.title) # Apple
|
|
93
|
+
print(result.url) # https://en.wikipedia.org/wiki/Apple
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
Parse links from MediaWiki text:
|
|
97
|
+
|
|
98
|
+
```python
|
|
99
|
+
import wmlinksfromhell
|
|
100
|
+
|
|
101
|
+
code = wmlinksfromhell.parse(
|
|
102
|
+
"[[w:en:Apple]] https://de.wikipedia.org/wiki/Berlin https://example.org/",
|
|
103
|
+
source="metawiki",
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
for link in code.filter_links():
|
|
107
|
+
print(link.original)
|
|
108
|
+
print(link.dbname, link.title, link.url)
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
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.
|
|
112
|
+
|
|
113
|
+
## Wikimedia links
|
|
114
|
+
|
|
115
|
+
The resolver understands Wikimedia language and project prefixes, including prefix chains:
|
|
116
|
+
|
|
117
|
+
```text
|
|
118
|
+
w:en:Apple
|
|
119
|
+
wikt:simple:uppercase
|
|
120
|
+
b:de:Main Page
|
|
121
|
+
:de:q:Hauptseite
|
|
122
|
+
:m:en:About
|
|
123
|
+
:w:it:b:Wiskunde
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
It also handles Wikimedia URL forms such as article paths and `index.php`, including fragments, revisions, diffs, actions, and page IDs.
|
|
127
|
+
|
|
128
|
+
For example:
|
|
129
|
+
|
|
130
|
+
```python
|
|
131
|
+
result = wmlinksfromhell.resolve(
|
|
132
|
+
"https://en.wikipedia.org/w/index.php?title=Apple&oldid=123456",
|
|
133
|
+
)
|
|
134
|
+
|
|
135
|
+
print(result.dbname) # enwiki
|
|
136
|
+
print(result.revision) # 123456
|
|
137
|
+
print(result.interwiki) # w:en:Special:PermanentLink/123456
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
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.
|
|
141
|
+
|
|
142
|
+
## Local and external links
|
|
143
|
+
|
|
144
|
+
Local links are resolved when a source wiki is available:
|
|
145
|
+
|
|
146
|
+
```python
|
|
147
|
+
result = wmlinksfromhell.resolve(
|
|
148
|
+
"[[Apple]]",
|
|
149
|
+
source="enwiki",
|
|
150
|
+
)
|
|
151
|
+
|
|
152
|
+
print(result.dbname) # enwiki
|
|
153
|
+
print(result.title) # Apple
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
Ordinary external URLs are kept as external destinations:
|
|
157
|
+
|
|
158
|
+
```python
|
|
159
|
+
result = wmlinksfromhell.resolve("https://example.org/page")
|
|
160
|
+
|
|
161
|
+
print(result.is_external)
|
|
162
|
+
print(result.url)
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
## Finding links
|
|
166
|
+
|
|
167
|
+
`filter_links()` can select links by the things you care about:
|
|
168
|
+
|
|
169
|
+
```python
|
|
170
|
+
code.filter_links(dbname="enwiki")
|
|
171
|
+
code.filter_links(family="wikipedia", language="en")
|
|
172
|
+
code.filter_links(namespace="MediaWiki")
|
|
173
|
+
code.filter_links(hostname="en.wikipedia.org")
|
|
174
|
+
|
|
175
|
+
code.filter_links(wiki_only=True)
|
|
176
|
+
code.filter_links(interwiki_only=True)
|
|
177
|
+
code.filter_links(local_only=True)
|
|
178
|
+
code.filter_links(external_only=True)
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
Links inside HTML comments are ignored by default. Include them when needed:
|
|
182
|
+
|
|
183
|
+
```python
|
|
184
|
+
code.filter_links(comment_links=True)
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
## Working with a link
|
|
188
|
+
|
|
189
|
+
Links expose both the original link and its resolved destination:
|
|
190
|
+
|
|
191
|
+
```python
|
|
192
|
+
for link in code.filter_links():
|
|
193
|
+
print(link.original)
|
|
194
|
+
print(link.dbname)
|
|
195
|
+
print(link.title)
|
|
196
|
+
print(link.interwiki)
|
|
197
|
+
print(link.url)
|
|
198
|
+
print(link.destination)
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
Links can also be matched and converted:
|
|
202
|
+
|
|
203
|
+
```python
|
|
204
|
+
link.matches(dbname="enwiki", title="Apple")
|
|
205
|
+
|
|
206
|
+
link.set_url()
|
|
207
|
+
link.set_interwiki()
|
|
208
|
+
link.set_local(source="enwiki")
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
## Pywikibot
|
|
212
|
+
|
|
213
|
+
When Pywikibot is installed, a `Page` can be passed directly to `parse_page()`:
|
|
214
|
+
|
|
215
|
+
```python
|
|
216
|
+
import pywikibot
|
|
217
|
+
import wmlinksfromhell
|
|
218
|
+
|
|
219
|
+
site = pywikibot.Site("en", "wikipedia")
|
|
220
|
+
page = pywikibot.Page(site, "Apple")
|
|
221
|
+
|
|
222
|
+
code = wmlinksfromhell.parse_page(page)
|
|
223
|
+
|
|
224
|
+
for link in code.filter_links():
|
|
225
|
+
print(link.original, link.url)
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
## Metadata
|
|
229
|
+
|
|
230
|
+
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.
|
|
231
|
+
|
|
232
|
+
See the [API Reference](https://wmlinksfromhell.readthedocs.io/en/latest/api-reference/) for the full API.
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
# wmlinksfromhell
|
|
2
|
+
|
|
3
|
+
[](https://github.com/MadMaxWP/wmlinksfromhell/actions/workflows/ci.yml)
|
|
4
|
+
[](https://pypi.org/project/wmlinksfromhell/)
|
|
5
|
+
[](https://pypi.org/project/wmlinksfromhell/)
|
|
6
|
+
[](https://pypi.org/project/wmlinksfromhell/)
|
|
7
|
+
[](https://github.com/MadMaxWP/wmlinksfromhell/blob/main/LICENSE)
|
|
8
|
+
[](https://github.com/MadMaxWP/wmlinksfromhell)
|
|
9
|
+
|
|
10
|
+
**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.
|
|
11
|
+
|
|
12
|
+
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.
|
|
13
|
+
|
|
14
|
+
Developed by [Max](https://github.com/MadMaxWP).
|
|
15
|
+
|
|
16
|
+
Full documentation is available on [Read the Docs](https://wmlinksfromhell.readthedocs.io/en/latest/). Development takes place on [GitHub](https://github.com/MadMaxWP/wmlinksfromhell), and bugs and feature requests can be reported in the [issue tracker](https://github.com/MadMaxWP/wmlinksfromhell/issues).
|
|
17
|
+
|
|
18
|
+
## Project links
|
|
19
|
+
|
|
20
|
+
- [Documentation](https://wmlinksfromhell.readthedocs.io/en/latest/)
|
|
21
|
+
- [API Reference](https://wmlinksfromhell.readthedocs.io/en/latest/api-reference/)
|
|
22
|
+
- [Changelog](https://github.com/MadMaxWP/wmlinksfromhell/blob/main/CHANGELOG.md)
|
|
23
|
+
- [Issues](https://github.com/MadMaxWP/wmlinksfromhell/issues)
|
|
24
|
+
- [Repository](https://github.com/MadMaxWP/wmlinksfromhell)
|
|
25
|
+
|
|
26
|
+
## Install
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
python3 -m pip install wmlinksfromhell
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Pywikibot support is optional:
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
python3 -m pip install 'wmlinksfromhell[pywikibot]'
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Supports Python 3.9 and newer.
|
|
39
|
+
|
|
40
|
+
## Quick start
|
|
41
|
+
|
|
42
|
+
Get information about a wiki:
|
|
43
|
+
|
|
44
|
+
```python
|
|
45
|
+
import wmlinksfromhell
|
|
46
|
+
|
|
47
|
+
wiki = wmlinksfromhell.wiki("enwiki")
|
|
48
|
+
|
|
49
|
+
print(wiki.language) # en
|
|
50
|
+
print(wiki.hostname) # en.wikipedia.org
|
|
51
|
+
print(wiki.page_url("Apple"))
|
|
52
|
+
print(wiki.interwiki("Apple"))
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Resolve a Wikimedia link:
|
|
56
|
+
|
|
57
|
+
```python
|
|
58
|
+
result = wmlinksfromhell.resolve("w:en:Apple")
|
|
59
|
+
|
|
60
|
+
print(result.dbname) # enwiki
|
|
61
|
+
print(result.title) # Apple
|
|
62
|
+
print(result.url) # https://en.wikipedia.org/wiki/Apple
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Parse links from MediaWiki text:
|
|
66
|
+
|
|
67
|
+
```python
|
|
68
|
+
import wmlinksfromhell
|
|
69
|
+
|
|
70
|
+
code = wmlinksfromhell.parse(
|
|
71
|
+
"[[w:en:Apple]] https://de.wikipedia.org/wiki/Berlin https://example.org/",
|
|
72
|
+
source="metawiki",
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
for link in code.filter_links():
|
|
76
|
+
print(link.original)
|
|
77
|
+
print(link.dbname, link.title, link.url)
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
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.
|
|
81
|
+
|
|
82
|
+
## Wikimedia links
|
|
83
|
+
|
|
84
|
+
The resolver understands Wikimedia language and project prefixes, including prefix chains:
|
|
85
|
+
|
|
86
|
+
```text
|
|
87
|
+
w:en:Apple
|
|
88
|
+
wikt:simple:uppercase
|
|
89
|
+
b:de:Main Page
|
|
90
|
+
:de:q:Hauptseite
|
|
91
|
+
:m:en:About
|
|
92
|
+
:w:it:b:Wiskunde
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
It also handles Wikimedia URL forms such as article paths and `index.php`, including fragments, revisions, diffs, actions, and page IDs.
|
|
96
|
+
|
|
97
|
+
For example:
|
|
98
|
+
|
|
99
|
+
```python
|
|
100
|
+
result = wmlinksfromhell.resolve(
|
|
101
|
+
"https://en.wikipedia.org/w/index.php?title=Apple&oldid=123456",
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
print(result.dbname) # enwiki
|
|
105
|
+
print(result.revision) # 123456
|
|
106
|
+
print(result.interwiki) # w:en:Special:PermanentLink/123456
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
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.
|
|
110
|
+
|
|
111
|
+
## Local and external links
|
|
112
|
+
|
|
113
|
+
Local links are resolved when a source wiki is available:
|
|
114
|
+
|
|
115
|
+
```python
|
|
116
|
+
result = wmlinksfromhell.resolve(
|
|
117
|
+
"[[Apple]]",
|
|
118
|
+
source="enwiki",
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
print(result.dbname) # enwiki
|
|
122
|
+
print(result.title) # Apple
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
Ordinary external URLs are kept as external destinations:
|
|
126
|
+
|
|
127
|
+
```python
|
|
128
|
+
result = wmlinksfromhell.resolve("https://example.org/page")
|
|
129
|
+
|
|
130
|
+
print(result.is_external)
|
|
131
|
+
print(result.url)
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
## Finding links
|
|
135
|
+
|
|
136
|
+
`filter_links()` can select links by the things you care about:
|
|
137
|
+
|
|
138
|
+
```python
|
|
139
|
+
code.filter_links(dbname="enwiki")
|
|
140
|
+
code.filter_links(family="wikipedia", language="en")
|
|
141
|
+
code.filter_links(namespace="MediaWiki")
|
|
142
|
+
code.filter_links(hostname="en.wikipedia.org")
|
|
143
|
+
|
|
144
|
+
code.filter_links(wiki_only=True)
|
|
145
|
+
code.filter_links(interwiki_only=True)
|
|
146
|
+
code.filter_links(local_only=True)
|
|
147
|
+
code.filter_links(external_only=True)
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
Links inside HTML comments are ignored by default. Include them when needed:
|
|
151
|
+
|
|
152
|
+
```python
|
|
153
|
+
code.filter_links(comment_links=True)
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
## Working with a link
|
|
157
|
+
|
|
158
|
+
Links expose both the original link and its resolved destination:
|
|
159
|
+
|
|
160
|
+
```python
|
|
161
|
+
for link in code.filter_links():
|
|
162
|
+
print(link.original)
|
|
163
|
+
print(link.dbname)
|
|
164
|
+
print(link.title)
|
|
165
|
+
print(link.interwiki)
|
|
166
|
+
print(link.url)
|
|
167
|
+
print(link.destination)
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
Links can also be matched and converted:
|
|
171
|
+
|
|
172
|
+
```python
|
|
173
|
+
link.matches(dbname="enwiki", title="Apple")
|
|
174
|
+
|
|
175
|
+
link.set_url()
|
|
176
|
+
link.set_interwiki()
|
|
177
|
+
link.set_local(source="enwiki")
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
## Pywikibot
|
|
181
|
+
|
|
182
|
+
When Pywikibot is installed, a `Page` can be passed directly to `parse_page()`:
|
|
183
|
+
|
|
184
|
+
```python
|
|
185
|
+
import pywikibot
|
|
186
|
+
import wmlinksfromhell
|
|
187
|
+
|
|
188
|
+
site = pywikibot.Site("en", "wikipedia")
|
|
189
|
+
page = pywikibot.Page(site, "Apple")
|
|
190
|
+
|
|
191
|
+
code = wmlinksfromhell.parse_page(page)
|
|
192
|
+
|
|
193
|
+
for link in code.filter_links():
|
|
194
|
+
print(link.original, link.url)
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
## Metadata
|
|
198
|
+
|
|
199
|
+
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.
|
|
200
|
+
|
|
201
|
+
See the [API Reference](https://wmlinksfromhell.readthedocs.io/en/latest/api-reference/) for the full API.
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"""read normal links or links that live inside html comments."""
|
|
2
|
+
|
|
3
|
+
import wmlinksfromhell
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
text = """
|
|
7
|
+
<!-- [[w:en:Hidden]] -->
|
|
8
|
+
[[w:en:Visible]]
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
code = wmlinksfromhell.parse(text, source="metawiki")
|
|
12
|
+
|
|
13
|
+
print("normal links:")
|
|
14
|
+
for link in code.filter_links():
|
|
15
|
+
print(link.original)
|
|
16
|
+
|
|
17
|
+
print()
|
|
18
|
+
print("comment links:")
|
|
19
|
+
for link in code.filter_links(comment_links=True, in_comment=True):
|
|
20
|
+
print(link.original)
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"""convert only the selected Wikimedia links in a text fragment"""
|
|
2
|
+
|
|
3
|
+
import wmlinksfromhell
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
text = "[[w:en:Apple|this article]] https://de.wikipedia.org/wiki/Berlin https://example.com/leave-me"
|
|
7
|
+
code = wmlinksfromhell.parse(text, source="enwiki")
|
|
8
|
+
|
|
9
|
+
for link in code.filter_links(family="wikipedia", language="en"):
|
|
10
|
+
link.set_interwiki()
|
|
11
|
+
|
|
12
|
+
print(code)
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"""optional Pywikibot integration"""
|
|
2
|
+
|
|
3
|
+
import pywikibot
|
|
4
|
+
|
|
5
|
+
import wmlinksfromhell
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
site = pywikibot.Site("meta", "meta")
|
|
9
|
+
page = pywikibot.Page(site, "Steward requests/Miscellaneous")
|
|
10
|
+
code = wmlinksfromhell.parse(page.text, source=site)
|
|
11
|
+
|
|
12
|
+
for link in code.filter_links():
|
|
13
|
+
print(link.raw, link.family, link.language, link.dbname, link.title)
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"""select links by structure, then inspect only those links."""
|
|
2
|
+
|
|
3
|
+
import wmlinksfromhell
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
text = """== Requests ==
|
|
7
|
+
* delete [[w:en:Apple]] because of the reason [[User:Example]]
|
|
8
|
+
* delete https://de.wikipedia.org/wiki/Berlin because of the reason [[User:Example]]
|
|
9
|
+
{{Request|target=[[w:fr:Paris]]}}
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
code = wmlinksfromhell.parse(text, source="metawiki")
|
|
13
|
+
|
|
14
|
+
# the parser keeps its normal structure, so scope first
|
|
15
|
+
# and then decide which links are interesting.
|
|
16
|
+
section = next(section for section in code.get_sections() if section.wikicode.filter_headings())
|
|
17
|
+
|
|
18
|
+
for link in section.filter_links(predicate=lambda item: item.is_wikimedia):
|
|
19
|
+
print(link.raw, link.family, link.language, link.dbname, link.title)
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"""small examples of the simple public api."""
|
|
2
|
+
|
|
3
|
+
import wmlinksfromhell
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
wiki = wmlinksfromhell.wiki("enwiki")
|
|
7
|
+
print(wiki.language)
|
|
8
|
+
print(wiki.project)
|
|
9
|
+
print(wiki.interwiki("Apple"))
|
|
10
|
+
print(wiki.page_url("Apple"))
|
|
11
|
+
print(wiki.link("Apple", "the fruit"))
|
|
12
|
+
|
|
13
|
+
info = wmlinksfromhell.interwiki("enwiki")
|
|
14
|
+
print(info.prefix)
|
|
15
|
+
print(info.project)
|
|
16
|
+
print(info.language)
|
|
17
|
+
|
|
18
|
+
print(wmlinksfromhell.url("dewiki", "Apfel"))
|
|
19
|
+
print(wmlinksfromhell.link("dewiki", "Apfel", "Apfel"))
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"""print the canonical interwiki target and url for every SiteMatrix wiki."""
|
|
2
|
+
|
|
3
|
+
import wmlinksfromhell as wm
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
TITLE = "MediaWiki:Gadget:Xtools.js"
|
|
7
|
+
|
|
8
|
+
metadata = wm.Metadata()
|
|
9
|
+
metadata.update_sitematrix()
|
|
10
|
+
|
|
11
|
+
for wiki in sorted(metadata.all_wikis(), key=lambda item: item.dbname):
|
|
12
|
+
info = metadata.interwiki(wiki.dbname)
|
|
13
|
+
|
|
14
|
+
if info is None:
|
|
15
|
+
print(f"{wiki.dbname} → [NO PREFIX]")
|
|
16
|
+
continue
|
|
17
|
+
|
|
18
|
+
try:
|
|
19
|
+
target = info.target(TITLE)
|
|
20
|
+
url = wiki.page_url(TITLE, metadata=metadata)
|
|
21
|
+
except Exception as error:
|
|
22
|
+
print(f"{wiki.dbname} → {info.prefix}: ERROR {error}")
|
|
23
|
+
continue
|
|
24
|
+
|
|
25
|
+
print(f"{wiki.dbname} → {target} - {url}")
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
site_name: wmlinksfromhell
|
|
2
|
+
site_description: Wikimedia interwiki and MediaWiki link tools for Python.
|
|
3
|
+
site_url: !ENV READTHEDOCS_CANONICAL_URL
|
|
4
|
+
repo_name: MadMaxWP/wmlinksfromhell
|
|
5
|
+
repo_url: https://github.com/MadMaxWP/wmlinksfromhell
|
|
6
|
+
|
|
7
|
+
theme:
|
|
8
|
+
name: material
|
|
9
|
+
features:
|
|
10
|
+
- navigation.sections
|
|
11
|
+
- navigation.top
|
|
12
|
+
- navigation.indexes
|
|
13
|
+
- search.highlight
|
|
14
|
+
- content.code.copy
|
|
15
|
+
|
|
16
|
+
nav:
|
|
17
|
+
- Home: index.md
|
|
18
|
+
- API Reference: api-reference.md
|
|
19
|
+
- Changelog: changelog.md
|
|
20
|
+
|
|
21
|
+
markdown_extensions:
|
|
22
|
+
- toc:
|
|
23
|
+
permalink: true
|
|
24
|
+
- tables
|
|
25
|
+
- fenced_code
|