API:Edit - Create&Edit pages
From MediaWiki.org
| This page is part of the MediaWiki API documentation. |
Version >1.13.3 required.
Requires $wgEnableAPI and $wgEnableWriteAPI set to true.
Contents |
[edit] Token
To edit a page, an edit token is required. This token is the same for all pages, but changes at every login. If you want to protect against edit conflicts (which is wise), you also need to get the timestamp of the last revision. You can obtain these as follows: Obtaining an edit token
<?xml version="1.0" encoding="utf-8"?> <api> <query> <pages> <page pageid="15580374" ns="0" title="Main Page" touched="2008-03-27T21:15:39Z" lastrevid="199631190" counter="0" length="4712" edittoken="000000000000000000+\"> <revisions> <rev revid="199631190" user="David Levy" timestamp="2008-03-20T17:26:39Z" comment="Have you tested it in every major browser at every resolution." /> </revisions> </page> </pages> </query> </api>
[edit] Editing pages
Pages can be edited and created with action=edit.
[edit] Parameters
title: Page to editsection: Section to edit. Use 0 for the top section, 'new' for a new sectiontext: New page (or section) contenttoken: Edit tokensummary: Edit comment- When using
section=new, this will be used for the section title
- When using
minor: If set, mark the edit as minornotminor: If set, don't mark the edit as minor, even if you have the "Mark all my edits minor by default" preference enabledbot: If set, mark the edit as bot, even if you are using a bot account the edits will not be marked unless you set this flagbasetimestamp: Timestamp of the last revision, used to detect edit conflicts. Leave unset to ignore conflictsstarttimestamp: Timestamp when you obtained the edit token. Used to detect page deletions and recreations. Leave unset and userecreateto ignore conflictsrecreate: If set, suppress errors about the page having been deleted in the meantime and recreate itcreateonly: If set, throw an error if the page already existsnocreate: If set, throw amissingtitleerror if the page doesn't existwatchlist: Specify how the watchlist is affected by this edit, set to one of "watch", "unwatch", "preferences", "nochange":watch: add the page to the watchlistunwatch: remove the page from the watchlistpreferences: use the preference settings (Default)nochange: don't change the watchlist
md5: MD5 hash of thetextparameter. If this parameter is set and the hashes don't match, the edit is rejected. This can be used to guard against data corruptioncaptchaid: CAPTCHA ID from the previous requestcaptchaword: Answer to the CAPTCHAundo: Revision ID to undo. Overridestext,prependtextandappendtextundoafter: Undo all revisions fromundoup to but not including this one. If not set, just undo one revision
[edit] Example
Note: In this example, all parameters are passed in a GET request just for the sake of simplicity. However, action=edit requires POST requests; GET requests will cause an error. Do not forget to set the Content-Type header of your request to application/x-www-form-urlencoded. The token that you received is terminated with +\, this needs to be urlencoded (so it will end with %2B%5C) before it is passed back. Adding a new section to en:Talk:Main Page
<?xml version="1.0" encoding="utf-8"?> <api> <edit result="Success" pageid="12" title="Talk:Main Page" oldrevid="465" newrevid="471" /> </api>
[edit] CAPTCHAs and extension errors
If an edit requires a CAPTCHA, you'll get something like:
<?xml version="1.0" encoding="utf-8"?> <api> <edit result="Failure"> <captcha type="math" mime="text/tex" id="509895952" question="36 + 4 = " /> </edit> </api>
In most cases, you won't get a math CAPTCHA, but a URL to an image (in the url field). When you've solved the CAPTCHA, retry the request (in this case with captchaid=509895952&captchaword=40).
Other extensions that use the APIEditBeforeSave hook may return errors using either the format described above or the usual error format.
[edit] Possible errors
In addition to the usual stuff:
- code: notitle
- info: The title parameter must be set
- code: notext
- info: One of the text, appendtext, prependtext and undo parameters must be set
- code: notoken
- info: The token parameter must be set
- code: invalidsection
- info: The section parameter must be set to an integer or 'new'
- code: protectedtitle
- info: This title has been protected from creation
- code: cantcreate
- info: You don't have permission to create new pages
- code: cantcreate-anon
- info: Anonymous users can't create new pages
- code: articleexists
- info: The article you tried to create has been created already
- code: noimageredirect-anon
- info: Anonymous users can't create image redirects
- code: noimageredirect
- info: You don't have permission to create image redirects
- code: spamdetected
- info: Your edit was refused because it contained a spam fragment: ``wikitext''
- code: filtered
- info: The filter callback function refused your edit
- code: contenttoobig
- info: The content you supplied exceeds the article size limit of bytes bytes
- code: noedit-anon
- info: Anonymous users can't edit pages
- code: noedit
- info: You don't have permission to edit pages
- code: pagedeleted
- info: The page has been deleted since you fetched its timestamp
- code: emptypage
- info: Creating new, empty pages is not allowed
- code: emptynewsection
- info: Creating empty new sections is not possible.
- code: editconflict
- info: Edit conflict detected
- code: revwrongpage
- info: rrevid is not a revision of ``pagename''
- Thrown if an invalid revid is given for
undoorundoafter
- Thrown if an invalid revid is given for
- info: rrevid is not a revision of ``pagename''
- code: undo-failure
- info: Undo failed due to conflicting intermediate edits