addwiki/mediawiki-api-base

A basic Mediawiki api base lib

Installs: 5 947

Dependents: 7

Stars: 8

Watchers: 7

Forks: 8

Open Issues: 0

2.2.0 2016-01-18 11:00 UTC

README

Build Status Code Coverage Scrutinizer Code Quality Latest Stable Version Download count

Issue tracker: https://phabricator.wikimedia.org/project/profile/1490/

addwiki/mediawiki-api-base is a PHP HTTP client wrapped around guzzle that makes it easy to interest with a mediawiki installation.

  • Uses PSR-3 interfaces for logging
  • Handles Mediawiki login, sessions, cookies and tokens
  • Handles response errors by throwing catchable UsageExceptions
  • Retries failed requests where possible
  • Allows Async requests

Installation

Use composer to install the library and all its dependencies:

composer require "addwiki/mediawiki-api-base:~2.1"

Example Usage

You can get an api object by simply passing the api endpoint:

$api = MediawikiApi::newFromApiEndpoint( 'http://localhost/w/api.php' );

You can event just pass a page:

$api = MediawikiApi::newFromPage( 'https://en.wikipedia.org/wiki/Berlin' );

You can easily log in and out:

$api->login( new ApiUser( 'username', 'password' ) );
$api->logout();

And make various requests:

$api->postRequest( FluentRequest::factory()->setAction( 'purge' )->setParam( 'titles', 'FooBar' ) );

$queryResponse = $api->getRequest( FluentRequest::factory()->setAction( 'query' )->setParam( 'meta', 'siteinfo' ) );

try{
    $api->postRequest( new SimpleRequest( 'FooBarBaz' ) );
}
catch ( UsageException $e ) {
    echo "Oh no the api returned an error!";
}

Including async requests:

// Initiate each request but do not block
$requestPromises = array(
    'Page1' => $api->postRequest( FluentRequest::factory()->setAction( 'purge' )->setParam( 'titles', 'Page1' ) ),
    'Page2' => $api->postRequest( FluentRequest::factory()->setAction( 'purge' )->setParam( 'titles', 'Page2' ) ),
    'Page3' => $api->postRequest( FluentRequest::factory()->setAction( 'purge' )->setParam( 'titles', 'Page3' ) ),
);

// Wait on all of the requests to complete.
$results = Promise\unwrap( $requestPromises );

// You can access each result using the key provided to the unwrap function.
print_r( $results['Page1'], $results['Page2'], $results['Page3'] )