API:Action API/ja: Difference between revisions
m Update to use wikilinks, 1 links replaced (BOT Edit) |
Updating to match new version of source page |
||
| Line 1: | Line 1: | ||
{{API/ja}} |
|||
<div style="background:#f0f0e0;border:2px solid #aaa;padding:4px 4px 4px 15px;margin:0 20%;text-align:center"> |
|||
'''このページをご覧になられている方のための注意書き''' |
|||
'''This is an introductory overview. See the menu bar on the right for more detailed sub-topics.''' |
|||
このページではMediaWikiのサーバへの外部APIを提供するための進行中の取り組みを説明しています。 |
|||
If you want to monitor a MediaWiki installation, or [[w:Wikipedia:Creating_a_bot|create a bot]] to automatically maintain one, you can use the MediaWiki web service [[wikipedia:application programming interface|API]]. The web service API provides direct, high-level access to the data contained in MediaWiki databases. [[API:Client_code|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 [[w:Wikipedia:Creating_a_bot|bots]], thin web-based JavaScript clients such as [[:en:Wikipedia:Tools/Navigation popups|Navigation popups]] and [[:fr:User:EDUCA33E/LiveRC/Documentation|LiveRC]], end-user applications such as [[:en:User:Henna/VF|Vandal Fighter]], and other web sites ([http://toolserver.org/ Toolserver]'s utilities). |
|||
<div style="text-align:left"> |
|||
MediaWikiには現在3つのインターフェイスがあります: |
|||
* [http://en.wikipedia.org/w/api.php MediaWiki API] - このページで説明している新しいAPI。 |
|||
* [[Special:Export]]機能(xml形式のデータのバルクエクスポート) |
|||
* 通常のウェブベースのインターフェイス |
|||
</div> |
|||
</div></div> |
|||
On new MediaWiki installations, the web service is enabled by default, but an administrator can [[API:Restricting_API_usage#Disabling_the_entire_API|disable it]]. |
|||
{{API Intro/ja}} |
|||
MediaWiki features also '''[[Special:Export]]''' page, which provides bulk export of wiki content as XML. Read the [[m:Help:Export|Export help article on meta.wikimedia.org]] for more information on exporting wiki content. |
|||
'''APIサブトピックは右のメニューをご覧ください''' |
|||
== 単純な例 == |
|||
This URL tells English Wikipedia's web service API to send you the content of the main page: |
|||
この[[:ja:Application Programming Interface|API]]の目標は、MediaWikiデータベースが保持しているデータへの直接かつ高度なアクセスを提供することです。クライアントプログラムはこのAPIを使ってログイン、データの取得、そして更新ができます。APIはWebベースのJavaScriptクライアント(例: [[:ja:Help:ナビゲーション・ポップアップ|ナビゲーション・ポップアップ]]や[[:ja:Wikipedia:LiveRC/解説|LiveRC]])、エンドユーザアプリケーション(例: [[:ja:Wikipedia:バンダル・ファイター|バンダル・ファイター]])、または他のウェブサイト(例: [[:ja:Wikipedia:ウィキメディア・ツールサーバー|ウィキメディア・ツールサーバー]])からのアクセスをサポートします。 |
|||
<pre style="overflow:auto"> |
|||
出力のフォーマットにはXML、JSON、YAML、WDDX、またはPHPシリアライズが選べます。強く型付けされたRSSやWSDLスタイルのフォーマットはラッパーを使って実装できます。 |
|||
http://en.wikipedia.org/w/api.php?format=json&action=query&titles=Main%20Page&prop=revisions&rvprop=content |
|||
</pre> |
|||
Use any programming language to make an HTTP GET request for that URL (or just visit that link in your browser), and you'll get a JSON document which includes the current wiki markup for the page titled "Main Page". Changing format to '''jsonfm''' will return a "pretty-printed" HTML result good for debugging. |
|||
各APIモジュールは引数の集合を用います。名前の衝突を避けるために、各モジュールには2文字の短縮形があり、引数名はその2文字から始めるようになっています。 |
|||
Let's pick that URL apart to show how it works. |
|||
* 告知と質問用メーリングリスト: '''[[mail:mediawiki-api|API mailing list]]''' |
|||
* バグの閲覧と報告: '''[http://bugzilla.wikimedia.org/buglist.cgi?bug_status=NEW&bug_status=ASSIGNED&bug_status=REOPENED&component=API&order=bugs.delta_ts BugZilla]''' (''新しいバグを報告するとき、Component=APIを設定することをお忘れなく'') |
|||
== |
=== エンドポイント === |
||
* [http://svn.wikimedia.org/viewvc/mediawiki/trunk/phase3/includes/api/ API source code in SVN] |
|||
* [[mw:Proposed Database Schema Changes|Proposed Database Schema Changes]] |
|||
* [[mw:Manual:Database layout|Database layout]] |
|||
* [http://svn.wikimedia.org/viewvc/mediawiki/trunk/phase3/maintenance/tables.sql?view=markup The current DB schema in SVN] |
|||
<code>http://en.wikipedia.org/w/api.php</code> |
|||
== アーカイブ == |
|||
* [[API:Wikimania 2006 API discussion]] |
|||
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 <code>http://en.wikipedia.org/wiki/</code> is the base URL for its web site. |
|||
[[Category:MediaWiki Development/ja]] |
|||
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 # English Wikipedia API |
|||
http://nl.wikipedia.org/w/api.php # Dutch Wikipedia API |
|||
http://commons.wikimedia.org/w/api.php # Wikimedia Commons 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|output formats]] such as [[w:WDDX|WDDX]], [[w:XML|XML]], [[w:YAML|YAML]] and [http://php.net/manual/en/function.serialize.php native PHP], there are plans to remove all formats except for JSON, so you might not want to use them. |
|||
=== 操作 === |
|||
<code>action=query</code> |
|||
This is the 'action'. The MediaWiki web service API supports over fifty actions, and they're all documented in the [//en.wikipedia.org/w/api.php 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 [[API:Query|extensive documentation of its own]]. What follows is just an explanation of a single example. |
|||
=== 操作特有のパラメーター === |
|||
<code>titles=Main%20Page</code> |
|||
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 [[w:Percent-encoding|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: <code>titles=PageA|PageB|PageC</code>. See the [[API:Query|query documentation]] for details. |
|||
<code>prop=revisions</code> |
|||
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. |
|||
<code>rvprop=content</code> |
|||
Finally, this parameter tells the web service API that we want the content of the latest revision of the page. If we passed in <code>rvprop=content|user</code> 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 [[API:Query|here]], and [http://en.wikipedia.org/w/api.php the API reference] lists all the possible actions, all the possible values for <code>rvprop</code>, and so on. |
|||
[[File:The MediaWiki Web API and How to use it - San Francisco Wikipedia Hackathon 2012.ogv|thumb|400px|An introduction to the API by [[User:Catrope|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: |
|||
* The [[API:FAQ|FAQ]]. |
|||
* The page about [[API:Data formats|input and output formats]] |
|||
* The page about [[API:Errors and warnings|errors and warnings]] |
|||
* Any policies that apply to the wiki you want to access, such as (for Wikimedia Foundation wikis) our [[wmf:Terms of Use|terms of use]]. These terms apply to you when you access or edit using the web service 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. |
|||
== あなたのクライアントの識別 == |
|||
When you make HTTP requests to the MediaWiki web service API, be sure to specify a <code>User-Agent</code> header that properly identifies your client. Don't use the default <code>User-Agent</code> 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 <code>User-Agent</code> 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 <code>User-Agent</code> 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: |
|||
<source lang="php">ini_set('user_agent', 'MyCoolTool/1.1 (http://example.com/MyCoolTool/; MyCoolTool@example.com) BasedOnSuperLib/1.4');</source> |
|||
Or if you use [[wikipedia:cURL|cURL]]: |
|||
<source lang="php">curl_setopt($curl, CURLOPT_USERAGENT, 'MyCoolTool/1.1 (http://example.com/MyCoolTool/; MyCoolTool@example.com) BasedOnSuperLib/1.4');</source> |
|||
== ログイン == |
|||
Your client will probably need to log in to MediaWiki, possibly via its own user account. See [[API:Login|the login manual page]] for details. |
|||
== APIのエチケット == |
|||
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 <code>api.php?....titles=Foo|Bar|Hello</code>, and cache the result, then a request for <code>api.php?....titles=Hello|Bar|Hello|Foo</code> 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: |
|||
* [[w:Special:ApiSandbox|The API sandbox]] on English Wikipedia makes it easy to try out different actions interactively. |
|||
* [//en.wikipedia.org/w/api.php The API reference] contains automatically-generated descriptions of all actions and parameters. |
|||
* [https://www.ibm.com/developerworks/xml/library/x-phpwikipedia/index.html Hook into Wikipedia information using PHP and the MediaWiki API] (IBM developerWorks article, 17 May 2011) |
|||
* [http://www.integratingstuff.com/2012/04/06/hook-into-wikipedia-using-java-and-the-mediawiki-api/ Hook into Wikipedia using Java and the MediaWiki API] (6 April 2012) |
|||
* [[API/Tutorial|The API tutorial]] leads you through hands-on exercises and includes a training video. |
|||
* Mailing list for notifications and questions: '''[[mail:mediawiki-api|API mailing list]]''' |
|||
** Low-traffic mailing list for announcements only (all posts to this list are posted to mediawiki-api as well): '''[[mail:mediawiki-api-announce|mediawiki-api-announce]]''' |
|||
* View and report API bugs: '''[http://bugzilla.wikimedia.org/buglist.cgi?bug_status=NEW&bug_status=ASSIGNED&bug_status=REOPENED&component=API&order=bugs.delta_ts Bugzilla]''' (''When [https://bugzilla.wikimedia.org/enter_bug.cgi?product=MediaWiki&component=API reporting new bugs], don't forget to set Component=API'') |
|||
* {{git file|action=tree|project=mediawiki/core|file=includes/api|text=Browse the API source code}} |
|||
* [[Manual:Database layout|The current MediaWiki database schema]] |
|||
* {{git file|project=mediawiki/core|branch=HEAD|file=maintenance/tables.sql|text=Browse the current database schema in git}} |
|||
== アーカイブされたリンク == |
|||
* [[API:Wikimania 2006 API discussion|2006 API discussion]] |
|||
[[Category:MediaWiki Development/ja|API]] |
|||
[[Category:Manual/ja]] |
|||
[[Category:Documentation/ja]] |
|||
[[Category:New contributors/ja]] |
|||
Revision as of 07:03, 17 September 2013
| このページは MediaWiki 操作 API の説明文書の一部です。 |
This is an introductory overview. See the menu bar on the right for more detailed sub-topics.
If you want to monitor a MediaWiki installation, or create a bot to automatically maintain one, you can use the MediaWiki web service API. The web service API 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 (Toolserver's utilities).
On new MediaWiki installations, the web service is enabled by default, but an administrator can disable it.
MediaWiki features also Special:Export page, which provides bulk export of wiki content as XML. Read the Export help article on meta.wikimedia.org for more information on exporting wiki content.
単純な例
This URL tells English Wikipedia's web service API to send you the content of the main page:
http://en.wikipedia.org/w/api.php?format=json&action=query&titles=Main%20Page&prop=revisions&rvprop=content
Use any programming language to make an HTTP GET request for that URL (or just visit that link in your browser), and you'll get a JSON document which includes the current wiki markup for the page titled "Main Page". Changing format to jsonfm will return a "pretty-printed" HTML result good for debugging.
Let's pick that URL apart to show how it works.
エンドポイント
http://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 http://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:
http://en.wikipedia.org/w/api.php # English Wikipedia API http://nl.wikipedia.org/w/api.php # Dutch Wikipedia API http://commons.wikimedia.org/w/api.php # Wikimedia Commons API
Template:MW 1.17/ja
Since r75621, we have RSD discovery 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 ScriptPath (like w/api.php).
Now let's move on to the parameters in the query string of the URL.
形式
format=json
This tells the Wikimedia web service 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. Even though the API supports many different output formats such as WDDX, XML, YAML and native PHP, there are plans to remove all formats except for JSON, so 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.
Getting started
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 (for Wikimedia Foundation wikis) our terms of use. These terms apply to you when you access or edit using the web service 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.
あなたのクライアントの識別
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のエチケット
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.
有用なリンク
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 on English Wikipedia 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: Bugzilla (When reporting new bugs, don't forget to set Component=API)
- Browse the API source code
- The current MediaWiki database schema
- Browse the current database schema in git