The following documentation is the output of Special:ApiHelp/query+categorymembers, automatically generated by the pre-release version of MediaWiki that is running on this site (MediaWiki.org).
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, sortkey, sortkeyprefix, timestamp, title, type
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.
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.
{"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"},...]}}}}
Sample code
get_category_items.py
Python
#!/usr/bin/python3""" get_category_items.py MediaWiki API Demos Demo of `Categorymembers` module : List twenty items in a category MIT License"""importrequestsS=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']forpageinPAGES: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*/varurl="https://en.wikipedia.org/w/api.php";varparams={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){returnresponse.json();}).then(function(response){varpages=response.query.categorymembers;for(varpageinpages){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*/varparams={action:'query',list:'categorymembers',cmtitle:'Category:Physics',cmlimit:'20',format:'json'},api=newmw.Api();api.get(params).done(function(data){varpages=data.query.categorymembers,page;for(pageinpages){console.log(pages[page].title);}});
Example 2: Get the ten articles most recently added to a category
{"api":{"query-continue":{"categorymembers":{"_cmcontinue":"Magnetic levitation|"}},"query":{"categorymembers":{"cm":[{"_pageid":"1653925","_ns":"100","_title":"Portal:Physics"},{"_pageid":"22939","_ns":"0","_title":"Physics"},{"_pageid":"3445246","_ns":"0","_title":"Glossary of classical physics"},...]}}}}
Sample code
get_recent_category_items.py
Python
#!/usr/bin/python3""" get_recent_category_items.py MediaWiki API Demos Demo of `Categorymembers` module : Get the ten articles most recently added to a category MIT License"""importrequestsS=requests.Session()URL="https://en.wikipedia.org/w/api.php"PARAMS={"cmdir":"desc","format":"json","list":"categorymembers","action":"query","cmtitle":"Category:Physics","cmsort":"timestamp"}R=S.get(url=URL,params=PARAMS)DATA=R.json()PAGES=DATA["query"]["categorymembers"]forpageinPAGES:print(page["title"])
PHP
<?php/* get_recent_category_items.php MediaWiki API Demos Demo of `Categorymembers` module : Get the ten articles most recently added to a category MIT License*/$endPoint="https://en.wikipedia.org/w/api.php";$params=["action"=>"query","list"=>"categorymembers","cmtitle"=>"Category:Physics","cmsort"=>"timestamp","cmdir"=>"desc","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$page){echo($page["title"]."\n");}
JavaScript
/* get_recent_category_items.js MediaWiki API Demos Demo of `Categorymembers` module : Get the ten articles most recently added to a category MIT License*/varurl="https://en.wikipedia.org/w/api.php";varparams={action:"query",list:"categorymembers",cmtitle:"Category:Physics",cmsort:"timestamp",cmdir:"desc",format:"json"};url=url+"?origin=*";Object.keys(params).forEach(function(key){url+="&"+key+"="+params[key];});fetch(url).then(function(response){returnresponse.json();}).then(function(response){varpages=response.query.categorymembers;for(varpageinpages){console.log(pages[page].title);}}).catch(function(error){console.log(error);});
MediaWiki JS
/* get_recent_category_items.js MediaWiki API Demos Demo of `Categorymembers` module : Get the ten articles most recently added to a category MIT License*/varparams={action:'query',list:'categorymembers',cmtitle:'Category:Physics',cmsort:'timestamp',cmdir:'desc',format:'json'},api=newmw.Api();api.get(params).done(function(data){varpages=data.query.categorymembers,page;for(pageinpages){console.log(pages[page].title);}});
{"api":{"query-continue":{"categorymembers":{"_cmcontinue":"subcat|57494b4950454449412050454f504c45|41491664"}},"query":{"categorymembers":{"cm":[{"_pageid":"1458692","_ns":"14","_title":"Category:Wikipedias by language"},{"_pageid":"22918730","_ns":"14","_title":"Category:Books about Wikipedia"},{"_pageid":"16957584","_ns":"14","_title":"Category:Critics of Wikipedia"},...]}}}}
The next subcategories (if there're more than 10) can be continued using the cmcontinue parameter from the response above.
Sample code
get_subcategories.py
Python
#!/usr/bin/python3""" get_subcategories.py MediaWiki API Demos Demo of `Categorymembers` module : Get ten subcategories of a category MIT License"""importrequestsS=requests.Session()URL="https://en.wikipedia.org/w/api.php"PARAMS={"action":"query","cmtitle":"Category:Wikipedia","cmtype":"subcat","list":"categorymembers","format":"json"}R=S.get(url=URL,params=PARAMS)DATA=R.json()PAGES=DATA["query"]["categorymembers"]forpageinPAGES:print(page["title"])
PHP
<?php/* get_subcategories.php MediaWiki API Demos Demo of `Categorymembers` module : Get ten subcategories of a category MIT License*/$endPoint="https://en.wikipedia.org/w/api.php";$params=["action"=>"query","list"=>"categorymembers","cmtitle"=>"Category:Wikipedia","cmtype"=>"subcat","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$cat){echo($cat["title"]."\n");}
JavaScript
/* get_subcategories.js MediaWiki API Demos Demo of `Categorymembers` module : Get ten subcategories of a category MIT License*/varurl="https://en.wikipedia.org/w/api.php";varparams={action:"query",list:"categorymembers",cmtitle:"Category:Wikipedia",cmtype:"subcat",format:"json"};url=url+"?origin=*";Object.keys(params).forEach(function(key){url+="&"+key+"="+params[key];});fetch(url).then(function(response){returnresponse.json();}).then(function(response){varcategory=response.query.categorymembers;for(varcatincategory){console.log(category[cat].title);}}).catch(function(error){console.log(error);});
MediaWiki JS
/* get_subcategories.js MediaWiki API Demos Demo of `Categorymembers` module : Get ten subcategories of a category MIT License*/varparams={action:'query',list:'categorymembers',cmtitle:'Category:Wikipedia',cmtype:'subcat',format:'json'},api=newmw.Api();api.get(params).done(function(data){varcategory=data.query.categorymembers,cat;for(catincategory){console.log(category[cat].title);}});
Possible errors
Code
Info
cmnotitle
The parameter cmtitle is required.
cminvalidcategory
The category name you entered is not valid.
cmbadcontinue
Invalid continue param. You should pass the original value returned by the previous query.