summaryrefslogtreecommitdiff
path: root/bin/wiki/vendor/addwiki/mediawiki-api-base/docs/quickstart.rst
blob: 5db0601f9c501e0a0088f0e406d6d228f087180a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
.. _quickstart:

==========
Quickstart
==========

This page provides a quick introduction to this library and introductory examples.
If you have not already installed the library head over to the :ref:`installation`
page.

Getting an API object
----------------------------------

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

.. code-block:: php

    use \Mediawiki\Api\MediawikiApi;

    $api = MediawikiApi::newFromApiEndpoint( 'https://en.wikipedia.org/w/api.php' );

You can even just pass a page:

.. code-block:: php

    use \Mediawiki\Api\MediawikiApi;

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

Logging in and out
----------------------------------

.. code-block:: php

    use \MediawikiApi\Api\ApiUser;

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

Making request objects
----------------------------------

The library provides two different way of constructing requests.

.. code-block:: php

    use Mediawiki\Api\SimpleRequest;
    use Mediawiki\Api\FluentRequest;

    $purgeRequest = new SimpleRequest( 'purge', array( 'titles' => 'Berlin' ) );
    // or
    $purgeRequest = FluentRequest::factory()->setAction( 'purge' )->setParam( 'titles', 'Berlin' ) );

Sending requests
----------------------------------

.. code-block:: php

    $api->postRequest( $purgeRequest );

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

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


Making async requests
----------------------------------

.. code-block:: php

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

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

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