API: Difference between revisions

From Meta, a Wikimedia project coordination wiki
Content deleted Content added
→‎Login: +overview
Revert to the revision prior to revision 22874674 dated 2022-02-22 05:23:35 by Ogmommy3 using popups
Tag: Manual revert
 
(78 intermediate revisions by 24 users not shown)
Line 1: Line 1:
{{MovedToMediaWiki|API}}
<div style="background: #f0f0e0; border: 2px solid #aaa; margin: 1em 10%; padding: 4px 4px 4px 15px; width:60%; text-align: center;">
'''Attention visitors'''


Moved preserving full histories. '''[[User:Robchurch|robchurch]]''' | [[User_talk:Robchurch|talk]] 07:09, 14 May 2007 (UTC)
This page discusses the future API for MediaWiki software.

<div style="text-align: left;">
MediaWiki at present has three interfaces:
* [http://en.wikipedia.org/w/query.php Query API] for retrieving any information in xml/json/php formats.
* [[Special:Export]] feature (bulk export of xml formatted data)
* Regular Web-based interface
</div>
</div>

{{MoveToMediaWiki}}

== Overview ==


== Login ==
'''Request:'''
api.php ? action=login & name=Yurik & password=12345 [& domain=wikipedia.org]
'''Result:'''
api:
login:
result: Success ''Other values: NoName, Illegal, WrongPluginPass,''
''NotExists, WrongPass, EmptyPass''
token: 1234567890ABCDEF ''Also returned as a cookie (i.e. enwikiToken)''
userName: Yurik ''Also returned as a cookie (i.e. enwikiUserName)''
userID: 12345 ''Also returned as a cookie (i.e. enwikiUserID)''

== Query - General ==
=== Limits ===
: To prevent server overloads, each query imposes a limit on how many items it can process. Anonymous and logged-in users have one limit, while bots have a considerably higher limit as they are trusted by the community. At present, each query simply lists the maximum request size it allows. For example, allpages list will allow aplimit= to be set no higher than 500, or in case of a bot - no higher than 5000.
: '''Drawbacks:''' Currently all limits are additive, so if the user requests allpages and backlinks, the user will get 500 of each. This is not very good, as the more items are compounded into one request, the heavier the load on the server will be. Instead, some sort of a weighted mechanism should be developed, where each request item has a certain "cost" associated with it, and each user is allocated a fixed allowance per request. The more information user requests, the less the limit becomes for that request. Unfortunately, that makes it very hard to figure out the maximum limits before executing the query, so might not be a workable solution.

=== Title Normalization ===
: Converts improper page titles to their proper form. Capitalizes first character, replaces '_' with ' ', changes canonical namespace names to their localized alternatives, etc.
'''Request:''' ''Note: articleA's first letter is not capitalized''
api.php ? action=query & titles=Project:articleA|ArticleB
'''Result:'''
api:
query:
pages:
Wikipedia:ArticleA: ''Project: is converted to Wikipedia: when running on en-wiki.''
ns: 4 ''Show title's namespace except when ns=0''
ArticleB:
normalized: ''Any requested titles not in the "proper" form will be here''
Project:articleA: Wikipedia:ArticleA

=== Redirects ===
: Redirects can be resolved by the server, so that the target of redirect is returned instead of the given title. This example is not very usefull without additional prop=... element, but shows the usage of redirect function. The 'redirects' section will contain the target of redirect and non-zero namespace code. Both normalization and redirection may take place. In case of redirect to a redirect, all redirections will be solved, and in case of a circular redirection, there might not be a page in the 'pages' section.
'''Request:'''
api.php ? action=query & titles=Main page & redirects
'''Result:'''
api:
query:
pages:
Main Page:
redirects:
Main page: Main Page
: Same request without the "redirects" parameter would treat "Main page" as a regular page, so revisions and other information may be obtained. In order to see that it is a redirect, the basic page info must be requested using prop=info.
'''Request:'''
api.php ? action=query & titles=Main page & prop=info
'''Result:'''
api:
query:
pages:
Main page:
id: 12342
redirect:

=== Circular Redirects ===
: Assume Page1 &rarr; Page2 &rarr; Page3 &rarr; Page1 (circular redirect). Also, in this example a non-normalized name 'page1' is used.
'''Request:'''
api.php ? action=query & titles=page1 & redirects
'''Result:'''
api:
query:
redirects:
Page1: Page2 ''Redirects are present, but not the 'pages' element.''
Page2: Page3
Page3: Page1
normalized:
page1: Page1

== Query - Page Information ==
Page information items are used to get various data about a list of pages provided with the ''titles='' parameter. Content, links, interwiki links, and other information may be obtained.

=== info (in) ===
: Gets the basic page information such as pageid, last revid, redirect, last touched, etc. Limit: 500/5000.
: '''Parameters:''' intokens=edit|rollback|delete|protect|move
: '''Issues:''' Should there be tokens for rollback/delete/protect/move be available in this way, as oppose to having an ''action='' for each task? There is a potential for abuse, as someone might have a link on their website to wiki, and that link would contain a "delete" action. If a logged in admin clicks on that link, the api will recognize them because of their cookie, and will allow the deletion.
'''Request:'''
api.php ? action=query & prop=info & titles=TitleA
'''Result:'''
api:
query:
pages:
TitleA:
id: 12341
lastrev: 23456
touched: 20060908025739

=== categories (cl) ===
: Gets a list of all templates used on the provided pages. Limit: 200/1000.
: '''Parameters:''' clprop=sortkey|timestamp
'''Request:'''
api.php ? action=query & prop=categories & titles=TitleA
'''Result:'''
api:
query:
pages:
TitleA:
categories:
Category:Cat1:
Category:Cat2:

=== content (??) ===
: Returns wiki markup for the given list of articles. Requesting content is just a shorthand for the last revision request with content, so it would be equivalent to prop=revisions & rvprop=content without any other rv* parameters. Limit: No more than 50 (user) / 200 (bot) page contents per request.
'''Request:'''
api.php ? action=query & prop=content & titles=ArticleA|ArticleB
'''Result:'''
api:
query:
pages:
ArticleA:
revisions:
67890:
content: ...raw page content...
ArticleB:
missing:

=== imageinfo (ii) ===
: Gets image information for any titles in the image namespace (#6).
: '''Parameters:''' iiprop=url|history|comment|stats|user|timestamp, iisource=local/shared/all (dflt=local)
:: url - path to the image, history - include every old image versions, stats - image size/type, user - uploader, iisource - look at the local or shared (commons) image repository, or both.
: '''Example:''' Get comments for all image uploads, both local and in the commons repository. Here, ImageA was uploaded 3 times to the local wiki, and 2 times to the shared (commons) repository.
'''Request:'''
api.php ? action=query & prop=imageinfo & titles=Image:ImageA & iiprop=comment|history & iisource=all
'''Result:'''
api:
query:
pages:
Image:ImageA:
ns:6
imageinfo:
local:
comment: last update comment
localhistory:
- ''history is an unordered list of items''
comment: some update
-
comment: another update
shared:
comment: last update on commons
sharedhistory:
-
comment: some update on commons

=== langlinks (ll) ===
: Gets a list of all language links (interwikies) from the provided pages to other languages. Limit: 200/1000.

=== links (pl) ===
: Gets a list of all links from the provided pages. Limit: 200/1000.
: '''Parameters:''' plnamespace (flt).

=== templates (tl) ===
: Gets a list of all templates used on the provided pages. Limit: 200/1000.

== Query - Revisions ==
Returns revisions for a given article based on the selection criteria. Revisions may be used with multiple titles only when working with the latest revision. When using rvlimit, rvdir=newer, rvstart, or rvend parameters, titles= must have only one title listed. By default, revisions shows only the id of the last revision.
'''Request:'''
api.php ? action=query & prop=revisions & titles=ArticleA & rvprop=timestamp|user|comment|content
'''Result:'''
api:
query:
pages:
ArticleA:
id: 12345
lastrev: 67890
revisions:
67890:
timestamp: 20060908025739
user: UserX
comment: ...change comment...
content: ...raw revision content...

; Additional 'revisions' samples
: Get the timestamps of up to 10 revisions, begining at 2006-09-01 and moving forward in time.
api.php ? action=query & prop=revisions & titles=ArticleA & rvprop=timestamp & rvlimit=10 & rvdir=newer & rvstart=20060901000000
: Get the timestamps of all revisions for the entire month of September 2006. rvlimit is optional. If the number of revisions exceeds the limit, the 'revisions' element will contain ''' 'continue':'rvstart=20060920122343' ''' with the timestamp to continue from.
api.php ? action=query & prop=revisions & titles=ArticleA & rvprop=timestamp & rvstart=20060901000000 & rvend=20061001000000
: Get the timestamps of up to 10 revisions, begining at 12345 and moving back in time. If more than 10 revisions are available, 'revisions' element will contain ''' 'continue':'revids=23512' ''', where revid is the next revision id in order.
api.php ? action=query & prop=revisions & revids=12345 & rvprop=timestamp & rvlimit=10 & rvdir=older
: Get the timestamps of all revisions between two given revision IDs. rvlimit is optional. If the number of revisions exceeds the limit, the 'revisions' element will contain ''' 'continue':'rvstartid=23512' ''' with the revid to continue from. Both rvstartid & rvendid must belong to the same title. The titles= parameter is not required, but if given, it must be set to the same title as revision IDs.
api.php ? action=query & prop=revisions & rvprop=timestamp & rvstartid=12345 & rvendid=67890

== Query - Lists ==
Lists differ from other properties in two aspects - instead of appending data to the elements under 'pages' element, each list has its own separated branch under 'query' element. Also, list output is limited by number of items, and may be continued using "paging" technique. Even when no limit is provided, the query will only return a set number of items, and will also provide a string point from which to continue paging. See allpages list for an example.

=== allpages (ap) ===
: Returns a list of pages in a given namespace starting at ''from'', ordered by page title.
: '''Parameters:''' apfrom (paging), apnamespace (dflt=0), apredirect (flt), aplimit (dflt=10, max=500/5000)

: '''Example:''' Request a list of 3 pages from namespace 10 (templates) begining at the first available page.
'''Request:'''
api.php ? action=query & list=allpages & apnamespace=10 & aplimit=3
'''Result:'''
api:
query:
allpages:
Template:A-Article:
id: 12341
ns: 10
Template:B-Article:
id: 12342
ns: 10
Template:C-Article:
id: 12343
ns: 10
query-status:
allpages:
continue: apfrom=D-Article ''The next item in this list would have been Template:D-Article.''
: The client may now make another request using the ''continue'' value as a parameter:
api.php ? action=query & list=allpages & apnamespace=10 & aplimit=3 & apfrom=D-Article

=== backlinks (bl) ===
: Lists pages that link to the given page. Ordered by linking page title.
: '''Parameters:''' bltitle, blfrom (paging), blnamespace (flt), blredirect (flt), bllimit (dflt=10, max=500/5000)
api.php ? action=query & list=backlinks & bltitle=ArticleA

=== categorymembers (cm) ===
: List of pages that belong to a given category, ordered by page title.
: '''Parameters:''' cmtitle (if title is in NS 0, treats it as category NS), cmfrom (paging), cmnamespace (flt), cmlimit (dflt=10, max=500/5000)
api.php ? action=query & list=categorymembers & cmtitle=category:title

=== embeddedin (ei) ===
: What pages include ''template:title'' page as a template. List of pages that include the given page using <nowiki>{{title}}</nowiki>. Ordered by including page title.
: '''Parameters:''' eititle, eifrom (paging), einamespace (flt), eiredirect (flt), eilimit (dflt=10, max=500/5000)
api.php ? action=query & list=embeddedin & eititle=template:title

=== imagelinks (il) ===
: List of pages that include a given image. Ordered by page title.
: '''Parameters:''' iltitle (if image title is in NS 0, treats it as an image NS), ilfrom (paging), ilnamespace (flt), illimit (dflt=10, max=500/5000)
api.php ? action=query & list=imagelinks & iltitle=image:title

=== logevents (le) ===
: List log events, filtered by time range, event type, user type, or the page it applies to. Ordered by event timestamp.
: '''Parameters:''' letype (flt), lefrom (paging timestamp), leto (flt), ledirection (dflt=older), leuser (flt), letitle (flt), lelimit (dflt=10, max=500/5000)
api.php ? action=query & list=logevents - ''List last 10 events of any type''

=== recentchanges (rc) ===
: Gets a list of pages recently changed, ordered by modification timestamp.
: '''Parameters:''' rcfrom (paging timestamp), rcto (flt), rcnamespace (flt), rcminor (flt), rcusertype (dflt=not|bot), rcdirection (dflt=older), rclimit (dflt=10, max=500/5000)
api.php ? action=query & list=recentchanges - ''List last 10 changes''

=== usercontribs (uc) ===
: Gets a list of pages modified by a given user, ordered by modification time.
: '''Parameters:''' ucuser, ucfrom (paging timestamp), ucto (flt), ucnamespace (flt), ucminor (flt), uctop (flt), ucdirection (dflt=older), uclimit (dflt=10, max=500/5000)
api.php ? action=query & list=usercontribs & ucuser=User:UserA - ''List last 10 changes made by userA''

=== users (us) ===
: Gets a list of registered users, ordered by user name.
: '''Parameters:''' usfrom (paging), uslimit (dflt=10, max=500/5000)

=== watchlist (wl) ===
: Get a list of pages on the user's watchlist but only if they were changed within the given time period. Ordered by time of the last change of the watched page.
: '''Parameters:''' wlfrom (paging timestamp), wlto (flt), wlnamespace (flt), wldirection (dflt=older), wllimit (dflt=10, max=500/5000)

== Query - Generators ==
Generator is way to use one of the above [[#lists]] instead of the titles= parameter. The output of the list must be a list of pages, whose titles get automatically used instead of the titles=/revids=/pageids= parameters. Other queries such as content, revisions, etc, will treat those pages as if they were provided by the user in the titles= parameter. Only one generator is allowed, and while it is possible to have both generator= and list= parameters in the same call, they may not contain the same values.

: '''Example:''' Use the allpages list as a generator, to get the links and categories for all titles returned by allpages.
'''Request:'''
api.php ? action=query & generator=allpages & apnamespace=3 & aplimit=10 & apfrom=A & prop=links|categories
'''Result:'''
api:
query:
pages:
Template:A-Article:
id: 12341
ns: 10
links:
Linked Article1: ''Linked Article1 is in the main namespace''
Talk:Linked Article2: ''For non-main ns, list it as a subelement''
ns: 1
...
categories:
Category:Cat1:
Category:Cat2:
...
Template:B-Article:
...
Template:C-Article:
...
query-status:
allpages:
continue: apfrom=D-Article ''The next item in this list would have been Template:D-Article.''


: '''Complex example:''' Using a "links" page property as a generator. This query will get all the links from all the pages that are linked from Title. For this example, assume that Title has links to TitleA and TitleB. TitleB is a redirect to TitleC. TitleA links to TitleA1, TitleA2, TitleA3; and TitleC links to TitleC1 & TitleC2. Redirect is solved because of the "redirects" parameter.

: The query will execute the following steps:
:# For all pages specified in titles=...|... parameter, get all links, and substitute original with the new titles=...|... parameter.
:# Execute regular prop=links query using the internally created list of titles.

'''Request:'''
api.php ? action=query & generator=links & titles=Title & prop=links & redirects
'''Result:'''
api:
query:
pages:
TitleA:
links:
TitleA1:
TitleA2:
TitleA3:
TitleC:
links:
TitleC1:
TitleC2:
redirects:
TitleB: TitleC

== Posting Data ==
'''Request:'''
api.php ? action=save & title=Project:articleA & edittoken=abc123 & summary=...lalala... & content=...wikitext...
'''Result:'''
api:
save:
status: Success ''Other values: 'Prohibited', 'Conflict', 'DbLcoked', 'BadToken''''
title: Wikipedia:ArticleA ''Always returns normalized title''
ns: 4 ''Show title's namespace except when ns=0''
id: 12345 ''On success, the ID of the page''
revid: 67891 ''On success, the new latest revision id''
redirect: ''On success, when saved page is now treated as a redirect''

== Implementation Strategy ==
''See [[/Implementation Strategy]]''.

== Wikimania 2006 API discussion ==
''See [[/Wikimania 2006 API discussion]].''

Latest revision as of 06:02, 22 February 2022

Moved preserving full histories. robchurch | talk 07:09, 14 May 2007 (UTC)[reply]