Zachary Rodriguez
Zachary Rodriguez

Reputation: 55

How do Wikipedia and other sites automatically transform what a user types in between certain tags into certain types of links?

I was just reading this Wikipedia page out of curiosity, and I was wondering if a programmer or web dev could explain to me how a certain function of it and other websites (including StackExchange ) work on the backend.

If you want to link to an article, but display some other text for the link, you can do so by adding the pipe "|" divider (SHIFT + BACKSLASH on English-layout and other keyboards) followed by the alternative name. Place the article you want to link to before the pipe divider, and the text to be seen after the pipe divider. Two examples:

  1. :[[Fox Broadcasting Company|Fox]] In the finished article, the text will read 'Fox', and link to the article called 'Fox Broadcasting Company'.

  2. ...

How do Wikipedia and StackExchange know to turn something in between brackets [[]] into a link to a Wikipedia article? Is it a server-side controller like I learned about in my .NET beginner's course?

Upvotes: 3

Views: 83

Answers (1)

Termininja
Termininja

Reputation: 7036

Wikipedia is powered by MediaWiki which is a free and open source software written in PHP. One of the key files in the MediaWiki source code is ​Linker.php which "contains methods to create internal, external or image links".

In very short..., here, in class Linker by function formatLinksInComment() all wiki links and media links are extracted from the text with regex: /\[\[(.*?)\]\]/ and \[\[:?([^\]|]+)(?:\|((?:]?[^\]|])*+))*\]\]([^[]*) (here and here you can see an example how it works). The links are checked by getLinkColour() for their color - blue for valid, and red for invalid links. After that these links are turned in HTML links (to the real targets) by link(), which also adds some classes to these a tags, as class="new" for red links, class="stub" for short articles, etc.

Upvotes: 1

Related Questions