Jump to content

API:Action API/ja: Difference between revisions

From mediawiki.org
Content deleted Content added
導入: translate
No edit summary
Line 1: Line 1:
}}
{{API/ja}}
:''This is o you might not want to use them.
:''This is an introductory overview. See the menu bar on the right for more detailed sub-topics.''

MediaWiki ウェブサービス '''[[wikipedia:application programming interface|API]]''' (もしくは '''[[w:Web API|WebAPI]]''')は、Wikiの機能、データ、メタデータへのアクセス手段を提供します。

== はじめに ==

{{note/ja|「内部API」や「PHP API」をお探しでしたら、PHP開発者が自分の設置したMediaWikiに新機能を追加する方法に関する'''[[Manual:Developing_extensions|拡張機能のインターフェイス]]'''をご覧ください。}}

MediaWiki のウェブAPIは自分が設置した MediaWiki を監視したり、メンテナンス用の[[w:Wikipedia:Creating_a_bot|ボットを作ったり]]するために使えます。ウェブAPIは MediaWiki データベースにあるデータへの直接の、高水準なアクセスを提供します。[[API:Client_code|クライアントプログラム]]はウェブサービスへのHTTPリクエストを介して自動的にウィキにログインし、データを取得し、変更を投稿できます。サポートされているクライアントには[[w:Wikipedia:Creating_a_bot|ボット]]、[[:en:Wikipedia:Tools/Navigation popups|Navigation popups]]や[[:fr:User:EDUCA33E/LiveRC/Documentation|LiveRC]] などの軽量なウェブベースのJavaScript製クライアント、[[:en:User:Henna/VF|Vandal Fighter]] などのエンドユーザーアプリケーション、 他のウェブサイト([//tools.wmflabs.org Tool Labs]' utilities)などがあります。

新しい MediaWiki ではウェブサービスはデフォルトで有効になっていますが、管理者はAPIを[[API:Restricting_API_usage/ja#API全体の無効化|無効化]]できます。

MediaWiki には外部向けインターフェイスが他に2つあります。
* '''[[Special:Export]]''' ページ。ウィキの本文をまとめてXML形式でエクスポートすることができます。詳しくは [[m:Help:Export|meta.wikimedia.org の Export のヘルプページ]]をご覧ください。
* (おそらく今あなたが使っている)'''標準のウェブベースのインターフェイス'''。ウェブベースのインターフェイスについて詳しくは [[Manual:Parameters to index.php]] をお読みください。

== 単純な例 ==

この URL によって、日本語版 Wikipedia のメインページを得ることができます。

<pre style="overflow:auto">
http://ja.wikipedia.org/w/api.php?format=json&action=query&titles=メインページ&prop=revisions&rvprop=content
</pre>

プログラムによってこの URL を HTTP GET リクエストによって送信すると、もしくはウェブブラウザによって閲覧すると、JSON 形式のページを得られます。このページには Wikipedia のメインページが Wiki マークアップされたものが含まれています。もし format を '''jsonfm''' に変更すると、HTMLに整形された結果を得られます。

Let's pick that URL apart to show how it works.

=== エンドポイント ===

<code>http://ja.wikipedia.org/w/api.php</code>

これが ''エンドポイント'' です。 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 <code>http://en.wikipedia.org/wiki/</code> 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:

http://en.wikipedia.org/w/api.php # 英語版ウィキペディアの API
http://nl.wikipedia.org/w/api.php # オランダ語版ウィキペディアの API
http://commons.wikimedia.org/w/api.php # ウィキメディア・コモンズの API
{{MW 1.17/ja}}
Since {{rev|75621}}, we have RSD discovery for the endpoint: look for the <code>link rel="EditURI"</code> in the HTML source of any page and extract the <code>api.php</code> URL; the actual link contains additional info. For instance, on this wiki it's:
<pre style="overflow:auto">
<link rel="EditURI" type="application/rsd+xml" href="//www.mediawiki.org/w/api.php?action=rsd" />
</pre>

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 [[Manual:$wgScriptPath|ScriptPath]] (like <code>w/api.php</code>).

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

=== 形式 ===

<code>format=json</code>

This tells the Wikimedia web service API that we want data to be returned in JSON format. You might also want to try <code>format=jsonfm</code> to get an HTML version of the result that is good for debugging. Even though the API supports many different [[API:Data_formats/ja|出力形式]] such as [[w:ja:WDDX|WDDX]], [[w:ja:Extensible Markup Language|XML]], [[w:ja:YAML|YAML]] and [http://php.net/manual/ja/function.serialize.php ネイティブ PHP], there are plans to remove all formats except for JSON, so you might not want to use them.


=== 操作 ===
=== 操作 ===

Revision as of 06:32, 1 February 2015

}}

This is o you might not want to use them.

操作

action=query

This is the 'action'. The MediaWiki web service API supports over fifty actions, and they're all documented in the API reference. In this case, we're using "query" to tell the API that we want to get some data.

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.

操作特有のパラメーター

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 work with multiple pages, please consider putting them all in one request to optimize network and server resources: titles=PageA|PageB|PageC. See the query documentation for details.

prop=revisions

This parameter tells the web service API that we are interested in 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.

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

Getting started

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.

あなたのクライアントの識別

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 (http://example.com/MyCoolTool/; MyCoolTool@example.com) 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. There is currently no other mechanism for a browser-based client to identify itself.

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

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

Or if you use cURL:

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

ログイン

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

APIのエチケット

こちらもお読みください: API:Etiquette/ja

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/ja. 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.

有用なリンク

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:

アーカイブされたリンク