| This page is part of the MediaWiki Action API documentation. | 
| MediaWiki version: | ≥ 1.12  | 
GET request to list all {{ll|Help:Categories|categories}} that fit certain criteria relating to their titles.
This module can be used as a {{ll|API:Query#Generators|generator}}.
API documentation
 Special:ApiHelp/query+allcategories  | 
Example
GET request
Get a list of all categories, starting from "15th-century caliphs".
api.php?action=query&format=json&acfrom=15th-century%20caliphs&list=allcategories     [try in ApiSandbox]
Response
{
    {
    "batchcomplete": "",
    "continue": {
        "accontinue": "15th-century_churches_in_Denmark",
        "continue": "-||"
    },
    "query": {
        "allcategories": [
            {
                "*": "15th-century caliphs"
            },
            {
                "*": "15th-century calligraphers"
            },
            {
                "*": "15th-century card games"
            },
            ...
        ]
    }
}
Sample code
Python
#!/usr/bin/python3
"""
    get_allcategories.py
    MediaWiki API Demos
    Demo of `Allcategories` module: Get all categories, starting from a
    certain point, as ordered by category title.
    MIT License
"""
import requests
S = requests.Session()
URL = "https://en.wikipedia.org/w/api.php"
PARAMS = {
    "action": "query",
    "format": "json",
    "list": "allcategories",
    "acfrom": "15th-century caliphs"
}
R = S.get(url=URL, params=PARAMS)
DATA = R.json()
CATEGORIES = DATA["query"]["allcategories"]
for cat in CATEGORIES:
    print(cat["*"])
PHP
<?php
/*
    get_allcategories.php
    MediaWiki API Demos
    Demo of `Allcategories` module: Get all categories, starting from a certian point, as ordered by category title.
    MIT License
*/
$endPoint = "https://en.wikipedia.org/w/api.php";
$params = [
    "action" => "query",
    "format" => "json",
    "list" => "allcategories",
    "acfrom" => "15th-century caliphs"
];
$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"]["allcategories"] as $k => $v ) {
    echo( $v["*"] . "\n" );
}
JavaScript
/*
    get_allcategories.js
    MediaWiki API Demos
    Demo of `Allcategories` module: Get all categories, starting from a certain point, as ordered by category title.
    MIT License
*/
var url = "https://en.wikipedia.org/w/api.php"; 
var params = {
    action: "query",
    format: "json",
    list: "allcategories",
    acfrom: "15th-century caliphs"
};
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 categories = response.query.allcategories;
        for (var cat in categories) {
            console.log(categories[cat]["*"]);
        }
    })
    .catch(function(error){console.log(error);});
MediaWiki JS
/*
	get_allcategories.js
	MediaWiki API Demos
	Demo of `Allcategories` module: Get all categories,
	starting from a certian point, as ordered by category title.
	MIT License
*/
var params = {
		action: 'query',
		format: 'json',
		list: 'allcategories',
		acfrom: '15th-century caliphs'
	},
	api = new mw.Api();
api.get( params ).done( function ( data ) {
	var categories = data.query.allcategories,
		cat;
	for ( cat in categories ) {
		console.log( categories[ cat ][ '*' ] );
	}
} );
Additional notes
- This module differs from list=allpages&alnamespace=14 in that categories without descriptions will be listed, while redirects and pages where the category was never used will not.
 
- The response may include categories that were previously used but have since been deleted.
 
- Because the response may include categories that are deleted or otherwise empty, it is recommended to filter the list using 
acmin=1, so as to only return categories containing one or more members. 
See also
- API:Categorymembers - list pages which are members of a certain category.
 - API:Categories -  a 
propmodule that gets all the categories associated with a particular page. - API:Allpages - another module that can access the category {{ll|Manual:Namespace|namespace}}.
 
    This article is issued from Mediawiki. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.