API:الصفحة الرئيسية

From MediaWiki.org
Jump to navigation Jump to search
This page is a translated version of the page API:Main page and the translation is 16% complete.

Other languages:
العربية • ‎azərbaycanca • ‎български • ‎বাংলা • ‎čeština • ‎dansk • ‎Deutsch • ‎English • ‎Esperanto • ‎español • ‎فارسی • ‎français • ‎हिन्दी • ‎Հայերեն • ‎Bahasa Indonesia • ‎italiano • ‎日本語 • ‎Taqbaylit • ‎ಕನ್ನಡ • ‎한국어 • ‎lietuvių • ‎文言 • ‎मैथिली • ‎मराठी • ‎Nederlands • ‎occitan • ‎polski • ‎پښتو • ‎português • ‎português do Brasil • ‎română • ‎русский • ‎سنڌي • ‎ไทย • ‎Türkçe • ‎українська • ‎Tiếng Việt • ‎粵語 • ‎中文
This page is part of the MediaWiki action API documentation.

MediaWiki action API

v · d · e
هذه نظرة عامة على واجهة برمجة التطبيقات "الإجراء". انظر إلى شريط القوائم على اليمين لمزيد من الموضوعات الفرعية المفصلة وواجهات برمجة التطبيقات الأخرى.

آبي ميدياويكي هي خدمة ويب توفر وصولا سهلا لمميزات وبيانات ويكي عبر بروتوكول [[:ar:بروتوكول api.phpManual:api.phpنقل النص الفائق|إتش تي تي بي]] بواسطة عنوان إنترنت الواقع في api.phpManual:api.php. يطلب العملاء أفعالا معينة من خلال تحديد المعامل action، مثلا يستعمل هذا المعامل لجلب المعلومات action=query. كانت هذه الخدمة سابقاً الآبي الوحيدة في ميدياويكي، ولكن الآن تتوفر عدة خدمات ويب مرتبطة بميدياويكي مثل: RESTBase وWikidata query service.


إذا كنت تبحث بدلاً من ذلك عن "آبي داخلية" أو "آبي PHP" ، فراجع "' extension interface"' ، والذي يسمح لمطوّري PHP بإضافة وظائف جديدة إلى تثبيت MediaWiki.
Deprecation notices are sent to the low-traffic mediawiki-api-announce mailing list. Subscription is recommended.

