Web hub based on Wikidata
This is a Web hub: it let's you craft URLs to go from an origin to a destination on the web, at the condition that you provide enough information on those points to be identified within Wikidata. It works primarily around Wikimedia sites, but given the amount Wikidata knows about the web at large, it can get you pretty far! And if you don't know where you want to go, that's ok too: this will just bring you to the closest Wikipedia article.
Target audience:
A few examples to catch your interest:
we can now link to Wikipedia articles about a concept in the user's favorite language:
but, after choosing your starting point, you can also customize your destination:
for your next prototype, illustrate your concepts the lazy way:
image | src |
---|---|
![]() |
/frwiki:Laniakea?property=image&width=256 |
Every URL is built as a bridge between two points, an origin and a destination:
The separation between the origin and the destination is expressed in the URL by the ?
: everything before the ?
aims to identify the origin, everything after identifies the destination.
Example:
Economy
article in the English Wikipedia
As the real hub in this story is Wikidata, every request needs to first resolve to a Wikidata id, which can thus be considered the primary origin point:
request | redirection |
---|---|
/Q1 | https://en.wikipedia.org/wiki/Universe |
/Q2 | https://en.wikipedia.org/wiki/Earth |
... | |
/Q1388426 | https://en.wikipedia.org/wiki/Bo%C3%ABn-sur-Lignon |
Alternatively to a Wikidata id, you can pass a key built from sitelinks as starting point, defaulting to enwiki
.
request | redirection |
---|---|
/P214:24597135 | https://en.wikipedia.org/wiki/Isaac_Asimov |
/viaf:24597135 | https://en.wikipedia.org/wiki/Isaac_Asimov |
/twitter:doctorow | https://en.wikipedia.org/wiki/Cory_Doctorow |
By default, the destination is Wikipedia in the user language, which is guessed from the request accept-language
header, falling back to English if the language header can't be found or the Wikipedia page doesn't exist in this language.
request | redirection |
---|---|
/Q184226 | https://en.wikipedia.org/wiki/Gilles_Deleuze |
Pass a lang
parameter (or just l
) to override the accept-language
header. Pass several values to set the fallback chain. The value auto
can be used to represent the value of the accept-language
header.
request | redirection |
---|---|
/Q184226?lang=fr | https://fr.wikipedia.org/wiki/Gilles_Deleuze |
/Q184226?lang=als,oc,auto,fr,en&site=wikiquote | https://oc.wikipedia.org/wiki/Gilles_Deleuze |
Pass a site
parameter (or just s
) to redirect to another site than wikipedia
. Pass several values to set the fallback chain. When combined with a lang
fallback chain, the site fallback has priority.
This can also include sites that can build URLs from Wikidata ids:
short site names
You can use short versions of those sites names:
long | short |
---|---|
wikidata |
wd |
wikipedia |
wp |
commons |
c , wc |
wikisource |
ws |
wikiquote |
wq |
wiktionary |
wt |
wikivoyage |
wv |
wikiversity |
wy |
wikinews |
wn |
inventaire |
inv |
portal |
po |
reasonator |
re |
scholia |
sc |
sqid |
sq |
Example: /Q184226?s=wq,wp,inv,wd&l=fr,en,de
Pass a property
parameter (or just p
) to get the destination from the entity claims associated to the desired property. The following examples illustrate the different behaviors depending on the property type:
Not supported: String
, Time
, Monolingualtext
, Quantity
, WikibaseProperty
, Math
A w
can be used for short for width
.
Instead of a list of properties, you can use special bundle keys, that behave like a list of properties.
The image
property is a bundles designed to be an easy way to give an image to an entity:
<img src="/Q624023?property=image&width=256" />
Did you ever wish to link to Stephan Zweig's (Q78491) spouse's (P26) place of death (P20) administrative territory (P131) time zone (P421) image (P18)? Now you can:
request | redirection |
---|---|
/Q78491?property=P26|P20|P131|P421|P18 | https://commons.wikimedia.org/wiki/Special:FilePath/Timezones2008_UTC-5_gray.png |
By default, when a destination is not found, you are redirected to the Wikidata entity page. This behavior can be customized:
request | redirection |
---|---|
/Q32689091?property=image&fallback=404 | 404 response |
/Q32689091?property=image&fallback=http%3A%2F%2Fexample.org%2F404.png | http://example.org/404.png |
In the case where you use a URL as a fallback, make sure that it is URL-encoded. In Javascript for example, that could be done like this:
const fallbackUrl = 'https://upload.wikimedia.org/wikipedia/commons/e/e0/Wikimedia_error_404.png'
const encodedFallbackUrl = encodeURIComponent(fallbackUrl)
const url = `https://hub.toolforge.org/Q32689091?property=image&fallback=${encodedFallbackUrl}`
You can get a JSON response (status code 200
) instead of a redirection (status code 302
) by adding the query parameter format=json
. Ex: /Q184226?lang=fr&format=json
This can be useful for debugging, or to use the internal resolver as a JSON API.
request | response |
---|---|
/Q184226?lang=fr&format=json | { origin: [Object], destination: [Object] } |
/Q184226?l=fr&f=j | { origin: [Object], destination: [Object] } |
Building Hub URLs from the URL bar requires a few steps:
Ctrl+L
or Alt+D
)https://hub.toolforge.org/
URLsBut we could be even more lazy by adding Hub as a search engine to your browser (see tutorials hereafter for firefox and chrome). The steps can now be as follow (assuming you set hub
as search engine keyword):
Ctrl+L
or Alt+D
)https://hub.toolforge.org/
URL, separating elements with spaces. Example: hub Q1 l=fr
hub
)Ctrl+L
or Alt+D
)hub Q1 l=fr
, that should bring you to https://fr.wikipedia.org/wiki/Univers
Ctrl+L
)hub
, press Tab
: the address bar should now display Search on Hub
Enter
(ex: Q1 l=fr
will bring you to https://fr.wikipedia.org/wiki/Univers)git clone https://github.com/maxlath/hub
cd hub
npm install
# Starts the server on port 2580 and watch for files changes to restart
npm run watch
The step followed to setup this tool on tools.wmflabs.org are documented here: deploy