واجهة برمجة التطبيقات:قارن

This page is a translated version of the page API:Compare and the translation is 100% complete.

طلب GET كي تحصل على الفرق بين صفحتين اثنين.

إصدار ميدياويكي:
1.18

توثيق واجهة برمجة التطبيقات


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-{slot}

Content serialization format of fromtext-{slot}.

This is a templated parameter. When making the request, {slot} in the parameter's name should be replaced with values of fromslots.
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
fromcontentmodel-{slot}

Content model of fromtext-{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 fromslots.
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
frompst

Do a pre-save transform on fromtext-{slot}.

Type: boolean (details)
fromtext
Deprecated.

Specify fromslots=main and use fromtext-main instead.

fromcontentformat
Deprecated.

Specify fromslots=main and use fromcontentformat-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
fromcontentmodel
Deprecated.

Specify fromslots=main and use fromcontentmodel-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
fromsection
Deprecated.

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

totitle

Second title to compare.

toid

Second page ID to compare.

Type: integer
torev

Second revision to compare.

Type: integer
torelative

Use a revision relative to the revision determined from fromtitle, fromid or fromrev. All of the other 'to' options will be ignored.

One of the following values: cur, next, prev
toslots

Override content of the revision specified by totitle, toid or torev.

This parameter specifies the slots that are to be modified. Use totext-{slot}, tocontentmodel-{slot}, and tocontentformat-{slot} to specify content for each slot.

Values (separate with | 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

مقارنة المراجعتين المحددتين لصفحتين اثنين مختلفتين.


النتيجة

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

عينة من الكود البرمجي

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 );
} );

الأخطاء المحتملة

الكود معلومات
compare-relative-to-nothing لا توجد مراجعة 'من' لـtorelative لتكون نسبة.
compare-relative-to-deleted لا يمكن استخدام torelative=value بالنسبة لمراجعة محذوفة.
missingrev-title لا توجد مراجعة حالية للعنوان title.
baddiff لا يمكن استرجاع الفرقك; إحدى أو كلا المراجعتين غير موجودة أو ليس لديك صلاحية لعرضها.
missingcontent-revid محتوى مفقود لمعرف المراجعة revid.
invalidtitle عنوان سيئ "title".
nosuchpageid لا توجد صفحة بالمعرف id.
missingtitle-byname الصفحة name غير موجودة.
nosuchrevid لا توجد مراجعة بالمعرف id.
missingcontent-revid-role محتوى مفقود لمعرف المراجعة id للدور main.
compare-nosuchfromsection لا يوجد قسم name في المحتوى 'من'.
compare-nosuchtosection لا يوجد قسم name في المحتوى 'إلى'.
compare-maintextrequired الوسيط fromtext-main مطلوب عندما يكون fromslots يحتوي على main (لا يمكن حذف الفتحة الرئيسية).
compare-maintextrequired الوسيط totext-main مطلوب عندما يكون toslots يحتوي على main (لا يمكن حذف الفتحة الرئيسية).
compare-notext لا يمكن استخدام الوسيط $1 بدون $2.
compare-no-title لا يمكن الحفظ المسبق للحفظ بدون عنوان; حاول تحديد fromtitle أو totitle.
compare-nofromrevision ليس 'من' مراجعة، حدد fromrev أو fromtitle أو fromid.
compare-notorevision ليس 'إلى' مراجعة، حدد torev أو totitle أو toid.
sectionsnotsupported الأقسام غير مدعومة لنموذج المحتوى model.
sectionreplacefailed لا يمكن دمج القسم المحدث.
missingparam مطلوب واحد على الأقل من الوسائط fromtitle، fromid، fromrev، fromtext، fromrelative و fromslots.
missingparam مطلوب واحد على الأقل من الوسائط totitle، toid، torev، totext، torelative و toslots.

تاريخ المتغيرات

  • v1.32: إضافة fromslots, toslots, fromtext-{slot}, fromsection-{slot}, fromcontentformat-{slot}, fromcontentmodel-{slot}, totext-{slot}, tosection-{slot}, tocontentformat-{slot}, tocontentmodel-{slot}
  • v1.30: إضافة frompst, torelative, topst, prop
  • v1.20: إضافة fromid, toid

ملاحظات إضافية

للحصول على الفرق بين صفحتين اثنين، يتعين تمرير رقم مراجعة أو عنوان صفحة أو معرّف صفحة لكلا من from و to.

مقارنة صلات بين المراجعة الأولى والمراجعة الأخيرة

  • يمكنك أن تطلب مقارنة صلات بين السابق «prev» في المراجعة الأولى لصفحة محددة، وسينتج عن هذا استخدام النص الكامل للمراجعة الأولى في صفة الاختلاف ذاته «diff». لاحظ أن هذا الأمر غير دقيق، منذ أنه في بعض الحالات ربما يكون هذا هو المحتوى الافتراضي للصفحة قبل مراجعتها الأولى. سوف تكون الخاصية fromrevid غير موجودة في النتائج.
  • شبيه بذلك يمكنك أن تطلب المقارنة التالية ذات الصلة على المراجعة الأخيرة لصفحة محددة.
  • (ملحوظة: هذه هي المراجعة الأخيرة من كتابة هذا الموضوع. ومنذ أن الصفحة محمية، ربما لن تتغير.) النتيجة ستكون عرض اختلافات «diff» فارغ وسوف تكون الخاصية torevid غير موجودة. خلافا لأغلب مقارنات المراجعات، سوف تتغير هذه النتائج لو استحدثت مراجعة جديدة.

مثل هذا المنحى قديم في واجهة برمجة التطبيقات. هذا الأمر لا يتماشى والنموذج المفاهيمي لتاريخ الصفحة في صفة مجموعة من المراجعات. لا توجد مراجعة previous تقارنها بالمراجعة الأولى، ولا توجد مراجعة next تقارنها بالمراجعة الأخيرة. بسبب هذا الأمر، ربما ينتج عن الإصدارات التالية من برمجيات ميدياويكي نتائج مختلفة عن رد واجهة برمجة التطبيقات هذا.

استخدام مخرجات إتش تي إم إل

إن قيم prop لكلا من diff و parsedcomment يكون ردها قصاصات إتش تي إم إل. إن إتش تي إم إل diff هي قائمة بصفوف من جدول (عناصر ‎<tr>) التي يجب أن تدمج في جدول يحتوي على الترميز التالي على أقل تقدير:

<table class="diff">
  <colgroup>
    <col class="diff-marker">
    <col class="diff-content">
    <col class="diff-marker">
    <col class="diff-content">
  </colgroup>
  <tbody>
    تضاف قصيصة اختلافات إتش تي إم إل
  </tbody>
</table>

كي تعرض البيانات الوصفية كذلك، مثل المستخدم أو التعليق، أضف صف آخر وضع البيانات الوصفية داخل عنصر <td colspan="2">.

كي تضمن أن هذه الاختلافات تعرض عرضًا صحيحًا (مثال أن واسم الاختلافات وأعمدة المحتوى ليست بذات العرض)، يمكنك إضافة الوحدة البرمجية mediawiki.diff.styles ResourceLoader إلى صفحتك:

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

لو كنت ترغب في عرض parsedcomment، سترغب أيضًا في إضافة الوحدة البرمجية mediawiki.legacy.shared التي تتضمن بعض الأساليب الأساسية المخصصة للتعليقات والتعليقات الآلية:

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

ولو رغبت في عرض كلا من diff و parsedcomment إتش تي إم إل على الصفحة، يمكنك ضم هذين الوسمين في وسم واحد:

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