API:Tutorial
| Language: | English • 日本語 |
|---|
This page contains the notes used to present Roan Kattouw's 2012 workshop on the MediaWiki web API and is not a complete tutorial on the MediaWiki web API by any means. It provides a quick introduction to the API, useful recipes for common GET requests, and a list of available resources with more information.
| This page is part of the MediaWiki action API documentation. |
MediaWiki action API
- Introduction and quick start
- FAQ
- Tutorial
- Formats
- Error reporting
- Restricting usage
- Cross-site requests
- Authentication
- Queries
- Searching (by title, content, coordinates...)
- Parsing wikitext and expanding templates
- Purging pages' caches
- Parameter information
- Changing wiki content
- Watchlist feed
- Wikidata
- Extensions
- Using the API in MediaWiki and extensions
- Miscellaneous
- Implementation
- Client code
- Asserting
Contents
Tutorial for MediaWiki's RESTful web service API[edit]
Why should you use the web API? Bots, AJAX, Gadgets, other things.
Roan says: generally any Ajax feature is going to use the api.php entry point. But right now the easiest thing to do is to write a bot or to use the API clients.
Definitions[edit]
- REST API for MediaWiki
- exposes things MediaWiki has in the database or otherwise understands
- does not include semantic stuff like "definition of a word in Wiktionary" or even "lead paragraph of an article"
- usage: send HTTP requests (GET or POST) to the
api.phpURL, receive XML or JSON or other formats. You'll usually want JSON or XML.- w:en:JSON and w:en:XML and w:en:Representational state transfer (RESTful)
There are other things that also get casually called the MediaWiki API, like the internal interfaces that extensions and special pages can hook into. We're not talking about that right now, just the web API.
-
- (possibly talk about how it works from the back end, if people ask)...
How to use it[edit]
- Entry point: https://en.wikipedia.org/w/api.php (see API:Main page#The endpoint)
- or any other wiki
- Talk about versioning and how non-WMF wikis might have different version of MediaWiki and thus the API
- https works too!
- or any other wiki
- Parameters are passed in query string. Not passing any will give you the help page with the autogenerated documentation.
Follow along by using w:en:Special:ApiSandbox – you will mostly use the query Action.
- Here is an example query:
-
action=queryis used for most read actions, separate action= modules exist for write actionstitles=takes one or more titles for the query to operate onprop=imageslists the images on a page; lots of other stuff in prop=, list=, meta=imlimit=sets the max # of results. Default is 10, 'max' works- popular values for format= : xml, json, xmlfm (default), jsonfm
- If you want to find sections from the table of contents, use
section=using theindexproperty; you can usesection=0for the wikitext that comes before the first section header.
- State-changing actions (e.g. editing)
- POST requests only
- two-step process involving token
- details of individual actions are complex, read the docs
- example: ?action=query&titles=Foo&prop=info&intoken=edit for obtaining edit token, then POST to
?action=edit&title=Foo&...
If you want to make a lot of API calls, and perhaps run very busy and active bots et al., please talk to the admins of that wiki ahead of time so they do not block you. Also run your requests in serial, not parallel. resource for contacting them to go here. TODO
- There are limits in the software on how many edits per second you can make.
- Example nouns to look up:
-
- Kanichar
- Kolar_Gold_Fields
- Cooperative_principle
- MS_Riverdance
Magic recipes[edit]
Nonobvious and very useful (these may be outdated - 12/2014)
- Things you'll definitely need:
prop=infofor basic page infoprop=revisionsfor page historyprop=revisions&rvprop=contentfor page wikitextaction=parsefor page HTML
- Doing crazy stuff
- multiple titles with
titles=Foo|Bar|Baz(This will make multiple calls count as one for the purpose of rate limiting)- This works for pages but not revisions. Read the documentation via the Sandbox or via
api.phpautodocs.
- This works for pages but not revisions. Read the documentation via the Sandbox or via
- multiple modules with
&prop=images|templates&list=allpages|blocks - generators (kind of like UNIX pipes) with
&titles=Foo&generator=links&prop=revisions
- multiple titles with
Resources[edit]
- Getting help
- Autogenerated documentation:
api.phpwith no parameters such as https://en.wikipedia.org/w/api.php, or equivalently visit Special:ApiHelp - Documentation on mediawiki.org: API:Main page (details about specific modules/parameters is often outdated, autogenerated docs are authoritative)
- The API Sandbox -- example w:en:Special:ApiSandbox
- mail:mediawiki-api -- mediawiki-api@lists.wikimedia.org
- mediawiki-api-announce@lists.wikimedia.org - PLEASE subscribe because we tell you about breaking changes, which happen a few times every year. mail:mediawiki-api-announce
#mediawikiconnect IRC channel- me! (Roan Kattouw)
- Autogenerated documentation:
You may actually want[edit]
- The dumps of all of Wikipedia so you can work with them locally - http://dumps.wikimedia.org/
- Offline Wikipedia readers such as Kiwix http://www.kiwix.org

