Package: wikitools3
wikitools3— Package for working with MediaWiki wikisRequirementsPython 3. Not compatible with Python 2. If you are using Python 2, use the originalwikitoolsinstead.Note:poetryorpipinstallation technically requires Python 3.8, but the code should mostly work on any version of Python 3.wikitools3usespoetryfor dependency management. If you are installing viapip, you should not need to installpoetryseparately.To upload files or import XML, you need Chris AtLee'sposter3package. This should be automatically installed bypipand/orpoetrywhen 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 tomanually enable the API.InstallationRunpip install wikitools3. This is the preferred installation method and should addwikitools3to your system path.If you are usingpoetryto manage dependencies for a project, you can addwikitools3 = "^3.0.1"to your project'spyproject.tomlunder[tool.poetry.dependencies], then runpoetry installin your project directory to installwikitools3in the project's virtual environment.Alternately, download the source repository and runpoetry installwithin thewikitools3directory or copy thewikitools3/wikitools3subdirectory directly into the top-level directory of your project.Available modulesapi.py- Contains theAPIRequestclass, for doing queries directly, see API examples belowwiki.py- Contains theWikiclass, used for logging in to the site, storing cookies, and storing basic site informationpage.py- Contains thePageclass 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-Categoryis a subclass ofPagewith extra functions for working with categorieswikifile.py-Fileis a subclass ofPagewith 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 theUserclass for getting information about and blocking/unblocking userspagelist.py- Contains several functions for getting a list ofPageobjects from lists of titles, pageids, or API query resultsFurther documentationThe legacy documentation forwikitools(for Python 2)is available at Google Code.Current limitationsCan 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.wikitools3has not been tested beyond the needs ofwikiteam. If functionality fromwikitoolsfor Python 2 works for you, but the same functionality does not work for you inwikitools3, please submit a bug report atgithub.com/elsiehupp/wikitools3/issues.Quick startTo make a simple query:#!/usr/bin/env python3fromwikitools3importwikifromwikitools3importapi# create a Wiki objectsite=wiki.Wiki("http://my.wikisite.org/w/api.php")# login - required for read-restricted wikissite.login("username","password")# define the params for the queryparams={'action':'query','titles':'Main Page'}# create the request objectrequest=api.APIRequest(site,params)# query the APIresult=request.query()The result will look something like:{u'query':{u'pages':{u'15580374':{u'ns':0,u'pageid':15580374,u'title':u'MainPage'}}}}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 queryparams={'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 namespace0in a category:#!/usr/bin/env python3fromwikitools3importwikifromwikitools3importcategorysite=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 0forarticleincat.getAllMembersGen(namespaces=[0]):# edit each pagearticle.edit(prependtext="{{template}}\n")See theMediaWiki API documentationfor 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 thejsonfmformat option.Licensewikitools3is 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, eitherversion 3 of the License, or (at your option) any later version.wikitools3is 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 withwikitools3. If not, seehttp://www.gnu.org/licenses/.AuthorsAlex Zaddach [@alexz-enwp]Brandon Weeks [@brandonweeks]Mark A. Hershberger [@hexmode]Thomas Jones-Low [@tjoneslo]MZMcBride [@mzmcbride]Elsie Hupp [@elsiehupp]