From MediaWiki.org
Jump to: navigation, search
Tools clipart.png Эта страница является частью документации по MediaWiki action API.
MediaWiki APIAPI:Main page

API действий MediaWiki

v · d · e

Модуль Query API позволяет приложениям получить необходимые данные из баз данных MediaWiki, и он частично основан на интерфейсе Query API, доступном в настоящее время на всех серверах MediaWiki.

Существует несколько типов запросов, понимаемых API (все можно сочетать в одном запросе):

  • Мета-информация о всём сайте и вошедшем пользователе
  • Версии - получает данные о содержимом страницы и истории изменений
  • Информация о странице - любые данные, содержащиеся в самой странице, такие как списки ссылок, шаблонов, категорий, интервики.
  • Списки страниц, совпадающих с параметрами запроса: все страницы, страницы в категории.

Нормализация заголовков (сделано)[edit]

Преобразует неправильные заголовки страниц в корректную форму. Капитализирует первый символ, заменяет '_' на ' ', изменяет канонические имена пространств на локализованный и т.д.

Капитализация, локализация, "_" => " ", "Project" => "Википедия", ...

Redirects (done)[edit]

Redirects can be resolved by the server, so that the target of redirect is returned instead of the given title. This example is not very useful without additional prop=... element, but shows the usage of redirect function. The 'redirects' section will contain the target of redirect and non-zero namespace code. Both normalization and redirection may take place. In case of redirect to a redirect, all redirections will be solved, and in case of a circular redirection, there might not be a page in the 'pages' section.

Using "redirects" parameter. "Main page" is a redirect to "Main Page"

Same request but without the "redirects" parameter.

Without "redirects" you may want to use prop=info to obtain redirect status.

Circular Redirects (done)[edit]

Assume Page1 → Page2 → Page3 → Page1 (circular redirect). Also, in this example a non-normalized name 'page1' is used.
  api.php ? action=query & titles=page1 & redirects
        Page1: Page2      Redirects are present, but not the 'pages' element.
        Page2: Page3
        Page3: Page1
        page1: Page1


To prevent server overloads, each query imposes a limit on how many items it can process. Anonymous and logged-in users have one limit, while bots and sysops have a considerably higher limit as they are trusted by the community. At present, each query simply lists the maximum request size it allows. For example, allpages list will allow aplimit= to be set no higher than 500, or in case of a bot or a sysop - no higher than 5000.
Drawbacks: Currently all limits are additive, so if the user requests allpages and backlinks, the user will get 500 of each. This is not very good, as the more items are compounded into one request, the heavier the load on the server will be. Instead, some sort of a weighted mechanism should be developed, where each request item has a certain "cost" associated with it, and each user is allocated a fixed allowance per request. The more information user requests, the less the limit becomes for that request. Unfortunately, that makes it very hard to figure out the maximum limits before executing the query, so might not be a workable solution.

Query - Generators (done)[edit]

Generator is way to use one of the above #lists instead of the titles= parameter. The output of the list must be a list of pages, whose titles get automatically used instead of the titles=/revids=/pageids= parameters. Other queries such as content, revisions, etc, will treat those pages as if they were provided by the user in the titles= parameter. Only one generator is allowed, and while it is possible to have both generator= and list= parameters in the same call, they may not contain the same values.

Using allpages as generator[edit]

Use the allpages list as a generator, to get the links and categories for all titles returned by allpages.

  api.php ? action=query & generator=allpages & apnamespace=3 & aplimit=10 & apfrom=A & prop=links|categories
          id: 12341
          ns: 10
            Linked Article1:            Linked Article1 is in the main namespace
            Talk:Linked Article2:       For non-main ns, list it as a sub-element
              ns: 1
        continue: apfrom=D-Article      The next item in this list would have been Template:D-Article.

Generators and redirects[edit]

Here, we use "links" page property as a generator. This query will get all the links from all the pages that are linked from Title. For this example, assume that Title has links to TitleA and TitleB. TitleB is a redirect to TitleC. TitleA links to TitleA1, TitleA2, TitleA3; and TitleC links to TitleC1 & TitleC2. Redirect is solved because of the "redirects" parameter.

The query will execute the following steps:
  1. Resolve titles parameter for redirects
  2. For all pages specified in titles=...|... parameter, get all links, and substitute original with the new titles=...|... parameter.
  3. Resolve new titles list for redirects
  4. Execute regular prop=links query using the internally created list of titles.
  api.php ? action=query & generator=links & titles=Title & prop=links & redirects
        TitleB: TitleC


Show info about 4 pages starting at the letter "T"
Show content of first 2 non-redirect pages begining at "Re"