BobR
BobR

Reputation: 283

Wikipedia API: how to retrieve multiple titles AND resolve redirects in 1 call?

It appears from the MediaWiki API:Query page that you can only resolve a redirect one at a time.

The document even says "The example below isn't really useful because it doesn't use any query modules, but shows how the redirects parameter works."

But how can you get the redirect information -- using a query module that does return multiple results?

Upvotes: 4

Views: 3723

Answers (2)

Jakub Klinkovský
Jakub Klinkovský

Reputation: 1362

You can also use prop=redirects with any generator, e.g. generator=allpages. This is a new feature since MW-1.23, fixing bug T59057.

When using generator=allpages with max limits (gaplimit=max and rdlimit=max) and apihighlimits right is available, all redirects on ArchWiki are resolved in a single query ;)
https://wiki.archlinux.org/api.php?action=query&generator=allpages&gapfilterredir=nonredirects&gaplimit=max&prop=redirects&rdprop=pageid|title|fragment&rdlimit=max

Upvotes: 1

svick
svick

Reputation: 244787

If you have any result that returns pages, then you can just append redirects to the query and it resolves the redirects. If you don't have results that returns pages, you can usually convert it to that by using a generator.

For example, the query

http://en.wikipedia.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:Redirects_from_gender&redirects

returns something like (shortened)

<api>
  <query>
    <categorymembers>
      <cm pageid="648" ns="0" title="Actress" />
      <cm pageid="19887132" ns="0" title="Administratrix" />
    </categorymembers>
  </query>
</api>

If you convert that into a generator

http://en.wikipedia.org/w/api.php?action=query&generator=categorymembers&gcmtitle=Category:Redirects_from_gender

you get

<api>
  <query>
    <pages>
      <page pageid="648" ns="0" title="Actress" />
      <page pageid="19887132" ns="0" title="Administratrix" />
    </pages>
  </query>
</api>

And if you now add redirects

http://en.wikipedia.org/w/api.php?action=query&generator=categorymembers&gcmtitle=Category:Redirects_from_gender&redirects

you get

<api>
  <query>
    <redirects>
      <r from="Actress" to="Actor" />
      <r from="Administratrix" to="Administrator (law)" />
    </redirects>
    <pages>
      <page pageid="21504235" ns="0" title="Actor" />
      <page pageid="6676496" ns="0" title="Administrator (law)" />
    </pages>
  </query>
</api>

Upvotes: 6

Related Questions