API:Edit/zh

From MediaWiki.org
Jump to: navigation, search
語言:Project:Language policy English  • Deutsch • español • français • 日本語 • русский • Tiếng Việt • 中文
本页是MediaWiki action API文档的一部分。

MediaWiki action API

v · d · e
MediaWiki版本: 1.13

需要以下MediaWiki选项设置为 true: $wgEnableAPI$wgEnableWriteAPI。見Manual:DefaultSettings.php

编辑令牌[edit]

要有编辑令牌才可以编辑页面。这个令牌在所有页面中都一样,但是每次登录都会不同;它也会过期。If you want to protect against edit conflicts (which is wise),你也需要得到最近版本的时间戳。You can obtain these as follows: 获取编辑令牌

当使用Edit API时,always pass the token 参数 last,or at least,after the text 参数。

创建或编辑页面[edit]

edit动作(action=edit)用于创建或编辑页面。

参数[edit]

Some of these 参数 seem to be revised in v1.20; for the current list,see ApiEditPage::getAllowedParams()。To see the 参数 list currently in use at Wikipedia,see:
  • title:要编辑的页面。不能和pageid连用。
    Note: If $wgCapitalLinks is set to false, 'Article' and 'article' will not be considered as the same 页面, as in Wiktionary, for example.
  • pageid 要编辑页面的ID。不能和title连用。
  • section:修改哪一章节。0是导言。章节可能是嵌套的,例如section=1包含了section=2的部分。section=new表示追加一个新章节。如果为空,则表示修改全文。可以用api.php?action=parse&prop=sections 获取所有章节标题。
  • sectiontitle:创建新话题的标题。If not specified,summary 将会替代。MW 1.19+
  • text:新页面(或者话题)的 内容
  • tokenEdit token。特别是您没有使用md5 参数,the token should be sent as the last 参数,or at least,after the text 参数,to prevent a bad edit from getting committed if transmission of the body is interrupted for some reason。This 参数 is required.
  • summary:编辑摘要
  • minor:如果设置为true,标记为小编辑。[1]
  • notminor:如果设置为true,不标记为小编辑,在您启用了 "Mark all my edits minor by default" 后适用。[1]
  • bot:如果设置,将编辑标记为机器人的编辑; 即使您作为机器人登陆,也必须设置本项才能将编辑标记为机器人编辑[1]
  • basetimestampTimestamp of the base revision (obtained through prop=revisions&rvprop=timestamp). Used to detect edit conflicts; leave unset to ignore conflicts
  • starttimestamp:Timestamp when you obtained the edit token. Used to detect edit conflicts; leave unset to ignore conflicts
  • recreate:如果设置,suppress errors about the 页面 having been deleted in the meantime and recreate it [1]
  • createonly:如果设置,throw an error if the 页面 already exists [1]
  • nocreate:如果设置,throw a missingtitle error if the 页面 doesn't exist [1]
  • watchlist:Specify how the watchlist is affected by this edit,set to one of "监视", "不监视", "缺省设置", "不改变":
    • watch:在监视列表添加此页
    • unwatch:从监视列表中移除此页
    • preferences:使用 (缺省)设置
    • nochange:不要改变监视列表
  • md5:MD5 hash (hex) of the text 参数 or the prependtext and appendtext 参数 concatinated. If this 参数 is set and the hashes don't match, the edit is rejected. This can be used to guard against data corruption
  • prependtext:Add this text to the beginning of the 页面. Overrides text
  • appendtext:Add this text to the end of the 页面. Overrides text. Use section=new to append a new section
  • 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
  • redirect:Automatically resolve redirects[1]
  • contentformat:内容 serialization format used for the input text
    • text/x-wiki for wikitext
    • text/javascript for javascript
    • text/css for css
    • text/plain for plaintext
    • application/json for json
  • contentmodel:内容 model of the new 内容
    • wikitext
    • javascript
    • css
    • text
    • JsonZeroConfig
    • Scribunto
    • JsonSchema
  • captchaid:CAPTCHA ID from the previous request
  • captchaword:验证码的答案
  1. 1.0 1.1 1.2 1.3 1.4 1.5 1.6 The presence of this variable activates its feature. For instance adding the value "recreate" with or without a value will suppress errors about the 页面 being previously deleted.

例子[edit]

注意: 在这个例子中,all 参数 are passed in a GET request just for the sake of simplicity。然而,action=edit 需要 POST 请求; GET 请求将会产生一个错误。不要忘记设置 the Content-Type 头 of your request to application/x-www-form-urlencoded。The token that you received is terminated with +\,this needs to be 进行URL编码 (将以%2B%5C结尾) 在passback之前。 Adding a new section to en:Talk:Main Page

验证码和拓展错误[edit]

如果一次编辑请求需要验证码,你将会得到类似下面的:

<?xml version="1.0" encoding="utf-8"?>
<api>
  <edit result="Failure">
    <captcha type="math" mime="text/tex" id="509895952" question="36 + 4 = " />
  </edit>
</api>

在很多情况下,you won't get a math CAPTCHA,but a URL to an image (in the url field)。When you've solved the CAPTCHA,重试请求 (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]

In addition to the usual stuff:

  • code: notitle
    • info: The title 参数 must be set
  • code: notext
    • info: One of the text,appendtext,prependtext and undo 参数 must be set
  • code: notoken
    • info: The token 参数 must be set
  • code: invalidsection
    • info: The section 参数 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
  • code: missingtitle

使用Ajax编辑[edit]

Below is sample code for editing a page via an Ajax request:

function addNewSection( summary, content, editToken ) {
    $.ajax({
        url: mw.util.wikiScript( 'api' ),
        data: {
            format: 'json',
            action: 'edit',
            title: mw.config.get( 'wgPageName' ),
            section: 'new',
            summary: summary,
            text: content,
            token: editToken
        },
        dataType: 'json',
        type: 'POST',
        success: function( data ) {
            if ( data && data.edit && data.edit.result == 'Success' ) {
                window.location.reload(); // reload page if edit was successful
            } else if ( data && data.error ) {
                alert( 'Error: API returned error code "' + data.error.code + '": ' + data.error.info );
            } else {
                alert( 'Error: Unknown result from API.' );
            }
        },
        error: function( xhr ) {
            alert( 'Error: Request failed.' );
        }
    });
}

大文本[edit]

当文本超过 8000 字时,使用 "multipart/form-data" as it does not require escaping and will be significantly smaller than its urlencoded counterpart。Multipart has roughly 160 bytes of overhead for each 参数,so for seven 参数 that's ~1.1kb overhead。 例子:

POST http://en.wikipedia.org/w/api.php HTTP/1.1
User-Agent: Bot Framework
Content-Type: multipart/form-data; boundary=---------------------------8ce61ec834cf268
Host: en.wikipedia.org
Cookie: <removed>
Content-Length: 1348
Accept-Encoding: gzip
Connection: Keep-Alive


-----------------------------8ce61ec834cf268
Content-Disposition: form-data; name="action"

edit
-----------------------------8ce61ec834cf268
Content-Disposition: form-data; name="title"
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Title here
-----------------------------8ce61ec834cf268
Content-Disposition: form-data; name="text"
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Text here
-----------------------------8ce61ec834cf268
Content-Disposition: form-data; name="summary"
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Summary
-----------------------------8ce61ec834cf268
Content-Disposition: form-data; name="token"

1f287ba00a908e9622045e7b18ffa352+\
-----------------------------8ce61ec834cf268
Content-Disposition: form-data; name="assert"

user
-----------------------------8ce61ec834cf268
Content-Disposition: form-data; name="format"

xml
-----------------------------8ce61ec834cf268--