API:Edit - Create&Edit pages

From MediaWiki.org

Jump to: navigation, search
This page is part of the MediaWiki API documentation.
MediaWiki API


MediaWiki version: 1.13

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

api.php ? action=query & prop=info|revisions & intoken=edit & titles=Main%20Page

<?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="+\">
        <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? Have you tested it in screen readers for the visually-impaired? Have you sought consensus for the altered appearance?  Please stop." />
        </revisions>
      </page>
    </pages>
  </query>
</api>

[edit] Editing pages

Pages can be edited and created with action=edit.

[edit] Parameters

  • title: Page to edit
  • section: Section to edit. Use 0 for the top section, 'new' for a new section
  • text: New page (or section) content
  • token: Edit token
  • summary: Edit comment
    • When using section=new, this will be used for the section title
  • minor: If set, mark the edit as minor
  • notminor: If set, don't mark the edit as minor, even if you have the "Mark all my edits minor by default" preference enabled
  • bot: If set, mark the edit as bot
  • basetimestamp: Timestamp of the last revision, used to detect edit conflicts. Leave unset to ignore conflicts
  • recreate: If set, suppress errors about the page having been deleted in the meantime and recreate it
  • createonly: If set, throw an error if the page already exists
  • nocreate: If set, throw an error if the page doesn't exist
  • watch: Add the page to your watchlist
  • unwatch: Remove the page from your watchlist, or don't add it even though you have a preference like "Automatically watch all pages I edit" enabled
  • md5: MD5 hash of the text parameter. If this parameter is set and the hashes don't match, the edit is rejected. This can be used to guard against data corruption
  • captchaid: CAPTCHA ID from the previous request
  • captchaword: Answer to the CAPTCHA

[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.

Adding a new section to en:Talk:Main Page

api.php ? action=edit & title=Talk:Main_Page & section=new & summary=Hello%20World & text=Hello%20everyone! & watch & basetimestamp=2008-03-20T17:26:39Z & token=cecded1f35005d22904a35cc7b736e18+\

<?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 in 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: The text parameter 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: editconflict
    • info: Edit conflict detected
Personal tools