Difference between revisions of "Mediawiki API"

From PKC
Jump to navigation Jump to search
 
(21 intermediate revisions by the same user not shown)
Line 3: Line 3:


=Process Flow=
=Process Flow=
This process flow is to document on how the front-end application can communicate with an instance of Mediawiki, which in this context PKC.
==Login Process==
==Login Process==
==Page Parse Process==
Mediawiki API has two different scenarios for handling the login process.
===Retrieving Page===
===1. Using Robot Account===
===Updating Page===
This scenario is to accommodate automation process on behalf of specified user. To enable the process, you will need to prepare your bot account, which can be done by visiting [Special:BotPasswords], then you decide the bot name and Mediawiki will prepare the username and password that can be use to access the API. Please see the right image for reference.
[[File:Bot-password.jpg|thumb|center|Bot Password setup on Mediawiki]]
'''First API to call'''
Type: GET
Endpoint: /api.php?
{| class="wikitable" style="vertical-align:bottom;"
|- style="font-weight:bold;"
! key
! value
|-
| action
| query
|-
| meta
| tokens
|-
| format
| json
|-
| type
| login
|}
Response:
<pre>
{
    "batchcomplete": "",
    "query": {
        "tokens": {
            "logintoken": "ed965e6288be0ab007b8eab63637cca762eb3af1+\\"
        }
    }
}
</pre>
'''Second API To Call'''
Type: POST
Endpoint: /api.php?
{| class="wikitable" style="vertical-align:bottom;"
|-
! key
! value
! style="font-weight:bold;" | location
|-
| action
| login
| parameter
|-
| format
| json
| parameter
|-
| lgname
| [your-bot-username]
| body
|-
| lgpassword
| [your-bot-password]
| body
|-
| lgtoken
| {from prev API}
| body
|}
Response
<pre>
{
    "login": {
        "result": "Success",
        "lguserid": 9,
        "lgusername": "[your-bot-username]"
    }
}
</pre>
Please noted on token value, as a response result from first API, you will need to pass excludes the last '\' character.
<pre>
examples response : ed965e6288be0ab007b8eab63637cca762eb3af1+\\
sending as        : ed965e6288be0ab007b8eab63637cca762eb3af1+\
</pre>
Once you have API login replied as success, you can start the transaction using the same login token that is sent on second API.
 
===2. Using Your Account===
This scenario is to accommodate a fully interactive process to Mediawiki, and position Mediawiki as the backend.
 
==How to get one page according to page title==
 
===1. Retrieving Page===
<nowiki>[your-media-wiki-url]?action=parse&format=json</nowiki>
 
Parameter
# action:  parse
# format: json
 
Body
# page: The title of the page
 
===2. Create/Update Page===
To enable the creation or update page process, you will need to retrieve the CSRF Token (Cross-Site-Request-Forgery) as part of Mediawiki security implementation. Below is get CSRF token query: <br>
<nowiki>
[your-media-wiki-url]?action=query&meta=tokens&format=json
</nowiki>
and below is the result
<syntaxhighlight lang="json">
{
    "batchcomplete": "",
    "query": {
        "tokens": {
            "csrftoken": "5eb82e0b32ba6b5a88e54a6e3d38473c6347d6cd+\\"
        }
    }
}
</syntaxhighlight>
Then, you can invoke the below API call to create or update the page. If the title is not yet available, API will automatically create the page.
* Type=POST
* [your-media-wiki-url]?action=edit&format=json
Parameter
# action=edit
# format=json
Body
# title=[Page title]
# appendtext=[text to be appended into the page]
# text=[text to post to page]
# token=[your CSRF token]
 
=References=
For complete reference, plase visit [https://pkc.pub?api.php PKC.PUB API Reference Page]
<references/>
[[Category:DevOps]]
[[Category:PKC API]]

Latest revision as of 09:25, 13 October 2022

Introduction to Mediawiki API

API stands for Application Programmer Interface, is one way of communicating through a system using a standard way of communication between applications. API Services of this mediawiki can be found in | API Documentation.

Process Flow

This process flow is to document on how the front-end application can communicate with an instance of Mediawiki, which in this context PKC.

Login Process

Mediawiki API has two different scenarios for handling the login process.

1. Using Robot Account

This scenario is to accommodate automation process on behalf of specified user. To enable the process, you will need to prepare your bot account, which can be done by visiting [Special:BotPasswords], then you decide the bot name and Mediawiki will prepare the username and password that can be use to access the API. Please see the right image for reference.

Bot Password setup on Mediawiki

First API to call Type: GET Endpoint: /api.php?

key value
action query
meta tokens
format json
type login

Response:

{
    "batchcomplete": "",
    "query": {
        "tokens": {
            "logintoken": "ed965e6288be0ab007b8eab63637cca762eb3af1+\\"
        }
    }
}

Second API To Call Type: POST Endpoint: /api.php?

key value location
action login parameter
format json parameter
lgname [your-bot-username] body
lgpassword [your-bot-password] body
lgtoken {from prev API} body

Response

{
    "login": {
        "result": "Success",
        "lguserid": 9,
        "lgusername": "[your-bot-username]"
    }
}

Please noted on token value, as a response result from first API, you will need to pass excludes the last '\' character.

examples response : ed965e6288be0ab007b8eab63637cca762eb3af1+\\
sending as        : ed965e6288be0ab007b8eab63637cca762eb3af1+\

Once you have API login replied as success, you can start the transaction using the same login token that is sent on second API.

2. Using Your Account

This scenario is to accommodate a fully interactive process to Mediawiki, and position Mediawiki as the backend.

How to get one page according to page title

1. Retrieving Page

[your-media-wiki-url]?action=parse&format=json

Parameter

  1. action: parse
  2. format: json

Body

  1. page: The title of the page

2. Create/Update Page

To enable the creation or update page process, you will need to retrieve the CSRF Token (Cross-Site-Request-Forgery) as part of Mediawiki security implementation. Below is get CSRF token query:
[your-media-wiki-url]?action=query&meta=tokens&format=json and below is the result

{
    "batchcomplete": "",
    "query": {
        "tokens": {
            "csrftoken": "5eb82e0b32ba6b5a88e54a6e3d38473c6347d6cd+\\"
        }
    }
}

Then, you can invoke the below API call to create or update the page. If the title is not yet available, API will automatically create the page.

  • Type=POST
  • [your-media-wiki-url]?action=edit&format=json

Parameter

  1. action=edit
  2. format=json

Body

  1. title=[Page title]
  2. appendtext=[text to be appended into the page]
  3. text=[text to post to page]
  4. token=[your CSRF token]

References

For complete reference, plase visit PKC.PUB API Reference Page