Jump to content

API:Templates/Sample code 1

From mediawiki.org

Python

[edit]
#!/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

[edit]
<?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

[edit]
/*
    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

[edit]
/*
	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 );
} );