How to get with Mediawiki API all images in a category which are not in another one?

1.3k Views Asked by At

I would like to get all images in a category in Wikimedia Commons. Let's say category X, but exclude those which are also in category Y. I do not understand if I can actually do this.

https://commons.wikimedia.org/w/api.php?action=query&list=categorymembers&cmtype=file&cmtitle=Category:X

This will get all of them, but how to exclude some?

moreover I would like in the result to have the description of the images, not just the name of the file, is that possible?

2

There are 2 best solutions below

0
On

AFAIK, there is no way to get that directly using the API. But, assuming both categories are reasonably small, you could get all images from both of them and then compute the complement in your code.

To retrieve the description, you can use prop=imageinfo&iiprop=extmetadata&iiextmetadatafilter=ImageDescription.

In the context of your example query, it would look like this:

https://commons.wikimedia.org/w/api.php?action=query&generator=categorymembers&gcmtype=file&gcmtitle=Category:X&prop=imageinfo&iiprop=extmetadata&iiextmetadatafilter=ImageDescription

0
On

MediaWiki has - by default - no built-in support for category building and querying intersections. To accomplish this task, extensions or external tools or multiple API queries and result processing is required.

CirrusSearch API

On Wikimedia Commons, like on the whole Wikimedia Wiki farm, CirrusSearch powers filtered search, including search for category intersections and is also available through API (action=query&list=search&srsearch=incategory:A+-incategory:B, this is Category:A minus Category:B).

FastCCI

One of the tools I can recommend (because it's a dedicated high-performance solution and actually running) is fastcci, developed by Daniel Schwen; specifically for Wikimedia Commons, there is already a database maintained and a webservice running but it's possible to set it up for any wiki, provided the tool set has a host to run on and has database access.

FastCCI in action

Query

Consider the following query URL:

https://fastcci.wmflabs.org/?c1=3302993&c2=15516712&d1=0&d2=0&s=200&a=not&t=js

  • https://fastcci.wmflabs.org/ - Host Wikimedia Commons fastcci runs on
  • c1 - ID of category 1
  • c2 - ID of category 2
  • d1 - depth of category 1 to search in (fastcci by default considers sub-categories)
  • d2 - depth of category 2 to search in (fastcci by default considers sub-categories)
  • s - Number or results to return
  • o - Offset
  • a - conjunction
  • t - connection type (t=js for a JSONP response; otherwise assumes being used as websocket)

Response

fastcciCallback( [ 'RESULT 27572680,0,0|1675043,0,0|27577015,0,0|27577043,0,0|27577106,0,0|27576896,0,0|27576790,0,0|23481936,0,0|17560964,0,0|11009066,0,0', 'OUTOF 10', 'DBAGE 378310', 'DONE'] );

RESULT followed by a | separated list of up to 50 integer triplets of the form pageId,depth,tag. Each triplet stands for one image or category

Resources

A note on pageIDs