Open main menu

Extension:ReplaceRedLinks

MediaWiki extensions manual
OOjs UI icon advanced.svg
ReplaceRedLinks
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
<ReplaceRedLinks/>
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.

UsageEdit

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 "|".

ExamplesEdit

InstallationEdit

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

CodeEdit

ReplaceRedLinks.php

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

<?php
//Extension supports replacing "red links" to Wikipedia for wikification in other projects.
if ( ! defined( 'MEDIAWIKI' ) )	die();
 
if ( defined( 'MW_SUPPORTS_PARSERFIRSTCALLINIT' ) ) {
	$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
		$this->setHooks();
	}
 
	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
		//http://www.mediawiki.org/wiki/Manual:Hooks
		$wgHooks['ParserBeforeTidy'][] = array( &$this, 'fnParserBeforeTidy' );
		//function fnParserBeforeTidy() - is below
	}
 
	//
	function fnReplaceRedLinks( $str, $argv, $parser ){
		//tag <ReplaceRedLinks/> found
		$this->SwitchOff = false;
		
		//parameter "lang"
		@$s=$argv['lang'];
		if($s){
			if (preg_match("/[a-z][a-z]/i", $s)) {
				$this->lang = $s;
			}else{
				//unproper language - needed 2 letters - ru, en etc.
			}
		}

		//parameter "exclusions"
		@$s=$argv['exclusions'];
		$arr=array();
		if($s){
			$arr = explode("|", $s);
		}
		foreach($arr as $el){
			$this->exclusions[]=trim(strtolower($el));
		}

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

		//process links
		$text=preg_replace_callback("/
			(\"\/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=
			/x", 
			array( __CLASS__, 'ParseRedLinkCallback' ), 
			$text);
	
		$this->SwitchOff = true; //to prevent drawing it in footer
		return true;
	}
 
 
	function ParseRedLinkCallback($matches){
		$s=$matches[2];
		$tt=trim(urldecode($s));
		$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;
}