API:Edit - Create&Edit pages

From MediaWiki.org

Jump to: navigation, search
Tools clipart.png This page is part of the MediaWiki API documentation.
MediaWiki API
MediaWiki version: 1.13

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 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, even if you are using a bot account the edits will not be marked unless you set this flag
  • basetimestamp: Timestamp of the last revision, used to detect edit conflicts. Leave unset to ignore conflicts
  • starttimestamp: Timestamp when you obtained the edit token. Used to detect page deletions and recreations. Leave unset and use recreate 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 a missingtitle error if the page doesn't exist
  • watchlist: Specify how the watchlist is affected by this edit, set to one of "watch", "unwatch", "preferences", "nochange":
    • watch: add the page to the watchlist
    • unwatch: remove the page from the watchlist
    • preferences: use the preference settings (Default)
    • nochange: don't change the watchlist
  • 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
  • undo: Revision ID to undo. Overrides text, prependtext and appendtext
  • undoafter: Undo all revisions from undo up 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 undo or undoafter
  • code: undo-failure
    • info: Undo failed due to conflicting intermediate edits