Jump to content

API:Categoryinfo

From mediawiki.org
MediaWiki version:
1.13

GET request to obtain information about few categories.

API documentation

[edit]

prop=categoryinfo (ci)

(main | query | categoryinfo)

Returns information about the given categories.

Specific parameter:
Other general parameters are available.
cicontinue

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

Example:
Get information about Category:Foo and Category:Bar.
api.php?action=query&prop=categoryinfo&titles=Category:Foo|Category:Bar [open in sandbox]


Example

[edit]

GET request

[edit]
Get info about a few categories

Response

[edit]
{
    "batchcomplete": "",
    "query": {
        "pages": {
            "-1": {
                "ns": 14,
                "title": "Category:Foo",
                "missing": "",
                "categoryinfo": {
                    "size": 1,
                    "pages": 1,
                    "files": 0,
                    "subcats": 0
                }
            },
            "1504": {
                "pageid": 1504,
                "ns": 0,
                "title": "Albert"
            },
            "3108204": {
                "pageid": 3108204,
                "ns": 14,
                "title": "Category:Infobox templates",
                "categoryinfo": {
                    "size": 2066,
                    "pages": 2048,
                    "files": 0,
                    "subcats": 18
                }
            }
        }
    }
}

Sample code

[edit]

Python

[edit]
#!/usr/bin/python3

"""
    get_category_info.py

    MediaWiki API Demos
    Demo of `Categoryinfo` module: Get information about few categories
    MIT license
"""

import requests

S = requests.Session()

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

PARAMS = {
    "action": "query",
    "format": "json",
    "titles": "Category:Foo|Category:Infobox templates",
    "prop": "categoryinfo"
}

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

PAGES = DATA["query"]["pages"]

for k, v in PAGES.items():
    print(v["title"] + " has " + str(v["categoryinfo"]["pages"]) + " pages.")

PHP

[edit]
<?php

//This file is autogenerated. See modules.json and autogenerator.py for details

/*
    get_category_info.php

    MediaWiki API Demos
    Demo of `Categoryinfo` module: Get information about a few categories

    MIT License
*/

$endPoint = "https://en.wikipedia.org/w/api.php";
$params = [
    "action" => "query",
    "format" => "json",
    "titles" => "Category:Foo|Category:Infobox templates",
    "prop" => "categoryinfo"
];

$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"]["pages"] as $k => $v ) {
    echo( $v["title"] . " has " . $v["categoryinfo"]["pages"] . " pages." . "\n"  );
}

JavaScript

[edit]
//This file is autogenerated. See modules.json and autogenerator.py for details

/*
    get_category_items.js

    MediaWiki API Demos
    Demo of `Categorymembers` module : List twenty items in a category

    MIT License
*/

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

var params = {
    action: "query",
    prop: "categoryinfo",
    titles: "Category:Foo|Category:Infobox 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) {
        var pages = response.query.pages;
        for (var page in pages) {
            console.log(pages[page].title + " has " + pages[page].categoryinfo.pages + " pages." );
        }
    })
    .catch(function(error){console.log(error);});

MediaWiki JS

[edit]
// This file is autogenerated. See modules.json and autogenerator.py for details

/*
	get_category_info.js

	MediaWiki API Demos
	Demo of `Categoryinfo` module: Get information about a few categories

	MIT License
*/

var params = {
		action: 'query',
		format: 'json',
		titles: 'Category:Foo|Category:Infobox templates',
		prop: 'categoryinfo'
	},
	api = new mw.Api();

api.get( params ).done( function ( data ) {
	var pages = data.query.pages,
		page;
	for ( page in pages ) {
		console.log( pages[ page ].title + ' has ' + pages[ page ].categoryinfo.pages + ' pages.' );
	}
} );

Additional notes

[edit]
  • If the category page exists, but the category has no members, a categoryinfo field will not be included in the API response.
  • When more results are available, use cicontinue parameter to continue.

See also

[edit]