Jump to content

API: Assert

From mediawiki.org
This page is a translated version of the page API:Assert and the translation is 31% complete.
Versão MediaWiki:
1.23

O parâmetro assert da API de Ação pode ser definido em qualquer pedido da API para verificar certas condições antes da execução da ação solicitada.

As opções disponíveis são:

  • assert=anon: Check that you are acting as an IP user . This prevents accidental use of a registered account. Since MediaWiki 1.35change 572374
  • assert=user: Check that you are using a registered account (either "named" or temporary). This prevents accidental fallback to an IP user.
  • assert=bot: Check that you are logged-in with an account that has the "bot " user right. This prevents your script from interacting with wikis where your bot does not have the bot flag.
  • assertuser=...: Check that you are logged-in with the expected user name. Since MediaWiki 1.28

Note that temporary users satisfy assert=user, and fail assert=anon.

Erros possíveis

If the assertion fails, one of the following error codes will be returned:

  • assertanonfailed
  • assertuserfailed
  • assertbotfailed
  • assertnameduserfailed

Justificativa e casos de utilização

These parameters are intended as a second line of defense against several kinds of problems:

Erros de operador robô

It's easy for a simple operator error to cause a lot of bad edits, such as the bot running on the wrong wiki or under the wrong username, especially if you're running more than one bot task. The assert=bot and assertuser=... parameters are intended to help prevent those mistakes.

Expiração da sessão de autenticação

After logging in, the cookies you receive are only valid for 30 days by default, after which time your bot will become logged out, and may continue working with its actions being attributed to the IP address or a temporary username (if logged-out users are allowed to perform them). The assert=user parameter is intended to prevent this.

Mudar contas de utilizador

Interactive tools (in the browser) using the API to edit often want to make sure the user understands how their work will be attributed. When the user logs in or out in another browser tab (or their login session expires), this won't be immediately reflected in the tool's interface, but the new login session will be used for saving actions. The assertuser=... and assert=anon parameters are intended to allow detecting this scenario and displaying an appropriate message before continuing.

Verificação única

If you want to check whether your HTTP client is logged into the Action API, without performing any other action, you can send a request with the parameters action=query&assert=user. This will return an empty response ({} in the JSON format) if you are indeed logged in, or the assertuserfailed error if not.

Normally, you will not need to perform a separate request like this. Instead, set the assert=user parameter on each of your requests.