This page is a translated version of the page API:Templates and the translation is 100% complete.
MediaWiki版本:
1.11

GET 请求获取所提供页面中包含的所有页面(通常是模板)的列表。

API帮助文档


prop=templates (tl)

(main | query | templates)
  • This module requires read rights.
  • This module can be used as a generator.
  • Source: MediaWiki
  • License: GPL-2.0-or-later

Returns all pages transcluded on the given pages.

Specific parameters:
Other general parameters are available.
tlnamespace

Show templates in these namespaces only.

Values (separate with | or alternative): -1, -2, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 90, 91, 92, 93, 100, 101, 102, 103, 104, 105, 106, 107, 486, 487, 710, 711, 828, 829, 1198, 1199, 2600, 5500, 5501
To specify all values, use *.
tllimit

How many templates to return.

Type: integer or max
The value must be between 1 and 500.
Default: 10
tlcontinue

When more results are available, use this to continue. More detailed information on how to continue queries can be found on mediawiki.org.

tltemplates

Only list these templates. Useful for checking whether a certain page uses a certain template.

Separate values with | or alternative.
Maximum number of values is 50 (500 for clients that are allowed higher limits).
tldir

The direction in which to list.

One of the following values: ascending, descending
Default: ascending
Examples:
Get the templates used on the page MediaWiki.
api.php?action=query&prop=templates&titles=MediaWiki [open in sandbox]
Get information about the template pages used on the page MediaWiki.
api.php?action=query&generator=templates&titles=MediaWiki&prop=info [open in sandbox]
Get pages in the User and Template namespaces that are transcluded on the page MediaWiki.
api.php?action=query&prop=templates&titles=MediaWiki&tlnamespace=2|10 [open in sandbox]

示例

GET请求

获取页面上使用的模板列表。


回应

{
    "continue": {
        "tlcontinue": "736|10|Birth_date",
        "continue": "||"
    },
    "query": {
        "pages": {
            "736": {
                "pageid": 736,
                "ns": 0,
                "title": "Albert Einstein",
                "templates": [
                    {
                        "ns": 0,
                        "title": "Einstein"
                    },
                    {
                        "ns": 10,
                        "title": "Template:20th Century Press Archives"
                    },
                    ...
                ]
            }
        }
    }

示例代码

Python

#!/usr/bin/python3

"""
    templates.py
    MediaWiki API Demos
    Demo of `Templates` module: Get a list of templates used on a page
    MIT License
"""

import requests

S = requests.Session()

URL = "https://en.wikipedia.org/w/api.php"

PARAMS = {
    "action": "query",
    "titles": "Albert Einstein",
    "prop": "templates",
    "format": "json"
}

R = S.get(url=URL, params=PARAMS)
DATA = R.json()

print(DATA)

PHP

<?php

/*
    templates.php
    MediaWiki API Demos
    Demo of `Templates` module: Get a list of templates used on a page
    MIT License
*/

$endPoint = "https://en.wikipedia.org/w/api.php";
$params = [
    "action" => "query",
    "titles" => "Albert Einstein",
    "prop" => "templates",
    "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

/*
    templates.js
    MediaWiki API Demos
    Demo of `Templates` module: Get a list of templates used on a page
    MIT License
*/

var url = "https://en.wikipedia.org/w/api.php"; 

var params = {
    action: "query",
    titles: "Albert Einstein",
    prop: "templates",
    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

/*
	templates.js
	MediaWiki API Demos
	Demo of `Templates` module: Get a list of templates used on a page
	MIT License
*/

var params = {
		action: 'query',
		titles: 'Albert Einstein',
		prop: 'templates',
		format: 'json'
	},
	api = new mw.Api();

api.get( params ).done( function ( data ) {
	console.log( data );
} );


参数历史

  • v1.19: 启用tldir
  • v1.17: 启用tltemplates
  • v1.x: 启用tlcontinue, tllimit

附加提醒

参见