4

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?

Damjan Pavlica
  • 31,277
  • 10
  • 71
  • 76
BobR
  • 273
  • 3
  • 10

2 Answers2

6

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>
svick
  • 236,525
  • 50
  • 385
  • 514
  • Yes, it was helpful. It took me a little while to apply it to the case of a generator on all redirect pages (see http://bit.ly/xbZQep), but it finally worked. Watch out for the aplimit messing things up! – BobR Jan 17 '12 at 12:18
1

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

Jakub Klinkovský
  • 1,248
  • 1
  • 12
  • 33