3

I'm trying to work with the mediawiki API for a project on FreeCodeCamp. I've read through these pages on the API docs:

  1. OpenSearch
  2. Generators
  3. Lists
  4. Query

And it's still not clear what the real differences are, and when and why I'd need to use each one. Here's three API calls I've made that each produce slightly different results:

  1. en.wikipedia.org/w/api.php?action=opensearch&search=Albert

In this, I get an array with 4 items, the first being the search term, the second being a list of result page titles, the third being a small snippet of each page, and the fourth being the URL to each page.

  1. en.wikipedia.org/w/api.php?action=query&generator=search&gsrsearch=Albert&format=json

In this one, there's a generator=search, which I don't understand. On the API page for generators, it just says: Get the list of pages to work on by executing the specified query module., which isn't really very helpful. What does this mean?

  1. en.wikipedia.org/w/api.php?action=query&list=search&srsearch=Albert&format=json

This is the same as the previous one except I'm using list=search.

So my questions are:

  • Between the first and second calls, I'm using action=opensearch and action=query: what are the real differences?

  • What are the differences between list and generator?

  • And finally, why is it that when using generator=search, the gsrprop=snippetdoesn't display any snippets: API Sandbox for this, and why does inprop=url not work when using list=search: API Sandbox for this

As you can see, both are nearly identical, except one uses the generator, and the other the list, but both mention inprop=url, and snippets (srprop & gsrprop).

firefiber
  • 155
  • 1
  • 2
  • 10

1 Answers1

3

opensearch is for the OpenSearch API. Unless you are implementing that API (ie. you are writing a browser or a search engine or something like that) you shouldn't use it. list=prefixsearch provides similar functionality without the syntax quirks that are required for OpenSearch comliance.

Generators are used to "pipe" results into another API. list=search and generator=search will give you the same set of pages, but in the first case the information about the pages will be provided by the search module (e.g. if you specify srprop=snippet it will add in snippets), in the second case it will be provided by whatever API modules you add to the prop= parameter (which is why gsrprop=snippet does not do anything). This is basically a shortcut for doing a search query and then querying some data for the results.

Tgr
  • 27,442
  • 12
  • 81
  • 118