Search code examples
http-redirectwikipedia-apimediawiki-api

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?


Solution

  • 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>