The MediaWiki action API can be used to monitor a MediaWiki installation, or create a bot to automatically maintain one. It provides direct, high-level access to the data contained in MediaWiki databases. Client programs can log in to a wiki, get data, and post changes automatically by making HTTP requests to the web service. Supported clients include bots, thin web-based JavaScript clients such as Navigation popups and LiveRC, end-user applications such as Vandal Fighter, and other web sites (Toolforge's utilities).

On new MediaWiki installations, the web service is enabled by default, but an administrator can disable it.

MediaWiki has two other outward-facing interfaces:

مثال بسيط

هذه الرابط يخبر ويكيبيديا الإنجليزية بأن ترسل لك محتوى الصفحة الرئيسية:


بإستعمال أي لغة برمجة يمكنك إرسال طلب إتش تي تي بي GET إلى ذلك الرابط (أو يمكنك ببساطة فتح الرابط في متصفحك)، وستحصل على مستند جسون يحتوي على الكود المصدري الخاص بالصفحة التي عنوانها "Main Page". إذا غيرت قيمة المعامل format إلى jsonfm فستحصل على صفحة إتش تي إم أل ذات تنسيق جميل وهي مفيدة عند.

آبي ميدياويكي هي خدمة ويب توفر وصولا سهلا لمميزات وبيانات ويكي عبر بروتوكول إتش تي تي بي بواسطة عنوان إنترنت الواقع في $api.

لنقم بتحليل ذلك الرابط حتى نظهر لك كيف تمت العملية.

آبي ميدياويكي هي خدمة ويب توفر وصولا سهلا لمميزات وبيانات ويكي عبر بروتوكول إتش تي تي بي بواسطة عنوان إنترنت الواقع في $api. https://en.wikipedia.org/w/api.php

This is the endpoint. It's like the home page of the MediaWiki web service API. This URL is the base URL for English Wikipedia's API, just as https://en.wikipedia.org/wiki/ is the base URL for its web site.

If you're writing a program to use English Wikipedia, every URL you construct will begin with this base URL. If you're using a different MediaWiki installation, you'll need to find its endpoint and use that instead. All Wikimedia wikis have endpoints that follow this pattern:

https://www.mediawiki.org/w/api.php     # MediaWiki API
https://en.wikipedia.org/w/api.php      # English Wikipedia API
https://nl.wikipedia.org/w/api.php      # Dutch Wikipedia API
https://commons.wikimedia.org/w/api.php # Wikimedia Commons API

MediaWiki version: 1.17

Since r75621, we have RSD for the endpoint: look for the link rel="EditURI" in the HTML source of any page and extract the api.php URL; the actual link contains additional info. For instance, on this wiki it's:

<link rel="EditURI" type="application/rsd+xml" href="//www.mediawiki.org/w/api.php?action=rsd" />

Otherwise, there's no safe way to locate the endpoint on any wiki. If you're lucky, either the full path to index.php will not be hidden under strange rewrite rules so that you'll only have to take the "edit" (or history) link and replace index.php (etc.) with api.php, or you'll be able to use the default script path (like w/api.php).

Now let's move on to the parameters in the query string of the URL.

The format

format=json This tells the API that we want data to be returned in JSON format. You might also want to try format=jsonfm to get an HTML version of the result that is good for debugging. The API supports other output formats such as XML and native PHP, but there are plans to remove less popular formats (phab:T95715), so you might not want to use them.

The action


The MediaWiki web service API implements dozens of actions and extensions implement many more; the dynamically generated API help documents all available actions on a wiki. In this case, we're using the "query" action to get some information. The "query" action is one of the API's most important actions, and it has extensive documentation of its own. What follows is just an explanation of a single example.

Action-specific parameters


The rest of the example URL contains parameters used by the "query" action. Here, we're telling the web service API that we want information about the Wiki page called "Main Page". (The %20 is a percent-encoded space.) If you need to query multiple pages, put them all in one request to optimize network and server resources: titles=PageA|PageB|PageC. See the query documentation for details.


You can request many kinds of information, or properties, about a page. This parameter tells the web service API that we want information about a particular revision of the page. Since we're not specifying any revision information, the API will give us information about the latest revision — the main page of Wikipedia as it stands right now.


Finally, this parameter tells the web service API that we want the content of the latest revision of the page. If we passed in rvprop=content|user instead, we'd get the latest page content and the name of the user who made the most recent revision.

Again, this is just one example. Queries are explained in more detail here, and the API reference lists all the possible actions, all the possible values for rvprop, and so on.

An introduction to the API by Roan Kattouw at the San Francisco Hackathon January 2012


Before you start using the MediaWiki web service API, be sure to read these documents:

Beyond that point, what you need to read depends on what you want to do. The right-hand menu links to detailed, task-specific documentation, and some more general guidelines are given below.

Identifying your client

When you make HTTP requests to the MediaWiki web service API, be sure to specify a User-Agent header that properly identifies your client. Don't use the default User-Agent provided by your client library, but make up a custom header that identifies your script or service and provides some type of means of contacting you (e.g., an e-mail address).

An example User-Agent string might look like:

MyCoolTool/1.1 (https://example.org/MyCoolTool/; MyCoolTool@example.org) BasedOnSuperLib/1.4

On Wikimedia wikis, if you don't supply a User-Agent header, or you supply an empty or generic one, your request will fail with an HTTP 403 error (cf. m:User-Agent policy). Other MediaWiki installations may have similar policies.

If you are calling the API from browser-based JavaScript, you won't be able to influence the User-Agent header: the browser will use its own. To work around this, use the Api-User-Agent header:

// Using XMLHttpRequest
xhr.setRequestHeader( 'Api-User-Agent', 'Example/1.0' );

// Using jQuery
$.ajax( {
    url: remoteUrlWithOrigin,
    data: queryData,
    dataType: 'json',
    type: 'POST',
    headers: { 'Api-User-Agent': 'Example/1.0' },
    success: function(data) {
       // do something with data
} );

// Using mw.Api, specify it when creating the mw.Api object
var api = new mw.Api( {
    ajax: {
        headers: { 'Api-User-Agent': 'Example/1.0' }
} );
api.get( {...} ).done(function(data) {
    // do something with data

// Using fetch
fetch( remoteUrlWithOrigin, {
    method: 'POST',
    headers: new Headers( {
        'Api-User-Agent': 'Example/1.0'
    } )
    // Other init settings such as 'credentials'
} ).then( function ( response ) {
    if ( response.ok ) {
        return response.json();
    throw new Error( 'Network response was not ok: ' + response.statusText );
} ).then( function ( data ) {
    // do something with data

In PHP, you can identify your user-agent with code such as this:

ini_set('user_agent', 'MyCoolTool/1.1 (https://example.org/MyCoolTool/; MyCoolTool@example.org) BasedOnSuperLib/1.4');

Or if you use cURL:

curl_setopt($curl, CURLOPT_USERAGENT, 'MyCoolTool/1.1 (https://example.org/MyCoolTool/; MyCoolTool@example.org) BasedOnSuperLib/1.4');

Logging in

Your client will probably need to log in to MediaWiki, possibly via its own user account. See the login manual page for details.

API etiquette

Please also read: API:Etiquette

If your requests obtain data that can be cached for a while, you should take steps to cache it, so you don't request the same data over and over again. More information about rate-limiting, concurrency, and general API etiquette can be found at API:Etiquette. Some clients may be able to cache data themselves, but for others (particularly JavaScript clients), this is not possible.

Per the HTTP specification, POST requests cannot be cached. Therefore, whenever you're reading data from the web service API, you should use GET requests, not POST.

Also note that a request cannot be served from cache unless the URL is exactly the same. If you make a request for api.php?....titles=Foo|Bar|Hello, and cache the result, then a request for api.php?....titles=Hello|Bar|Hello|Foo will not go through the cache — even though MediaWiki returns the same data!

You should take care to normalize the URLs you send to the MediaWiki web service, so that slightly different user input won't cause you to waste time on unnecessary HTTP requests. You can normalize a list of page titles by removing duplicates and sorting the titles alphabetically. Similar techniques will work for other kinds of data.

Useful links

The menu bar on the right side of this page links to more detailed, task-specific documentation. Here are some links having to do with the API as a whole:

Archived links