hallowelt / mwstake-mediawiki-component-commonwebapis

0 stars 2 forks source link

MediaWiki Stakeholders Group - Components

Common Web APIs for MediaWiki

Provides various web APIs (Action API and REST).

This code is meant to be executed within the MediaWiki application context. No standalone usage is intended.

Compatibility

Prerequisites

Use in a MediaWiki extension

Require this component in the composer.json of your extension:

{
    "require": {
        "mwstake/mediawiki-component-commonwebapis": "~2"
    }
}

Getting the available endpoints

$endpoints = MediaWikiServices::getInstance()->getService( 'MWStakeCommonWebAPIs' )->getAvailableEndpoints();

will yield a list of all registered endpoints as well as their REST path configuration

Clientside abstraction

To make it easier to access these endpoints from JS, an abstraction is implemented.

To enable it, load RL module ext.mws.commonwebapis and use it like this:

mw.loader.using( 'ext.mws.commonwebapis' ).then( function () {
    mws.commonwebapi.user.query( {
        query: 'MyUser'
    } );
} );

REST API

Filtering

In order to specify filters, you can use the filter parameter. It is an JSON-encoded array of objects. Each object has the following properties:

Sorting

In order to specify sorting, you can use the sort parameter. It is an JSON-encoded array of objects. Each object has the following properties:

Example

mw.loader.using( 'ext.mws.commonwebapis' ).then( function () {
    mws.commonwebapi.user.query( {
        filter: JSON.stringify( [
            {
                field: 'user_name',
                value: 'MyUser',
                operator: 'eq',
                type: 'string'
            }
        ] ),
        sort: JSON.stringify( [
            {
                field: 'user_name',
                direction: 'asc'
            }
        ] )
    } );
} );