From MediaWiki.org
Jump to navigation Jump to search
MediaWiki extensions manual
Crystal Clear action run.svg
Release status: beta
Implementation Tag
Description Supports replacing "red links" to Wikipedia for wikification in other projects
Author(s) X-romix
Latest version 1.1 (2010-05-13)
MediaWiki 1.15+
Database changes No
License Creative Commons Attribution/Share-Alike License 3.0 and GFDL
Download See the code section
Example http://wikiext.org/index.php/Red_links_sample
Hooks used
Tested on MediaWiki 1.15.3
Translate the ReplaceRedLinks extension if it is available at translatewiki.net
Check usage and version matrix.

The ReplaceRedLinks extension supports replacing "red links" to Wikipedia for simple wikification in other projects.


Use tag <ReplaceRedLinks/> to switch extension on. If in page there is not tag <ReplaceRedLinks/>, extension does not anything.

Language parameter

Use parameter lang e.g. <ReplaceRedLinks lang="en"/> to switch Wikipedia language. There is need a two-letter language identifier, e.g. "ru", "fr" etc. If there is no this parameter or if it incorrect - system uses "en" for default.

Exclusions parameter

Use parameter exclusions e.g. <ReplaceRedLinks lang="en" exclusions="apple|orange"/> to switch off processing for any article names. Delimitier - is vertical pipe "|".



  • Add the following code at the bottom of your LocalSettings.php:
    require_once "$IP/extensions/ReplaceRedLinks/ReplaceRedLinks.php";
  • Yes Done – Navigate to Special:Version on your wiki to verify that the extension is successfully installed.



Create this file in ANSI encoding (do not add any spaces before starting "<?php" and after end "?>").

//Extension supports replacing "red links" to Wikipedia for wikification in other projects.
if ( ! defined( 'MEDIAWIKI' ) )	die();
	$wgHooks['ParserFirstCallInit'][] = 'wfReplaceRedLinks';
} else {
	$wgExtensionFunctions[] = 'wfReplaceRedLinks';
// Extension credits that will show up on the page [[Special:Version]]    
$wgExtensionCredits['parserhook'][] = array(
       'path'         => __FILE__,
	'name'         => 'ReplaceRedLinks',
	'version'      => '1.1',
	'author'       => 'X-romix', 
	'url'          => 'https://www.mediawiki.org/wiki/Extension:ReplaceRedLinks',
	'description'  => 'Allows replacing "red links" to Wikipedia for wikification in other projects'
class ReplaceRedLinks{
	var $SwitchOff = true;
	var $lang = "en";
	var $exclusions=array();
	function ReplaceRedLinks() { //Constructor
	function setHooks() {
		global $wgParser, $wgHooks;
		//Hook for tag <ReplaceRedLinks> 
		//see http://www.mediawiki.org/wiki/Manual:Tag_extensions for details
		$wgParser->setHook( 'ReplaceRedLinks' , array( &$this, 'fnReplaceRedLinks' ) );
		//function fnReplaceRedLinks) - is below
		//Hook to ParserBeforeTidy event - "Used to process the nearly-rendered html code for the page (but before any html tidying occurs)"
		//see also http://www.mediawiki.org/wiki/Manual:Hooks/ParserBeforeTidy
		$wgHooks['ParserBeforeTidy'][] = array( &$this, 'fnParserBeforeTidy' );
		//function fnParserBeforeTidy() - is below
	function fnReplaceRedLinks( $str, $argv, $parser ){
		//tag <ReplaceRedLinks/> found
		$this->SwitchOff = false;
		//parameter "lang"
			if (preg_match("/[a-z][a-z]/i", $s)) {
				$this->lang = $s;
				//unproper language - needed 2 letters - ru, en etc.

		//parameter "exclusions"
			$arr = explode("|", $s);
		foreach($arr as $el){

		return $parser->recursiveTagParse($str);
	function fnParserBeforeTidy(&$parser, &$text){
		global $IP;
		if($this->SwitchOff == true){
			return true;

		//process links
			(\"\/index\.php\?title\=)	# start of link
			([^\&]+) 					# any text before &
			(\&amp\;action\=edit)		# action=edit
			(\&amp\;redlink\=1\")		# redlink=1
			(\sclass\=\"new\")			# class=new
			(\stitle\=\"[^\"]*\")		# title=
			array( __CLASS__, 'ParseRedLinkCallback' ), 
		$this->SwitchOff = true; //to prevent drawing it in footer
		return true;
	function ParseRedLinkCallback($matches){
		$tt=str_replace("_", " ", $tt); 
		if (in_array(strtolower($tt), $this->exclusions)){
			//there is exclusion
			//do not modify anything
			return $matches[0];

		return '"http://'.$this->lang.'.wikipedia.org/wiki/'.$s.'" title="'.$tt.' (Wikipedia)"';
function wfReplaceRedLinks() {
	new ReplaceRedLinks;
	return true;