Jump to content

API:Allpages/Sample code 1/ja

From mediawiki.org
This page is a translated version of the page API:Allpages/Sample code 1 and the translation is 89% complete.

Python

#!/usr/bin/python3

"""
    get_allpages.py

    MediaWiki API デモ
    Demo of `Allpages` module: Get all pages whose title contains the text "Jungle," in whole or part.

    MIT ライセンス
"""

import requests

S = requests.Session()

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

PARAMS = {
    "action": "query",
    "format": "json",
    "list": "allpages",
    "apfrom": "jungle",
}

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

PAGES = DATA["query"]["allpages"]

for page in PAGES:
    print(page["title"])

PHP

<?php
/*
    get_allpages.php

    MediaWiki API デモ
    Demo of `Allpages` module: Get all pages whose title contains the text "Jungle," in whole or part.

    MIT ライセンス
*/

$endPoint = "https://en.wikipedia.org/w/api.php";
$params = [
    "action" => "query",
    "format" => "json",
    "list" => "allpages",
    "apfrom" => "jungle"
];

$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 );

foreach( $result["query"]["allpages"] as $k => $v ) {
    echo( $v["title"] . "\n" );
}

JavaScript

/*
    get_allpages.js

    MediaWiki API デモ
    Demo of `Allpages` module: Get all pages whose title contains the text "Jungle," in whole or part.

    MIT ライセンス
*/

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

const params = {
    action: "query",
    format: "json",
    list: "allpages",
    apfrom: "jungle"
}

url += "?origin=*"

Object.keys(params).forEach((key) => {
    url += `&${key}=${params[key]}`
})

fetch(url)
    .then((response) => {
        return response.json()
    })
    .then((response) => {
        const pages = response.query.allpages

        for (let p in pages) {
            console.log(pages[p].title)
        }
    })
    .catch((error) => {
        console.log(error)
    })

MediaWiki JS

/*
	get_allpages.js

	MediaWiki API デモ
	Demo of `Allpages` module: Get all pages whose title contains the text "Jungle," in whole or part.

	MIT ライセンス
*/

var params = {
		action: 'query',
		format: 'json',
		list: 'allpages',
		apfrom: 'jungle'
	},
	api = new mw.Api();

api.get( params ).done( function ( data ) {
	var pages = data.query.allpages,
		p;
	for ( p in pages ) {
		console.log( pages[ p ].title );
	}
} );