TL;DR (MW 1.25+)
Install curl
, fileinfo
, intl
and mbstring
for PHP. Put this in your composer.local.json
:, (*1)
{
"require": {
"noris/jsoner": "~1.0"
}
}
and run composer update
. Then, append this to your LocalSettings.php:, (*2)
wfLoadExtension( 'Jsoner' );
$jsonerBaseUrl = 'https://example.com/api/';
$jsonerUser = '<your_user>';
$jsonerPass = '<your_pass>';
Jsoner
This is a MediaWiki extension that allows one to embed external JSON data (i.e. from
a REST API) into an article., (*3)
Requirements
This extension requires at least PHP >= 5.6
and the following PHP extensions:, (*4)
- curl
- fileinfo
- intl
- mbstring
Using Debian / Ubuntu you can install the extensions like this:, (*5)
sudo apt-get install php5-curl php5-intl
sudo service apache2 restart
To test if they are enabled (use your php.ini):, (*6)
$ php5 --php-ini /etc/php5/apache2/php.ini -m | grep -E 'fileinfo|mbstring|intl|curl'
curl
fileinfo
intl
mbstring
Installation
Download (recommended, with Composer)
Put this to your composer.local.json
:, (*7)
{
"require": {
"noris/jsoner": "~1.0"
}
}
and run composer update
(or composer install
if you don't have a composer.lock yet)., (*8)
Download (not recommended, manually)
Download the extension and put it in your extension/
folder., (*9)
To enable this extension, add this to your LocalSettings.php
:, (*10)
wfLoadExtension( 'Jsoner' );
This will enable the Jsoner extension and add the following functions to the MediaWiki parser:, (*11)
-
#jsoner
with parameters url
and filters, see below.
Configuration
The extension has multiple settings. Please put them after the wfLoadExtension( 'Jsoner' );
., (*12)
$jsonerBaseUrl (default = null)
$jsonerBaseUrl = 'https://example.com/api/';
This can be used to prefix all #jsoner
calls (the url
argument specifically) with this url
so that you don't have to repeat yourself, if you only consume data from one domain. If omitted,
you have to provide complete domains in url
., (*13)
$jsonerUser / $jsonerPass (default = null)
$jsonerUser = '<your_user>';
$jsonerPass = '<your_pass>';
If both are set, this is passed to cURL to authenticate. If omitted, cURL tries unauthenticated., (*14)
Usage
Jsoner has a pipes and filters architecture. First, data is fetched, then filters are applied and
finally, the data is transformed in a representation., (*15)
Fetch β [Filter ...] β Transformer
This looks like this in MediaWiki syntax:, (*16)
// Fetch β Filter β Filter β Transformer
{{ #jsoner:url=β¦ | f-SelectSubtree=foo | f-SelectKeys=name,email | t-JsonDump }}
Lets run something interesting:, (*17)
{{ #jsoner:url=http://pokeapi.co/api/v2/pokemon/1/ | f-SelectSubtree=stats | t-JsonDump }}
β
[
{
"base_stat": 45,
"effort": 0,
"stat": {
"name": "speed",
"url": "http://pokeapi.co/api/v2/stat/6/"
}
},
{
"base_stat": 65,
"effort": 0,
"stat": {
"name": "special-defense",
"url": "http://pokeapi.co/api/v2/stat/5/"
}
},
{
"base_stat": 65,
"effort": 1,
"stat": {
"name": "special-attack",
"url": "http://pokeapi.co/api/v2/stat/4/"
}
},
{
"base_stat": 49,
"effort": 0,
"stat": {
"name": "defense",
"url": "http://pokeapi.co/api/v2/stat/3/"
}
},
{
"base_stat": 49,
"effort": 0,
"stat": {
"name": "attack",
"url": "http://pokeapi.co/api/v2/stat/2/"
}
},
{
"base_stat": 45,
"effort": 0,
"stat": {
"name": "hp",
"url": "http://pokeapi.co/api/v2/stat/1/"
}
}
]
As you can see, Filters are prefixed with f-
and Transformers are prefixed with t-
., (*18)
Available Filters
A typical call looks like this, (*19)
{{ #jsoner:url=β¦ | f-SelectSubtree=foo | }}
CensorKeysFilter (f-CensorKeys
)
Runs on a list and returns a list. Usage: f-CensorKeys=key(,key)*,replacement
, (*20)
Example: f-CensorKeys=email,--protected--
, (*21)
[
{
"name": "Bob",
"email": "bob@example.com"
},
{
"name": "Tom",
"email": "tom@example.com"
}
]
β
[
{
"name": "Bob",
"email": "--protected--"
},
{
"name": "Tom",
"email": "--protected--"
}
]
ReduceKeysFilter (f-Reduce
)
Runs on a list and returns a list. Usage: f-Reduce=(\w+),(\w+)(\.\w+)*
, (*22)
Example: f-Reduce=mail,data.email
, (*23)
[
{
"id": "1",
"data": {
"email": "bob@example.com",
"city": "Berlin"
}
},
{
"id": 2,
"data": {
"email": "tom@example.com",
"city": "Hamburg"
}
}
]
β
[
{
"id": "1",
"data": {
"email": "bob@example.com",
"city": "Berlin"
},
"mail": "bob@example.com"
},
{
"id": 2,
"data": {
"email": "tom@example.com",
"city": "Hamburg"
},
"mail": "tom@example.com"
}
]
RemoveKeysFilter (f-RemoveKeys
)
Runs on a list and returns a list. Usage: f-RemoveKeys=key(,key)*
, (*24)
Example: f-RemoveKeys=email
, (*25)
[
{
"name": "Bob",
"email": "bob@example.com"
},
{
"name": "Tom",
"email": "tom@example.com"
}
]
β
[
{
"name": "Bob"
},
{
"name": "Tom"
}
]
SelectKeysFilter (f-SelectKeys
)
Runs on a list and returns a list. Usage: f-SelectKeys=key(,key)*
, (*26)
Example: f-SelectKeys=email
, (*27)
[
{
"name": "Bob",
"email": "bob@example.com"
},
{
"name": "Tom",
"email": "tom@example.com"
}
]
β
[
{
"email": "bob@example.com"
},
{
"email": "tom@example.com"
}
]
SelectSubtreeFilter (f-SelectSubtree
)
Runs on an object and returns a list. Usage: f-SelectSubtree=key
, (*28)
Example: f-SelectSubtree=records
, (*29)
{
"recordCount": 2,
"records": [
{
"name": "Bob",
"email": "bob@example.com"
},
{
"name": "Tom",
"email": "tom@example.com"
}
]
}
β
[
{
"name": "Bob",
"email": "bob@example.com"
},
{
"name": "Tom",
"email": "tom@example.com"
}
]
SelectRecordFilter (f-SelectRecord
)
Runs on a list and returns a list. Usage: [f-SelectRecord=key:value
], (*30)
Example: f-SelectRecord=email:test2@example.com
, (*31)
[
{
"name": "Bob",
"email": "test1@example.com"
},
{
"name": "Tom",
"email": "test2@example.com"
}
]
β
[
{
"name": "Tom",
"email": "test2@example.com"
}
]
There must be always a transformer at the end of the pipeline., (*32)
Creates a comma-separated list of values from a list., (*33)
Usage: t-InlineList=key
, (*34)
With a list as input, calling t-InlineList=email
, (*35)
[
{
"name": "Bob",
"email": "bob@example.com"
},
{
"name": "Tom",
"email": "tom@example.com"
}
]
β
bob@example.com, tom@example.com
Good for, you guessed it: lists!, (*36)
Dumps the JSON data into a <pre>
tag. Nice for debugging., (*37)
Returns a single JSON value out of an object or a list. If the input is a list,
the SingleElementTransformer will use the first element in the list to display something., (*38)
Usage: t-SingleElement=key
, (*39)
With a list as input, calling t-SingleElement=name
, (*40)
[
{
"name": "Bob",
"email": "bob@example.com"
},
{
"name": "Tom",
"email": "tom@example.com"
}
]
β
Bob
With an object as input, calling t-SingleElement=name
, (*41)
{
"name": "Bob",
"email": "bob@example.com"
}
β
Bob
Nice for single values like IDs., (*42)
Creates a <br />
separated (on top of each other) stack out of an object or a list. If the input
is a list, the StackedElementTransformer uses the first element in the list and displays that., (*43)
With a list as input:, (*44)
[
{
"name": "Bob",
"email": "bob@example.com"
},
{
"name": "Tom",
"email": "tom@example.com"
}
]
β
Bob
bob@example.com
With an object as input:, (*45)
{
"name": "Tom",
"email": "tom@example.com"
}
β
Tom
tom@example.com
Useful for address data., (*46)
WikitextTableTransformer (t-WikitextTable
)
Creates a nice and sortable Wikitext table out of a list of objects., (*47)
[
{
"name": "Bob",
"email": "bob@example.com"
},
{
"name": "Tom",
"email": "tom@example.com"
}
]
β
ββββββββββ¦ββββββββββββββββββ
β name βΌ β email βΌ β
β βββββββββ¬ββββββββββββββββββ£
β Bob β bob@example.com β
β Tom β tom@example.com β
ββββββββββ©ββββββββββββββββββ
Creates Wikitext depending on the given template.
You probably have to create a suiting template for the query.
Uses key=value pairs., (*48)
Wiki-String: {{ template |key=value }}, (*49)
Usage: t-mwTemplate=template
, (*50)
With t-mwTemplate=jsoner-template
, (*51)
[
{
"name": "Bob",
"email": "bob@example.com"
"username": "bobexample"
},
{
"name": "Tom",
"email": "tom@example.com"
"username": "tomexample"
}
]
β
ββββββββββ¦ββββββββββββββββββ¦βββββββββββββββ
β name βΌ β email βΌ βusername βΌ β
β βββββββββ¬ββββββββββββββββββ£βββββββββββββββ£
β Bob β bob@example.com β bobexample β
β Tom β tom@example.com β tomexample β
ββββββββββ©ββββββββββββββββββ©βββββββββββββββ
The output is depending on the template you use., (*52)
Creates Wikitext depending on the given template.
You probably have to create a suiting template for the query.
Doesn't use key=value pairs, uses the Anonymous templating in the Mediawiki.
Template in this use case :
template= {{{1}}} {{{2}}}, (*53)
Usage: t-mwTemplateAnonymous=template
, (*54)
[
{
"name": "Bob",
"email": "bob@example.com"
"username": "bobexample"
},
{
"name": "Tom",
"email": "tom@example.com"
"username": "tomexample"
}
]
β
Bob bob@example.com Tom tom@example.com
The output is depending on the template you use., (*55)
Limitations
- If you set
$jsonerUser
and $jsonerPass
, the authentification is used for every request. There
is currently no per-domain or per-request level setting for username and password (and maybe
rightfully so). One possibility would be to put a separate call, like {{ #jsoner-unauth:url=β¦ }}
or something like that.
Development
This extension is under development. Anything may change., (*56)
You can clone is using, (*57)
git clone git@github.com:noris-network/Jsoner.git && cd Jsoner
make devenv
To install it into your development MediaWiki, just symlink it to your extensions
folder, (*58)
# Assuming you are in Jsoner folder
cd /path/to/your/extensions/folder
ln -s /path/to/the/Jsoner/extension Jsoner
Then, install it like described above., (*59)
To see what you can do run one of, (*60)
make
make help
To test, you can run, (*61)
make test
To fix warnings etc. from make test
, you can run:, (*62)
make fix
To clean, you can run, (*63)
make clean
License
GPL v3, (*64)