Jump to content

Extension:Apiunto

From mediawiki.org
MediaWiki extensions manual
Apiunto
Release status: beta
Implementation Data extraction
Description Provides a Scribunto (Lua) library for fetching data from REST APIs
Author(s) Octfx, alistair3149
Latest version 3.0.0
Compatibility policy For every MediaWiki release that is a Long Term Support release there is a corresponding branch in the extension.
MediaWiki 1.43+
PHP 8.1+
Composer starcitizentools/apiunto
  • $wgApiuntoEnableCache
  • $wgApiuntoSources
Licence MIT License
Download

The Apiunto extension lets MediaWiki templates pull live data from REST APIs. You configure an API source once in LocalSettings.php, then fetch from it in Lua with mw.ext.Apiunto.fetch(). Responses are cached automatically, so repeated lookups do not re-hit the upstream API.

Apiunto requires the Scribunto extension.

Installation

[edit]
  • Download and place the file(s) in a directory called Apiunto in your extensions/ folder.
  • Add the following code at the bottom of your LocalSettings.php file:
    wfLoadExtension( 'Apiunto' );
    
  • Yes Done – Navigate to Special:Version on your wiki to verify that the extension is successfully installed.

After enabling the extension, define at least one API source in LocalSettings.php:

$wgApiuntoSources = [
	'SWAPI' => [
		'baseUrl'       => 'https://www.swapi.tech',
		'token'         => '',   // optional
		'timeout'       => 5,    // optional, seconds, default 5
		'cacheDuration' => 3600, // optional, seconds, default 86400
	],
];

Configuration

[edit]
Name Description Default
$wgApiuntoSources Associative array of API sources keyed by source name. Each source requires baseUrl and accepts the optional keys token, timeout (seconds), and cacheDuration (seconds). []
$wgApiuntoEnableCache Whether to cache API responses. true

Usage

[edit]

Lua

[edit]

Call mw.ext.Apiunto.fetch() from a Scribunto module:

local api = mw.ext.Apiunto

-- SWAPI files Darth Vader (4) and Anakin Skywalker (11) as two different people
local vader  = api.fetch( 'SWAPI', 'api/people/4' )
local anakin = api.fetch( 'SWAPI', 'api/people/11' )

fetch( source, uri, args ) takes:

  • source: a source name from $wgApiuntoSources
  • uri: the endpoint path to request
  • args (optional): a table of query parameters to append. Array values are joined with commas; empty values are dropped.
-- Resolves to api/people/?name=skywalker (Luke, Anakin, and Shmi)
local skywalkers = api.fetch( 'SWAPI', 'api/people', {
	name = 'skywalker',
} )

Caching

[edit]

Each response is cached for its source's cacheDuration. Purging a page (action=purge) clears the Apiunto responses cached for it, and a page's information page (action=info) shows the current cache status, URL, and time remaining.

To purge the entire cache from the command line:

php maintenance/run.php Apiunto:purgeCache

Add --dry-run to preview what would be purged without deleting anything.

See also

[edit]