API:Alllinks
This page is part of the MediaWiki Action API documentation. |
MediaWiki version: | ≥ 1.11 |
GET request to list links that point to a given namespace , ordered by title.
This module can be used as a generator.
API documentation
edit
Example
editBy 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
editList distinct links (i.e. no duplicates) which point to the main namespace.
Response
edit{
"batchcomplete": "",
"continue": {
"alcontinue": "!!!!Hashtagging",
"continue": "-||"
},
"query": {
"alllinks": [
{
"ns": 0,
"title": "!"
},
{
"ns": 0,
"title": "!!"
},
{
"ns": 0,
"title": "!!!"
},
...
}
Sample code
editPython
edit#!/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
edit<?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
edit/*
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
edit/*
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
editCode | Info |
---|---|
badcontinue | Invalid continue param. You should pass the original value returned by the previous query. |
invalidparammix | The alprop=ids parameter cannot be used with alunique. This happens when you use
alprop=ids and alunique together |
Additional notes
edit- As with other link modules within the 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 ifalunique
is true.
See also
edit- 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 aprop
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.