Jump to content

API:Compare/zh

From mediawiki.org
This page is a translated version of the page API:Compare and the translation is 3% complete.

GET request to get the difference between two pages.

MediaWiki版本:
1.18

API documentation

action=compare

(main | compare)

Get the difference between two pages.

A revision number, a page title, a page ID, text, or a relative reference for both "from" and "to" must be passed.

Specific parameters:
Other general parameters are available.
fromtitle

First title to compare.

fromid

First page ID to compare.

Type: integer
fromrev

First revision to compare.

Type: integer
fromslots

Override content of the revision specified by fromtitle, fromid or fromrev.

This parameter specifies the slots that are to be modified. Use fromtext-{slot}, fromcontentmodel-{slot}, and fromcontentformat-{slot} to specify content for each slot.

Values (separate with | or alternative): main
fromtext-{slot}

Text of the specified slot. If omitted, the slot is removed from the revision.

This is a templated parameter. When making the request, {slot} in the parameter's name should be replaced with values of fromslots.
fromsection-{slot}

When fromtext-{slot} is the content of a single section, this is the section identifier. It will be merged into the revision specified by fromtitle, fromid or fromrev as if for a section edit.

This is a templated parameter. When making the request, {slot} in the parameter's name should be replaced with values of fromslots.
fromcontentformat or alternative): main
totext-{slot}

Text of the specified slot. If omitted, the slot is removed from the revision.

This is a templated parameter. When making the request, {slot} in the parameter's name should be replaced with values of toslots.
tosection-{slot}

When totext-{slot} is the content of a single section, this is the section identifier. It will be merged into the revision specified by totitle, toid or torev as if for a section edit.

This is a templated parameter. When making the request, {slot} in the parameter's name should be replaced with values of toslots.
tocontentformat-{slot}

Content serialization format of totext-{slot}.

This is a templated parameter. When making the request, {slot} in the parameter's name should be replaced with values of toslots.
One of the following values: application/json, application/octet-stream, application/unknown, application/x-binary, text/css, text/javascript, text/plain, text/unknown, text/x-wiki, unknown/unknown
tocontentmodel-{slot}

Content model of totext-{slot}. If not supplied, it will be guessed based on the other parameters.

This is a templated parameter. When making the request, {slot} in the parameter's name should be replaced with values of toslots.
One of the following values: GadgetDefinition, Json.JsonConfig, JsonSchema, Map.JsonConfig, MassMessageListContent, NewsletterContent, Scribunto, SecurePoll, Tabular.JsonConfig, css, flow-board, javascript, json, sanitized-css, text, translate-messagebundle, unknown, wikitext
topst

Do a pre-save transform on totext.

Type: boolean (details)
totext
Deprecated.

Specify toslots=main and use totext-main instead.

tocontentformat
Deprecated.

Specify toslots=main and use tocontentformat-main instead.

One of the following values: application/json, application/octet-stream, application/unknown, application/x-binary, text/css, text/javascript, text/plain, text/unknown, text/x-wiki, unknown/unknown
tocontentmodel
Deprecated.

Specify toslots=main and use tocontentmodel-main instead.

One of the following values: GadgetDefinition, Json.JsonConfig, JsonSchema, Map.JsonConfig, MassMessageListContent, NewsletterContent, Scribunto, SecurePoll, Tabular.JsonConfig, css, flow-board, javascript, json, sanitized-css, text, translate-messagebundle, unknown, wikitext
tosection
Deprecated.

Only use the specified section of the specified 'to' content.

prop

Which pieces of information to get.

diff
The diff HTML.
diffsize
The size of the diff HTML, in bytes.
rel
The revision IDs of the revision previous to 'from' and after 'to', if any.
ids
The page and revision IDs of the 'from' and 'to' revisions.
title
The page titles of the 'from' and 'to' revisions.
user
The username and ID of the 'from' and 'to' revisions. If the user has been revision deleted, a fromuserhidden or touserhidden property will be returned.
comment
The comment on the 'from' and 'to' revisions. If the comment has been revision deleted, a fromcommenthidden or tocommenthidden property will be returned.
parsedcomment
The parsed comment on the 'from' and 'to' revisions. If the comment has been revision deleted, a fromcommenthidden or tocommenthidden property will be returned.
size
The size of the 'from' and 'to' revisions.
timestamp
The timestamp of the 'from' and 'to' revisions.
Values (separate with | or alternative): comment, diff, diffsize, ids, parsedcomment, rel, size, timestamp, title, user
Default: diff|ids|title
slots

Return individual diffs for these slots, rather than one combined diff for all slots.

Values (separate with | or alternative): main
To specify all values, use *.
difftype

Return the comparison formatted as inline HTML.

One of the following values: inline, table, unified
Default: table
Example:
Create a diff between revision 1 and 2.
api.php?action=compare&fromrev=1&torev=2 [open in sandbox]


示例

GET request

Compare the current revisions of two different pages.

Response

{
    "compare": {
        "fromid": 1882196,
        "fromrevid": 739666518,
        "fromns": 10,
        "fromtitle": "Template:Unsigned",
        "toid": 32371774,
        "torevid": 909784724,
        "tons": 10,
        "totitle": "Template:UnsignedIP",
        ...
    }
}

Sample code

Python

#!/usr/bin/python3

"""
    compare.py

    MediaWiki Action API Code Samples
    Demo of `Compare` module: Compare the current revisions of two different pages
    
    MIT license
"""

import requests

S = requests.Session()

URL = "https://en.wikipedia.org/w/api.php"

PARAMS = {
    'action':"compare",
    'format':"json",
    'fromtitle':'Template:Unsigned',
    'totitle':'Template:UnsignedIP'
}

R = S.get(url=URL, params=PARAMS)
DATA = R.json()

print(DATA)

