# mediawiki-client notebook

```elixir
Mix.install([
  {:mediawiki_client, path: "#{__DIR__}/.."}
])
```

## Site matrix

Wiki domains can be organized into a farm such as the default Wikimedia cluster, and information about all member sites can be retrieved from the [SiteMatrix](https://www.mediawiki.org/wiki/Extension:SiteMatrix#API) extension API.

This library provides the `Wiki.SiteMatrix` module to fetch and filter the site list,

```elixir
site_matrix = Wiki.SiteMatrix.new()

{:ok, sites} = Wiki.SiteMatrix.get_all(site_matrix)
```

Once you know the `dbname` or `base_url` for a site you can easily query it, or filter by project eg. to list all Wiktionaries.

```elixir
dewiki = Wiki.SiteMatrix.get!(site_matrix, :dewiki)
```

## Working with a wiki

You can pass a site spec to the initializer for other modules, or you can supply the base API URL if this is already known.

```elixir
Wiki.Action.new(dewiki) == Wiki.Action.new("https://de.wikipedia.org/w/api.php")
```

## Free-form Action API

MediaWiki's main external interface is called the [Action API](https://www.mediawiki.org/wiki/API:Main_page), and this module allows access to any arbitrary command using keyword syntax:

```elixir
{:ok, %{result: %{"query" => %{"babel" => babel}}}} =
  Wiki.Action.new(:mediawikiwiki)
  |> Wiki.Action.get(
    action: :query,
    meta: :babel,
    babuser: "Adamw"
  )

babel
```

## Streaming continuation

The Wiki.Action transparently handles continuation and can provide output as a stream.  This example snippet returns 50 results, making 10 calls with 5 results per call:

```elixir
Wiki.Action.new(:dewiki)
|> 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)
|> Enum.to_list()
```

## Site statistics

The [siteinfo](https://www.mediawiki.org/wiki/API:Siteinfo) API returns summary statistics for a wiki.

```elixir
Wiki.Action.new(:dewiki)
|> Wiki.Action.get!(
  action: :query,
  meta: :siteinfo,
  siprop: :statistics
)
|> Map.get(:result)
```

## Event Streams

You can monitor many different events in real-time using the [EventStreams](https://wikitech.wikimedia.org/wiki/Event_Platform/EventStreams) interface. These snippets sample from the most recent edits,

```elixir
{:ok, pid} = Wiki.EventStreams.start_link(streams: "recentchange")

pid
|> Wiki.EventStreams.stream()
|> Stream.take(6)
|> Enum.each(fn event -> IO.inspect(event) end)

Process.exit(pid, :normal)
```

and from the feed of [ORES](https://www.mediawiki.org/wiki/ORES) results for recent edits,

```elixir
{:ok, pid} = Wiki.EventStreams.start_link(streams: "revision-create")

pid
|> Wiki.EventStreams.stream()
|> Stream.take(6)
|> Enum.each(fn event -> IO.inspect(event) end)

Process.exit(pid, :normal)
```

## Machine learning scores

To query ORES directly, use the Wiki.ORES module.  List the models available for your target wiki: (FIXME: eliminate the dummy request parameter)

```elixir
Wiki.Ores.new("enwiki")
|> Wiki.Ores.request!(dummy: [])
```

Retrieve scores for multiple revisions by ID:

```elixir
Wiki.Ores.new("enwiki")
|> Wiki.Ores.request!(
  models: [:damaging, :wp10],
  revids: [456_789, 123_456]
)
```

## Wikidata

Wikibase provides an Action API you can use as below.

This snippet runs a search:

```elixir
Wiki.Action.new(:wikidatawiki)
|> Wiki.Action.get!(
  action: :wbsearchentities,
  search: "alphabet",
  language: :en
)
|> Map.fetch(:result)
```

This returns the detailed, structured data for a single Wikidata item:

```elixir
Wiki.Action.new(:wikidatawiki)
|> Wiki.Action.get!(
  action: :wbgetentities,
  ids: "Q42"
)
|> Map.get(:result)
```
