API:Compare/ru
Эта страница является частью документации по API действий MediaWiki. |
GET request to get the difference between two pages.
Версия MediaWiki: | ≥ 1.18 |
API documentation
Пример
GET request
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 | Нет версии 'from', к которой относится torelative. |
compare-relative-to-deleted | Cannot use torelative=value relative to a deleted revision. |
missingrev-title | Нет текущей версии для заголовка title. |
baddiff | Сравнение версий не может быть проведено. Одна или обе версии не существуют или у вас не достаточно прав чтобы просматривать их. |
missingcontent-revid | Отсутствует содержимое версии с идентификатором revid. |
invalidtitle | Плохой заголовок «title». |
nosuchpageid | Нет страницы с идентификатором id. |
missingtitle-byname | Страница name не существует. |
nosuchrevid | Нет версии с идентификатором id. |
missingcontent-revid-role | Missing content for revision ID id for role main. |
compare-nosuchfromsection | Нет раздела name в содержимом «from». |
compare-nosuchtosection | Нет раздела name в содержимом «to». |
compare-maintextrequired | Parameter fromtext-main is required when fromslots contains main (cannot delete the main slot). |
compare-maintextrequired | Parameter totext-main is required when toslots contains main (cannot delete the main slot). |
compare-notext | Параметр $1 нельзя использовать без $2. |
compare-no-title | Невозможно выполнить преобразование перед записью правки без заголовка. Попробуйте задать fromtitle или totitle. |
compare-nofromrevision | No 'from' revision. Specify fromrev, fromtitle, or fromid. |
compare-notorevision | No 'to' revision. Specify torev, totitle, or toid. |
sectionsnotsupported | Разбиение на разделы не поддерживается моделью содержимого model. |
sectionreplacefailed | Невозможно объединить обновлённый раздел. |
missingparam | Как минимум один из параметров fromtitle , fromid , fromrev , fromtext , fromrelative и fromslots обязателен.
|
missingparam | Как минимум один из параметров totitle , toid , torev , totext , torelative и toslots обязателен.
|
Parameter history
- v1.32: Introduced
fromslots
,toslots
,fromtext-{slot}
,fromsection-{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">