API:쿼리
이 문서는 MediaWiki Action API 문서의 부분입니다. |
action=query
은 데이터를 불러오거나 저장하는 기능으로, 이 데이터에는 문서들의 위키텍스트나 링크, 분류, 그리고 문서를 변경 하기 위한 토큰 등이 포함될 수 있습니다.
API 문서
쿼리 모듈
쿼리 기능은 세 가지로 나뉘어집니다.
- 위키와 로그인한 사용자에 대한 메타데이터 를 제공하는 기능.
- 문서의 버전이나 내용 등 문서가 가진 정보 들을 제공하는 기능.
- 특정 조건에 따라 나열된 문서들의 목록 을 제공하는 기능.
예시
예제 1: 문서 지정하기
메타데이터와 목록을 불러오는 기능과는 달리 프로퍼티 정보를 제공하는 모든 쿼리 기능은 아래와 같이 문서를 지정하는 방식들에 따라 작동합니다.
titles
매개변수는titles=Foo|Bar|Main_Page
와 같은 형식을 지닙니다.- 문서의 일련번호인
pageids
매개변수는pageids=123|456|75915
와 같은 형식을 지닙니다. - 문서 버전의 일련번호인
revids
매개변수는revids=478198|54872|54894545
와 같은 형식을 지닙니다. 쿼리 기능 대부분은 문서 버전 일련번호에서 문서 일련번호로의 변환을 지원합니다. 실제로도 문서 버전 일련번호는 prop=revisions 의 형식으로만 사용됩니다. - 이 일련번호를 구하기 위해선 문서 일련번호 생성기를 이용하세요.
불러오기(GET) 요청
응답
{
"batchcomplete": true,
"query": {
"pages": [
{
"pageid": 1130,
"ns": 0,
"title": "Avicenna"
},
{
"pageid": 17412,
"ns": 0,
"title": "Klein bottle"
},
{
"pageid": 33642,
"ns": 0,
"title": "Warrant"
}
]
}
}
예제 2: 문서 제목을 위키 규칙대로 바꾸기
문서 제목을 위키의 규범대로 바꿉니다.
밑줄 문자(언더바)를 띄어쓰기로 바꾸거나 이름공간을 위키에서 정의하는 형식의 제목으로 바꾸는 것 등을 의미합니다.
불러오기(GET) 요청
응답
{
"batchcomplete": true,
"query": {
"normalized": [
{
"fromencoded": false,
"from": "Project:articleA",
"to": "Wikipedia:ArticleA"
},
{
"fromencoded": false,
"from": "article_B",
"to": "Article B"
}
],
"pages": [
{
"ns": 0,
"title": "Article B",
"missing": true
},
{
"ns": 4,
"title": "Wikipedia:ArticleA",
"missing": true
}
]
}
}
예제 3: 문서의 존재 및 문서명의 유효성 여부 확인
요청에 보내는 제목의 문서가 없을 경우엔 missing
이, 제목이 유효하지 않을 경우에는 invalid
이 참으로 표시됩니다.
In output formats that support numeric array keys, missing and invalid titles will have negative page IDs.
In some cases, a title can be viewed by a user but cannot be accessed by the API, such as pages that mirror content from another wiki.
These titles will have a known
attribute set in the response.
GET 요청
응답
{
"batchcomplete": true,
"query": {
"pages": [
{
"ns": 0,
"title": "Doesntexist",
"missing": true
},
{
"title": "Talk:",
"invalidreason": "The requested page title is empty or contains only the name of a namespace.",
"invalid": true
},
{
"pageid": 15580374,
"ns": 0,
"title": "Main Page"
}
]
}
}
Example 4: Continuing queries
When all the data is not returned in the response of a query, there will be a continue
attribute to indicate that there is more data.
GET 요청
응답
{
"continue": {
"accontinue": "List_of_largest_companies_in_Sri_Lanka",
"continue": "-||"
},
"query": {
"allcategories": [
{
"category": "List of BioWare characters"
},
{
"category": "List of Harlequin Romance novels"
},
{
"category": "List of MPs elected in UK elections templates"
},
{
"category": "List of Metamorphoses characters"
},
{
"category": "List of Rockstar Games characters"
},
{
"category": "List of Star Trek awards and nominations"
},
{
"category": "List of Swedish films of the 2020s"
},
{
"category": "List of association football clubs in the Republic of Ireland templates"
},
{
"category": "List of awards and nominations received by Aleksej Pechkuroy"
},
{
"category": "List of cabinet templates"
}
]
}
}
To get further data, add its values to the original request:
GET request
Response
{
"batchcomplete": true,
"query": {
"allcategories": [
{
"category": "List of largest companies in Sri Lanka"
},
{
"category": "List of longest beaches of the world"
},
{
"category": "List of ministers by ministry of Bangladesh"
},
{
"category": "List of people from Palm Beach, Florida"
},
{
"category": "List of video game characters"
}
]
}
}
Example 5: Batchcomplete
The API returns a batchcomplete
element to indicate that all data for the current batch of items has been returned.
MW 1.25+
In the response of the sample query below, batchcomplete
has been included to indicate that all the data for each of the three images has been returned.
The next continuation will begin returning data for the next set of 3 images.
GET 요청
응답
{
"batchcomplete": true,
"continue": {
"aicontinue": "20020822143445|Do_You_Want_to_Know_a_Secret_(Beatles_song_-_sample).ogg",
"continue": "-||"
},
"query": {
"allimages": [
{
"name": "Simon_and_Garfunkel_-_Mrs_Robinson.ogg",
"timestamp": "2002-08-04T19:55:17Z",
"url": "https://upload.wikimedia.org/wikipedia/en/6/64/Simon_and_Garfunkel_-_Mrs_Robinson.ogg",
"descriptionurl": "https://en.wikipedia.org/wiki/File:Simon_and_Garfunkel_-_Mrs_Robinson.ogg",
"descriptionshorturl": "https://en.wikipedia.org/w/index.php?curid=67723",
"ns": 6,
"title": "File:Simon and Garfunkel - Mrs Robinson.ogg"
},
{
"name": "Simon_and_Garfunkel_-_Scarborough_Fair.ogg",
"timestamp": "2002-08-04T20:01:36Z",
"url": "https://upload.wikimedia.org/wikipedia/en/c/c1/Simon_and_Garfunkel_-_Scarborough_Fair.ogg",
"descriptionurl": "https://en.wikipedia.org/wiki/File:Simon_and_Garfunkel_-_Scarborough_Fair.ogg",
"descriptionshorturl": "https://en.wikipedia.org/w/index.php?curid=67779",
"ns": 6,
"title": "File:Simon and Garfunkel - Scarborough Fair.ogg"
},
{
"name": "Beatles_please_me.ogg",
"timestamp": "2002-08-22T14:34:00Z",
"url": "https://upload.wikimedia.org/wikipedia/en/e/ee/Beatles_please_me.ogg",
"descriptionurl": "https://en.wikipedia.org/wiki/File:Beatles_please_me.ogg",
"descriptionshorturl": "https://en.wikipedia.org/w/index.php?curid=74826",
"ns": 6,
"title": "File:Beatles please me.ogg"
}
]
}
}
Example 6: Generators
Use generators if you want to get data about a set of pages.
For example, to get data about pages in a certain category, instead of querying list=categorymembers
and then querying again with pageids
set to all the returned pages, combine the two API calls into one by using generator=categorymembers
.
When using a list module as a generator, you don't need to specify the pages.
However, for a property module, you should specify the pages which the generator will work on.
For example, to load all pages that are linked to from the main page, use generator=links&titles=Main%20Page
.
Parameters passed to a generator must be prefixed with a g
. For instance, when using generator=backlinks
, use gbllimit
instead of bllimit
.
The sample query below gets links and categories for the first three pages in the main namespace starting with "Ba".
GET 요청
응답
{
"continue": {
"plcontinue": "14977970|0|Kirkwall_Ba_game",
"continue": "||categories"
},
"query": {
"pages": [
{
"pageid": 98178,
"ns": 0,
"title": "Ba",
"links": [
{
"ns": 0,
"title": "BA"
},
{
"ns": 4,
"title": "Wikipedia:Mainspace"
},
{
"ns": 4,
"title": "Wikipedia:Naming conventions (capitalization)"
},
{
"ns": 4,
"title": "Wikipedia:Protection policy"
},
{
"ns": 4,
"title": "Wikipedia:Redirect"
},
{
"ns": 10,
"title": "Template:R from miscapitalisation"
},
{
"ns": 10,
"title": "Template:R from modification"
},
{
"ns": 10,
"title": "Template:R to disambiguation page"
},
{
"ns": 14,
"title": "Category:Redirects from ambiguous terms"
},
{
"ns": 14,
"title": "Category:Redirects from other capitalisations"
}
],
"categories": [
{
"ns": 14,
"title": "Category:Redirects from ambiguous terms"
},
{
"ns": 14,
"title": "Category:Redirects from other capitalisations"
},
{
"ns": 14,
"title": "Category:Unprintworthy redirects"
}
]
},
{
"pageid": 14977970,
"ns": 0,
"title": "Ba'"
},
{
"pageid": 33351890,
"ns": 0,
"title": "Ba'Al Shem Tov"
}
]
}
}
Continuing queries
Queries will often have more results available than are just shown in the original query. (Often this is because a List query's result limit has been reached.) In these cases, queries can be continued. More detailed information on continuing queries can be at API:Continue .
발생할 수 있는 경고
경고 메시지 | 원인 |
---|---|
특수 문서 지원은 구현되어 있지 않습니다. | Thrown if a title in the Special: or Media: namespace is given. The pages in these namespaces cannot be queried. |
Redirect resolution cannot be used together with the revids parameter. Any redirects the revids point to have not been resolved. | Thrown if the redirect parameter is used in a query that specifies pages using revids .
|
변수 역사
- v1.34: Introduced
exportschema
- v1.24: Introduced
rawcontinue
(note: raw continuation was the default behavior until v1.26) - v1.21: Introduced
continue
추가 정보
- Specifying titles through
titles
orpageids
is limited to 50 titles per query, or 500 for those with theapihighlimits
right. - Use multiple query modules together to get what you need in one request, e.g.
prop=info|revisions&list=backlinks|embeddedin|allimages&meta=userinfo
. - Generators always pass page titles to the query module. Unlike lists (which may include additional data by default), generators should not output any information themselves, unless when explicitly requested via the generator module's query parameters.
Resolving redirects
Redirects can be resolved automatically, so that the target of a redirect is returned instead of the given title.
When present, they will always contain from
and to
attributes and may contain a tofragment
attribute for those redirects that point to specific sections.
Both normalization and redirection may take place.
In the case of multiple redirects, all redirects will be resolved, and in case of a circular redirect, there might not be a page in the 'pages' section (see an example request).
Redirect resolution cannot be used in combination with the revids=
parameter or with a generator generating revids; doing that will produce a warning and will not resolve redirects for the specified revids.
The examples below show how the redirects
parameter works.
Result |
---|
{
"batchcomplete": true,
"query": {
"redirects": [
{
"from": "Main page",
"to": "Main Page"
}
],
"pages": [
{
"pageid": 15580374,
"ns": 0,
"title": "Main Page"
}
]
}
}
|
Result |
---|
{
"batchcomplete": true,
"query": {
"pages": [
{
"pageid": 217225,
"ns": 0,
"title": "Main page"
}
]
}
}
|
Result |
---|
{
"batchcomplete": true,
"query": {
"pages": [
{
"pageid": 217225,
"ns": 0,
"title": "Main page",
"contentmodel": "wikitext",
"pagelanguage": "en",
"pagelanguagehtmlcode": "en",
"pagelanguagedir": "ltr",
"touched": "2017-12-17T02:02:05Z",
"lastrevid": 777924062,
"length": 170,
"redirect": true
}
]
}
}
|
Result |
---|
{
"batchcomplete": true,
"query": {
"redirects": [
{
"from": "Wikipedia:!--",
"to": "Wikipedia:Manual of Style",
"tofragment": "Invisible comments"
}
],
"pages": [
{
"pageid": 33697,
"ns": 4,
"title": "Wikipedia:Manual of Style"
}
]
}
}
|
Result |
---|
{
"batchcomplete": true,
"query": {
"normalized": [
{
"fromencoded": false,
"from": "page1",
"to": "Page1"
}
],
"redirects": [
{
"from": "Page1",
"to": "Page2"
},
{
"from": "Page2",
"to": "Page3"
},
{
"from": "Page3",
"to": "Page1"
}
],
"pages": [
{
"ns": 0,
"title": "Page1",
"missing": true
}
]
}
}
|
같이 보기
- API:대문 - 빠른 시작 가이드.
- API:Raw query continue -
rawcontinue
변수 사용 방법에 관한 정보를 포함.
- Maintained by MediaWiki Interfaces Team.
- Live chat (IRC): #mediawiki-core connect
- Issue tracker: Phabricator MediaWiki-Action-API (Report an issue)