API:Categorymembers

From MediaWiki.org
Jump to navigation Jump to search
This page is a translated version of the page API:Categorymembers and the translation is 53% complete.

Other languages:
English • ‎dansk • ‎français • ‎polski • ‎русский • ‎日本語
Wersja MediaWiki: 1.11

GET request to list pages that belong to a given category.

Dokumentacja API


list=categorymembers (cm)

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

List all pages in a given category.

Parameters:
cmtitle

Which category to enumerate (required). Must include the Category: prefix. Cannot be used together with cmpageid.

cmpageid

Page ID of the category to enumerate. Cannot be used together with cmtitle.

Type: integer
cmprop

Which pieces of information to include:

ids
Adds the page ID.
title
Adds the title and namespace ID of the page.
sortkey
Adds the sortkey used for sorting in the category (hexadecimal string).
sortkeyprefix
Adds the sortkey prefix used for sorting in the category (human-readable part of the sortkey).
type
Adds the type that the page has been categorised as (page, subcat or file).
timestamp
Adds the timestamp of when the page was included.
Values (separate with | or alternative): ids, title, sortkey, sortkeyprefix, type, timestamp
Default: ids|title
cmnamespace

Only include pages in these namespaces. Note that cmtype=subcat or cmtype=file may be used instead of cmnamespace=14 or 6.

Note: Due to miser mode, using this may result in fewer than cmlimit results returned before continuing; in extreme cases, zero results may be returned.

Values (separate with | or alternative): 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 90, 91, 92, 93, 100, 101, 102, 103, 104, 105, 106, 107, 486, 487, 828, 829, 1198, 1199, 2300, 2301, 2302, 2303, 2600, 5500, 5501
To specify all values, use *.
cmtype

Which type of category members to include. Ignored when cmsort=timestamp is set.

Values (separate with | or alternative): page, subcat, file
Default: page|subcat|file
cmcontinue

When more results are available, use this to continue.

cmlimit

The maximum number of pages to return.

No more than 500 (5,000 for bots) allowed.
Type: integer or max
Default: 10
cmsort

Property to sort by.

One of the following values: sortkey, timestamp
Default: sortkey
cmdir

In which direction to sort.

One of the following values: asc, desc, ascending, descending, newer, older
Default: ascending
cmstart

Timestamp to start listing from. Can only be used with cmsort=timestamp.

Type: timestamp (allowed formats)
cmend

Timestamp to end listing at. Can only be used with cmsort=timestamp.

Type: timestamp (allowed formats)
cmstarthexsortkey

Sortkey to start listing from, as returned by cmprop=sortkey. Can only be used with cmsort=sortkey.

cmendhexsortkey

Sortkey to end listing at, as returned by cmprop=sortkey. Can only be used with cmsort=sortkey.

cmstartsortkeyprefix

Sortkey prefix to start listing from. Can only be used with cmsort=sortkey. Overrides cmstarthexsortkey.

cmendsortkeyprefix

Sortkey prefix to end listing before (not at; if this value occurs it will not be included!). Can only be used with cmsort=sortkey. Overrides cmendhexsortkey.

cmstartsortkey
Deprecated.

Use cmstarthexsortkey instead.

cmendsortkey
Deprecated.

Use cmendhexsortkey instead.

Przykłady

Example 1: List twenty pages in a category

Żądanie GET

Odpowiedź

{
	"api": {
		"query-continue": {
			"categorymembers": {
				"_cmcontinue": "subcat|44594e414d494353|10998823"
			}
		},
		"query": {
			"categorymembers": {
				"cm": [
					{
						"_pageid": "22688097",
						"_ns": "0",
						"_title": "Branches of physics"
					},
					{
						"_pageid": "3445246",
						"_ns": "0",
						"_title": "Glossary of classical physics"
					},
					{
						"_pageid": "24489",
						"_ns": "0",
						"_title": "Outline of physics"
					},
					...
				]
			}
		}
	}
}

Przykładowy kod

Python

#!/usr/bin/python3

"""
    get_category_items.py

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

    MIT License
"""

import requests

S = requests.Session()

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

PARAMS = {
    "action": "query",
    "cmtitle": "Category:Physics",
    "cmlimit": "20",
    "list": "categorymembers",
    "format": "json"
}

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

PAGES = DATA['query']['categorymembers']

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

PHP

<?php
/*
    get_category_items.php

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

    MIT License
*/

$endPoint = "https://en.wikipedia.org/w/api.php";
$params = [
    "action" => "query",
    "list" => "categorymembers",
    "cmtitle" => "Category:Physics",
    "cmlimit" => "20",
    "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"]["categorymembers"] as $pages ){
    echo( $pages["title"] . "\n" );
}

JavaScript

/*
    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",
    list: "categorymembers",
    cmtitle: "Category:Physics",
    cmlimit: "20",
    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.categorymembers;
        for (var page in pages) {
            console.log(pages[page].title);
        }
    })
    .catch(function(error){console.log(error);});

MediaWiki JS

/*
	get_category_items.js

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

	MIT License
*/

var params = {
		action: 'query',
		list: 'categorymembers',
		cmtitle: 'Category:Physics',
		cmlimit: '20',
		format: 'json'
	},
	api = new mw.Api();

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

Example 2: Get the ten articles most recently added to a category

Żądanie GET

Odpowiedź

Przykładowy kod

Example 3: Get ten subcategories of a category

Żądanie GET

Odpowiedź

The next subcategories (if there're more than 10) can be continued using the cmcontinue parameter from the response above.

Przykładowy kod

Możliwe błędy

Kod Info
cmnotitle The parameter cmtitle is required.
cminvalidcategory Wprowadzona nazwa kategorii jest nieprawidłowa.
cmbadcontinue Invalid continue param. You should pass the original value returned by the previous query.

Parameter history

  • v1.24: Deprecated cmstartsortkey, cmendsortkey
  • v1.18: Introduced cmstartsortkeyprefix, cmendsortkeyprefix
  • v1.17: Introduced sortkeyprefix, type
  • v1.14: Introduced cmstartsortkey, cmendsortkey
  • v1.12: Introduced cmtype,cmstart, cmend, cmdir

See also