PHP

<?php
/*
    compare.php

    MediaWiki Action API Code Samples
    Demo of `Compare` module: Compare the current revisions of two different pages
    
    MIT license
*/

$endPoint = "https://en.wikipedia.org/w/api.php";
$params = [
    "action" => "compare",
    "format" => "json",
    "fromtitle" => "Template:Unsigned",
    "totitle" => "Template:UnsignedIP"

];

$url = $endPoint . "?" . http_build_query( $params );

$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$output = curl_exec( $ch );
curl_close( $ch );

echo( $output );

JavaScript

/*
    compare.js

    MediaWiki Action API Code Samples
    Demo of `Compare` module: Compare the current revisions of two different pages

    MIT license
*/

var url = "https://en.wikipedia.org/w/api.php"; 

var params = {
    action: "compare",
    format: "json",
    fromtitle: "Template:Unsigned",
    totitle: "Template:UnsignedIP"

};
request.get({ url: url, qs: params }, function(error, res, body) {
    if (error) {
        return;
    }
    console.log(body);
});

MediaWiki JS

/*
    compare.js

    MediaWiki Action API Code Samples
    Demo of `Compare` module: Compare the current revisions of two different pages

    MIT license
*/

var params = {
    action: "compare",
    format: "json",
    fromtitle: "Template:Unsigned",
    totitle: "Template:UnsignedIP"
},
api = new mw.Api();

api.get( params ).done( function ( data ) {
console.log( data );
} );

Possible errors

Code Info
compare-relative-to-nothing 没有与torelative的“from”修订版本相对的版本。
compare-relative-to-deleted 不能相对于已删除的修订版本使用torelative=value
missingrev-title 没有标题title的当前修订版本。
baddiff 不能取得差异。一个或多个修订版本不存在,或您没有权限查看它们。
missingcontent-revid 丢失ID为revid的修订版本的内容。
invalidtitle 错误标题“title”。
nosuchpageid 没有ID为id的页面。
missingtitle-byname 页面name不存在。
nosuchrevid 没有ID为id的修订版本。
missingcontent-revid-role 角色main的修订ID id缺少内容。
compare-nosuchfromsection 在“from”内容中没有章节name
compare-nosuchtosection 在“to”内容中没有章节name
compare-maintextrequired fromslots包含main时,需要参数fromtext-main (不能删除主要部分)。
compare-maintextrequired toslots包含main时,需要参数totext-main (不能删除主要部分)。
compare-notext 参数$1不能在没有$2的情况下使用。
compare-no-title 不能在没有标题的情况下预保存转换。尝试指定fromtitletotitle
compare-nofromrevision 没有“from”修订版本。 请指定fromrevfromtitlefromid
compare-notorevision 没有“to”修订版本。 请指定torevtotitletoid
sectionsnotsupported 内容模型model不支持章节。
sectionreplacefailed 不能合并更新的章节。
missingparam 需要fromtitle、​fromid、​fromrev、​fromtext、​fromrelativefromslots中的至少一个参数。
missingparam 需要totitle、​toid、​torev、​totext、​torelativetoslots中的至少一个参数。

Parameter history

  • v1.32: Introduced fromslots, toslots, fromtext-{slot}, fromsection-{Language converter depth limit exceeded (10)slot}, fromcontentformat-{slot}, fromcontentmodel-{slot}, totext-{slot}, tosection-{slot}, tocontentformat-{slot}, tocontentmodel-{slot}
  • v1.30: Introduced frompst, torelative, topst, prop
  • v1.20: Introduced fromid, toid

Additional notes

To get the difference between two pages, a revision number, a page title, or a page ID for both from and to must be passed.

Relative comparison at first and last revision

  • You can ask for a relative comparison to prev at the first revision of a page, this will result in the full text of the first revision being returned as the diff. Note that this is not accurate, since in some cases there may be default content for the page before its first revision. The fromrevid property will be absent from the results.
  • Similarly, you can ask for the next relative comparison on the last revision of a page.
  • (Note: this is the last revision as of the writing of this topic. Because the page is protected, it probably should not change.) The result is an empty diff, and the torevid will be absent. Unlike most revision comparisons, these results will change if a new revision is created.

This behavior in the API is historical. It isn't consistent with the conceptual model of page history as a series of revisions. There is no previous revision to compare against the first revision, nor is there a next revision to compare against the last. Because of this, future versions of MediaWiki may give different results for this API call.

Using the HTML output

The prop values diff and parsedcomment return HTML snippets. The diff HTML is a list of table rows (‎<tr> elements) which should be embedded into a table with at least the following markup:

<table class="diff">
  <colgroup>
    <col class="diff-marker">
    <col class="diff-content">
    <col class="diff-marker">
    <col class="diff-content">
  </colgroup>
  <tbody>
    DIFF HTML SNIPPET GOES HERE
  </tbody>
</table>

To display metadata as well, e. g. the user or comment, add extra rows and place the metadata inside a <td colspan="2"> element.

To make sure this diff displays correctly (e. g., that the diff marker and content columns aren’t the same width), you can add the mediawiki.diff.styles ResourceLoader module to your page:

<link rel="stylesheet" href="https://www.mediawiki.org/w/load.php?modules=mediawiki.diff.styles&only=styles">

If you want to display the parsedcomment, you will likewise want to add the mediawiki.legacy.shared module, which includes some basic styles for comments and autocomments:

<link rel="stylesheet" href="https://www.mediawiki.org/w/load.php?modules=mediawiki.legacy.shared&only=styles">

And if you want to display both the diff and the parsedcomment HTML on the page, you can combine these two tags into one:

<link rel="stylesheet" href="https://www.mediawiki.org/w/load.php?modules=mediawiki.legacy.shared|mediawiki.diff.styles&only=styles">