API:Halaman utama
| This page is part of the MediaWiki action API documentation. |
MediaWiki action API
- Introduction and quick start
- FAQ
- Tutorial
- Formats
- Error reporting
- Restricting usage
- Cross-site requests
- Authentication
- Queries
- Searching (by title, content, coordinates...)
- Parsing wikitext and expanding templates
- Purging pages' caches
- Parameter information
- Changing wiki content
- Create and edit pages
- Move pages
- Merge pages
- Rollback
- Delete pages
- Restore deleted revisions
- (Un)protect pages
- (Un)block users
- (Un)watch pages
- Mark revisions of watched pages as visited
- Send email
- Patrol changes
- Import pages
- Change user group membership
- Upload files
- User options
- Tokens
- Page language
- Watchlist feed
- Wikidata
- Extensions
- Using the API in MediaWiki and extensions
- Miscellaneous
- Implementation
- Client code
- Asserting
- Ini adalah ikhtisar dari "action" API. Lihat menu bar di sebelah kanan untuk lebih detail tentang sub-topik dan API lainnya
Media Wiki action API adalah web service yang menyediakan kemudahan akses untuk fitur wiki , data dan meta-data melalui HTTP, dengan sebuah URL biasanya dalam api.php Permintaan klien merincikan "actions" dengan menentukan action parameter, terutama action=query untuk mendapatkan informasi. Dikenal sebagai the MediaWiki API, tapi sekarang web APIs lainnya tersedia untuk terhubung ke MediaWiki seperti RESTBase dan Wikidata query service.
Contents
Pengantar[edit]
Note: Jika Anda menginginkan mencari "internal API" atau "PHP API", lihat antarmuka tambahan, dimana mengizinkan pengembang PHP untuk menambahkan fungsi baru ke pemasangan MediaWiki.
MediaWiki action API dapat digunakan untuk mengamati pemasangan MediaWiki, atau membuat bot untuk memeliharanya. Menyediakan langsung, akses tingkat tinggi ke data yang terdapat di database MediaWiki. Program klien dapat masuk ke wiki, mendapatkan data, dan menulis perubahan secara otomatis dengan menggunakan permintaan HTTP ke layanan web. Mendukung klien di dalamnya bots, Javascript berbasis web ringan klien seperti Popup navigasi dan LiveRC, pengguna aplikasi seperti Vandal Fighter, dan situs web (Tool Labs' perangkat).
Dalam pemasangan MediWiki terbaru, layanan web diaktifkan secara default, tapi administrator bisa menonaktifkannya.
MediWiki memiliki dua antarmuka luar lainnya.
- Halaman Special:Export, dimana menampilkan ekspor besar konten wiki sebagai XML. Baca Help:Export untuk informasi lainnya.
"Standar antarmuka berbasis web" (dimana seperti yang Anda gunakan untuk melihat halaman ini). Baca Manual:Parameters to index.php untuk informasi dalam menggunakan antarmuka berbasis web.
Contoh mudah[edit]
URL ini memberitahukan layanan web API Wikipedia Indonesia untuk mengirimkan ke Anda konten dari halaman utama:
Gunakan bahasa program apapun untuk membuat permintaan HTTP GET dari URL tersebut (atau kunjungi saja pranala pada penjelajah web Anda), dan Anda akan mendapatkan dokumen JSON yang didalamnya terdapat markah wiki untuk halaman dengan judul "Halaman Utama". Mengubah format dari jsonfm akan menampilkan hasil "pretty-printed" HTML yang baik untuk pengawakutuan.
Disini URL jsonfm sebagai pranala yang mudah untuk dibaca yang dapat diklik.
Mari pilih URL tersendiri untuk menampilkan bagaimana cara kerjanya.
Titik akhir[edit]
https://en.wikipedia.org/w/api.php
Ini "titik akhir". Ini seperti halaman beranda dari API layanan web MediaWiki. URL ini bedasarkan URL dari API Wikipedia Bahasa Inggris, seperti https://en.wikipedia.org/wiki/ adlah dasar dari situs web.
Jika anda menulis program untuk menggunakan Wikipedia Bahasa Inggris, setiap URL yang Anda buat akan dimulai dengan URL dasar ini. Jika Anda menggunakan pemasangan MediaWiki yang berbeda, Anda akan membutuhkan pencarian titik akhir dan menggunakannya sebagai gantinya. Semua Wikimedia memiliki titik akhir yang dapat diikuti dengan bentuk ini:
https://www.mediawiki.org/w/api.php # MediaWiki API
https://en.wikipedia.org/w/api.php # API Wikipedia Bahasa Inggris
https://nl.wikipedia.org/w/api.php # API Wikipedia Bahasa Belanda
https://commons.wikimedia.org/w/api.php # API Wikimedia Umum
| MediaWiki version: | ≥ 1.17 |
Sejak r75621, kami memiliki RSD pendeteksian dari titik akhir: lihat pada link rel="EditURI" dalam sumber HTML dari halaman apapun dan ekstrak URL api.php; link sesungguhnya yang mengandung info tambahan. Untuk contoh, dalam wiki ini:
<link rel="EditURI" type="application/rsd+xml" href="//www.mediawiki.org/w/api.php?action=rsd" />
Sebaliknya, tidak ada cara yang aman untuk mencari titik akhir dalam wiki apapun. Jika Anda beruntung, baik path lengkap ke index.php tidak akan tersembunyi dalam keanehan peraturan penulisan-kembali jadi Anda hanya akan dapat mengambil pranala "sunting" (atau riwayat) dan mengganti index.php (dll.) dengan api.php, atau Anda akan bisa untuk menggunakan skrip path standar (seperti w/api.php).
Sekarang mari beralih ke parameter dalam "query string" dari URL.
Format[edit]
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[edit]
action=query
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[edit]
titles=Main%20Page
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 comes from percent-encoding a 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.
prop=revisions
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.
rvprop=content
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.
Getting started[edit]
Before you start using the MediaWiki web service API, be sure to read these documents:
- The FAQ.
- The page about input and output formats
- The page about errors and warnings
- Any policies that apply to the wiki you want to access, such as Wikimedia Foundation wikis' terms of use, trademark policy. These terms apply to you when you access or edit using the API, just as they do when you use your web browser.
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[edit]
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/; [email protected]) 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
});
In PHP, you can identify your user-agent with code such as this:
ini_set('user_agent', 'MyCoolTool/1.1 (https://example.org/MyCoolTool/; [email protected]) BasedOnSuperLib/1.4');
Or if you use cURL:
curl_setopt($curl, CURLOPT_USERAGENT, 'MyCoolTool/1.1 (https://example.org/MyCoolTool/; [email protected]) BasedOnSuperLib/1.4');
Logging in[edit]
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[edit]
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[edit]
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:
- The API sandbox available on all Wikimedia wikis makes it easy to try out different actions interactively.
- The API reference contains automatically-generated descriptions of all actions and parameters.
- Hook into Wikipedia information using PHP and the MediaWiki API (IBM developerWorks article, 17 May 2011)
- Hook into Wikipedia using Java and the MediaWiki API (6 April 2012)
- The API tutorial leads you through hands-on exercises and includes a training video.
- Mailing list for notifications and questions: API mailing list
- Low-traffic mailing list for announcements only (all posts to this list are posted to mediawiki-api as well): mediawiki-api-announce
- View and report API bugs in the MediaWiki-API Phabricator project (When reporting new bugs, don't forget to add MediaWiki-API to Projects)
- Browse the API source code
- Manual:Database layout — The current MediaWiki database schema
- Browse the current database schema in git