mediawiki api check if page exists

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.

 
    
        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:


{
    "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:


{
    "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.


    
        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:


{
    "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:


{
    "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.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe