Skip to content
the poor coder

mediawiki api check if page exists

Poor Man
Published
Reading time
3 min read
On this page

How to check if a page exists using MediaWiki API?#

As a blogger, I use MediaWiki API to fetch data from various wikis. One of the frequent requirements is to check if a specific page exists or not. Here are a few ways to do it:

1. Using action=query#

We can use the action=query module of MediaWiki API to retrieve information about a page. The 'titles' parameter in the query can be used to specify the page's title we want to fetch the details for. If the page exists, we will receive the properties of the page in the response. Otherwise, we will get an 'missing' property in the response.

Text
 
    
        https://en.wikipedia.org/w/api/php
        ?action=query
        &format=json
        &titles=Page_Title
        &prop=info
    
  • https://en.wikipedia.org/w/api.php: The API endpoint URL of the wiki we want to query.
  • action=query: The module we want to use to retrieve information.
  • format=json: The format in which we want to receive the response.
  • titles=Page_Title: The title of the page we want to retrieve the details for.
  • prop=info: The property we want to retrieve for the page. Here we are using 'info' to retrieve basic information about the page like whether it exists or not.

If the page exists, we will receive a response like:

Text

{
    "batchcomplete": "",
    "query": {
        "pages": {
            "123456": {
                "pageid": 123456,
                "ns": 0,
                "title": "Page_Title",
                "contentmodel": "wikitext",
                "pagelanguage": "en",
                "touched": "2021-05-18T11:27:40Z",
                "lastrevid": 123456789,
                "length": 1234,
                "fullurl": "https://en.wikipedia.org/wiki/Page_Title",
                "editurl": "https://en.wikipedia.org/w/index.php?title=Page_Title&action=edit",
                "canonicalurl": "//en.wikipedia.org/wiki/Page_Title"
            }
        }
    }
}

If the page does not exist, we will receive a response like:

Text

{
    "batchcomplete": "",
    "query": {
        "pages": {
            "-1": {
                "ns": 0,
                "title": "Page_Title",
                "missing": ""
            }
        }
    }
}

2. Using action=parse#

We can also use the action=parse module of MediaWiki API to check if a page exists or not. The 'page' parameter in the query can be used to specify the page's title or ID we want to check. If the page exists, we will receive the parsed HTML of the page in the response. Otherwise, we will get an error message in the response.

Text

    
        https://en.wikipedia.org/w/api/php
        ?action=parse
        &format=json
        &page=Page_Title
    
  • https://en.wikipedia.org/w/api.php: The API endpoint URL of the wiki we want to query.
  • action=parse: The module we want to use to retrieve information.
  • format=json: The format in which we want to receive the response.
  • page=Page_Title: The title of the page we want to retrieve the parsed HTML for.

If the page exists, we will receive a response like:

Text

{
    "parse": {
        "title": "Page_Title",
        "pageid": 123456,
        "text": {
            "*": "<!DOCTYPE html>\n<html class="client-nojs">\n<head>\n<meta charset="UTF-8">\n<title>Page Title - Wikipedia</title>\n</head>\n<body>\n\n<p><b>Page Title</b> is a ...",
            "revid": 123456789,
            "timestamp": "2021-05-18T11:27:40Z",
            "contributors": [
                {
                    "username": "User_Name",
                    "userid": 12345
                }
            ]
        },
        "properties": {
            "displaytitle": "Page Title",
            "protection": [
                {
                    "type": "edit",
                    "level": "autoconfirmed",
                    "expiry": "infinity"
                },
                {
                    "type": "move",
                    "level": "autoconfirmed",
                    "expiry": "infinity"
                }
            ]
        }
    }
}

If the page does not exist, we will receive a response like:

Text

{
    "error": {
        "code": "missingtitle",
        "info": "The page you specified doesn't exist.",
        "*": "See https://en.wikipedia.org/w/api.php for API usage. Subscribe to the mediawiki-api-announce mailing list at <https://lists.wikimedia.org/postorius/lists/mediawiki-api-announce.lists.wikimedia.org/> for notice of API deprecations and breaking changes."
    },
    "servedby": "server_name"
}

Using any of the above methods should give you a clear indication if a page exists or not.

Conclusion#

Checking if a page exists using MediaWiki API is a straightforward process. We can use either the action=query or action=parse modules to retrieve information about the page we want to check. Both ways are simple and effective, and it depends on your specific use case to choose which one to use.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC