API:Opensearch
このページは MediaWiki 操作 API の説明文書の一部です。 |
タイトルによってウィキページを検索するGET リクエストで、これはOpenSearch形式を利用します。
APIの説明文書
例
GET リクエスト
レスポンス
[
"Hampi",
[
"Hampi",
"Hampi (town)",
"Hampi Express",
...
],
[
"Hampi, also referred to as the Group of Monuments at Hampi, is a UNESCO World Heritage Site located in east-central Karnataka, India.",
"Hampi is a town in Hospet taluk of the Ballari district in the Indian state of Karnataka. Located along the Tungabhadra River in the east and center part of the state, near the border of Andhra Pradesh, Hampi is near the city of Hosapete.",
"The Hampi Express is a daily express train running between the Mysooru and Hubballi Junction, the headquarters of the South Western Railway in India.",
...
],
[
"https://en.wikipedia.org/wiki/Hampi",
"https://en.wikipedia.org/wiki/Hampi_(town)",
"https://en.wikipedia.org/wiki/Hampi_Express",
...
]
]
ウィキメディアのウィキではパフォーマンス上の理由から説明文を省略してあり、2列目は空欄になります。 T241437 を参照してください。
サンプル コード
Python
#!/usr/bin/python3
"""
opensearch.py
MediaWiki API Demos
Demo of `Opensearch` module: Search the wiki and obtain
results in an OpenSearch (http://www.opensearch.org) format
MIT License
"""
import requests
S = requests.Session()
URL = "https://en.wikipedia.org/w/api.php"
PARAMS = {
"action": "opensearch",
"namespace": "0",
"search": "Hampi",
"limit": "5",
"format": "json"
}
R = S.get(url=URL, params=PARAMS)
DATA = R.json()
print(DATA)
PHP
<?php
/*
opensearch.php
MediaWiki API Demos
Demo of `Opensearch` module: Search the wiki and obtain
results in an OpenSearch (http://www.opensearch.org) format
MIT License
*/
$endPoint = "https://en.wikipedia.org/w/api.php";
$params = [
"action" => "opensearch",
"search" => "Hampi",
"limit" => "5",
"namespace" => "0",
"format" => "json"
];
$url = $endPoint . "?" . http_build_query( $params );
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$output = curl_exec( $ch );
curl_close( $ch );
$result = json_decode( $output, true );
var_dump( $result );
JavaScript
/*
opensearch.js
MediaWiki API Demos
Demo of `Opensearch` module: Search the wiki and obtain
results in an OpenSearch (http://www.opensearch.org) format
MIT License
*/
var url = "https://en.wikipedia.org/w/api.php";
var params = {
action: "opensearch",
search: "Hampi",
limit: "5",
namespace: "0",
format: "json"
};
url = url + "?origin=*";
Object.keys(params).forEach(function(key){url += "&" + key + "=" + params[key];});
fetch(url)
.then(function(response){return response.json();})
.then(function(response) {console.log(response);})
.catch(function(error){console.log(error);});
MediaWiki JS
/*
opensearch.js
MediaWiki API Demos
Demo of `Opensearch` module: Search the wiki and obtain
results in an OpenSearch (http://www.opensearch.org) format
MIT License
*/
var params = {
action: 'opensearch',
search: 'Hampi',
limit: '5',
namespace: '0',
format: 'json'
},
api = new mw.Api();
api.get( params ).done( function ( data ) {
console.log( data );
} );
起こりうるエラー
コード | 情報 |
---|---|
nosearch | パラメーター search を設定してください。 |
unknown_format | Unrecognized value for parameter format: aaa. |
追加的な注記
このAPI関連でMediaWiki サイト管理者と拡張機能の開発者向けのその他のヒント:
- Extension:TitleKey - このAPIの示す検索結果一覧で大文字小文字を区別。
- Extension:TextExtracts と
$wgExtractsExtendOpenSearchXml
をLocalSettings.php
で true にするとそれぞれの項目を XML 形式で示し、記事から抽出したテキストと<Description>
タグ付けします。 LocalSettings.php
で Extension:PageImages と$wgPageImagesExpandOpenSearchXml
をtrue
にするとそれぞれの項目を XML 形式で表示、記事から抽出した<Image>
のサムネイル画像にタグ付け。