mediawiki-client-tools / wikitools3

Python package for working with MediaWiki wikis
3 stars 3 forks source link

wikitools3 — Package for working with MediaWiki wikis

Requirements

Note: poetry or pip installation technically requires Python 3.8, but the code should mostly work on any version of Python 3.

Installation

Available modules

Further documentation

The legacy documentation for wikitools (for Python 2) is available at Google Code.

Current limitations

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