User:Prisicilavilemen/Sandbox/API:Pageswithprop

MediaWiki version:
1.21

GET request List all pages using a given page property.

API documentation

edit

list=pageswithprop (pwp)

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

List all pages using a given page property.

Specific parameters:
Other general parameters are available.
pwppropname

Page property for which to enumerate pages (action=query&list=pagepropnames returns page property names in use).

This parameter is required.
pwpprop

Which pieces of information to include:

ids
Adds the page ID.
title
Adds the title and namespace ID of the page.
value
Adds the value of the page property.
Values (separate with | or alternative): ids, title, value
Default: ids|title
pwpcontinue

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

pwplimit

The maximum number of pages to return.

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

In which direction to sort.

One of the following values: ascending, descending
Default: ascending


Example

edit

GET request

edit

Response

edit
{
    "batchcomplete": "",
    "continue": {
        "ppncontinue": "kartographer_frames",
        "continue": "-||"
    },
    "query": {
        "pageswithprop": [
            {
                "pwppropname": "defaultsort"
            },
            {
                "pwppropname": "disambiguation"
            },
            {
                "pwppropname": "displaytitle"
            }
            ...
        ]
    }
}

Sample code

edit

Python

edit
#!/usr/bin/python3

"""
    get_pageswithprop.py

    MediaWiki API Demos
    Demo of `Pageswithprop` module: List all pages using a given page property.

    MIT License
"""

import requests

S = requests.Session()

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

PARAMS = {
    "action": "query",
    "list": "pageswithprop",
    "pwppropname": "displaytitle",
    "format": "json"
}

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

PAGESWITHPROP = DATA["query"]["pageswithprop"]

for p in PAGESWITHPROP:
    print(p["title"])
<?php
/*
    get_pageswithprop.php

    MediaWiki API Demos
    Demo of `Pageswithprop` module:  List all pages using a given page property.

    MIT License
*/

$endPoint = "https://en.wikipedia.org/w/api.php";
$params = [
    "action" => "query",
    "list" => "pageswithprop",
    "pwppropname" => "displaytitle",
    "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 );

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

JavaScript

edit
/*
    get_pageswithprops.js

    MediaWiki API Demos
    Demo of `Pageswithprop` module:  List all pages using a given page property.

    MIT License
*/

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

var params = {
    action: "query",
    list: "pageswithprop",
    pwppropname: "displaytitle",
    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) {
        var pageswithprop = response.query.pageswithprop;
        for (var p in pageswithprop) {
            console.log(pageswithprop[p].title);
        }
    })
    .catch(function(error){console.log(error);});

MediaWiki JS

edit
/*
	get_pageswithprop.js

	MediaWiki API Demos
	Demo of `Pageswithprop` module: List all pages using a given page property.

	MIT License
*/

var params = {
		action: 'query',
		list: 'pageswithprop',
		pwppropname: "displaytitle",
		format: 'json'
	},

api = new mw.Api();

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


See also

edit