MediaWiki version:
1.11

GET request to list links that point to a given {{ll|Manual:Namespace|namespace}}, ordered by title.

This module can be used as a generator.

API documentation


Special:ApiHelp/query+alllinks

Example

By default, this module will return duplicates if a page contains multiple links pointing to the same namespace. This example uses alunique=1 to remove any duplicate titles in the response.

GET request

List distinct links (i.e. no duplicates) which point to the main namespace.

Response

{
    "batchcomplete": "",
    "continue": {
        "alcontinue": "!!!!Hashtagging",
        "continue": "-||"
    },
    "query": {
        "alllinks": [
            {
                "ns": 0,
                "title": "!"
            },
            {
                "ns": 0,
                "title": "!!"
            },
            {
                "ns": 0,
                "title": "!!!"
            },
            ...
}

Sample code

Python

#!/usr/bin/python3

"""
    get_alllinks.py

    MediaWiki API Demos
    Demo of `Alllinks` module: List links pointing to the given namespace.

    MIT License
"""

import requests

S = requests.Session()

URL = "https://en.wikipedia.org/w/api.php"

PARAMS = {
    "action": "query",
    "format": "json",
    "list": "alllinks",
    "alnamespace": "0",
    "alunique": "1"
}

R = S.get(url=URL, params=PARAMS)
DATA = R.json()

LINKS = DATA["query"]["alllinks"]

for l in LINKS:
    print(l["title"])

PHP

<?php
/*
    get_alllinks.php

    MediaWiki API Demos
    Demo of `Alllinks` module: List links pointing to the given namespace.

    MIT License
*/

$endPoint = "https://en.wikipedia.org/w/api.php";
$params = [
    "action" => "query",
    "format" => "json",
    "list" => "alllinks",
    "alnamespace" => "0",
    "alunique" => "1"
];

$url = $endPoint . "?" . http_build_query( $params );

$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$output = curl_exec( $ch );
curl_close( $ch );

$result = json_decode( $output, true );

foreach( $result["query"]["alllinks"] as $k => $v ) {
    echo( $v["title"] . "\n" );
}

JavaScript

/*
    get_alllinks.js

    MediaWiki API Demos
    Demo of `Alllinks` module: List links pointing to the given namespace.

    MIT License
*/

var url = "https://en.wikipedia.org/w/api.php"; 

var params = {
    action: "query",
    format: "json",
    list: "alllinks",
    alnamespace: "0",
    alunique: "1"
};

url = url + "?origin=*";
Object.keys(params).forEach(function(key){url += "&" + key + "=" + params[key];});

fetch(url)
    .then(function(response){return response.json();})
    .then(function(response) {
        var links = response.query.alllinks;
        for (var l in links) {
            console.log(links[l].title);
        }
    })
    .catch(function(error){console.log(error);});

MediaWiki JS

/*
	get_alllinks.js

	MediaWiki API Demos
	Demo of `Alllinks` module: List links pointing to the given namespace.

	MIT License
*/

var params = {
		action: 'query',
		format: 'json',
		list: 'alllinks',
		alnamespace: '0',
		alunique: '1'
	},
	api = new mw.Api();

api.get( params ).done( function ( data ) {
	var links = data.query.alllinks,
		l;
	for ( l in links ) {
		console.log( links[ l ].title );
	}
} );

Possible errors

Code Info
badcontinueInvalid continue param. You should pass the original value returned by the previous query.
invalidparammixThe alprop=ids parameter cannot be used with alunique.
This happens when you use alprop=ids and alunique together

Additional notes

  • As with other link modules within the {{ll|API:Main_page|Action API}}, this module returns the titles of the pages that link to the namespace, not the exact URIs to those pages.
  • This module can be used as a generator.
  • Previous versions would return an error if the user tried to run this module as a generator, and alunique was set to true. This was altered in v1.24, to allow using the module as a generator even if alunique is true.

See also

  • API:Backlinks - lists links to a given page.
  • API:Linkshere - similar to API:Backlinks, gets links to a given page. Note that, unlike API:Backlinks, which is a list module, API:Linkshere is a prop module. See the respective pages on API:Properties and API:Lists for how these two kinds of modules differ.
  • API:Links - retrieves links on a given page or pages.
This article is issued from Mediawiki. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.