Wiki.Action (wiki_elixir v0.2.5) View Source

Adapter to the MediaWiki Action API

Most commands return a pipeable Wiki.Action.Session, which can be reused repeatedly.

Anonymous requests,

"dewiki"
|> Wiki.Site.get()
|> Wiki.Action.new()
|> Wiki.Action.get(
  action: :query,
  meta: :siteinfo,
  siprop: :statistics
)
# %Wiki.Action.Session{
#   __client__: %Tesla.Client{
#     adapter: {Tesla.Adapter.Hackney, :call, [[]]},
#     fun: nil,
#     post: [],
#     pre: [
#       {Tesla.Middleware.BaseUrl, :call, ["https://de.wikipedia.org/w/api.php"]},
#       {Tesla.Middleware.Compression, :call, [[format: "gzip"]]},
#       {Wiki.StatefulClient.CookieJar, :call, [[]]},
#       {Tesla.Middleware.FormUrlencoded, :call, [[]]},
#       {Tesla.Middleware.Headers, :call,
#       [[{"user-agent", "wiki_elixir/0.2.2 (spam@ludd.net)"}]]},
#       {Tesla.Middleware.JSON, :call, [[]]}
#     ]
#   },
#   result: %{
#     "batchcomplete" => true,
#     "query" => %{
#       "statistics" => %{
#         "activeusers" => 19393,
#         "admins" => 188,
#         "articles" => 2583636,
#         "edits" => 211249646,
#         "images" => 130213,
#         "jobs" => 0,
#         "pages" => 7164514,
#         "queued-massmessages" => 0,
#         "users" => 3716049
#       }
#     }
#   },
#   state: [
#     cookies: %{
#       "GeoIP" => "DE:BE:Berlin:52.57:13.42:v4",
#       "WMF-Last-Access" => "04-Jun-2021",
#       "WMF-Last-Access-Global" => "04-Jun-2021"
#     }
#   ]
# }

Commands can be pipelined while accumulating results, and logged-in user permissions delegated by supplying a bot password.

"enwiki"
|> Wiki.Site.get()
|> Wiki.Action.new(
  accumulate: true
)
|> Wiki.Action.authenticate(
  Application.get_env(:example_app, :bot_username),
  Application.get_env(:example_app, :bot_password)
)
|> Wiki.Action.get(
  action: :query,
  meta: :tokens,
  type: :csrf
)
|> (&Wiki.Action.post(&1,
  action: :edit,
  title: "Sandbox",
  assert: :user,
  token: &1.result["query"]["tokens"]["csrftoken"],
  appendtext: "~~~~ was here."
)).()
|> Map.get(:result)
|> IO.inspect()

Streaming results from multiple requests using continuation,

"https://de.wikipedia.org/w/api.php"
|> Wiki.Action.new()
|> Wiki.Action.stream(
  action: :query,
  list: :recentchanges,
  rclimit: 5
)
|> Stream.take(10)
|> Enum.flat_map(fn response -> response["query"]["recentchanges"] end)
|> Enum.map(fn rc -> rc["timestamp"] <> " " <> rc["title"] end)
|> IO.inspect()

Link to this section Summary

Functions

Make requests to authenticate a client session. This should only be done using a bot username and password, which can be created for any normal user account.

Make an API GET request

Create a new client session

Make an API POST request.

Make a GET request and follow continuations until exhausted or the stream is closed.

Link to this section Functions

Link to this function

authenticate(session, username, password)

View Source

Specs

Make requests to authenticate a client session. This should only be done using a bot username and password, which can be created for any normal user account.

Arguments

  • session - Base session pointing to a wiki.
  • username - Bot username, may be different than the final logged-in username.
  • password - Bot password. Protect this string, it allows others to take on-wiki actions on your behalf.

Return value

Authenticated session object.

Link to this function

get(session, params, opts \\ [])

View Source

Specs

Make an API GET request

Arguments

  • session - Wiki.Action.Session object.
  • params - Keyword list of query parameters as atoms or strings.
  • opts - Options to pass to the adapter.

Return value

Session object with its .result populated.

Specs

Create a new client session

Arguments

  • site - Site.Spec or raw api.php endpoint for the wiki you will connect to. For example, "https://en.wikipedia.org/w/api.php".
  • opts
    • :accumulate - Merge results from each step of a pipeline, rather than overwriting with the latest response.
Link to this function

post(session, params, opts \\ [])

View Source

Specs

Make an API POST request.

Arguments

  • session - Wiki.Action.Session object. If credentials are required for this action, you should have created this object with the authenticate/3 function.
  • params - Keyword list of query parameters as atoms or strings.
  • opts - Options to pass to the adapter.

Return value

Session object with a populated :result attribute.

Specs

Make a GET request and follow continuations until exhausted or the stream is closed.

Arguments

  • session - Wiki.Action.Session object.
  • params - Keyword list of query parameters as atoms or strings.

Return value

Enumerable Stream, where each returned chunk is a raw result map, possibly containing multiple records. This corresponds to session.result from the other entry points.