Python package for interacting with a MediaWiki wiki. It is used by WikiTeam for archiving MediaWiki wikis.
Project description
wikitools3
— Package for working with MediaWiki wikis
Requirements
- Python 3. Not compatible with Python 2. If you are using Python 2, use the original
wikitools
instead.
Note:
poetry
orpip
installation technically requires Python 3.8, but the code should mostly work on any version of Python 3.
wikitools3
usespoetry
for dependency management. If you are installing viapip
, you should not need to installpoetry
separately.- To upload files or import XML, you need Chris AtLee's
poster3
package. This should be automatically installed bypip
and/orpoetry
when you installwikitools3
. - The MediaWiki instance you are working with should be version 1.13 or later. If you are running a MediaWiki version earlier than 1.32.0 you may need to manually enable the API.
Installation
- Run
pip install wikitools3
. This is the preferred installation method and should addwikitools3
to your system path. - If you are using
poetry
to manage dependencies for a project, you can addwikitools3 = "^3.0.1"
to your project'spyproject.toml
under[tool.poetry.dependencies]
, then runpoetry install
in your project directory to installwikitools3
in the project's virtual environment. - Alternately, download the source repository and run
poetry install
within thewikitools3
directory or copy thewikitools3/wikitools3
subdirectory directly into the top-level directory of your project.
Available modules
api.py
- Contains theAPIRequest
class, for doing queries directly, see API examples belowwiki.py
- Contains theWiki
class, used for logging in to the site, storing cookies, and storing basic site informationpage.py
- Contains thePage
class for dealing with individual pages on the wiki. Can be used to get page info and text, as well as edit and other actions if enabled on the wikicategory.py
-Category
is a subclass ofPage
with extra functions for working with categorieswikifile.py
-File
is a subclass ofPage
with extra functions for working with files - note that there may be some issues with shared repositories, as the pages for files on shared repos technically don't exist on the local wiki.user.py
- Contains theUser
class for getting information about and blocking/unblocking userspagelist.py
- Contains several functions for getting a list ofPage
objects from lists of titles, pageids, or API query results
Further documentation
The legacy documentation for wikitools
(for Python 2) is available at Google Code.
Current limitations
- Can only do what the API can do. On a site without the edit-API enabled (disabled by default prior to MediaWiki 1.14), you cannot edit/delete/protect pages, only retrieve information about them.
- May have issues with some non-ASCII characters. Most of these bugs should be resolved, though full UTF-8 support is still a little flaky.
- Usage on restricted-access (logged-out users can't read) wikis is mostly untested.
wikitools3
has not been tested beyond the needs ofwikiteam
. If functionality fromwikitools
for Python 2 works for you, but the same functionality does not work for you inwikitools3
, please submit a bug report at github.com/elsiehupp/wikitools3/issues.
Quick start
To make a simple query:
#!/usr/bin/env python3
from wikitools3 import wiki
from wikitools3 import api
# create a Wiki object
site = wiki.Wiki("http://my.wikisite.org/w/api.php")
# login - required for read-restricted wikis
site.login("username", "password")
# define the params for the query
params = {'action':'query', 'titles':'Main Page'}
# create the request object
request = api.APIRequest(site, params)
# query the API
result = request.query()
The result will look something like:
{u'query':
{u'pages':
{u'15580374':
{u'ns': 0, u'pageid': 15580374, u'title': u'Main Page'}
}
}
}
If the API module you need requires a token, you first do something like:
params = { 'action':'query', 'meta':'tokens' }
token = api.APIRequest(site, params).query()['query']['tokens']['csrftoken']
# define the params for the query
params = { 'action':'thank', 'rev':diff, 'token':token }
For most normal usage, you may not have to do API queries yourself and can just use the various classes. For example, to add a template to the top of all the pages in namespace 0
in a category:
#!/usr/bin/env python3
from wikitools3 import wiki
from wikitools3 import category
site = wiki.Wiki("http://my.wikisite.org/w/api.php")
site.login("username", "password")
# Create object for "Category:Foo"
cat = category.Category(site, "Foo")
# iterate through all the pages in ns 0
for article in cat.getAllMembersGen(namespaces=[0]):
# edit each page
article.edit(prependtext="{{template}}\n")
See the MediaWiki API documentation for more information about using the MediaWiki API. You can get an example of what query results will look like by doing the queries in your web browser using the jsonfm
format option.
License
wikitools3
is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
wikitools3
is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with wikitools3
. If not, see http://www.gnu.org/licenses/.
Authors
- Alex Zaddach [@alexz-enwp]
- Brandon Weeks [@brandonweeks]
- Mark A. Hershberger [@hexmode]
- Thomas Jones-Low [@tjoneslo]
- MZMcBride [@mzmcbride]
- Elsie Hupp [@elsiehupp]
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file wikitools3-3.0.1.tar.gz
.
File metadata
- Download URL: wikitools3-3.0.1.tar.gz
- Upload date:
- Size: 39.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.63.0 CPython/3.8.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b493e6806fb1985ad1af294c56364aeba6ea67a366553bc11b65b7f5279d0012 |
|
MD5 | 10119e94eb12075cb2917d242cdf675f |
|
BLAKE2b-256 | 70292d26fcff4f84059751c74aecbf94096e457b086f7b25b9e2377163d0db95 |
File details
Details for the file wikitools3-3.0.1-py3-none-any.whl
.
File metadata
- Download URL: wikitools3-3.0.1-py3-none-any.whl
- Upload date:
- Size: 43.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.63.0 CPython/3.8.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ab1a13d9be831eb1186cf0f2b8ac487dde0932dad82c6c6031f28fa415840fe4 |
|
MD5 | 440cd03fc9d5eefa826443c2ddb48afb |
|
BLAKE2b-256 | b07aced6c0e529ab06b554cf75222f105b059140109e61d63d9ef85beb9933d2 |